Multi object animation in continous time

When I want to roll a cube from the world coordinate along the X_axis (on oxy plane) three times by edge contact then rolling aside (parallel with Y_axis ) two times and stop.
When I try to do simulation using glutDisplayFunc(Display), I do not know how to stop the 1st rolling then do the 2nd rolling.

angle = 0;
// 1st rolling the cube 
void Display(){
while (angle < 90) {
                        DisplayInit();
                        glPushMatrix();
			glRotatef(angle, 0, 0, 1);
			glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, blue);
			SolidCuboid(cube.cubeCenter[i], 1.0);
			glPopMatrix();
			glFlush();
                        glutSwapBuffers();
angle += 1.0;
}
 void OpenGLCallBack(){
glutDisplayFunc(Display);
glutIdleFunc(onIdle);
....
}

From the code, the glutDisplayFunc(Display) make the animation whole time.
Please hellp!!!

p/s this is old OpenGl version (not modern OpenGl)

Pick up any intro to 3D graphics book, and it’ll point out what you need to do here.

When called, don’t think of your Display() method as a “Display the scene for all possible times and then return” function, but rather a “Display the scene as it appears right now and then return” function. That’s how you should use it.

So what you want is something more like:

void Display(){
  time = getCurrentTime()
  computeSceneState( time, &angle )
  drawScene( angle )
  glutSwapBuffers()
}

You can then arrange for the Display() function to be called repeatedly by calling glutPostRedisplay() at the end of your Display() function. Alternatively, you can arrange to call glutPostRedisplay() at some other rate by calling it based on a system timer.

Thanks for advice.

I tried but it did not work. The problem is that in order to roll a cube along its edge. There are some steps (just like youtube " MODO | Animate a Rolling Cube"):

In OpenGL, as you know that in order to roll an object with an arbitrary axis (here is a cube edge), we have to do 3 steps:

  1. translate the cube until the contact edge for rolling == Y_axis.)
  2. rotate the cube with new coordinate (90 degrees) around Y_Axis.
  3. translate the rotated cube to a new coordinate.

How can I only show the rotation because it has to pass these 3 above steps?

I don’t understand your question. Why don’t you do exactly what you said? Instead of just calling glRotatef(), add a translate before and a translate after.

It sounds like you’re struggling with “how do I transform objects within a coordinate space”. Pick up any decent Intro to 3D Graphics book and it’ll cover this. Here’s one that discusses this w.r.t. the old version of OpenGL you’re using: Chapter 3: Viewing.

1 Like

Thanks so much for your tips.
Actually, the object transformation is in apart of the rolling issue. With my code below, I have just rolled the cube along the edge 90 degrees only one time when I pressed “r”.

If I want to roll 3 times then change the direction along with Y_axis with 2 rolling times, I do not where can I put the loop. I tried to put the for then while loop in the display() but receiving wrong results.
Would you please help me to change this code.

int angle = 0;
//int rotCount = 0;
void display(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	//while(rotCount < 3){
	glPushMatrix();
        ...
	glRotatef((GLfloat)angle, 0.0, 0.0, 1.0); //rolling along X_axis
	glTranslatef(-0.5, 0.5, 0.0); //rolling at edge contact
	glutWireCube(1.0);
	glPopMatrix();
	//rotCount++;
	//}
	glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
	switch (key) {
	case 'r':
		angle = (angle - 5) % 90;
		glutPostRedisplay();
		break;
        ....
	}
}
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
        ...
	init();
	glutDisplayFunc(display);
        ...
	glutKeyboardFunc(keyboard);
	glutMainLoop();
	return 0;
}