0% found this document useful (0 votes)
34 views4 pages

PG 5

Uploaded by

gmaheshreddy229
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)
34 views4 pages

PG 5

Uploaded by

gmaheshreddy229
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
You are on page 1/ 4

#include <GL/glut.

h>

// Variables to store transformation parameters


GLfloat angleX = 0.0f;
GLfloat angleY = 0.0f;
GLfloat scale = 1.0f;
GLfloat translateX = 0.0f;
GLfloat translateY = 0.0f;
GLfloat translateZ = -5.0f;

// Function declarations
void init();
void display();
void reshape(int w, int h);
void keyboard(unsigned char key, int x, int y);

int main(int argc, char** argv) {


// Initialize GLUT and create a window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(800, 600);
glutCreateWindow("3D Transformation on Cube");

// Set up OpenGL
init();

// Register callbacks
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);

// Start the main loop


glutMainLoop();

return 0;
}

void init() {
// Set up the viewport
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}

void display() {
// Clear the color and depth buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// Reset transformations
glLoadIdentity();

// Apply transformations
glTranslatef(translateX, translateY, translateZ);
glScalef(scale, scale, scale);
glRotatef(angleX, 1.0f, 0.0f, 0.0f);
glRotatef(angleY, 0.0f, 1.0f, 0.0f);

// Draw the cube


glBegin(GL_QUADS);
// Front face
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
// Back face
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Top face
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// Bottom face
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
// Left face
glColor3f(0.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();

// Swap buffers
glutSwapBuffers();
}

void reshape(int w, int h) {


// Set the viewport to cover the entire window
glViewport(0, 0, w, h);

// Set the perspective


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)w / (float)h, 0.1f, 100.0f);

// Switch back to the modelview matrix mode


glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y) {


switch (key) {
case 'x':
angleX += 5.0f;
break;
case 'X':
angleX -= 5.0f;
break;
case 'y':
angleY += 5.0f;
break;
case 'Y':
angleY -= 5.0f;
break;
case 's':
scale += 0.1f;
break;
case 'S':
scale -= 0.1f;
break;
case 't':
translateX += 0.1f;
break;
case 'T':
translateX -= 0.1f;
break;
case 'u':
translateY += 0.1f;
break;
case 'U':
translateY -= 0.1f;
break;
case 'z':
translateZ += 0.1f;
break;
case 'Z':
translateZ -= 0.1f;
break;
default:
break;
}

// Redraw the scene


glutPostRedisplay();
}

You might also like