[译]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(,);
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教程 - 动画的更多相关文章
- [译]GLUT教程(目录)
http://www.lighthouse3d.com/tutorials/glut-tutorial/ GLUT是OpenGL Utility Toolkit的意思.作者Mark J. Kilgar ...
- [译]GLUT教程 - 游戏模式
Lighthouse3d.com >> GLUT Tutorial >> Extras >> Game Mode 根据GLUT官网的说明,GLUT的游戏模式是为开启 ...
- [译]GLUT教程 - glutPostRedisplay函数
Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> glutPostRedisplay 直 ...
- [译]GLUT教程 - 安装
Lighthouse3d.com >> GLUT Tutorial >> Basics >> Setup 你需要什么 要用GLUT库开发程序,你可以下载最新版本3. ...
- [译]GLUT教程 - 整合代码8
Lighthouse3d.com >> GLUT Tutorial >> Avoiding the Idle Func >> The Code So Far VII ...
- [译]GLUT教程 - 整合代码7
Lighthouse3d.com >> GLUT Tutorial >> Extras >> The Code So Far VII 以下是子窗体的最终版本代码. ...
- [译]GLUT教程 - 渲染到子窗体
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Rendering to Subwindows 先回顾一下之前 ...
- [译]GLUT教程 - 重整子窗体
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Reshape Subwindows 重整函数的回调需要处理两 ...
- [译]GLUT教程 - 创建和关闭子窗体
Lighthouse3d.com >> GLUT Tutorial >> Subwindows >> Creating and Destroying Subwind ...
随机推荐
- Python与数据库[1] -> 数据库接口/DB-API[2] -> SQL Server 适配器
SQL_Server适配器 / SQL_Server Adapter 1 环境配置 / Environment Configuration 安装SQL_Server的Python适配器包 pip in ...
- 洛谷——P2656 采蘑菇
P2656 采蘑菇 题目描述 小胖和ZYR要去ESQMS森林采蘑菇. ESQMS森林间有N个小树丛,M条小径,每条小径都是单向的,连接两个小树丛,上面都有一定数量的蘑菇.小胖和ZYR经过某条小径一次, ...
- oracle Update a table with data from another table
UPDATE table1 t1 SET (name, desc) = (SELECT t2.name, t2.desc FROM table2 t2 WHERE t1.id = t2.id) WHE ...
- 集合框架(04)HashMap扩展知识
Map扩展知识 map集合被使用是具备映射关系 “bigclass”: “001”, ”zhangsan” “002”, ”lisi” “smallclass” : ”001”, “wangwu” : ...
- Windows 8.1中WinRT的变化(二)——新增功能
首先我们来看看现有控件中新增的功能: FlipView编程方式切换时支持平滑滚动: 在Windows8中,FlipView在用手触控翻页的时候是有动画效果的,但当我们使用键盘或代码编程翻页时,却没有这 ...
- IdHTTPServer(indy10)开发REST中间件
IdHTTPServer(indy10)开发REST中间件 浏览器通过“get”方式查询数据URL样例:http://127.0.0.1:7777/query?sql=select * from t1 ...
- android 小技巧
1. 模拟器横坚屏切换 ctrl + F11, ctrl + F12
- elasticsearch 插入数据
1.单条插入(推荐设定主键id防止重复) public static String addIndex(String index,String type,HashMap<String, Objec ...
- Angular 学习笔记——ng-disable
<!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <met ...
- Quaternion 四元数
Quaternions are used to represent rotations. 四元数用于表示旋转. They are compact, don't suffer from gimbal l ...