0% found this document useful (0 votes)
20 views7 pages

OpenGL Color Cube Rotation Tutorial

The document outlines an assignment to create and rotate a 3D color cube using OpenGL transformation matrices. It includes a program that defines the cube's vertices and colors, as well as functions for displaying and spinning the cube. The conclusion emphasizes the successful demonstration of 3D transformations in computer graphics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views7 pages

OpenGL Color Cube Rotation Tutorial

The document outlines an assignment to create and rotate a 3D color cube using OpenGL transformation matrices. It includes a program that defines the cube's vertices and colors, as well as functions for displaying and spinning the cube. The conclusion emphasizes the successful demonstration of 3D transformations in computer graphics.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment No.

4
Name: Ankit chaudhari

PRN: 202401100034

Batch: A2

RollN0: 24

Problem Statement:

Draw a color cube and spin it using OpenGL transformation matrices.


Theory:
Program:
#include <GL/glut.h>
float angleX = 0.0f; // rotation angle around X-axis
float angleY = 0.0f; // rotation angle around Y-axis
void drawCube() { glBegin(GL_QUADS);
glColor3f(0.0, 1.0, 1.0); glVertex3f( 1.0, 1.0, 1.0);
glVertex3f(-1.0, 1.0, 1.0); glVertex3f(-1.0,-1.0,
1.0); glVertex3f( 1.0,-1.0, 1.0);

// Back Face (Red)


glColor3f(1.0, 0.0, 0.0);
glVertex3f( 1.0, 1.0,-1.0);
glVertex3f(-1.0, 1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f( 1.0,-1.0,-1.0);

// Top Face (Green)


glColor3f(0.0, 1.0, 0.0);
glVertex3f( 1.0, 1.0,-1.0);
glVertex3f(-1.0, 1.0,-1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f( 1.0, 1.0, 1.0);

// Bottom Face (Blue)


glColor3f(0.0, 0.0, 1.0);
glVertex3f( 1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0,-1.0);
glVertex3f(-1.0,-1.0, 1.0);
glVertex3f( 1.0,-1.0, 1.0); //
Right Face (Yellow) glColor3f(1.0,
1.0, 0.0); glVertex3f( 1.0, 1.0,-
1.0); glVertex3f( 1.0, 1.0, 1.0);
glVertex3f( 1.0,-1.0, 1.0);
glVertex3f( 1.0,-1.0,-1.0);
// Left Face (Magenta)
glColor3f(1.0, 0.0, 1.0);
glVertex3f(-1.0, 1.0,-1.0);
glVertex3f(-1.0, 1.0, 1.0);
glVertex3f(-1.0,-1.0, 1.0);
glVertex3f(-1.0,-1.0,-1.0);

glEnd();
}

// Display function void


display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// Move the cube away from camera
glTranslatef(0.0f, 0.0f, -7.0f);

// Rotate the cube


glRotatef(angleX, 1.0f, 0.0f, 0.0f);
glRotatef(angleY, 0.0f, 1.0f, 0.0f);

// Draw cube
drawCube();
glutSwapBuffers();

// Function to update rotation angles


void spinCube() { angleX += 0.2f;
angleY += 0.3f;

if (angleX > 360) angleX -= 360;


if (angleY > 360) angleY -= 360;
glutPostRedisplay(); // Trigger next frame
}

// Initialization void
init() {
glClearColor(0.0, 0.0, 0.0, 1.0); // background black
glEnable(GL_DEPTH_TEST); // enable 3D depth

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}

// Main
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600); glutCreateWindow("Color Cube
Rotation");

init();
glutDisplayFunc(display);
glutIdleFunc(spinCube); // continuously spin the cube
glutMainLoop(); return 0;
}
Output:
Conclusion
The experiment successfully demonstrates how a 3D color cube can be
created and rotated using OpenGL transformation matrices. By
applying rotation transformations, the cube spins smoothly around its
axes, and the use of different colors on each face helps visualize its
orientation in 3D space.
This experiment clearly illustrates the practical application of
translation, rotation, scaling, and viewing transformations in
computer graphics for creating animated 3D objects.

You might also like