240 likes | 444 Views
Lecture 3. OpenGL. Lecture 3. Review Rasterization OpenGL Libraries: GL, GLU, GLUT Buffers: Buffer Types Clearing Buffers Primitives Colors Sample Code. Lecture Three. Color Modes RGB vs. Color-Index mode Event Driven Programming Call back Registration Mouse, Keyboard call backs
E N D
Lecture 3 OpenGL
Lecture 3 • Review • Rasterization • OpenGL Libraries: GL, GLU, GLUT • Buffers: • Buffer Types • Clearing Buffers • Primitives • Colors • Sample Code
Lecture Three • Color Modes • RGB vs. Color-Index mode • Event Driven Programming • Call back Registration • Mouse, Keyboard call backs • Reshape, Display, Idle • Double buffering • Animation
Color Modes • RGB Mode: A color is specified by: • Red • Green • Blue • Alpha • Number of colors simultaneously displayed: • n bits represents 2^n colors. • 8 to 16 bits represent 256 to 64K colors
Color Modes • Color Index Mode: • Uses Color Index Table • We can still use only 2^n color simultaneously • But the set of 2^n colors are picked from a table, Color Index Table • Void glutInitDisplayMode (GLUT_RGBA or GLUT_INDEX)
Choosing Color Model • In order to choose a color Model use: • Void glutInitDisplayMode (GLUT_RGBA or GLUT_INDEX) • Need more details? • Visit ‘Online Resources’ in course website, or click following link • http://www.cse.yorku.ca/~alireza/courses/540/rgba.pdf
Event Driven Programming • Definition. A programming where the primary activity is reaction to the receipt of event. • Applications: GUI, HCI • Ingredients: • Events: Can be from any source, human input, e.g. mouse or other sensors. • Event Dispatcher: Assigns events to event handlers. • Event Handler
Event Processing in GLUT • Initial Setup: • Initializing Display Modes, e.g. Color mode, Single/Double Buffer, etc. • Entering GLUT event processing loop • void glutMainLoop (void)
Call Back Registration • Definitions: • current window • Normal Plane • Assigning events to event-handler. • glutReshapeFunction • glutDisplayFunction • glutKeyboardFunction • glutMouseFunction
Display Function • The display function of current window • When is it called? • If the normal plane of a window needs to be redisplayed as a result of window damage. GLUT determines this based on redisplay state of a window. • When a user requests. • How is it called? • Implicitly: By GLUT, e.g. when the window system reports a region of the window's normal plane is undefined (for example, damaged by another window moving or being initially shown) • Explicitly: Using glutPostRedisplay function.
Reshape Function • Takes care of Window Resize • When is it called? • Whenever current window is reshaped • How is it called? • By GLUT • Width and height of new window is passed • void reshape (int w, int h)
main method • GLUT initialization • glutInit (&argc, argv) • glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB) • glutInitWindowSize (width, height) • glutInitWindowPosition (X, Y) • Registering Event Handler functions • Display Function, Reshape Function, etc. • Entering GLUT Event Processing • glutMainLoop function
Sample Code in main method • glutInit(&argc, argv); • glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); • glutInitWindowSize (250, 250); • glutInitWindowPosition (100, 100); • glutCreateWindow (argv[0]); • glClearColor (0.0, 0.0, 0.0, 0.0); • glShadeModel (GL_FLAT); • glutDisplayFunc(display); • glutReshapeFunc(reshape); • glutMouseFunc(mouse); • glutKeyboardFunc(keyboard); • glutMainLoop(); • return 0;
Event Handling: Keyboard • Registration • void glutKeyboardFunc (void (*func) (unsigned char key, int x, int y)) • X,Y are the coordinate of mouse when key is pressed • Refer to online resources GLUT API for details about key callbacks. • For non-ASCII key strokes • F1~F12, Direction, Del, Ins, PgUp, etc. • glutSpecialFunc
Event Handling: Mouse • Registration • void glutMouseFunc (void (*func) (int button, int state, int x, int y)) • Button value: • GLUT_LEFT_BUTTON, GLUT_MIDDLE_BUTTON, or GLUT_RIGHT_BUTTON. • State Value: • GLUT_UP or GLUT_DOWN: Represents that the event happened because of press or release. • void glutMotionFunc(void (*func) (int x, int y)) • Is called whenever the mouse moves within the window while one or more mouse buttons are pressed. • void glutPassiveMotionFunc(void (*func) (int x, int y)) • Is called whenever the mouse moves within the window while no mouse buttons are pressed.
Idle Function • Even sitting Idle is an event • Handling Idle event • glutIdleFunc (void (*func) (void)) • Most useful for creating animations
Example • Download the “Keyboard Event” sample code from the course website. • Let’s explain the code • Add an arbitrary mouse event to the code
Double Buffering • Using Two buffers • Front Buffer • Back Buffer • Is specified in initialization step • glutInitDisplayMode (GLUT_DOUBLE) • Replace glFlush() with glSwapBuffers() • Increases the performance • It’s NOT free, you have to pay: memory
Example • Download the “Double Buffer Example” sample code from the course website. • Compare the performance of Single buffer and Double buffer • Try to add events to the program.
Animation • Algorithm • Initialize GLUT windowing system • Initialize Event Handlers • Display • Reshape • Mouse, Keyboard, etc • Idle • Single/Double Buffering • glFlush or glSwapBuffers
Conclusion • What we covered today: • Color Modes • Event Driven Programming • Registering Callback Functions • Double Buffering • Animation
Next Week • Basic 3D Graphics • Basic Camera Setup and Control • Orthographic Projection • Perspective Projection • Drawing 3D Objects • GLUT solid and wireframe objects