PDA

View Full Version : Use of TimerFunction, glPushMatrix(); , and glPopmatrix();

Cquest
01-18-2014, 09:49 PM
Greetings! I encounter some problems while learning openGL.
Here is my problem:
I use 241 points to draw a curve as track.
Then draw a cylinder move on the track(241 points) at the same time rotate about axis which parallel to x-axis.
rotate rate:9 degrees per step
I want to use TimerFunction(or any other ways) to make the cylinder move but remain the track fixed, but they will rotate together.
Could anyone tell me how to use TimerFunction, glPushMatrix(); , and glPopmatrix(); ?
Here is my code: use visual C++ 6.0

//draw a cylinder

glPushMatrix();
glTranslatef(-2.0f,-2.0f,0.0f);
glColor3f(1.0f,0.0f,0.0f);
glColor3f(0.9f,0.91f,0.98f);
glTranslatef(0.0f,0.0f,0.5f);
glColor3f(0.0f,0.0f,1.0f);
glPopMatrix();
glutSwapBuffers();

//draw the curve
glBegin(GL_LINE_LOOP);
glVertex2f(-2.0f,-2.0f);
glVertex2f(-1.94109f,-2.05415f);
glEnd();

//I simply post on all 241 points to draw the curve.

glFlush();
}

void TimerFunction(int value)
{
glTranslatef(241 points);
glRotatef(9,1.0,0.0,0.0);
glutPostRedisplay();
glutTimerFunc(33,TimerFunction,1);
}

Thank you very much.

Dark Photon
01-19-2014, 01:37 PM
I want to use TimerFunction(or any other ways) to make the cylinder move but remain the track fixed, but they will rotate together.
Could anyone tell me how to use TimerFunction, glPushMatrix(); , and glPopmatrix(); ?

Sounds like two separate questions. First, how to make the cylinder move without affecting the track. Second, if/how TimerFunction should play a role in this. As to the first question, push/pop transforms around the cylinder and draw the track outside of this push/pop. That way, the local MODELING transform applied to the cylinder won't affect the track.

As to the second, this is a somewhat messy way to modify transforms because you're outside of your display function. Who knows what transform (if any) is active on the MODELVIEW stack at this point. A cleaner method might be to have TimerFunction bump some variable(s) indicating what the new rotate and translate should be next time the display function runs, but don't actually make the GL calls right there. Another option is to just make your display function (or a rtn it calls) smarter so that it knows how to continuously advance the transform.