Develop a program to demonstrate basic geometric operations on the 3D object

Develop a program to demonstrate basic geometric operations on the 3D object

Program:

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

// Vertex coordinates of the cube
GLfloat vertices[][3] = {
    {-0.5, -0.5, -0.5},
    {0.5, -0.5, -0.5},
    {0.5, 0.5, -0.5},
    {-0.5, 0.5, -0.5},
    {-0.5, -0.5, 0.5},
    {0.5, -0.5, 0.5},
    {0.5, 0.5, 0.5},
    {-0.5, 0.5, 0.5}
};

// Transformation parameters
GLfloat tx = 0.0, ty = 0.0, tz = 0.0; // Translation
GLfloat sx = 1.0, sy = 1.0, sz = 1.0; // Scaling
GLfloat angleX = 0.0, angleY = 0.0, angleZ = 0.0; // Rotation angles

void drawCube() {
    glBegin(GL_QUADS);
    // Front face
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[3]);
    // Back face
    glVertex3fv(vertices[4]);
    glVertex3fv(vertices[5]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[7]);
    // Left face
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[7]);
    glVertex3fv(vertices[4]);
    // Right face
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[5]);
    // Top face
    glVertex3fv(vertices[3]);
    glVertex3fv(vertices[2]);
    glVertex3fv(vertices[6]);
    glVertex3fv(vertices[7]);
    // Bottom face
    glVertex3fv(vertices[0]);
    glVertex3fv(vertices[1]);
    glVertex3fv(vertices[5]);
    glVertex3fv(vertices[4]);
    glEnd();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    // Apply transformations
    glPushMatrix();
    
    // Translation
    glTranslatef(tx, ty, tz);
    
    // Rotation
    glRotatef(angleX, 1.0, 0.0, 0.0);
    glRotatef(angleY, 0.0, 1.0, 0.0);
    glRotatef(angleZ, 0.0, 0.0, 1.0);
    
    // Scaling
    glScalef(sx, sy, sz);
    
    // Draw the cube
    drawCube();
    
    glPopMatrix();
    
    glFlush();
    glutSwapBuffers();
}

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, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard(unsigned char key, int x, int y) {
    switch (key) {
        case 't': tx += 0.1; break; // Translate right
        case 'r': angleX += 5.0; break; // Rotate around X axis
        case 'f': angleY -= 5.0; break; // Rotate around Y axis
        case 'g': angleZ -= 5.0; break; // Rotate around Z axis
        case 's': sy += 0.1; break; // Scale up vertically
        case 27: exit(0); // ESC key to exit
    }
    glutPostRedisplay();
}

int main(int argc, char** argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("3D Geometric Operations");
    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

Leave a Reply

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