#include <glut.h>
#include <stdlib.h>
#include<stdio.h>
#include<math.h>
#define p 3.1415926
float x,y,dx,dy;
float s,c;
void init(void) {
glClearColor (0.0, 1.0, 1.0, 0.0);
glShadeModel (GL_FLAT);
}
///该函数用于设置渲染状态
void SetupRC(void)
{
glClearColor(1.0f, 0.0f, 1.0f,1.0f); //设置背景的颜色
}
void display(void){
s=p*c/180;
if(c<=45){
dx=0.002;
dy=0.002*tan(s);
while(x<3&&x>-3&&y<3&&y>-3){
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 1.0);
glPushMatrix();
if((x-0.2)<(-2.86))
{dx=0.002;}
if((y-0.2)<(-2.86))
{dy=0.002*tan(s);}
if((x+0.2)>2.86){
dx=-0.002;}
if((y+0.2)>2.86)
{dy=-0.002*tan(s);}
x=x+dx;
y=y+dy;
glTranslatef (x, y, 0.0);
glRotatef ((GLfloat) 0.0, 0.0, 0.0, 0.0);
glutWireSphere(0.2, 100, 100); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
}
}
else{
dy=0.002;
dx=0.002/tan(s);
while(y<3&&y>-3&&x<3&&x>-3){
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 0.0, 1.0);
glPushMatrix();
if((y+0.2)>2.86){
dy=-0.002;}
if((x+0.2)>2.86)
{dx=-0.002/tan(s);}
if((y-0.2)<-2.86)
{dy=0.002;}
if((x-0.2)<-2.86)
{dx=0.002/tan(s);}
x=x+dx;
y=y+dy;
glTranslatef (x, y, 0.0);
glRotatef ((GLfloat) 0.0, 0.0, 0.0, 0.0);
glutWireSphere(0.2, 100, 100); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
}
}
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
int main(int argc, char** argv){
printf("请输入小球弹射的角度(0~90):");
scanf("%f",&c);
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}