Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 17 Animations, Loops.

Similar presentations


Presentation on theme: "1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 17 Animations, Loops."— Presentation transcript:

1 1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 17 Animations, Loops

2 2 Using Variables in Expressions Variables can be used in arithmetic expressions and function calls. Example--Multiplication: float area, width, height; width = 5.0; height = 3.0; area = width * height; Example--Adding 1: int count = 0; count = count + 1; count += 1; count++; Using variables in function calls: float theta = 45.0; glRotatef(theta, 0, 0, 1.0);

3 3 Local and Global Variables Variables declared at the beginning of the program and outside any function definition are global variables. They can be accessed by any function in the program. Variables declared inside a function definition are local to that function. They can only be accessed within that function. Demo. (We will show this in the example code).

4 4 Simple Animation Simple Animation in OpenGL is accomplished with an idle( ) function. The idle( ) function is called every tick of the computer clock. We can update a variable in the idle( ) function to change the position of an object. Declaring the idle function (inside the main( ) function): glutIdleFunc(idle); Updating a global variable in the idle function: void idle(void) { theta = theta + 0.1; glutPostRedisplay( ); }

5 5 Animation continued We then use theta when drawing our object: void display(void) { glClear(GL_COLOR_BUFFER_BIT); glPushMatrix( ); glRotatef(theta, 0, 0, 1.0); glBegin(GL_POLYGON); glVertex3f(50.0, 50.0, -200.0); glVertex3f(50.0, 100.0, -200.0); glVertex3f(100.0, 100.0, -200.0); glVertex3f(100.0, 50.0, -200.0); glEnd( ); glPopMatrix( ); glFlush(); glutSwapBuffers( ); }

6 6 Use of glPushMatrix( ) and glPop Matrix( ) We often want to perform a transformation on only one or a few objects in our scene. We need to save the current transformation information, so we can get it back after we have transformed our objects. glPushMatrix( ) saves the current transformation information. glPopMatrix( ) restores the most recently saved transformation information. glPushAttrib(GL_ALL_ATTRIB_BITS) saves other attributes such as color. glPopAttrib( ) restores the most recently saved attributes.

7 7 Double Buffering If we use a single frame buffer when animating, the drawing into the frame buffer can become out of synchronization with the drawing to the monitor. This can cause the animation to flicker. To avoid this, we use double buffering. We use two frame buffers. We draw to one frame buffer, while the other is being displayed. Then we swap buffers. In OpenGL: In the main( ) function: glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); In the display( ) function: glutSwapBuffers( );

8 8 Setting the Color OpenGL uses RGB color coordinates. The values for each color range from 0 to 1.0 Set each color with: glColor3f(red, green, blue) Example: glColor3f(1.0, 0.0, 0.0); //draw in red... glColor3f(0.5, 0, 0.5); //draw in purple

9 9 Loops We often want to repeat an action multiple times, with only small changes. For example, we might want to draw an object in multiple different places. We don't want to re-write the code every time. We can repeat a piece of code multiple times, using a loop.

10 10 A While Loop int count = 0; while (count < 10) { glPushMatrix( ); glTranslatef(25 * count, 0, 0); glBegin(GL_LINES); glVertex3f(0.0, 25.0, -200); glVertex3f(0.0, -25.0, -200); glEnd( ); glPopMatrix( ); count = count + 1; }

11 11 General Form of a While Loop while( condition) { //Body of loop statements; } Condition Body of Loop true Rest of program false


Download ppt "1 Perception, Illusion and VR HNRS 299, Spring 2008 Lecture 17 Animations, Loops."

Similar presentations


Ads by Google