Develop a program to demonstrate Animation effects on simple objects.

Develop a program to demonstrate Animation effects on simple objects.

Program:

#include <GL/glut.h>
#include <cmath>

GLfloat angle = 0.0; // Initial angle of rotation

void drawCube() {
    glutWireCube(1.0); // Draw a wireframe cube
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    // Apply rotation and draw cube
    glPushMatrix();
    glTranslatef(0.0, 0.0, -5.0);
    glRotatef(angle, 1.0, 1.0, 1.0); // Rotate around (1,1,1) axis
    glColor3f(1.0, 0.0, 0.0); // Red color for cube
    drawCube();
    glPopMatrix();

    glutSwapBuffers();
}

void update(int value) {
    angle += 2.0; // Increment the angle of rotation
    if (angle > 360) {
        angle -= 360; // Keep the angle within 0-360 range
    }

    glutPostRedisplay(); // Tell GLUT to redraw the screen
    glutTimerFunc(16, update, 0); // Call update function in 16 milliseconds
}

void init() {
    glClearColor(0.0, 0.0, 0.0, 1.0);
    glColor3f(1.0, 1.0, 1.0);
    glEnable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    gluPerspective(45.0, 1.0, 1.0, 10.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -5.0, 0.0, 1.0, 0.0);
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Animation Effects");
    init();
    glutDisplayFunc(display);
    glutTimerFunc(25, update, 0); // Call update function every 25 milliseconds
    glutMainLoop();
    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *