I have to develop a small application in which a ball moves from left to right.
- The application should run only for 3 minutes.And the speed of the ball should be increased with every minute.
- On key press like up button the ball should jump
I learnt that for time glutTimer function has to be used and for keypress jump i have to use keypressed event and just have to play with coordinates of ball.
But i don’t know how to write the code for above 2 things?
Here is the following code which i have written:
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#define ESCAPE 27
int window;
static int flag=1;
float rtri = 0.10f;
void InitGL(int Width, int Height)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
void ReSizeGLScene(int Width, int Height)
{
if (Height==0)
Height=1;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)Width/(GLfloat)Height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
}
float ballX = -0.8f;
float ballY = -0.3f;
float ballZ = -1.2f;
void drawBall(void) {
glColor3f(0.0, 1.0, 0.0);
glTranslatef(ballX,ballY,ballZ);
glutSolidSphere (0.3, 20, 20);
glTranslatef(ballX+1.5,ballY,ballZ);
}
void DrawGLScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(rtri,0.0f,-6.0f);
drawBall();
rtri+=0.01f;
if(rtri>3)
rtri=-3.0f;
glutSwapBuffers();
}
void keyPressed(unsigned char key, int x, int y)
{
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(640, 480);
glutInitWindowPosition(0, 0);
window = glutCreateWindow(“Ball Game”);
glutDisplayFunc(&DrawGLScene);
glutIdleFunc(&DrawGLScene);
glutReshapeFunc(&ReSizeGLScene);
glutKeyboardFunc(&keyPressed);
InitGL(640, 480);
// glutTimerFunc(25, update, 0);
glutMainLoop();
return 1;
}