[译]GLUT教程 - 动画

Lighthouse3d.com >> GLUT Tutorial >> Basics >> Animation

 

前面章节我们已经创建了一个白色三角形的窗体.还没到高潮,现在开始感受OpenGL动画的乐趣.我们会让三角形旋转.

首先我们要告知GLUT应用程序当处于空闲时,渲染函数要被调用.这样可促使GLUT保持调用渲染函数来启用动画效果.GLUT提供一个函数glutIdleFunc来让你注册一个回调函数用于绑定应用程序空闲时事件.

void glutIdleFunc(void (*func)(void));

func – 空闲事件触发的函数

 

在我们之前的例子中,当应用程序空闲时我们想要调用之前定义的实体渲染函数: renderScene. main函数的代码如下:

int main(int argc, char **argv) {

    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D- GLUT Tutorial");

    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);

    // here is the idle func registration
    glutIdleFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

 

然后再来讨论渲染函数的细节.先声明一个浮点型值的角变量,初始值为0.0.接着添加必要的实现代码到renderScene函数.

float angle = 0.0f;

void renderScene(void) {

    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(    0.0f, 0.0f, 10.0f,
            0.0f, 0.0f,  0.0f,
            0.0f, 1.0f,  0.0f);

    glRotatef(angle, 0.0f, 1.0f, 0.0f);

    glBegin(GL_TRIANGLES);
        glVertex3f(-2.0f,-2.0f, 0.0f);
        glVertex3f( 2.0f, 0.0f, 0.0);
        glVertex3f( 0.0f, 2.0f, 0.0);
    glEnd();

    angle+=0.1f;

    glutSwapBuffers();
}

 

注意此处需要用到双缓冲

你回忆一下,我们之前在main函数设置双缓冲模式作为显示模式.该特征提供两种显示缓冲.当前正在显示的是前台缓冲,另外的是后台缓冲.后台缓冲在后台按设定绘图.当我们发送切换缓冲的绘制命令时(例如当驱动完成时,前后缓冲会切换),然后下一帧会取代并渲染到屏幕.

glutSwapBuffers函数促发前后缓冲的切换,就是把后台绘制好的缓冲图像绘制到屏幕.原型如下:

void glutSwapBuffers();

#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

void changeSize(int w, int h) {

    // Prevent a divide by zero, when window is too short
    // (you cant make a window of zero width).
    if (h == 0)
        h = 1;

    float ratio =  w * 1.0 / h;

    // Use the Projection Matrix
    glMatrixMode(GL_PROJECTION);

    // Reset Matrix
    glLoadIdentity();

    // Set the viewport to be the entire window
    glViewport(0, 0, w, h);

    // Set the correct perspective.
    gluPerspective(45.0f, ratio, 0.1f, 100.0f);

    // Get Back to the Modelview
    glMatrixMode(GL_MODELVIEW);
}

float angle = 0.0f;

void renderScene(void) {

    // Clear Color and Depth Buffers
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Reset transformations
    glLoadIdentity();
    // Set the camera
    gluLookAt(    0.0f, 0.0f, 10.0f,
                0.0f, 0.0f,  0.0f,
                0.0f, 1.0f,  0.0f);

    glRotatef(angle, 0.0f, 1.0f, 0.0f);

    glBegin(GL_TRIANGLES);
        glVertex3f(-2.0f,-2.0f, 0.0f);
        glVertex3f( 2.0f, 0.0f, 0.0);
        glVertex3f( 0.0f, 2.0f, 0.0);
    glEnd();

    angle+=0.1f;

    glutSwapBuffers();
}

int main(int argc, char **argv) {

    // init GLUT and create window
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(320,320);
    glutCreateWindow("Lighthouse3D- GLUT Tutorial");

    // register callbacks
    glutDisplayFunc(renderScene);
    glutReshapeFunc(changeSize);
    glutIdleFunc(renderScene);

    // enter GLUT event processing cycle
    glutMainLoop();

    return 1;
}

 

posted @ 2013-10-23 15:43  Clotho_Lee  阅读(682)  评论(0编辑  收藏  举报