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 ;
}

最新文章

  1. Python 静态方法、类方法
  2. 执行shell脚本,报错坏的解释器
  3. UTF-8 &#39;s format
  4. ios调用第三方程序打开文件,以及第三方调用自己的APP打开文件
  5. HTML DOM元素
  6. 转载,javascript 设计模式
  7. org.hibernate.MappingException: duplicate import异常
  8. 登录超时自动退出,计算时间差-b
  9. 如何解决Rally模板提示angular js加载错误
  10. 常用工具类,文件和内存的大小获取,shell脚本的执行
  11. Linux svnserver存储路径和文件的详细解释
  12. iframe自适应高度计算,iframe自适应
  13. Zabbix的安装及简单配置
  14. FutureTask分析(1.8)
  15. Python进阶---面向对象第二弹
  16. [HNOI 2016]序列
  17. OO第二单元多线程电梯总结分析
  18. B. School Marks(典型贪心)
  19. 关于在 java 8 下开启 TLS_RSA_WITH_3DES_EDE_CBC_SHA 支持 xp ie8 tls1.0 的正常访问
  20. FI 创建资产接口AS01

热门文章

  1. servlet多线程同步问题
  2. AppScan 浏览器兼容解决
  3. Java JVM:内存溢出(栈溢出,堆溢出,持久代溢出以及 nable to create native thread)
  4. [LOJ6208]树上询问
  5. sql server mvp 听风吹雨
  6. 在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符
  7. sql 分组后按时间降序排列再取出每组的第一条记录
  8. readlink命令
  9. Windows查看所有的端口及端口对应的程序
  10. python 时间 相关