1 / 24

Lecture 3

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

landen
Télécharger la présentation

Lecture 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 3 OpenGL

  2. Lecture 3 • Review • Rasterization • OpenGL Libraries: GL, GLU, GLUT • Buffers: • Buffer Types • Clearing Buffers • Primitives • Colors • Sample Code

  3. 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

  4. 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

  5. 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)

  6. 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

  7. 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

  8. 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)

  9. Call Back Registration • Definitions: • current window • Normal Plane • Assigning events to event-handler. • glutReshapeFunction • glutDisplayFunction • glutKeyboardFunction • glutMouseFunction

  10. 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.

  11. 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)

  12. 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

  13. 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;

  14. 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

  15. 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.

  16. Idle Function • Even sitting Idle is an event • Handling Idle event • glutIdleFunc (void (*func) (void)) • Most useful for creating animations

  17. Example • Download the “Keyboard Event” sample code from the course website. • Let’s explain the code • Add an arbitrary mouse event to the code

  18. 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

  19. 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.

  20. Animation • Algorithm • Initialize GLUT windowing system • Initialize Event Handlers • Display • Reshape • Mouse, Keyboard, etc • Idle • Single/Double Buffering • glFlush or glSwapBuffers

  21. Conclusion • What we covered today: • Color Modes • Event Driven Programming • Registering Callback Functions • Double Buffering • Animation

  22. QUESTIONS?

  23. Next Week • Basic 3D Graphics • Basic Camera Setup and Control • Orthographic Projection • Perspective Projection • Drawing 3D Objects • GLUT solid and wireframe objects

  24. END

More Related