Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SER332 Lecture 10

SER332 Lecture 10

Introduction to Graphics and Game Development
Display List and Menus
(201804)

Javier Gonzalez-Sanchez

February 15, 2018
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. jgs SER332 Introduction to Graphics and Game Development Lecture 10:

    Display Lists and Menus Javier Gonzalez-Sanchez [email protected] PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 2 jgs

    Performance myDisplay { // Square 1 – Vertex 4 // Square 2 – Vertex 4 // Square 3 – Vertex 4 // Square 4 – Vertex 4 }
  3. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 4 jgs

    Display Lists § What is Display Lists? A group of OpenGL functions to be executed on GPU § Why using Display Lists is fast? glNewList(index, GL_COMPILE); glBegin(GL_TRIANGLES); … glEnd(); glEndList();
  4. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 5 jgs

    Display List Functions § GLuint glGenLists (GLsizei index) Generates one or more unused indices. Each display list is identified by an integer index. § void glNewList(GLuint list, GLenum mode) § void glEndList(void) Specifies the start and the end of a display list. GL_COMPILE: creates the list but the commands are not executed. GL_COMPILE_AND_EXECUTE: creates the list and executes the commands immediately. § void glCallList(GLuint listID) Calls a display list and to render a single frame now.
  5. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 6 jgs

    Example //https://github.com/javiergs/SER332/blob/master/Lecture10/example_one.c #include "glut.h" GLuint drawDL; void createDisplayList() { drawDL = glGenLists(1); glNewList(drawDL, GL_COMPILE); glBegin(GL_POLYGON); glVertex2f(-0.7, 0.7); glVertex2f(0.6, 0.7); glVertex2f(-0.7, -0.6); glEnd(); glEndList(); } void myInit() { displayList(); ... }
  6. jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 7

    // myDisplay void myDisplay() { glClear(GL_COLOR_BUFFER_BIT); // compare this with example from Lecture 05. glCallList(drawDL); // call the list glTranslatef(0.05f, 0.05f, 0.0f); glRotatef(180, 0.0, 0.0, 1.0); glCallList(drawDL); // call the list glFlush(); //forces issued commands to execute }
  7. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 8 jgs

    Reading § To understand the design of display list: Red Book: chapter 7, pp.199-201 § To learn how to use display list: Red Book: chapter 7, example 7-1, 7-2
  8. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 10 jgs

    Pop-up Menu § Create a menu: int glutCreateMenu(void (*func)(int value)) func – the function that will handle the menu events. Returned value is the menu identifier. § Add entries to the menu: void glutAddMenuEntry(char *name, int value) Name – the string that show up in the menu Value – return to the function when menu entry is selected
  9. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 11 jgs

    Pop-up Menu § Attach menu to a mouse button void glutAttachMenu(int mouseButton) § Attach a submenu void glutAddSubMenu(char *entryName, int index) entryName – name of submenu entry in the menu. index – the index of submenu
  10. Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 12 jgs

    Example 2 // https://github.com/javiergs/SER332/blob/master/Lecture10/ScreenShot%20example_two.png void createMenus() { //add entries to submenu Colores int menuA = glutCreateMenu(menuListener); glutAddMenuEntry("Red", 1); glutAddMenuEntry("Blue", 2); glutAddMenuEntry("Green", 3); glutAddMenuEntry("White", 4); //add entries to submenu Position int menuB = glutCreateMenu(menuListener); glutAddMenuEntry("Top", 5); glutAddMenuEntry("Center", 6); glutAddMenuEntry("Down", 7); // create main menu int menu = glutCreateMenu(menuListener); glutAddSubMenu("Colores", menuA); glutAddSubMenu("Position", menuB); // attach the menu to the right button glutAttachMenu(GLUT_RIGHT_BUTTON); } void myInit() { createMenus();
  11. jgs Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 13

    // processMenuEvents void menuListener(int option) { switch (option) { case 1: r = 1.0f; g=0.0f; b=0.0f; break; case 2: r = 0.0f; g = 0.0f; b = 1.0f; break; case 3: r = 0.0f; g = 1.0f; b = 0.0f; break; case 4: r = 1.0f; g = 1.0f; b = 1.0f; break; case 5: position_y = 0.3f; break; case 6: position_y = 0.0f; break; case 7: position_y = -0.3f; break; } glutPostRedisplay(); }
  12. jgs SER332 Introduction to Graphics Javier Gonzalez-Sanchez [email protected] 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.