www.pudn.com > Bounce-opengl.rar > Bounceone.cpp


#include  
#include  
#include  
#include  
#include  
#define GLUT_DISABLE_ATEXIT_HACK 
 
GLfloat x1=100.0f; 
GLfloat y1=100.0f; 
GLsizei rsize=50; 
 
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); 
glRotatef(45,0,0,1); 
glScalef(1.0,0.5,1.0); 
 
glutSwapBuffers(); 
} 
 
void TimerFunction(int value) 
{ 
if(x1>windowWidth-rsize || x1<0) 
xstep=-xstep; 
 
if(y1>windowWidth-rsize || y1<0) 
ystep=-ystep; 
 
if(x1>windowWidth-rsize) 
x1=windowWidth-rsize-1; 
 
if(y1>windowHeight-rsize) 
y1=windowHeight-rsize-1; 
 
x1+=xstep; 
y1+=ystep; 
 
glutPostRedisplay(); 
glutTimerFunc(922,TimerFunction,1); 
} 
 
void SetupRC(void) 
{ 
glClearColor(0.0f,0.0f,1.0f,1.0f); 
} 
 
void ChangeSize(GLsizei w,GLsizei h) 
{ 
if(h==0) 
h=1; 
 
glViewport(0,0,w,h); 
 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
 
if(w<=h) 
{ 
windowHeight=250.0f*h/w; 
windowWidth=250.0f; 
} 
 
else 
{ 
windowHeight=250.0f*h/w; 
windowWidth=250.0f; 
} 
 
glOrtho(0.0f,windowWidth,0.0f,windowHeight,1.0f,-1.0f); 
 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 
} 
 
void main(void) 
{ 
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); 
glutCreateWindow("Bounce"); 
glutDisplayFunc(RenderScene); 
glutReshapeFunc(ChangeSize); 
glutTimerFunc(922,TimerFunction,1); 
 
SetupRC(); 
glutMainLoop(); 
}