首页 > 代码库 > Opengl
Opengl
移动的方块
代码:
#include <windows.h>//#include <GLUT/glut.h>#include <GL/glut.h>#include <iostream>using namespace std;GLfloat x1 = 0.0f;GLfloat y1 = 0.0f;GLfloat rsize = 25;GLfloat xstep = 1.0f;GLfloat ystep = 1.0f;GLfloat windowWidth;GLfloat windowHeight;void RenderScene(){ glClear(GL_COLOR_BUFFER_BIT); glColor3f(1.0f,0.0f,0.0f); cout<<"(x1,y1)= ("<<x1<<","<<y1<<") (x2,y2)= ("<<(x1+rsize)<<","<<y1-rsize<<")"<<endl; glRectf(x1,y1,x1+rsize,y1-rsize); glutSwapBuffers();}void TimerFunction(int value){ cout<<"WindowWidth: "<<windowWidth<<" WindowHeight: "<<windowHeight<<endl; 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);}void ChangeSize(GLsizei w,GLsizei h){ if(h==0) h = 1; GLfloat aspectRatio = (GLfloat)w/(GLfloat)h; glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if(w<h) { glOrtho(-100.0,100.0,-100.0/aspectRatio,100.0/aspectRatio,1.0,-1.0); windowWidth = 100; windowHeight = 100/aspectRatio; } else { glOrtho(-100.0*aspectRatio,100.0*aspectRatio,-100.0,100.0,1.0,-1.0); windowWidth = 100*aspectRatio; windowHeight = 100; } glMatrixMode(GL_MODELVIEW); glLoadIdentity();}void SetupRC(){ glClearColor(0.0f,0.0f,1.0f,1.0f);}int main(int argc, char *argv[]){ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(800,600); glutCreateWindow("Simple"); glutDisplayFunc(RenderScene); glutReshapeFunc(ChangeSize); glutTimerFunc(33,TimerFunction,1); SetupRC(); glutMainLoop(); return 0;}
Opengl
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。