Rectangle Animation Code
#include <GL/freeglut.h>
#include <iostream>
int x = 20;
int flag = 0;
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(0, 400);
glVertex2f(679, 400);
glEnd();
if (flag == 0)
{
x += 2;
if (x >= 665)
flag = 1;
}
else if (flag == 1)
{
x -= 2;
if (x <= 15)
flag = 0;
}
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
glTranslatef(x, 200, 0.0f);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(50, 0);
glVertex2f(50, 50);
glVertex2f(0, 50);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void update(int value)
{
glutPostRedisplay();
glutTimerFunc(10, update, 0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(680, 400);
glutCreateWindow("Moving Rectangle");
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 680, 0, 400);
glutDisplayFunc(display);
glutTimerFunc(10, update, 0);
glutMainLoop();
return 0;
}