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(,);
glutInitWindowSize(,);
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 ;
}

然后再来讨论渲染函数的细节.先声明一个浮点型值的角变量,初始值为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 == )
h = ; 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(, , 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(,);
glutInitWindowSize(,);
glutCreateWindow("Lighthouse3D- GLUT Tutorial"); // register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene); // enter GLUT event processing cycle
glutMainLoop(); return ;
}

[译]GLUT教程 - 动画的更多相关文章

  1. [译]GLUT教程(目录)

    http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...

  2. [译]GLUT教程 - 游戏模式

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...

  3. [译]GLUT教程 - glutPostRedisplay函数

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> glutPostRedisplay 直 ...

  4. [译]GLUT教程 - 安装

    Lighthouse3d.com >> GLUT Tutorial >> Basics >> Setup 你需要什么 要用GLUT库开发程序,你可以下载最新版本3. ...

  5. [译]GLUT教程 - 整合代码8

    Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...

  6. [译]GLUT教程 - 整合代码7

    Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII 以下是子窗体的最终版本代码. ...

  7. [译]GLUT教程 - 渲染到子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Rendering to Subwindows 先回顾一下之前 ...

  8. [译]GLUT教程 - 重整子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows 重整函数的回调需要处理两 ...

  9. [译]GLUT教程 - 创建和关闭子窗体

    Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Creating and Destroying Subwind ...

随机推荐

  1. C++大数板子

    C++大数板子 使用样例在主函数里看就好,必要的运算符都重载了. #include <iostream> using namespace std; ;/*精度位数,自行调整*/ //1.如 ...

  2. lor实践

    1.启动一个nginx监听8888端口, -p 指定工作目录 -c 指定加载配置文件 在nginx.conf中,写一个server,进入lor项目的入口文件main.lua 2.main.lua执行r ...

  3. 无需重新编译php加入ftp扩展的解决方法

    无需重新编译php加入ftp扩展的解决方法   本文为大家介绍无需重新编译php加入ftp扩展的方法,有需要的朋友可以参考下   首先,进入源码目录cd php-5.2.13/ext/ftp #运行p ...

  4. Jenkins配置Java项目1(Java+Maven+Tomcat+SVN/Git)

    先收集几个网址,后续再自己动手过一遍 http://www.cnblogs.com/leefreeman/p/4211530.html http://www.cnblogs.com/sunzhench ...

  5. .Net中的不可变集合(Immutable Collection)简介

    今天发现MS在Nuget上发布了一个Immutable Collection的程序集,提供了对不可变对象的集合的支持. 简单的看了一下,貌似支持的还比较全: ImmutableArray<T&g ...

  6. tiny4412 串口驱动分析二 --- printk的实现

    作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...

  7. 模拟Spring中的getBean方法

    一直知道Spring是运用反射技术的,但具体怎么用呢?今天就模拟下getBean方法. 步骤: 1.用Dom4j解析xml配置文件,取出我们需要的信息 2.遍历Bean节点,根据每个Bean节点的cl ...

  8. Windows查看所有的端口及端口对应的程序

    步骤一.Windows查看所有的端口 点击电脑左下角的开始,然后选择运行选项,接着我们在弹出的窗口中,输入[cmd]命令,进行命令提示符.然后我们在窗口中输入[netstat -ano]按下回车,即会 ...

  9. selenium _WaitForSingleObject(self._handle, 0) == _WAIT_OBJECT_0

    在关闭driver时,如果用close,而不是用quit,会出现如下错误: Exception ignored in: <bound method Popen.__del__ of <su ...

  10. rpc接口mock平台

    转载:http://blog.csdn.net/ronghuanye/article/details/71124320 1.简介 平台采用struts.spring.mybatis框架开发设计,主要用 ...