Basic Program with Open GL and GLUT Outline
Basic Program with Open. GL and GLUT
Outline Development Environment What is GLUT? Set up a GLUT Project Overview of GLUT Programs A Basic Program Example Some GLUT Functions API Documentation 2
Development Environment OS IDE Windows Library GLUT 10 Visual Studio 2013 (includes Windows 8 Community version Open. GL and Windows 7 GLU) 3
What is GLUT? (1 / 2) Open. GL Utility Toolkit (GLUT) Window system independent toolkit Simple windowing API for Open. GL Functions start with a prefix of “glut” (e. g. , glut. Init, glut. Main. Loop) 4
What is GLUT? (2 / 2) What can GLUT do? Initializing and creating window Handling window and input events Drawing preset 3 D objects Running the program 5
Set up a GLUT Project (1 / 9) Project include lib dll glut. h gl. h (glut. h) glut 32. lib glut 32. dll 6
Set up a GLUT Project (2 / 9) 7
Set up a GLUT Project (3 / 9) 8
Set up a GLUT Project (4 / 9) 9
Set up a GLUT Project (5 / 9) 10
Set up a GLUT Project (6 / 9) 11
Set up a GLUT Project (7 / 9) 12
Set up a GLUT Project (8 / 9) 13
Set up a GLUT Project (9 / 9) 14
Overview of GLUT Programs GLUT Initialization Set function pointers Main loop Point to Callback functions while(true){ if(func. Bool){ func. Bool = false; (*func. Ptr)(…); } } Call 15
A Basic Program Example (1 / 3) 16
A Basic Program Example (2 / 3) 17
A Basic Program Example (3 / 3) 18
Some GLUT Functions This section includes the following: Initialization and window Callback Registration Geometric Object Rendering Beginning Event Processing 19
Initialization and Window (1 / 3) void glut. Init(int *argcp, char **argv); Initializing the GLUT library Should be called before any GLUT functions void glut. Init. Display. Mode(unsigned int mode); Specify a display mode for windows created Color: GLUT_RGBA, GLUT_RGB or GLUT_INDEX Framebuffer: GLUT_SINGLE or GLUT_DOUBLE Buffer: GLUT_DEPTH, GLUT_STENCIL and GLUT_ACCUM 20
Initialization and Window (2 / 3) void glut. Init. Window. Size(int width, int height); Set the initial window size void glut. Init. Window. Position(int x, int y); Set the initial window position The actual position is left to the window system to determine int glut. Create. Window(char *name); Create and open a window with previous settings 21
Initialization and Window (3 / 3) void glut. Post. Redisplay(void); Mark the current window as needing to be redisplayed The window‘s display callback will be called void glut. Swap. Buffers(void); Swap the buffers of the current window if double buffered An implicit gl. Flush is done by glut. Swap. Buffers 22
Callback Registration (1 / 5) void glut. Display. Func(void (*func)(void)); Put whatever you want to render in the callback The callback is called when the window need to be redisplayed Call glut. Post. Redisplay() to trigger the callback void glut. Reshape. Func(void (*func)(int width, int height)); The callback is called when a window is created, resized or moved Always call gl. Viewport() to resize your viewport 23
Callback Registration (2 / 5) void glut. Keyboard. Func(void (*func)(unsigned char key, int x, int y)); Each key press generating a keyboard callback key: The ASCII character generated by the pressed key x and y: The mouse location in window relative coordinates when the key was pressed 24
Callback Registration (3 / 5) void glut. Mouse. Func(void (*func)(int button, int state, int x, int y)); Each press and each release mouse button in a window generates a mouse callback button: GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON or GLUT_RIGHT_BUTTON state: GLUT_UP or GLUT_DOWN x and y: The mouse location in window relative coordinates when the mouse button state changed 25
Callback Registration (4 / 5) void glut. Motion. Func(void (*func)(int x, int y)); The callback is called when the mouse moves within the window while any mouse buttons are pressed x and y: the mouse location in window relative coordinates void glut. Passive. Motion. Func(void (*func)(int x, int y)); The callback is called when the mouse moves within the window while no mouse buttons are pressed x and y: the mouse location in window relative coordinates 26
Callback Registration (5 / 5) void glut. Idle. Func(void (*func)(void)); Perform background processing tasks or continuous animation when window system events are not being received The idle callback is continuously called when events are not being received 27
Geometric Object Rendering Provide many objects such as sphere, cube, cone, torus All objects have wireframe and solid versions void glut. Solid. Cube(GLdouble size); void glut. Wire. Teapot(GLdouble size); 28
Beginning Event Processing void glut. Main. Loop(void); Enter the GLUT event processing loop Once called, this routine will never return while(true){ if(func. Bool){ func. Bool = false; (*func. Ptr)(…); } } Callback functions 29
API Documentation (1 / 2) Open. GL 2. 1 Reference Pages Open. GL and GLU This course use legacy Open. GL (1. 0 – 2. 1) 30
API Documentation (2 / 2) GLUT API Version 3 For GLUT 3. x 31
Thanks for listening! Any questions?
- Slides: 32