OpenGL since 1.0 -- computer animation

There are many techniques for generating computer animation in computer graphics, and we don't want to dabble in them too much now, just simply use double buffering technology to achieve animation effects.
The technique of using two flush buffers to generate real-time animation in a raster system is called double buffering. In the initial state, one frame of animation is created in the first buffer; then, when the screen is refreshed with the frame in this buffer, the next frame is created in the second buffer; after the next frame is created, the two frames are swapped. the role of a buffer; thus the refresh process begins to refresh the screen with frames from the second buffer, while creating the next frame in the first buffer.
The following code gives an example of an animation program that continuously rotates a regular hexagon around the z-axis in the xy plane.

#include <gl/glut.h>
#include <math.h>
#include <stdlib.h>
const double TWO_PI = 6.2831853;
GLsizei winWidth = 500, winHeight = 500;
GLuint regHex;
static GLfloat rotTheta = 0.0;
class scrPt{
public:
    GLint x, y;
};
static void init(void)
{
    scrPt hexVertex;
    GLdouble hexTheta;
    GLint k;
    glClearColor(1.0, 1.0, 1.0, 0.0);
    regHex = glGenLists(1);
    glNewList(regHex, GL_COMPILE);
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_POLYGON);
    for (k = 0; k < 6;k++)
    {
        hexTheta = TWO_PI*k / 6;
        hexVertex.x = 150 + 100 * cos(hexTheta);
        hexVertex.y = 150 + 100 * sin(hexTheta);
        glVertex2i(hexVertex.x, hexVertex.y);
    }
    glEnd();
    glEndList();
}
void displayHex(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
    glRotatef(rotTheta, 0.0, 0.0, 1.0);
    glCallList(regHex);
    glPopMatrix();
    glutSwapBuffers();//互换两个帧缓存
    glFlush();
}
void rotateHex(void)//随时间旋转
{
    rotTheta += 3.0;
    if (rotTheta>360.0)
    {
        rotTheta -= 360.0;
    }
    glutPostRedisplay();//显示窗口重新绘制
}
void winReshapeFcn(GLint newWidth, GLint newHeight)
{
    glViewport(0, 0, (GLsizei)newWidth, (GLsizei)newHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-320.0, 320.0, -320.0, 320.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glClear(GL_COLOR_BUFFER_BIT);
}
void mouseFcn(GLint button, GLint action, GLint x, GLint y)
{
    switch (button)
    {
    case GLUT_MIDDLE_BUTTON://如果中键响应
        if (action == GLUT_DOWN)//按下动作
            glutIdleFunc(rotateHex);//开始旋转
        break;
    case GLUT_RIGHT_BUTTON://如果右键响应
        if (action == GLUT_DOWN)//按下动作
            glutIdleFunc(NULL);//停止旋转
        break;
    default:
        break;
    }
}
void main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);//使用双缓存模式
    glutInitWindowPosition(150, 150);
    glutInitWindowSize(winWidth, winHeight);
    glutCreateWindow("Animation Example");
    init();
    glutDisplayFunc(displayHex);
    glutReshapeFunc(winReshapeFcn);
    glutMouseFunc(mouseFcn);//鼠标响应函数
    glutMainLoop();
}

I believe that with everyone's current knowledge, it is easy to understand this code with comments. Let's focus on a few functions.
To enable double cache mode:

glutInitDisplayMode(GLUT_DOUBLE);

After the double buffer is activated, two buffers that refresh the screen alternately are generated, respectively called the front buffer and the back buffer.
Swap the roles of the two caches:

glutSwapBuffers();

Activate animation:

glutIdleFunc(rotateHex);

As long as there is no window processing time, the callback function rotateHex will be called all the time, so as to truly achieve the animation effect.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325525969&siteId=291194637