Reputation: 2108
Had some trouble with animation in my OpenGL project (window was non-responsive after a few seconds or when clicked, and animations were getting stuck and looping at seemingly random points), so I was advised to add an idle callback and then make my Display
function only render the "current" frame each time it's called.
Although I don't know how to make my Display
function only render the "current" frame.
Here's my Display
function:
// GLUT display callback function
void Display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); // transformations are represented by matrices
// for placing camera
gluLookAt(0,1,50,0,0,0,0,1,0);
// for mouse movement
glTranslatef(g_fTransX,g_fTransY,g_fZoom);
glRotatef (g_fSpinX,1.0f,0.0f,0.0f);
glRotatef (g_fSpinY,0.0f,1.0f,0.0f);
glLightfv(GL_LIGHT0,GL_POSITION,lpos);
// xyz axes
glDisable(GL_LIGHTING);
glLineWidth(2.0);
glBegin(GL_LINES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(20.0, 0.0, 0.0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 20.0, 0.0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 0.0, 20.0);
glEnd();
glEnable(GL_LIGHTING);
float Ambient_m[] = {0.0f,1.0f,0.0f,0.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,Ambient_m);
float Ambient_l[] = {0.2f,0.2f,0.2f,0.0f};
glLightfv(GL_LIGHT0,GL_AMBIENT,Ambient_l);
// implementing our custom cylinder function
//draw_cylinder(g_translate_x, g_translate_y, g_dof3_angle);
// our csg object with 4 DOF
GLUquadricObj * qobj = gluNewQuadric();
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//draw_lamp(g_dof3_angle);
//glutSwapBuffers();
/*animate(time_from, time_to, dof1_from, dof1_to,
dof2_from, dof2_to, dof3_from, dof3_to,
dof4_from, dof4_to, dof5_from, dof5_to,
dof6_from, dof6_to, dof7_from, dof7_to)*/
animate_lamp(0.0f, 5.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 45.0f, 0.0f, -45.0f,
0.0f, 0.0f, 0.0f, 0.0f);
animate_lamp(5.0f, 7.0f, 0.0f, 5.0f,
0.0f, 5.0f, 0.0f, 0.0f,
45.0f, -45.0f, -45.0f, 45.0f,
0.0f, 0.0f, 0.0f, 0.0f);
animate_lamp(7.0f, 10.0f, 5.0f, 10.0f,
5.0f, 0.0f, 0.0f, 0.0f,
45.0f, -45.0f, -45.0f, 45.0f,
0.0f, 0.0f, 0.0f, 0.0f);
//glutSwapBuffers(); // swap back buffer to front buffer (which is drawn on screen)
}
Upvotes: 0
Views: 91
Reputation: 171177
It's simple - store the current state of the animation somewhere (since you're using C++, you can create an object for it), and then, in your render function, query the state from that object and render it.
Here's a simplified example:
class AnimationData
{
float pos;
public:
void step() { pos += 0.1f; }
void render() const
{
glBegin(GL_TRIANGLES);
glVertex3f(pos, 1.f, 1.f);
glVertex3f(pos, -1.f, 1.f);
glVertex3f(pos + 1.f, 0.f, 1.f);
glEnd(GL_TRIANGLES);
}
};
AnimationData myAnimation;
void Display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glLoadIdentity(); // transformations are represented by matrices
// for placing camera
gluLookAt(0,1,50,0,0,0,0,1,0);
// for mouse movement
glTranslatef(g_fTransX,g_fTransY,g_fZoom);
glRotatef (g_fSpinX,1.0f,0.0f,0.0f);
glRotatef (g_fSpinY,0.0f,1.0f,0.0f);
glLightfv(GL_LIGHT0,GL_POSITION,lpos);
// And any other setup
myAnimation.render();
glutSwapBuffers();
}
void timerCallbackFunction()
{
myAnimation.step();
glutPostRedisplay();
}
This will animate a triangle by moving it to the right a bit each time timerCallbackFunction()
is called. The display function simply renders the triangle on its current position.
Upvotes: 2