#include <GL/gl.h>
#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265f
char title[]="Bouncing Ball (2D)";
int windowWidth=640;
int windowHeight=480;
int windowPosx=50;
int windowPosy=50;
GLfloat ballRadius=0.5f;
GLfloat ballx=0.0f;
GLfloat bally=0.0f;
GLfloat ballXMax, ballXMin, ballYMax, ballYMin;
GLfloat xSpeed=0.02f;
GLfloat ySpeed=0.007f;
int refreshMills=30;
GLdouble clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop;
void initGL()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(ballx,bally,0.0f);
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(0.0f,0.0f);
int numSegments=100;
GLfloat angle;
for(int i=0; i<=numSegments; i++)
{
angle=i*2.0f*PI/numSegments;
glVertex2f(cos(angle)*ballRadius,sin(angle)*ballRadius);
}
glEnd();
#include <GL/glut.h>
#include <math.h>
#define PI 3.14159265f
char title[]="Bouncing Ball (2D)";
int windowWidth=640;
int windowHeight=480;
int windowPosx=50;
int windowPosy=50;
GLfloat ballRadius=0.5f;
GLfloat ballx=0.0f;
GLfloat bally=0.0f;
GLfloat ballXMax, ballXMin, ballYMax, ballYMin;
GLfloat xSpeed=0.02f;
GLfloat ySpeed=0.007f;
int refreshMills=30;
GLdouble clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop;
void initGL()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(ballx,bally,0.0f);
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f,0.0f,0.0f);
glVertex2f(0.0f,0.0f);
int numSegments=100;
GLfloat angle;
for(int i=0; i<=numSegments; i++)
{
angle=i*2.0f*PI/numSegments;
glVertex2f(cos(angle)*ballRadius,sin(angle)*ballRadius);
}
glEnd();
glutSwapBuffers();
ballx +=xSpeed;
bally +=ySpeed;
if(ballx > ballXMax)
{
ballx=ballXMax;
xSpeed=-xSpeed;
}
else if(ballx < ballXMin)
{
ballx=ballXMin;
xSpeed=-xSpeed;
}
if(bally > ballYMax)
{
bally=ballYMax;
ySpeed=-ySpeed;
}
else if(bally < ballYMin)
{
bally=ballYMin;
ySpeed=-ySpeed;
}
}
void reshape(GLsizei width, GLsizei height)
{
if(height==0) height=1;
GLfloat aspect=(GLfloat)width/((GLfloat) height);
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(width>=height)
{
clipAreaXLeft=-1.0*aspect;
clipAreaXRight=1.0*aspect;
clipAreaYBottom=-1.0;
clipAreaYTop=1.0;
}
ballx +=xSpeed;
bally +=ySpeed;
if(ballx > ballXMax)
{
ballx=ballXMax;
xSpeed=-xSpeed;
}
else if(ballx < ballXMin)
{
ballx=ballXMin;
xSpeed=-xSpeed;
}
if(bally > ballYMax)
{
bally=ballYMax;
ySpeed=-ySpeed;
}
else if(bally < ballYMin)
{
bally=ballYMin;
ySpeed=-ySpeed;
}
}
void reshape(GLsizei width, GLsizei height)
{
if(height==0) height=1;
GLfloat aspect=(GLfloat)width/((GLfloat) height);
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(width>=height)
{
clipAreaXLeft=-1.0*aspect;
clipAreaXRight=1.0*aspect;
clipAreaYBottom=-1.0;
clipAreaYTop=1.0;
}
else
{
clipAreaXLeft=-1.0;
clipAreaXRight=1.0;
clipAreaYBottom=-1.0/aspect;
clipAreaYTop=1.0/aspect;
{
clipAreaXLeft=-1.0;
clipAreaXRight=1.0;
clipAreaYBottom=-1.0/aspect;
clipAreaYTop=1.0/aspect;
}
gluOrtho2D(clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop);
ballXMin=clipAreaXLeft+ballRadius;
ballXMax=clipAreaXRight-ballRadius;
ballYMin=clipAreaYBottom+ballRadius;
ballYMax=clipAreaYTop-ballRadius;
}
void Timer(int value)
{
glutPostRedisplay();
glutTimerFunc(refreshMills,Timer,0);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(windowPosx,windowPosy);
glutCreateWindow(title);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0,Timer,0);
initGL();
glutMainLoop();
return 0;
}
gluOrtho2D(clipAreaXLeft,clipAreaXRight,clipAreaYBottom,clipAreaYTop);
ballXMin=clipAreaXLeft+ballRadius;
ballXMax=clipAreaXRight-ballRadius;
ballYMin=clipAreaYBottom+ballRadius;
ballYMax=clipAreaYTop-ballRadius;
}
void Timer(int value)
{
glutPostRedisplay();
glutTimerFunc(refreshMills,Timer,0);
}
int main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(windowPosx,windowPosy);
glutCreateWindow(title);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0,Timer,0);
initGL();
glutMainLoop();
return 0;
}

Comments
Post a Comment