glut生成opengl反弹小方块效果
2014-05-02 17:21
453 查看
利用opengl实现了反弹小方块的效果,蓝宝书代码并不全,所以自己实现了没有缺少的部分。
#include "../../shared/gltools.h"
GLfloat x1 = 0.0f;
GLfloat y1 = 0.0f;
GLfloat rSize = 50.0f;
GLfloat xStep = 5.0f;
GLfloat yStep = 5.0f;
GLfloat windowWidth = 400;
GLfloat windowHeight = 300;
void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rSize,y1-rSize);
glutSwapBuffers();
}
void Change(GLsizei w,GLsizei h){
GLfloat aspectRatio;
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w/(GLfloat)h;
if(w<=h){
glOrtho(-h/2,h/2,-w/2*aspectRatio,w/2*aspectRatio,1.0,-1.0);
windowWidth = w/2*aspectRatio;
windowHeight = h/2;
}
else{
glOrtho(-h/2*aspectRatio,h/2*aspectRatio,-w/2,w/2,1.0,-1.0);
windowWidth = h/2*aspectRatio;
windowHeight = w/2;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void SetClearColor(void){
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
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("lqwang");
glutDisplayFunc(RenderScene);
glutReshapeFunc(Change);
glutTimerFunc(33,TimerFunction,1);
SetClearColor();
glutMainLoop();
return 0;
}
#include "../../shared/gltools.h"
GLfloat x1 = 0.0f;
GLfloat y1 = 0.0f;
GLfloat rSize = 50.0f;
GLfloat xStep = 5.0f;
GLfloat yStep = 5.0f;
GLfloat windowWidth = 400;
GLfloat windowHeight = 300;
void RenderScene(void){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f,0.0f,0.0f);
glRectf(x1,y1,x1+rSize,y1-rSize);
glutSwapBuffers();
}
void Change(GLsizei w,GLsizei h){
GLfloat aspectRatio;
if(h==0)
h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
aspectRatio = (GLfloat)w/(GLfloat)h;
if(w<=h){
glOrtho(-h/2,h/2,-w/2*aspectRatio,w/2*aspectRatio,1.0,-1.0);
windowWidth = w/2*aspectRatio;
windowHeight = h/2;
}
else{
glOrtho(-h/2*aspectRatio,h/2*aspectRatio,-w/2,w/2,1.0,-1.0);
windowWidth = h/2*aspectRatio;
windowHeight = w/2;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void SetClearColor(void){
glClearColor(0.0f,0.0f,1.0f,1.0f);
}
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("lqwang");
glutDisplayFunc(RenderScene);
glutReshapeFunc(Change);
glutTimerFunc(33,TimerFunction,1);
SetClearColor();
glutMainLoop();
return 0;
}
相关文章推荐
- 《OpenGL超级宝典》P38“2.6使用OpenGL和GLUT生成动画”勘误
- 使用OpenGL和GLUT,结合双缓冲区生成动画
- 使用OpenGL和GLUT,结合双缓冲区生成动画
- android 滚动条下拉反弹的效果(类似微信朋友圈)
- opengl中的GLUT环境配置
- linux 下 Qt 5.x OpenGl 下GLUT 调用
- android随机生成圆形云标签效果
- 点击Ajax.Action生成的链接后,没有启用ajax请求,而是普通超链接的效果
- 【OpenGL】GLSurfaceView绘制bitmap图片及glViewport调整的效果
- Android官方开发文档Training系列课程中文版:OpenGL绘图之添加动态效果
- OpenGL开发学习指南一(freeglut+glew)
- 在 visual studio 2015 下配置 opengl (GLU/GLUT以及glew/freeglut)
- OpenGL Bezier曲线及曲面生成
- php lcg_value与mt_rand生成0~1随机小数的效果对比分析
- vue生成文件本地打开查看效果的实例
- 利用glut和glew查看openGL的版本以及支持扩展的信息等
- OpenGL无法打开文件“freeglut.lib”错误的解决办法
- 如何使用SVG生成超酷的页面预加载素描动画效果
- 关于JQ的Easyui框架,动态生成的dom没有效果,没被渲染
- 使用jQuery生成div元素闪烁效果