320 likes | 567 Views
Basic Program with OpenGL and GLUT. Outline. Development Environment What is GLUT? S et up a GLUT Project Overview of GLUT Programs A Basic Program Example Some GLUT Functions API Documentation. Development Environment. IDE. OS. Library . GLUT (includes OpenGL and GLU). Windows 10
E N D
Outline • Development Environment • What is GLUT? • Set up a GLUT Project • OverviewofGLUTPrograms • A Basic Program Example • Some GLUTFunctions • API Documentation
Development Environment IDE OS Library GLUT (includes OpenGL andGLU) Windows 10 Windows 8 Windows 7 Visual Studio 2013Community version
What is GLUT? (1 / 2) • OpenGL Utility Toolkit (GLUT) • Window system independent toolkit • Simple windowing API for OpenGL • Functions start with a prefix of “glut” • (e.g., glutInit, glutMainLoop)
What is GLUT? (2 / 2) • What can GLUT do? • Initializing and creating window • Handling window and input events • Drawing preset 3D objects • Running the program
Set up a GLUT Project (1 / 9) Project include dll lib glut.h gl.h (glut.h) glu.h (glut.h) glut32.lib glut32.dll
Overview of GLUT Programs GLUT Point to Call Callback functions Initialization Set function pointers while(true){ if(funcBool){ funcBool = false; (*funcPtr)(…); } } Main loop
Some GLUT Functions • This section includes the following: • Initialization and window • Callback Registration • Geometric Object Rendering • Beginning Event Processing
Initialization and Window (1 / 3) • voidglutInit(int *argcp, char **argv); • Initializing the GLUT library • Should be called before any GLUT functions • voidglutInitDisplayMode(unsigned intmode); • Specify a display mode for windows created • Color: GLUT_RGBA, GLUT_RGB or GLUT_INDEX • Framebuffer:GLUT_SINGLEor GLUT_DOUBLE • Buffer: GLUT_DEPTH, GLUT_STENCIL and GLUT_ACCUM
Initialization and Window (2 / 3) • voidglutInitWindowSize(int width, int height); • Set the initial window size • voidglutInitWindowPosition(int x, int y); • Set the initial window position • The actual position is left to the window system to determine • intglutCreateWindow(char *name); • Create and open a window with previous settings
Initialization and Window (3 / 3) • voidglutPostRedisplay(void); • Mark the current window as needing to be redisplayed • The window‘s display callback will be called • voidglutSwapBuffers(void); • Swap the buffers of the current window if double buffered • An implicit glFlushis done by glutSwapBuffers
Callback Registration (1 / 5) • voidglutDisplayFunc(void (*func)(void)); • Put whatever you want to render in the callback • The callback is called when the window need to be redisplayed • Call glutPostRedisplay() to trigger the callback • voidglutReshapeFunc(void (*func)(int width, int height)); • The callback is called when a window is created, resized or moved • Always call glViewport() to resize your viewport
Callback Registration (2 / 5) • voidglutKeyboardFunc(void (*func)(unsigned char key, intx, 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
Callback Registration (3 / 5) • voidglutMouseFunc(void (*func)(int button, int state, intx, 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 • xand y: The mouse location in window relative coordinates when the mouse button state changed
Callback Registration (4 / 5) • voidglutMotionFunc(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 • voidglutPassiveMotionFunc(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
Callback Registration (5 / 5) • voidglutIdleFunc(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
Geometric Object Rendering • Provide many objects such as sphere, cube, cone, torus • All objects have wireframe and solid versions • voidglutSolidCube(GLdouble size); • voidglutWireTeapot(GLdouble size);
Beginning Event Processing • voidglutMainLoop(void); • Enter the GLUT event processing loop • Once called, this routine will never return while(true){ if(funcBool){ funcBool = false; (*funcPtr)(…); } } Callback functions Call
API Documentation (1 / 2) • OpenGL 2.1 Reference Pages • OpenGL and GLU • This course use legacy OpenGL (1.0 – 2.1)
API Documentation (2 / 2) • GLUT API Version 3 • For GLUT 3.x
Thanks for listening! Any questions?