Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <iostream>
- // Transformation variables
- float tx = 0.0f, ty = 0.0f, tz = 0.0f; // Translation
- float sx = 1.0f, sy = 1.0f, sz = 1.0f; // Scaling
- float angle = 0.0f; // Rotation angle
- char axis = 'x'; // Rotation axis
- // Function to draw a 3D cube
- void drawCube() {
- glBegin(GL_QUADS);
- // Front face (z = 1.0)
- glColor3f(1.0f, 0.0f, 0.0f); // Red
- 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 (z = -1.0)
- glColor3f(0.0f, 1.0f, 0.0f); // Green
- 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 (y = 1.0)
- glColor3f(0.0f, 0.0f, 1.0f); // Blue
- 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 (y = -1.0)
- glColor3f(1.0f, 1.0f, 0.0f); // Yellow
- 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 (x = 1.0)
- glColor3f(1.0f, 0.0f, 1.0f); // Magenta
- 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 (x = -1.0)
- glColor3f(0.0f, 1.0f, 1.0f); // Cyan
- 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();
- }
- // Display callback
- void display() {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- // Apply transformations
- glTranslatef(tx, ty, tz); // Translation
- glScalef(sx, sy, sz); // Scaling
- // Rotation
- if (axis == 'x') glRotatef(angle, 1.0f, 0.0f, 0.0f);
- else if (axis == 'y') glRotatef(angle, 0.0f, 1.0f, 0.0f);
- else if (axis == 'z') glRotatef(angle, 0.0f, 0.0f, 1.0f);
- // Draw the cube
- drawCube();
- glutSwapBuffers();
- }
- // Keyboard input callback
- void keyboard(unsigned char key, int x, int y) {
- switch (key) {
- case 'w': ty += 0.1f; break; // Move up
- case 's': ty -= 0.1f; break; // Move down
- case 'a': tx -= 0.1f; break; // Move left
- case 'd': tx += 0.1f; break; // Move right
- case 'z': tz += 0.1f; break; // Move closer
- case 'x': tz -= 0.1f; break; // Move farther
- case '+': sx += 0.1f; sy += 0.1f; sz += 0.1f; break; // Scale up
- case '-': sx -= 0.1f; sy -= 0.1f; sz -= 0.1f; break; // Scale down
- case 'r': angle += 5.0f; break; // Rotate
- case 'X': axis = 'x'; break; // Rotate about X-axis
- case 'Y': axis = 'y'; break; // Rotate about Y-axis
- case 'Z': axis = 'z'; break; // Rotate about Z-axis
- case 27: exit(0); // Exit on 'Esc' key
- }
- glutPostRedisplay();
- }
- // Reshape callback
- void reshape(int w, int h) {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
- glMatrixMode(GL_MODELVIEW);
- }
- // Main function
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- glutCreateWindow("3D Cube Transformations");
- glEnable(GL_DEPTH_TEST);
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment