Advertisement
gurumutant

LinuxJournal - OpenGL Cube

Aug 9th, 2017
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.98 KB | None | 0 0
  1. // Programmer: Mihalis Tsoukalos
  2. // Date: Wednesday 04 June 2014
  3. //
  4. // A simple OpenGL program that draws a colorful cube
  5. // that rotates as you move the arrow keys!
  6. //
  7. // g++ cube.cc -lm -lglut -lGL -lGLU -o cube
  8.  
  9. #define GL_GLEXT_PROTOTYPES
  10. #ifdef __APPLE__
  11. #include <GLUT/glut.h>
  12. #else
  13. #include <GL/glut.h>
  14. #endif
  15. #include <math.h>
  16.  
  17. // Rotate X
  18. double rX=0;
  19. // Rotate Y
  20. double rY=0;
  21.  
  22. // The coordinates for the vertices of the cube
  23. double x = 0.6;
  24. double y = 0.6;
  25. double z = 0.6;
  26.  
  27. void drawCube()
  28. {
  29.         // Set Background Color
  30.     glClearColor(0.4, 0.4, 0.4, 1.0);
  31.         // Clear screen
  32.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  33.  
  34.     // Reset transformations
  35.     glLoadIdentity();
  36.  
  37.     // Rotate when user changes rX and rY
  38.     glRotatef( rX, 1.0, 0.0, 0.0 );
  39.     glRotatef( rY, 0.0, 1.0, 0.0 );
  40.  
  41.     // BACK
  42.         glBegin(GL_TRIANGLES);
  43.             glColor3f(0.4, 0.3, 0.5);
  44.                 glVertex3f(x, y, z);
  45.                 glVertex3f(x, -y, z);
  46.                 glVertex3f(-x, y, z);
  47.         glEnd();
  48.  
  49.         glBegin(GL_TRIANGLES);
  50.             glColor3f(0.5, 0.3, 0.2);
  51.                 glVertex3f(-x, -y, z);
  52.                 glVertex3f(x, -y, z);
  53.                 glVertex3f(-x, y, z);
  54.         glEnd();
  55.  
  56.         // FRONT
  57.         // Using 4 trianges!
  58.         glBegin(GL_TRIANGLES);
  59.             glColor3f(0.1, 0.5, 0.3);
  60.                 glVertex3f(-x, y, -z);
  61.                 glVertex3f(0, 0, -z);
  62.                 glVertex3f(-x, -y, -z);
  63.         glEnd();
  64.  
  65.         glBegin(GL_TRIANGLES);
  66.                 glColor3f(0.0, 0.5, 0.0);
  67.                 glVertex3f(-x, -y, -z);
  68.                 glVertex3f(0, 0, -z);
  69.                 glVertex3f(x, -y, -z);
  70.         glEnd();
  71.  
  72.         glBegin(GL_TRIANGLES);
  73.             glColor3f(0.1, 0.3, 0.3);
  74.                 glVertex3f(-x, y, -z);
  75.                 glVertex3f(x, y, -z);
  76.                 glVertex3f(0, 0, -z);
  77.         glEnd();
  78.  
  79.         glBegin(GL_TRIANGLES);
  80.                 glColor3f(0.2, 0.2, 0.2);
  81.                 glVertex3f(0, 0, -z);
  82.                 glVertex3f(x, y, -z);
  83.                 glVertex3f(x, -y, -z);
  84.         glEnd();
  85.  
  86.         // LEFT
  87.         glBegin(GL_TRIANGLES);
  88.         glColor3f(0.3, 0.5, 0.6);
  89.                 glVertex3f(-x, -y, -z);
  90.                 glVertex3f(-x, -y, z);
  91.                 glVertex3f(-x, y, -z);
  92.         glEnd();
  93.  
  94.         glBegin(GL_TRIANGLES);
  95.                 glColor3f(0.5, 0.5, 0.5);
  96.                 glVertex3f(-x, y, z);
  97.                 glVertex3f(-x, -y, z);
  98.                 glVertex3f(-x, y, -z);
  99.         glEnd();
  100.  
  101.         // RIGHT
  102.         glBegin(GL_TRIANGLES);
  103.         glColor3f(0.2, 0.2, 0.2);
  104.                 glVertex3f(x, y, z);
  105.                 glVertex3f(x, y, -z);
  106.                 glVertex3f(x, -y, z);
  107.         glEnd();
  108.  
  109.         glBegin(GL_TRIANGLES);
  110.         glColor3f(0.0, 0.0, 0.0);
  111.                 glVertex3f(x, -y, -z);
  112.                 glVertex3f(x, y, -z);
  113.                 glVertex3f(x, -y, z);
  114.         glEnd();
  115.  
  116.         // TOP
  117.         glBegin(GL_TRIANGLES);
  118.         glColor3f(0.6, 0.0, 0.0);
  119.                 glVertex3f(x, y, z);
  120.                 glVertex3f(x, y, -z);
  121.                 glVertex3f(-x, y, -z);
  122.         glEnd();
  123.  
  124.         glBegin(GL_TRIANGLES);
  125.         glColor3f(0.6, 0.1, 0.2);
  126.                 glVertex3f(-x, y, z);
  127.                 glVertex3f(x, y, z);
  128.                 glVertex3f(-x, y, -z);
  129.         glEnd();
  130.  
  131.         // BOTTOM
  132.         glBegin(GL_TRIANGLES);
  133.         glColor3f(0.4, 0.0, 0.4);
  134.                 glVertex3f(-x, -y, -z);
  135.                 glVertex3f(-x, -y, z);
  136.                 glVertex3f(x, -y, z);
  137.         glEnd();
  138.  
  139.         glBegin(GL_TRIANGLES);
  140.                 glColor3f(0.3, 0.0, 0.3);
  141.                 glVertex3f(x, -y, -z);
  142.                 glVertex3f(-x, -y, -z);
  143.                 glVertex3f(x, -y, z);
  144.         glEnd();
  145.  
  146.     glFlush();
  147.     glutSwapBuffers();
  148. }
  149.  
  150. void keyboard(int key, int x, int y)
  151. {
  152.     if (key == GLUT_KEY_RIGHT)
  153.         {
  154.                 rY += 15;
  155.         }
  156.     else if (key == GLUT_KEY_LEFT)
  157.         {
  158.                 rY -= 15;
  159.         }
  160.     else if (key == GLUT_KEY_DOWN)
  161.         {
  162.                 rX -= 15;
  163.         }
  164.     else if (key == GLUT_KEY_UP)
  165.         {
  166.                 rX += 15;
  167.         }
  168.  
  169.     // Request display update
  170.     glutPostRedisplay();
  171. }
  172.  
  173.  
  174. int main(int argc, char **argv)
  175. {
  176.         // Initialize GLUT and process user parameters
  177.         glutInit(&argc, argv);
  178.  
  179.         // Request double buffered true color window with Z-buffer
  180.         glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  181.  
  182.         glutInitWindowSize(700,700);
  183.         glutInitWindowPosition(100, 100);
  184.  
  185.         // Create window
  186.         glutCreateWindow("Linux Journal OpenGL Cube");
  187.  
  188.         // Enable Z-buffer depth test
  189.         glEnable(GL_DEPTH_TEST);
  190.  
  191.         // Callback functions
  192.         glutDisplayFunc(drawCube);
  193.         glutSpecialFunc(keyboard);
  194.  
  195.         // Pass control to GLUT for events
  196.         glutMainLoop();
  197.  
  198.         return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement