SlideShare a Scribd company logo
1 of 24
SER332
Introduction to Graphics and
Game Development
Lecture 04:
Callback functions
Javier Gonzalez-Sanchez
javiergs@asu.edu
PERALTA 230U
Office Hours: By appointment
Part 2
GLUT: OpenGL Utility Toolkit
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 3
Example 1
/**
https://github.com/javiergs/SER332/blob/master/Lecture03/main.c
*/
// main
void main(int argc, char** argv){
glutInit(&argc, argv); // glut init
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500); // actual window size
glutInitWindowPosition(0,0); // window location
glutCreateWindow("simple");
myInit();
glutDisplayFunc(myDisplay);
glutMainLoop(); //event loop
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4
// myInit
void myInit() {
glClearColor(0.764f, 0.129f, 0.282f, 1.0); // maroon
glColor3f(1.0f, 0.843f, 0.0f); // gold
// projection transformations
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // units inside
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 5
// myDisplay
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT); // clear the window
glBegin(GL_POLYGON); // fill connected polygon
glVertex2f(-0.7, 0.7); // vertices of the square
glVertex2f( 0.6, 0.7);
glVertex2f(-0.7, -0.6);
glEnd();
glBegin(GL_POLYGON); // fill connected polygon
glVertex2f( 0.7, 0.6); // vertices of the square
glVertex2f(-0.6, -0.7);
glVertex2f( 0.7, -0.7);
glEnd();
glFlush(); //forces issued commands to execute
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6
Example 1
500
500
(0.7,0.7)
(-0.7,-0.7)
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 7
Double Buffering
§ Double buffering is necessary for almost all OpenGL
applications:
§ Render into back buffer
§ Swap buffers when finished rendering a frame: The old
back buffer becomes the front buffer that is displayed. The
old front buffer becomes the back buffer that is rendered
into.
§ What happens when you do not use double buffering?
§ flickering artifacts, tearing artifacts
§ Commands:
§ glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE );
§ glutSwapBuffers(); //instead of glFlush()
Part 3
Callback functions
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 9
Callback Functions
• Callback functions refresh
o void (*func)(void) – what is this thing?
• Virtually all interactive graphics programs are event driven
• Glut uses callbacks to handle events
o Windows system invokes a particular procedure when an event of
particular type occurs.
o MOST IMPORTANT: display event. It is signaled when window first
displays and whenever portions of the window reveals from
blocking window
o glutDisplayFunc(void (*func)(void)) registers the display callback
function
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 10
Callbacks Functions
• glutDisaplyFunc(void (*func)(void))
whenever GLUT decides to redisplay the window, the registerd
callback is executed.
• glutReshapeFunc(void (*func)(int w, int h))
indicates what action should be taken when the window is resized.
• glutKeyboardFunc(void (*func)(unsigned char key, int x,
int y)) glutMouseFunc(void (*func)(int button, int state,
int x, int y))
glutSpecialFunc(void (*func)(unsigned char key, int x,
int y))
allow you to link a keyboard key or a mouse button with a routine
that's invoked when the key or mouse button is pressed or released.
• glutMotionFunc(void (*func)(int x, int y))
registers a routine to call back when the mouse is moved while a
mouse button is also pressed.
• glutIdleFunc(void (*func)(void))
registers a function that's to be executed if no other events are
pending – use for animation or continuous update
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 11
Example 2
/**
https://github.com/javiergs/SER332/blob/master/Lecture04/main.c
*/
#include "glut.h"
// main
void main(int argc, char** argv) {
glutInit(&argc, argv); // glut init
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("simple");
myInit();
glutDisplayFunc(myDisplay);
glutKeyboardFunc(myKeyboard);
glutSpecialFunc(mySpecialKeys);
glutMouseFunc(myMouseClick);
glutMotionFunc(myMouseMotion);
glutMainLoop();
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 12
// lets play with global variables
float triangle_color_R = 1.0f;
float triangle_color_G = 0.843f;
float triangle_color_B = 0.0f;
float corner_x = -0.7f;
float corner_y = 0.7f;
int mouse_button;
int mouse_button_state;
int shake =10;
// myInit
void myInit() {
glClearColor(0.764f, 0.129f, 0.282f, 1.0); // maroon
// projection transformations
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // units inside
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 13
// display callback code
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT); // clear the window
glColor3f(triangle_color_R, triangle_color_G, triangle_color_B); //gold
glBegin(GL_POLYGON); // fill connected polygon
glVertex2f(corner_x, corner_y);
glVertex2f(corner_x+1.30, corner_y);
glVertex2f(corner_x, corner_y-1.30);
glEnd();
glBegin(GL_POLYGON); // fill connected polygon
glVertex2f(corner_x+1.40, corner_y-0.1);
glVertex2f(corner_x+0.1, corner_y-1.40);
glVertex2f(corner_x+1.40, corner_y-1.40);
glEnd();
glutSwapBuffers(); //forces issued commands to execute
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 14
// keyboard callback code
void myKeyboard(unsigned char key, int x, int y) {
switch (key) {
case 'q': case 'Q':
exit(0); // exit the program
break;
case 'i': case 'I':
int mod = glutGetModifiers();
if (mod == GLUT_ACTIVE_ALT) { // if ALT key
triangle_color_R = 1.0f;
triangle_color_G = 1.0f;
triangle_color_B = 1.0f;
} else {
triangle_color_R = 1.0f;
triangle_color_G = 0.843f;
triangle_color_B = 0.0f;
}
glutPostRedisplay(); // update the display
break;
}
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 15
Key Modifiers
int glutGetModifiers(void)
to detect if any modifier key is pressed
§ GLUT_ACTIVE_SHIFT SHIFT key
§ GLUT_ACTIVE_CTRL CTRL key
§ GLUT_ACTIVE_ALT ALT key
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 16
//special key callback code
void mySpecialKeys(int key, int x, int y) {
switch (key) {
case GLUT_KEY_UP:
triangle_color_R = triangle_color_R + 0.1;
break;
case GLUT_KEY_DOWN:
triangle_color_R = triangle_color_G - 0.1;
break;
case GLUT_KEY_LEFT:
triangle_color_B = triangle_color_B + 0.1;
break;
case GLUT_KEY_RIGHT:
triangle_color_B = triangle_color_B - 0.1;
break;
}
glutPostRedisplay();
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 17
Special Keys
§ GLUT_KEY_F1 F1 function key
§ …
§ GLUT_KEY_F12 F12 function key
§ GLUT_KEY_LEFT Left function key
§ GLUT_KEY_RIGHT Up function key
§ GLUT_KEY_UP Right function key
§ GLUT_KEY_DOWN Down function key
§ GLUT_KEY_PAGE_UP Page Up function key
§ GLUT_KEY_PAGE_DOWN Page Down function key
§ GLUT_KEY_HOME Home function key
§ GLUT_KEY_END End function key
§ GLUT_KEY_INSERT Insert function key
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 18
// mouse callback function
void myMouseClick(int button, int state, int x, int y) {
// button: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON
// state: GLUT_DOWN, GLUT_UP
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) {
corner_x = (x/250.0) -1; // from 0 : 500 to -1 : 1
corner_y =- ((y/250.0)-1);
}
mouse_button = button;
mouse_button_state = state;
glutPostRedisplay();
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 19
// mouse motion callback
void myMouseMotion(int x, int y) {
if (mouse_button == GLUT_LEFT_BUTTON &&
mouse_button_state== GLUT_DOWN) {
corner_x = (x / 250.0)-1; // from 0 : 500 to -1 : 1
corner_y = -((y / 250.0)-1);
} else {
shake=-shake; // shaking triangles
corner_x=((x-shake)/250.0)-1; //from 0 : 500 to -1 : 1
corner_y = -(((y-shake) / 250.0) - 1);
}
glutPostRedisplay();
}
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 20
Mouse Passive Motion Callback
glutPassiveMotionFunc( myMousePMotion );
§ Almost as same function as the glutMotionFunc();
§ The (active) motion callback is called when the mouse moves
within the window while one or more mouse buttons are
pressed.
§ The passive motion callback is called when the mouse moves
within the window while no mouse buttons are pressed.
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 21
Example 2
• Change color with the arrow keys
• Move the triangles with the mouse (left click)
• Move and shake with the mouse (right click)
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 22
Reading
OpenGL command syntax
§ Red Book(pdf): ch1 p17 – p18
Understand OpenGL’s state machine model
§ Red Book(pdf):ch1 p18
Understand OpenGL’s client server model
§ An example, glFlush, Red Book(pdf): ch2 p32
§ Think about the differences between glFlush, glFinish, glutSwapBuffers. To better understand glFinish,
think if you want to measure the exact rendering time of one frame on GPU.
Animation & Double buffering
§ Red Book(pdf): ch1 p24, explains double buffering and glutSwapBuffers
GLUT (important!!!)
§ Red Book(pdf): ch1 p21 - p28
§ Hearn Baker ch6 p307 – p34, similar as above but in more details
§ Red Book Appendix D
§ Example 1-2, Example 1-3 (Animation)
§ Glut.h will show you the technical details
Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 23
Homework
Programming, Programming, and Programming
SER332 Introduction to Graphics
Javier Gonzalez-Sanchez
javiergs@asu.edu
Spring 2018
Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.

More Related Content

Similar to 201801 SER332 Lecture 04

Intro to Computer Graphics.ppt
Intro to Computer Graphics.pptIntro to Computer Graphics.ppt
Intro to Computer Graphics.pptadil104135
 
01.Opengl_intro-2.ppt
01.Opengl_intro-2.ppt01.Opengl_intro-2.ppt
01.Opengl_intro-2.pptEngrZamaan
 
Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Aila Gema Safitri
 
The Ring programming language version 1.8 book - Part 64 of 202
The Ring programming language version 1.8 book - Part 64 of 202The Ring programming language version 1.8 book - Part 64 of 202
The Ring programming language version 1.8 book - Part 64 of 202Mahmoud Samir Fayed
 
Computer Graphics Project Report on Sinking Ship using OpenGL
Computer Graphics Project Report on Sinking Ship using OpenGL Computer Graphics Project Report on Sinking Ship using OpenGL
Computer Graphics Project Report on Sinking Ship using OpenGL Sharath Raj
 
The Ring programming language version 1.10 book - Part 68 of 212
The Ring programming language version 1.10 book - Part 68 of 212The Ring programming language version 1.10 book - Part 68 of 212
The Ring programming language version 1.10 book - Part 68 of 212Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202Mahmoud Samir Fayed
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension Yuren Ju
 
The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189Mahmoud Samir Fayed
 
HCI302-Computer Graphics-Section 6 (1).pptx
HCI302-Computer Graphics-Section 6 (1).pptxHCI302-Computer Graphics-Section 6 (1).pptx
HCI302-Computer Graphics-Section 6 (1).pptxnadinAlaa
 
The Ring programming language version 1.5.1 book - Part 56 of 180
The Ring programming language version 1.5.1 book - Part 56 of 180The Ring programming language version 1.5.1 book - Part 56 of 180
The Ring programming language version 1.5.1 book - Part 56 of 180Mahmoud Samir Fayed
 
OpenGL Introduction.
OpenGL Introduction.OpenGL Introduction.
OpenGL Introduction.Girish Ghate
 
The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180Mahmoud Samir Fayed
 

Similar to 201801 SER332 Lecture 04 (20)

opengl.ppt
opengl.pptopengl.ppt
opengl.ppt
 
Intro to Computer Graphics.ppt
Intro to Computer Graphics.pptIntro to Computer Graphics.ppt
Intro to Computer Graphics.ppt
 
01.Opengl_intro-2.ppt
01.Opengl_intro-2.ppt01.Opengl_intro-2.ppt
01.Opengl_intro-2.ppt
 
201707 SER332 Lecture 06
201707 SER332 Lecture 06 201707 SER332 Lecture 06
201707 SER332 Lecture 06
 
Open gl
Open glOpen gl
Open gl
 
Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)Tutorial Open GL (Listing Code)
Tutorial Open GL (Listing Code)
 
Opengl (1)
Opengl (1)Opengl (1)
Opengl (1)
 
201801 SER332 Lecture 03
201801 SER332 Lecture 03201801 SER332 Lecture 03
201801 SER332 Lecture 03
 
201707 SER332 Lecture10
201707 SER332 Lecture10   201707 SER332 Lecture10
201707 SER332 Lecture10
 
The Ring programming language version 1.8 book - Part 64 of 202
The Ring programming language version 1.8 book - Part 64 of 202The Ring programming language version 1.8 book - Part 64 of 202
The Ring programming language version 1.8 book - Part 64 of 202
 
Computer Graphics Project Report on Sinking Ship using OpenGL
Computer Graphics Project Report on Sinking Ship using OpenGL Computer Graphics Project Report on Sinking Ship using OpenGL
Computer Graphics Project Report on Sinking Ship using OpenGL
 
The Ring programming language version 1.10 book - Part 68 of 212
The Ring programming language version 1.10 book - Part 68 of 212The Ring programming language version 1.10 book - Part 68 of 212
The Ring programming language version 1.10 book - Part 68 of 212
 
The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202The Ring programming language version 1.8 book - Part 60 of 202
The Ring programming language version 1.8 book - Part 60 of 202
 
Input and Interaction
Input and InteractionInput and Interaction
Input and Interaction
 
step by step to write a gnome-shell extension
step by step to write a gnome-shell extension step by step to write a gnome-shell extension
step by step to write a gnome-shell extension
 
The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189The Ring programming language version 1.6 book - Part 56 of 189
The Ring programming language version 1.6 book - Part 56 of 189
 
HCI302-Computer Graphics-Section 6 (1).pptx
HCI302-Computer Graphics-Section 6 (1).pptxHCI302-Computer Graphics-Section 6 (1).pptx
HCI302-Computer Graphics-Section 6 (1).pptx
 
The Ring programming language version 1.5.1 book - Part 56 of 180
The Ring programming language version 1.5.1 book - Part 56 of 180The Ring programming language version 1.5.1 book - Part 56 of 180
The Ring programming language version 1.5.1 book - Part 56 of 180
 
OpenGL Introduction.
OpenGL Introduction.OpenGL Introduction.
OpenGL Introduction.
 
The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180The Ring programming language version 1.5.1 book - Part 52 of 180
The Ring programming language version 1.5.1 book - Part 52 of 180
 

More from Javier Gonzalez-Sanchez (20)

201804 SER332 Lecture 01
201804 SER332 Lecture 01201804 SER332 Lecture 01
201804 SER332 Lecture 01
 
201801 SER332 Lecture 02
201801 SER332 Lecture 02201801 SER332 Lecture 02
201801 SER332 Lecture 02
 
201801 CSE240 Lecture 26
201801 CSE240 Lecture 26201801 CSE240 Lecture 26
201801 CSE240 Lecture 26
 
201801 CSE240 Lecture 25
201801 CSE240 Lecture 25201801 CSE240 Lecture 25
201801 CSE240 Lecture 25
 
201801 CSE240 Lecture 24
201801 CSE240 Lecture 24201801 CSE240 Lecture 24
201801 CSE240 Lecture 24
 
201801 CSE240 Lecture 23
201801 CSE240 Lecture 23201801 CSE240 Lecture 23
201801 CSE240 Lecture 23
 
201801 CSE240 Lecture 22
201801 CSE240 Lecture 22201801 CSE240 Lecture 22
201801 CSE240 Lecture 22
 
201801 CSE240 Lecture 21
201801 CSE240 Lecture 21201801 CSE240 Lecture 21
201801 CSE240 Lecture 21
 
201801 CSE240 Lecture 20
201801 CSE240 Lecture 20201801 CSE240 Lecture 20
201801 CSE240 Lecture 20
 
201801 CSE240 Lecture 19
201801 CSE240 Lecture 19201801 CSE240 Lecture 19
201801 CSE240 Lecture 19
 
201801 CSE240 Lecture 18
201801 CSE240 Lecture 18201801 CSE240 Lecture 18
201801 CSE240 Lecture 18
 
201801 CSE240 Lecture 17
201801 CSE240 Lecture 17201801 CSE240 Lecture 17
201801 CSE240 Lecture 17
 
201801 CSE240 Lecture 16
201801 CSE240 Lecture 16201801 CSE240 Lecture 16
201801 CSE240 Lecture 16
 
201801 CSE240 Lecture 15
201801 CSE240 Lecture 15201801 CSE240 Lecture 15
201801 CSE240 Lecture 15
 
201801 CSE240 Lecture 14
201801 CSE240 Lecture 14201801 CSE240 Lecture 14
201801 CSE240 Lecture 14
 
201801 CSE240 Lecture 13
201801 CSE240 Lecture 13201801 CSE240 Lecture 13
201801 CSE240 Lecture 13
 
201801 CSE240 Lecture 12
201801 CSE240 Lecture 12201801 CSE240 Lecture 12
201801 CSE240 Lecture 12
 
201801 CSE240 Lecture 11
201801 CSE240 Lecture 11201801 CSE240 Lecture 11
201801 CSE240 Lecture 11
 
201801 CSE240 Lecture 10
201801 CSE240 Lecture 10201801 CSE240 Lecture 10
201801 CSE240 Lecture 10
 
201801 CSE240 Lecture 09
201801 CSE240 Lecture 09201801 CSE240 Lecture 09
201801 CSE240 Lecture 09
 

Recently uploaded

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAndikSusilo4
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsHyundai Motor Group
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Azure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & ApplicationAzure Monitor & Application Insight to monitor Infrastructure & Application
Azure Monitor & Application Insight to monitor Infrastructure & Application
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter RoadsSnow Chain-Integrated Tire for a Safe Drive on Winter Roads
Snow Chain-Integrated Tire for a Safe Drive on Winter Roads
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

201801 SER332 Lecture 04

  • 1. SER332 Introduction to Graphics and Game Development Lecture 04: Callback functions Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  • 2. Part 2 GLUT: OpenGL Utility Toolkit
  • 3. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 3 Example 1 /** https://github.com/javiergs/SER332/blob/master/Lecture03/main.c */ // main void main(int argc, char** argv){ glutInit(&argc, argv); // glut init glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500,500); // actual window size glutInitWindowPosition(0,0); // window location glutCreateWindow("simple"); myInit(); glutDisplayFunc(myDisplay); glutMainLoop(); //event loop }
  • 4. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4 // myInit void myInit() { glClearColor(0.764f, 0.129f, 0.282f, 1.0); // maroon glColor3f(1.0f, 0.843f, 0.0f); // gold // projection transformations glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // units inside }
  • 5. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 5 // myDisplay void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); // clear the window glBegin(GL_POLYGON); // fill connected polygon glVertex2f(-0.7, 0.7); // vertices of the square glVertex2f( 0.6, 0.7); glVertex2f(-0.7, -0.6); glEnd(); glBegin(GL_POLYGON); // fill connected polygon glVertex2f( 0.7, 0.6); // vertices of the square glVertex2f(-0.6, -0.7); glVertex2f( 0.7, -0.7); glEnd(); glFlush(); //forces issued commands to execute }
  • 6. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6 Example 1 500 500 (0.7,0.7) (-0.7,-0.7)
  • 7. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 7 Double Buffering § Double buffering is necessary for almost all OpenGL applications: § Render into back buffer § Swap buffers when finished rendering a frame: The old back buffer becomes the front buffer that is displayed. The old front buffer becomes the back buffer that is rendered into. § What happens when you do not use double buffering? § flickering artifacts, tearing artifacts § Commands: § glutInitDisplayMode( GLUT_RGBA | GLUT_DOUBLE ); § glutSwapBuffers(); //instead of glFlush()
  • 9. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 9 Callback Functions • Callback functions refresh o void (*func)(void) – what is this thing? • Virtually all interactive graphics programs are event driven • Glut uses callbacks to handle events o Windows system invokes a particular procedure when an event of particular type occurs. o MOST IMPORTANT: display event. It is signaled when window first displays and whenever portions of the window reveals from blocking window o glutDisplayFunc(void (*func)(void)) registers the display callback function
  • 10. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 10 Callbacks Functions • glutDisaplyFunc(void (*func)(void)) whenever GLUT decides to redisplay the window, the registerd callback is executed. • glutReshapeFunc(void (*func)(int w, int h)) indicates what action should be taken when the window is resized. • glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)) glutMouseFunc(void (*func)(int button, int state, int x, int y)) glutSpecialFunc(void (*func)(unsigned char key, int x, int y)) allow you to link a keyboard key or a mouse button with a routine that's invoked when the key or mouse button is pressed or released. • glutMotionFunc(void (*func)(int x, int y)) registers a routine to call back when the mouse is moved while a mouse button is also pressed. • glutIdleFunc(void (*func)(void)) registers a function that's to be executed if no other events are pending – use for animation or continuous update
  • 11. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 11 Example 2 /** https://github.com/javiergs/SER332/blob/master/Lecture04/main.c */ #include "glut.h" // main void main(int argc, char** argv) { glutInit(&argc, argv); // glut init glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(500, 500); glutInitWindowPosition(0, 0); glutCreateWindow("simple"); myInit(); glutDisplayFunc(myDisplay); glutKeyboardFunc(myKeyboard); glutSpecialFunc(mySpecialKeys); glutMouseFunc(myMouseClick); glutMotionFunc(myMouseMotion); glutMainLoop(); }
  • 12. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 12 // lets play with global variables float triangle_color_R = 1.0f; float triangle_color_G = 0.843f; float triangle_color_B = 0.0f; float corner_x = -0.7f; float corner_y = 0.7f; int mouse_button; int mouse_button_state; int shake =10; // myInit void myInit() { glClearColor(0.764f, 0.129f, 0.282f, 1.0); // maroon // projection transformations glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(-1.0, 1.0, -1.0, 1.0); // units inside }
  • 13. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 13 // display callback code void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); // clear the window glColor3f(triangle_color_R, triangle_color_G, triangle_color_B); //gold glBegin(GL_POLYGON); // fill connected polygon glVertex2f(corner_x, corner_y); glVertex2f(corner_x+1.30, corner_y); glVertex2f(corner_x, corner_y-1.30); glEnd(); glBegin(GL_POLYGON); // fill connected polygon glVertex2f(corner_x+1.40, corner_y-0.1); glVertex2f(corner_x+0.1, corner_y-1.40); glVertex2f(corner_x+1.40, corner_y-1.40); glEnd(); glutSwapBuffers(); //forces issued commands to execute }
  • 14. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 14 // keyboard callback code void myKeyboard(unsigned char key, int x, int y) { switch (key) { case 'q': case 'Q': exit(0); // exit the program break; case 'i': case 'I': int mod = glutGetModifiers(); if (mod == GLUT_ACTIVE_ALT) { // if ALT key triangle_color_R = 1.0f; triangle_color_G = 1.0f; triangle_color_B = 1.0f; } else { triangle_color_R = 1.0f; triangle_color_G = 0.843f; triangle_color_B = 0.0f; } glutPostRedisplay(); // update the display break; } }
  • 15. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 15 Key Modifiers int glutGetModifiers(void) to detect if any modifier key is pressed § GLUT_ACTIVE_SHIFT SHIFT key § GLUT_ACTIVE_CTRL CTRL key § GLUT_ACTIVE_ALT ALT key
  • 16. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 16 //special key callback code void mySpecialKeys(int key, int x, int y) { switch (key) { case GLUT_KEY_UP: triangle_color_R = triangle_color_R + 0.1; break; case GLUT_KEY_DOWN: triangle_color_R = triangle_color_G - 0.1; break; case GLUT_KEY_LEFT: triangle_color_B = triangle_color_B + 0.1; break; case GLUT_KEY_RIGHT: triangle_color_B = triangle_color_B - 0.1; break; } glutPostRedisplay(); }
  • 17. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 17 Special Keys § GLUT_KEY_F1 F1 function key § … § GLUT_KEY_F12 F12 function key § GLUT_KEY_LEFT Left function key § GLUT_KEY_RIGHT Up function key § GLUT_KEY_UP Right function key § GLUT_KEY_DOWN Down function key § GLUT_KEY_PAGE_UP Page Up function key § GLUT_KEY_PAGE_DOWN Page Down function key § GLUT_KEY_HOME Home function key § GLUT_KEY_END End function key § GLUT_KEY_INSERT Insert function key
  • 18. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 18 // mouse callback function void myMouseClick(int button, int state, int x, int y) { // button: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, GLUT_RIGHT_BUTTON // state: GLUT_DOWN, GLUT_UP if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { corner_x = (x/250.0) -1; // from 0 : 500 to -1 : 1 corner_y =- ((y/250.0)-1); } mouse_button = button; mouse_button_state = state; glutPostRedisplay(); }
  • 19. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 19 // mouse motion callback void myMouseMotion(int x, int y) { if (mouse_button == GLUT_LEFT_BUTTON && mouse_button_state== GLUT_DOWN) { corner_x = (x / 250.0)-1; // from 0 : 500 to -1 : 1 corner_y = -((y / 250.0)-1); } else { shake=-shake; // shaking triangles corner_x=((x-shake)/250.0)-1; //from 0 : 500 to -1 : 1 corner_y = -(((y-shake) / 250.0) - 1); } glutPostRedisplay(); }
  • 20. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 20 Mouse Passive Motion Callback glutPassiveMotionFunc( myMousePMotion ); § Almost as same function as the glutMotionFunc(); § The (active) motion callback is called when the mouse moves within the window while one or more mouse buttons are pressed. § The passive motion callback is called when the mouse moves within the window while no mouse buttons are pressed.
  • 21. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 21 Example 2 • Change color with the arrow keys • Move the triangles with the mouse (left click) • Move and shake with the mouse (right click)
  • 22. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 22 Reading OpenGL command syntax § Red Book(pdf): ch1 p17 – p18 Understand OpenGL’s state machine model § Red Book(pdf):ch1 p18 Understand OpenGL’s client server model § An example, glFlush, Red Book(pdf): ch2 p32 § Think about the differences between glFlush, glFinish, glutSwapBuffers. To better understand glFinish, think if you want to measure the exact rendering time of one frame on GPU. Animation & Double buffering § Red Book(pdf): ch1 p24, explains double buffering and glutSwapBuffers GLUT (important!!!) § Red Book(pdf): ch1 p21 - p28 § Hearn Baker ch6 p307 – p34, similar as above but in more details § Red Book Appendix D § Example 1-2, Example 1-3 (Animation) § Glut.h will show you the technical details
  • 23. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 23 Homework Programming, Programming, and Programming
  • 24. SER332 Introduction to Graphics Javier Gonzalez-Sanchez javiergs@asu.edu Spring 2018 Disclaimer. These slides can only be used as study material for the class SER332 at ASU. They cannot be distributed or used for another purpose.