







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Computer Science and Electrical Engineering Dept. West Virginia University
September 3, 2008
(^1) Interaction
(^2) Display lists
3 Animation
Interaction
Menu interaction is controlled through callbacks. int glutCreateMenu(void (*func)(int value)) : I (^) register the callback function and return menu id I (^) func is the callback function, which accepts an int parameter void glutAddMenuEntry(char *name, int value) : I (^) name appears on the menu button I (^) id is passed to the callback void glutAttachMenu(int button) : I (^) menu pops up on mouse button press
Interaction
glutCreateMenu(demo_menu); //register callback glutAddMenuEntry("quit", 1); glutAddMenuEntry("increase size", 2); glutAddMenuEntry("decrease size", 3); glutAttachMenu(GLUT_RIGHT_BUTTON);
void demo_menu(int id) { switch(id) { case 1: exit(0); break; case 2: size = 2*size; break; case 3: if(size > 1) size = size/2; break; } }
Display lists
Immediate mode For HW2 we used immediate-mode : primitives are sent as soon as they are defined. Primitives are not stored within OpenGL. When you want to redraw the primitive, you must redefine and resend the primitive.
Retained mode We may store primitives on the graphics server within a display list. The display list can be redrawn with a single command.
Display lists
To compile (create) a display list:
#define BOX 1 glNewList(BOX, GL_COMPILE); glBegin(GL_LINE_LOOP); glVertex2f(-1.0f, -1.0f); glVertex2f(1.0f, -1.0f); glVertex2f(1.0f, 1.0f); glVertex2f(-1.0f, 1.0f); glEnd(); glEndList();
This stores the commands to render a square in a display list which is referenced by BOX.
Display lists
Benefits Reduced data traffic between client and server. Reduced overhead from function calls on the client.
Disadvantages Require additional memory on the server. There is overhead associated with creating the display list. Most useful for unchanging data.
Animation
glutGet(GLUT_ELAPSED_TIME); Returns an integer number of milliseconds since glutInit was called. Timer code is generally not very portable. There are system calls for more accurate timers in Linux and Windows - don’t use them in homework.
int last_time = 0; void idle() { int time = glutGet(GLUT_ELAPSED_TIME); int elapsed = time-last_time; float delta_seconds = 0.001f*elapsed; last_time = time; // ... do some animation using // delta_seconds here }
Animation
Vector2f pos(0.0f, 0.0f); int last_time = 0; void idle() { int time = glutGet(GLUT_ELAPSED_TIME); int elapsed = time-last_time; float delta_seconds = 0.001f*elapsed; last_time = time;
Vector2f speed(0.1f, 0.2f); pos.x = pos.x + speed.x * delta_seconds; pos.y = pos.y + speed.y * delta_seconds; }