《OpenGL超级宝典》P38“2.6使用OpenGL和GLUT生成动画”勘误
来源:互联网 发布:淘宝的旅游团怎么样 编辑:程序博客网 时间:2024/05/21 19:31
原文来自:http://zerfew.blog.163.com/blog/static/1938101692012730111051342/
书链接:《OpenGL超级宝典》
书中的源代码本来就是不全的,根据main中的函数调用,源代码缺失了ChangeSize()和SetupRC()两个函数。所以,将前一节两个函数粘贴过来,编译没有问题,运行后的小方块在窗口中心位置第一象限和第三象限不停闪耀。
#include <windows.h>//#include "glee.h"#include <gl\gl.h>#include <gl\glu.h>#include <gl\glut.h>//方块的初始位置和大小GLfloat x1=0.0f;GLfloat y1=0.0f;GLfloat rsize=25;//在x和y方向的步进大小GLfloat xstep=1.0f;GLfloat ystep=1.0f;//追踪窗口宽度和高度的变化GLfloat windowWidth;GLfloat windowHeight;//绘制场景void RenderScene(void){ //用当前清除颜色清楚窗口 glClear(GL_COLOR_BUFFER_BIT); //把绘图颜色设置为红色 glColor3f(1.0f,0.0f,0.0f); //用当前颜色绘制一个填充矩形 glRectf(x1,y1,x1+rsize,y1-rsize); //刷新绘制命令并进行交换 //glFlush(); glutSwapBuffers();}//设置渲染状态void SetupRC(void){ glClearColor(0.0f,0.0f,1.0f,1.0f);}//设置渲染状态void ChangeSize(GLsizei w,GLsizei h){ GLfloat aspectRatio; //防止被0所除 if(h==0) h=1; //把视口设置为窗口的大小 glViewport(0,0,w,h); //重置坐标系统 glMatrixMode(GL_PROJECTION); glLoadIdentity(); //建立裁剪区域(左、右、底、顶、近、远) aspectRatio=(GLfloat)w/(GLfloat)h; if(w<=h) glOrtho(-100.0,100.0,-100.0/aspectRatio,100.0/aspectRatio,1.0,-1.0); else glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100.0,100.0,1.0,-1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}//当空闲时由GLUT函数库调用void TimerFunction(int value){ //在到达左边或右边时翻转方向 if(x1>windowWidth-rsize||x1<-windowWidth) xstep=-xstep; //在到达顶边或底边时翻转方向 if(y1>windowHeight||y1<-windowHeight+rsize) ystep=-ystep; //实际移动方块 x1+=xstep; y1+=ystep; //检查边界 if(x1>(windowWidth-rsize+xstep)) x1=windowWidth-rsize-1; else if(x1<-(windowWidth+xstep)) x1=-windowWidth-1; if(y1>(windowHeight+ystep)) y1=windowHeight-1; else if(y1<-(windowHeight-rsize+ystep)) y1=-windowHeight+rsize-1; //用新坐标重新绘制场景 glutPostRedisplay(); glutTimerFunc(33,TimerFunction,1);}int main(int argc,char* argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(800,600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutTimerFunc(33,TimerFunction,1); SetupRC(); glutMainLoop(); return 0;}
#include <windows.h>//#include "glee.h"#include <gl\gl.h>#include <gl\glu.h>#include <gl\glut.h>//方块的初始位置和大小GLfloat x1=0.0f;GLfloat y1=0.0f;GLfloat rsize=25;//在x和y方向的步进大小GLfloat xstep=1.0f;GLfloat ystep=1.0f;//追踪窗口宽度和高度的变化GLfloat windowWidth=100.0f;GLfloat windowHeight=100.0f;//绘制场景void RenderScene(void){ //用当前清除颜色清楚窗口 glClear(GL_COLOR_BUFFER_BIT); //把绘图颜色设置为红色 glColor3f(1.0f,0.0f,0.0f); //用当前颜色绘制一个填充矩形 glRectf(x1,y1,x1+rsize,y1-rsize); //刷新绘制命令并进行交换 //glFlush(); glutSwapBuffers();}//设置渲染状态void SetupRC(void){ glClearColor(0.0f,0.0f,1.0f,1.0f);}//设置渲染状态void ChangeSize(GLsizei w,GLsizei h){
windowWidth=100.0f; windowHeight=100.0f; GLfloat aspectRatio; //防止被0所除 if(h==0) h=1; //把视口设置为窗口的大小 glViewport(0,0,w,h); //重置坐标系统 glMatrixMode(GL_PROJECTION); glLoadIdentity(); //建立裁剪区域(左、右、底、顶、近、远) aspectRatio=(GLfloat)w/(GLfloat)h; if(w<=h) { windowHeight=windowHeight/aspectRatio; glOrtho(-windowWidth,windowWidth,-windowHeight,windowHeight,1.0,-1.0); } else { windowWidth*=aspectRatio; glOrtho(-windowWidth,windowWidth,-windowHeight,windowHeight,1.0,-1.0); } glMatrixMode(GL_MODELVIEW); glLoadIdentity();}//当空闲时由GLUT函数库调用void TimerFunction(int value){ //在到达左边或右边时翻转方向 if(x1>windowWidth-rsize||x1<-windowWidth) xstep=-xstep; //在到达顶边或底边时翻转方向 if(y1>windowHeight||y1<-windowHeight+rsize) ystep=-ystep; //实际移动方块 x1+=xstep; y1+=ystep; //检查边界 if(x1>(windowWidth-rsize+xstep)) x1=windowWidth-rsize-1; else if(x1<-(windowWidth+xstep)) x1=-windowWidth-1; if(y1>(windowHeight+ystep)) y1=windowHeight-1; else if(y1<-(windowHeight-rsize+ystep)) y1=-windowHeight+rsize-1; //用新坐标重新绘制场景 glutPostRedisplay(); glutTimerFunc(33,TimerFunction,1);}int main(int argc,char* argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(800,600); glutCreateWindow("Bounce"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutTimerFunc(33,TimerFunction,1); SetupRC(); glutMainLoop(); return 0;}
- 《OpenGL超级宝典》P38“2.6使用OpenGL和GLUT生成动画”勘误
- 使用OpenGL和GLUT,结合双缓冲区生成动画
- 使用OpenGL和GLUT,结合双缓冲区生成动画
- OpenGL超级宝典 - 第一章
- OPENGL超级宝典-6.6
- 使用OpenGL中的GLUT
- opengl和glut
- OpenGL---GLUT教程(四) GLUT动画
- OpenGL---GLUT教程(四) GLUT动画
- OpenGL---GLUT教程(四) GLUT动画
- 《OpenGL超级宝典》笔记(一)
- OpenGL超级宝典笔记(三)
- OpenGL超级宝典笔记4
- OpenGL超级宝典笔记5
- OpenGL超级宝典笔记6
- OpenGL超级宝典笔记7
- OpenGL超级宝典笔记8
- OpenGL 超级宝典 读书笔记-1
- UltraEdit中Matlab语法高亮显示的操作方法
- Linux改变文件夹访问权限
- java map容器类实现统计功能
- C++编程规范---第11章 其它编程经验
- C++编程规范---第10章 类的继承与组合
- 《OpenGL超级宝典》P38“2.6使用OpenGL和GLUT生成动画”勘误
- 软件工程总结——传统软件开发方法与面向对象开发方法的比较
- vmware网络连接外一篇
- 全面解读WM_NOTIFY
- 《我是一只IT小小鸟》 读书笔记
- 实现Textview 有垂直滚动条
- windows下androidNDK环境配置
- 图像处理和计算机视觉中的经典论文
- 配置samba的操作