#include <GL/glut.h>
#include<math.h>
#define PI 3.1415926535898
GLfloat BXMAX,BXMIN,BYMAX,BYMIN;
GLint circle_points=100;
float rd=0.2;
GLfloat x=0.0,y=0.0;
GLdouble l,r,b,t;
GLfloat m = 0.02f;
GLfloat n= 0.007f;
void initGL() {
glClearColor(0.0, 0.0, 0.0, 1.0);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
}
void idle() {
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glTranslatef(x, y, 0.00);
glBegin(GL_POLYGON);
int i;
float angle;
glColor3f(1.0,0.0,0.0);
for(i=0;i<circle_points;i++)
{
angle=2*PI*i/circle_points;
glVertex2f((cos(angle))*rd,(sin(angle))*rd);
}
glEnd();
glPopMatrix();
glutSwapBuffers();
x=x+m;
y=y+n;
if(x > BXMAX){
m=-m;
}else if(x < BXMIN){
m=-m;
}if(y > BYMAX){
n=-n;
}else if(y < BYMIN){
n=-n;
}
}
void reshape(int width, int height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)width / (GLfloat)height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width >= height) {
l=-1.0*aspect;
r=1.0*aspect;
b=-1.0;
t= 1.0;
} else {
l=-1.0;
r=1.0;
b=-1.0/aspect;
t= 1.0/aspect;
}
gluOrtho2D(l,r,b,t);
BXMAX=r-(rd);
BXMIN=l+(rd);
BYMAX=t-(rd);
BYMIN=b+(rd);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutInitWindowPosition(50, 50);
glutCreateWindow("ball");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(idle);
initGL();
glutMainLoop();
return 0;
}
No comments:
Post a Comment