Advertisement
gurumutant

LinuxJournal - OpenGL RotateCube

Aug 9th, 2017
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. // Programmer: Mihalis Tsoukalos
  2. // Date: Wednesday 04 June 2014
  3. //
  4. // A simple OpenGL program that draws a triangle
  5. // and automatically rotates it.
  6. //
  7. // g++ rotateCube.cc -lm -lglut -lGL -lGLU -o rotateCube
  8.  
  9. #include <iostream>
  10. #include <stdlib.h>
  11.  
  12. // the GLUT and OpenGL libraries have to be linked correctly
  13. #ifdef __APPLE__
  14. #include <OpenGL/OpenGL.h>
  15. #include <GLUT/glut.h>
  16. #else
  17. #include <GL/glut.h>
  18. #endif
  19.  
  20. using namespace std;
  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. float angle = 0.0;
  28.  
  29. void drawCube()
  30. {
  31.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  32.  
  33.     // Reset transformations
  34.         glMatrixMode(GL_MODELVIEW);
  35.         glLoadIdentity();
  36.  
  37.         glTranslatef(0.0, 0.0, -5.0);
  38.  
  39.         // Add an ambient light
  40.         GLfloat ambientColor[] = {0.2, 0.2, 0.2, 1.0};
  41.         glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
  42.  
  43.         // Add a positioned light
  44.         GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};
  45.         GLfloat lightPos0[] = {4.0, 0.0, 8.0, 1.0};
  46.         glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
  47.         glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
  48.  
  49.         glTranslatef(0.5, 1.0, 0.0);
  50.         glRotatef(angle, 1.0, 1.0, 1.0);
  51.  
  52.     glRotatef( angle, 1.0, 0.0, 1.0 );
  53.     glRotatef( angle, 0.0, 1.0, 1.0 );
  54.         glTranslatef(-0.5, -1.0, 0.0);
  55.  
  56.         // Create the 3D cube
  57.  
  58.     // BACK
  59.     glBegin(GL_POLYGON);
  60.     glColor3f(0.5, 0.3, 0.2);
  61.     glVertex3f(x, -y, z);
  62.     glVertex3f(x, y, z);
  63.     glVertex3f(-x, y, z);
  64.     glVertex3f(-x, -y, z);
  65.     glEnd();
  66.  
  67.         // FRONT
  68.         glBegin(GL_POLYGON);
  69.         glColor3f(0.0, 0.5, 0.0);
  70.         glVertex3f(-x, y, -z);
  71.         glVertex3f(-x, -y, -z);
  72.         glVertex3f(x, -y, -z);
  73.         glVertex3f(x, y, -z);
  74.         glEnd();
  75.  
  76.         // LEFT
  77.         glBegin(GL_POLYGON);
  78.         glColor3f(0.5, 0.5, 0.5);
  79.         glVertex3f(-x, -y, -z);
  80.         glVertex3f(-x, -y, z);
  81.         glVertex3f(-x, y, z);
  82.         glVertex3f(-x, y, -z);
  83.         glEnd();
  84.  
  85.  
  86.         // RIGHT
  87.         glBegin(GL_POLYGON);
  88.         glColor3f(0.0, 0.0, 0.0);
  89.         glVertex3f(x, -y, -z);
  90.         glVertex3f(x, -y, z);
  91.         glVertex3f(x, y, z);
  92.         glVertex3f(x, y, -z);
  93.         glEnd();
  94.  
  95.         // TOP
  96.         glBegin(GL_POLYGON);
  97.         glColor3f(0.6, 0.0, 0.0);
  98.         glVertex3f(x, y, z);
  99.         glVertex3f(-x, y, z);
  100.         glVertex3f(-x, y, -z);
  101.         glVertex3f(x, y, -z);
  102.         glEnd();
  103.  
  104.  
  105.         // BOTTOM
  106.         glBegin(GL_POLYGON);
  107.         glColor3f(0.3, 0.0, 0.3);
  108.         glVertex3f(-x, -y, -z);
  109.         glVertex3f(-x, -y, z);
  110.         glVertex3f(x, -y, z);
  111.         glVertex3f(x, -y, -z);
  112.         glEnd();
  113.  
  114.         glFlush();
  115.     glutSwapBuffers();
  116. }
  117.  
  118. // Function for increasing the angle variable smoothly,
  119. // keeps it <=360
  120. // It can also be implemented using the modulo operator.
  121. void update(int value)
  122. {
  123.         angle += 1.0f;
  124.         if (angle > 360)
  125.                 {
  126.                         angle -= 360;
  127.         }
  128.  
  129.         glutPostRedisplay();
  130.         glutTimerFunc(25, update, 0);
  131. }
  132.  
  133. // Initializes 3D rendering
  134. void initRendering()
  135. {
  136.         glEnable(GL_DEPTH_TEST);
  137.         glEnable(GL_COLOR_MATERIAL);
  138.  
  139.         // Set the color of the background
  140.         glClearColor(0.7f, 0.8f, 1.0f, 1.0f);
  141.         glEnable(GL_LIGHTING);
  142.         glEnable(GL_LIGHT0);
  143.         glEnable(GL_NORMALIZE);
  144. }
  145.  
  146.  
  147. // Called when the window is resized
  148. void handleResize(int w, int h)
  149. {
  150.         glViewport(0, 0, w, h);
  151.         glMatrixMode(GL_PROJECTION);
  152.         glLoadIdentity();
  153.         gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
  154. }
  155.  
  156.  
  157. int main(int argc, char **argv)
  158. {
  159.     glutInit(&argc, argv);
  160.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  161.     glutInitWindowSize(700, 700);
  162.     glutInitWindowPosition(100, 100);
  163.     glutCreateWindow("OpenGL - Rotating a Cube");
  164.         initRendering();
  165.  
  166.     glutDisplayFunc(drawCube);
  167.         glutReshapeFunc(handleResize);
  168.  
  169.         // Add a timer for the update(...) function
  170.     glutTimerFunc(25, update, 0);
  171.  
  172.     glutMainLoop();
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement