GLUTTutorial
Animation
So far, we have an OpenGL window with a white triangle, but it is not exciting at all. Now let's go to this tutorial to rotate the triangle.
Let's go back to the main () function and add some additional settings. First, tell glut that we want a dual-buffer. The dual buffer creates a smooth animation by painting in the latter buffer and constantly switching the front and back buffers (visible buffers. Use dual-buffer to prevent flickering.
.............
Gluinitdisplaymode (gl_depth | glu_double | glu_rgba );
............
Next, we need to tell glut that the rendering function should be called when the application is idle. This causes glut to always call the rendering function to generate an animation. GLUT provides a function: glutidlefunc. This function enables another function to be called when the program is idle.
Void glutidlefunc (void (* func) (void ));
Parameters:
FUNC: the name of the function called when the program is idle.
According to our idea, the function that should be called when the program is idle is the rendering function we previously defined: renderscene. Because OpenGL does not enable deep test by default, We need to enable it so that we can know which object is in front and which object is in the back. Enable the deep test in the main () function. Let's take a look at the current main function.
void main(int argc, char **argv) {
glutInit(&argc, argv);
//
Set dual-buffer here.
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("3D Tech- GLUT Tutorial");
glutDisplayFunc(renderScene);
//
Renderscene is called when the program is idle,
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
//
Enable deep test.
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
The following is the rendering function renderscene. We have defined a floating point variable and initialized it to 0.0. Next we will add some necessary things to the renderscene function.
float angle=0.0;
void renderScene(void) {
//
Note that we have cleared the depth buffer here.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
Save the current model view matrix.
glPushMatrix();
glRotatef(angle,0.0,1.0,0.0);
glBegin(GL_TRIANGLES);
glVertex
3f
(-0.5,-0.5,0.0);
glVertex
3f
(0.5,0.0,0.0);
glVertex
3f
(0.0,0.5,0.0);
glEnd();
//
Pop-up stack
glPopMatrix();
//
Swap Buffer
glutSwapBuffers();
//
Automatically add angle.
angle++;
}
The gluswapbuffers function exchanges the front and back buffers. The function prototype is as follows:
Void gluswapbuffers ();
Now, we have a rotating triangle. You can download this vcss project here (glu2.zip ). Great ?. But again, we will not render some very exquisite images to keep the code concise. It is also because it is mainly used to learn glut.