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. Jmeter插件之PerfMon监控插件使用说明

    PerfMon是Jmeter用来监控系统资源的一款插件,可以用来监控系统的cpu.内存.I/O等性能指标. 首先是Jmeter的插件安装,需要先下载JMeter Plugins Manager这个插件 ...

  2. java collection get 方法

    Collections unmodifiableList

  3. Maximum Size Subarray Sum Equals k -- LeetCode

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  4. command for ContextMenu in DataTemplate

    准备教程. 1. 无难度方式 <DataTemplate x:Key="DataTemplate1"> <StackPanel  > <toolkit ...

  5. MySQL 中的 base64 函数

    1. 5.6版本及之后的版本的base64 主要就是两个mysql内部函数to_base64和from_base64,使用也很简单,如下: 5.6之前不支持 mysql> select vers ...

  6. Android Developer -- Bluetooth篇 开发实例之二 连接设备

    连接设备 In order to create a connection between your application on two devices, you must implement bot ...

  7. 排序算法的实现(冒泡,选择,插入 O(N*N)--理解方法实现

    以前也看过很多排序算法的原理,每次都想自己实现一下,一直都再拖,现在着牛课网学习算法课程,希望自己能够坚持练习. //对于一个int数组,请编写一个选择冒泡算法,对数组元素排序. //给定一个int数 ...

  8. 模式识别hw2-------基于matconvnet,用CNN实现人脸图片性别识别

    主要来源模式识别课程大作业,本文首先感谢当初的助教和一起完毕作业的队友 matconvnet在matlab下封装了CNN常见算法,网址http://www.vlfeat.org/matconvnet/ ...

  9. Linux使用nginx部署Laravel

    问题描述 Laravel是PHP下当今最受欢迎的web应用开发框架,github上start数远超第二名Symfony,以前我用这个框架做项目的时候通常就是扔到apache里面,然后配置.htacce ...

  10. PV、UV

    1.什么是PV值 PV(page view)即页面浏览量或点击量,是衡量一个网站或网页用户访问量.具体的说,PV值就是所有访问者在24小时(0点到24点)内看了某个网站多少个页面或某个网页多少次.PV ...