Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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 }

Slide 3

Slide 3 text

jgs Display Lists

Slide 4

Slide 4 text

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();

Slide 5

Slide 5 text

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.

Slide 6

Slide 6 text

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(); ... }

Slide 7

Slide 7 text

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 }

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

jgs Pop-Up Menus

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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();

Slide 13

Slide 13 text

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(); }

Slide 14

Slide 14 text

Javier Gonzalez-Sanchez | SER332 | Spring 2018 | 14 jgs Example 2

Slide 15

Slide 15 text

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.