I've started OpenGL animation and I am trying to create a program that moves a dot up and down. It's meant to work by if functions, keeping track of when my variable for transformations (y axis) goes above or below 0.9 and -0.9 respectively.
However, on running the applications I briefly see the dot just fly out the top of the window and I can't work out what's going wrong with it. Could anybody take a look and see if they know the problem? Thank you so much - here's the code:
Code:
#include <glut.h>
void display(void);
void timer(void);
float amount = 0.91;
int main(int argc , char **argv)
{
glutInit(&argc , argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(600 , 300);
glutInitWindowPosition(250 , 250);
glutCreateWindow("moving dot");
glutDisplayFunc(display);
glutTimerFunc(500 , timer , 0);
glClearColor(0.0 , 0.0 , 0.0 , 1.0);
glColor3f(1.0 , 1.0 , 1.0);
glutMainLoop();
}
void timer(void)
{
if(amount > 0.9)
{
amount = amount - 0.01;
}
else if(amount < -0.9)
{
amount = amount + 0.01;
}
glTranslatef(0.0 , amount , 0.0);
glutPostRedisplay();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(5);
glBegin(GL_POINTS);
glVertex2f(0.0 , 0.0);
glEnd();
glutSwapBuffers();
glFlush();
glutTimerFunc(500 , timer , 0);
}