PDA

View Full Version : Creating animations in OpenGL scene



yaro_dup1
02-03-2003, 08:33 AM
Hi,

I need help with constructing animations.
In main loop I've got a function called RenderScene() which renders
a room.
Now I want to open the door of the room. I have the door in diffrent display list.
What is the best method to make this animation?
I tried that:
I'm breaking the main loop and calling OpenDoor() function which looks like that:

void OpenDoor()
{
for (float x = 0; x < DOOR_OPENED_ANGLE; x += 0.6f)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

gluLookAt(DOOR_CENTER_X, DOOR_CENTER_Y, DOOR_CENTER_Z - 0.6f,
DOOR_CENTER_X, DOOR_CENTER_Y, DOOR_CENTER_Z,
0, 1, 0);


glMatrixMode(GL_MODELVIEW);
glPushMatrix();

glTranslatef(DOOR_LEFT + DOOR_WIDTH , 0, 0);
glRotatef(-x, 0, 1, 0);
glTranslatef(-DOOR_LEFT - DOOR_WIDTH, 0, 0);

glCallList(DOOR_CLOSED_LIST);

glPopMatrix();

glCallList(ROOM_LIST);

SwapBuffers(hDC);
Sleep(1);
}
}

Is it a good way? Because I don't think so.
For every animation I have to call glClear and SwapBuffers in else function?
Maybe is there any way to create main rendering function with glClear and SwapBuffers
and passing to it next frames of animation?

I have also the else problem. If I open the door, from that moment I have to call
list glCallList(DOOR_OPENED_LIST) or is there any fastest way to draw opened door?
I mean standard position of door which is described by glVertex position is door closed that
means I have to rotate the door modelview matrix every time after opening the door.

thanks for any help

nexusone
02-03-2003, 08:48 AM
Here is sort of an example of how it should be done.

Note you should do two display list, one for the room and one for the door.
Trying to change the door in the list is too hard, when it can be done much easier by making two list.

display()
{

// Setup Projection view

// Setup Matrix view


draw_room();

SwapBuffers();
}


void draw_room( void )
{

// Draw wall's etc.
glPushMatrix();
// note may also need to call glTranslate or Rotate to get room in correct position
glCallList(ROOM_LIST);
glPopMatrix();

// Draw Door
// door angle 0 = closed; 90 = open
Draw_Door( door_angle )

}

void Draw_Door( int angle )
{
//
glPushMatrix();
glRotatef(angle, 0, 0, 1); // Rotate on

// Draw door vertex and stuff
// We don't need to change the display list of how the door is drawn
// We just rotate the door on its hing.

glCallList(DOOR_LIST);

glPopMatrix();
}

void event_animation_loop()
{
// We need to have sometime that is called all the time to control our animations

if ( door == OPEN_DOOR )
{
door_angle++;
if (door_angle > 90 ) door_angle = 90; // max amount of angle to open the door
}

if ( door == CLOSE_DOOR )
{
door_angle--;
if (door_angle < 0) door_angle = 0; // Door min amount of rotation
}

Update_display(); // added this line to update screen in event loop
}

Then you need only a key press or timmer function to change state of 'door' from open to close or back.

I hope this gives you the idea.

[This message has been edited by nexusone (edited 02-03-2003).]

[This message has been edited by nexusone (edited 02-03-2003).]

[This message has been edited by nexusone (edited 02-03-2003).]