0% found this document useful (0 votes)
6 views3 pages

Rectangle Animation Code

The document contains C++ code for a simple OpenGL animation that displays a moving rectangle. The rectangle moves horizontally across the window, reversing direction when it reaches the edges. The program uses GLUT for window management and rendering, and updates the display at regular intervals.

Uploaded by

odevgirikar05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views3 pages

Rectangle Animation Code

The document contains C++ code for a simple OpenGL animation that displays a moving rectangle. The rectangle moves horizontally across the window, reversing direction when it reaches the edges. The program uses GLUT for window management and rendering, and updates the display at regular intervals.

Uploaded by

odevgirikar05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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;
}

You might also like