sidrs

CGR Ass 7 - 3D Transformations

Dec 15th, 2024
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.64 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <iostream>
  3.  
  4. // Transformation variables
  5. float tx = 0.0f, ty = 0.0f, tz = 0.0f; // Translation
  6. float sx = 1.0f, sy = 1.0f, sz = 1.0f; // Scaling
  7. float angle = 0.0f;                    // Rotation angle
  8. char axis = 'x';                       // Rotation axis
  9.  
  10. // Function to draw a 3D cube
  11. void drawCube() {
  12.     glBegin(GL_QUADS);
  13.     // Front face (z = 1.0)
  14.     glColor3f(1.0f, 0.0f, 0.0f); // Red
  15.     glVertex3f(-1.0f, -1.0f, 1.0f);
  16.     glVertex3f(1.0f, -1.0f, 1.0f);
  17.     glVertex3f(1.0f, 1.0f, 1.0f);
  18.     glVertex3f(-1.0f, 1.0f, 1.0f);
  19.  
  20.     // Back face (z = -1.0)
  21.     glColor3f(0.0f, 1.0f, 0.0f); // Green
  22.     glVertex3f(-1.0f, -1.0f, -1.0f);
  23.     glVertex3f(-1.0f, 1.0f, -1.0f);
  24.     glVertex3f(1.0f, 1.0f, -1.0f);
  25.     glVertex3f(1.0f, -1.0f, -1.0f);
  26.  
  27.     // Top face (y = 1.0)
  28.     glColor3f(0.0f, 0.0f, 1.0f); // Blue
  29.     glVertex3f(-1.0f, 1.0f, -1.0f);
  30.     glVertex3f(-1.0f, 1.0f, 1.0f);
  31.     glVertex3f(1.0f, 1.0f, 1.0f);
  32.     glVertex3f(1.0f, 1.0f, -1.0f);
  33.  
  34.     // Bottom face (y = -1.0)
  35.     glColor3f(1.0f, 1.0f, 0.0f); // Yellow
  36.     glVertex3f(-1.0f, -1.0f, -1.0f);
  37.     glVertex3f(1.0f, -1.0f, 1.0f);
  38.     glVertex3f(1.0f, -1.0f, 1.0f);
  39.     glVertex3f(1.0f, -1.0f, -1.0f);
  40.  
  41.     // Right face (x = 1.0)
  42.     glColor3f(1.0f, 0.0f, 1.0f); // Magenta
  43.     glVertex3f(1.0f, -1.0f, -1.0f);
  44.     glVertex3f(1.0f, 1.0f, -1.0f);
  45.     glVertex3f(1.0f, 1.0f, 1.0f);
  46.     glVertex3f(1.0f, -1.0f, 1.0f);
  47.  
  48.     // Left face (x = -1.0)
  49.     glColor3f(0.0f, 1.0f, 1.0f); // Cyan
  50.     glVertex3f(-1.0f, -1.0f, -1.0f);
  51.     glVertex3f(-1.0f, -1.0f, 1.0f);
  52.     glVertex3f(-1.0f, 1.0f, 1.0f);
  53.     glVertex3f(-1.0f, 1.0f, -1.0f);
  54.     glEnd();
  55. }
  56.  
  57. // Display callback
  58. void display() {
  59.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  60.     glLoadIdentity();
  61.  
  62.     // Apply transformations
  63.     glTranslatef(tx, ty, tz); // Translation
  64.     glScalef(sx, sy, sz);     // Scaling
  65.  
  66.     // Rotation
  67.     if (axis == 'x') glRotatef(angle, 1.0f, 0.0f, 0.0f);
  68.     else if (axis == 'y') glRotatef(angle, 0.0f, 1.0f, 0.0f);
  69.     else if (axis == 'z') glRotatef(angle, 0.0f, 0.0f, 1.0f);
  70.  
  71.     // Draw the cube
  72.     drawCube();
  73.  
  74.     glutSwapBuffers();
  75. }
  76.  
  77. // Keyboard input callback
  78. void keyboard(unsigned char key, int x, int y) {
  79.     switch (key) {
  80.         case 'w': ty += 0.1f; break; // Move up
  81.         case 's': ty -= 0.1f; break; // Move down
  82.         case 'a': tx -= 0.1f; break; // Move left
  83.         case 'd': tx += 0.1f; break; // Move right
  84.         case 'z': tz += 0.1f; break; // Move closer
  85.         case 'x': tz -= 0.1f; break; // Move farther
  86.         case '+': sx += 0.1f; sy += 0.1f; sz += 0.1f; break; // Scale up
  87.         case '-': sx -= 0.1f; sy -= 0.1f; sz -= 0.1f; break; // Scale down
  88.         case 'r': angle += 5.0f; break; // Rotate
  89.         case 'X': axis = 'x'; break; // Rotate about X-axis
  90.         case 'Y': axis = 'y'; break; // Rotate about Y-axis
  91.         case 'Z': axis = 'z'; break; // Rotate about Z-axis
  92.         case 27: exit(0); // Exit on 'Esc' key
  93.     }
  94.     glutPostRedisplay();
  95. }
  96.  
  97. // Reshape callback
  98. void reshape(int w, int h) {
  99.     glViewport(0, 0, w, h);
  100.     glMatrixMode(GL_PROJECTION);
  101.     glLoadIdentity();
  102.     gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
  103.     glMatrixMode(GL_MODELVIEW);
  104. }
  105.  
  106. // Main function
  107. int main(int argc, char** argv) {
  108.     glutInit(&argc, argv);
  109.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  110.     glutInitWindowSize(800, 600);
  111.     glutCreateWindow("3D Cube Transformations");
  112.  
  113.     glEnable(GL_DEPTH_TEST);
  114.     glutDisplayFunc(display);
  115.     glutReshapeFunc(reshape);
  116.     glutKeyboardFunc(keyboard);
  117.  
  118.     glutMainLoop();
  119.     return 0;
  120. }
  121.  
Advertisement
Add Comment
Please, Sign In to add comment