I have a lamp that I made and it worked fine when I transformed or rotated it using keys I set up with a key function, but when I try animating it with timer functions it either keeps looping through the first animation function or only gets through a couple before it keeps looping. Also when I click on the OpenGL window it stops responding and I have to force close it.
Any ideas what’s causing this?
Thanks.
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);
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(0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);
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, 3.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
0.0f, 25.0f, 0.0f, -50.0f,
0.0f, 0.0f, 0.0f, -75.0f);
animate_lamp(3.0f, 4.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f, 0.0f,
25.0f, 25.0f, -50.0f, -50.0f,
0.0f, 25.0f, -75.0f, 10.0f);
animate_lamp(4.0f, 5.0f, 0.0f, 5.0f,
0.0f, 5.0f, 0.0f, 0.0f,
25.0f, -45.0f, -50.0f, 25.0f,
25.0f, 20.0f, 10.0f, 10.0f);
animate_lamp(5.0f, 6.0f, 5.0f, 10.0f,
5.0f, 0.0f, 0.0f, 0.0f,
-45.0f, 10.0f, 25.0f, -30.0f,
20.0f, 20.0f, 10.0f, -5.0f);
animate_lamp(6.0f, 7.0f, 10.0f, 10.0f,
0.0f, 0.0f, 0.0f, 0.0f,
10.0f, 25.0f, -30.0f, -65.0f,
20.0f, -5.0f, -5.0f, -5.0f);
animate_lamp(7.0f, 8.0f, 10.0f, 10.0f,
0.0f, 0.0f, 0.0f, 0.0f,
25.0f, 0.0f, -65.0f, -20.0f,
-5.0f, 20.0f, -5.0f, -5.0f);
//glutSwapBuffers(); // swap back buffer to front buffer (which is drawn on screen)
}
Here’s my animate_lamp function:
void animate_lamp(float time_from, float time_to, float dof1_from, float dof1_to, float dof2_from, float dof2_to, float dof3_from, float dof3_to, float dof4_from, float dof4_to, float dof5_from, float dof5_to, float dof6_from, float dof6_to, float dof7_from, float dof7_to){
_ftime64_s( &timebuffer );
timeline = _ctime64( & ( timebuffer.time ) );
int time2 = convert_total_msec((int) timebuffer.millitm, timeline);
float diff_time = (float) (time2 - g_time1) / 1000.0f;
while ( (time_from <= diff_time) && (diff_time-time_to) ) {
float dof1_trans = (diff_time-time_from)/(time_to-time_from) * (dof1_to-dof1_from) + dof1_from;
float dof2_trans = (diff_time-time_from)/(time_to-time_from) * (dof2_to-dof2_from) + dof2_from;
float dof3_angle = (diff_time-time_from)/(time_to-time_from) * (dof3_to-dof3_from) + dof3_from;
float dof4_angle = (diff_time-time_from)/(time_to-time_from) * (dof4_to-dof4_from) + dof4_from;
float dof5_angle = (diff_time-time_from)/(time_to-time_from) * (dof5_to-dof5_from) + dof5_from;
float dof6_angle = (diff_time-time_from)/(time_to-time_from) * (dof6_to-dof6_from) + dof6_from;
float dof7_angle = (diff_time-time_from)/(time_to-time_from) * (dof7_to-dof7_from) + dof7_from;
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
draw_lamp(dof1_trans, dof2_trans, dof3_angle, dof4_angle, dof5_angle, dof6_angle, dof7_angle);
glutSwapBuffers();
//
_ftime64_s( &timebuffer );
timeline = _ctime64( & ( timebuffer.time ) );
time2 = convert_total_msec((int) timebuffer.millitm, timeline);
diff_time = (float) (time2 - g_time1) / 1000.0f;
}
}