Advertisement
Guest User

Untitled

a guest
Oct 24th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. /* Permission is hereby granted, free of charge, to any person obtaining a copy
  2. * of this software and associated documentation files (the "Software"), to deal
  3. * in the Software without restriction, including without limitation the rights
  4. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  5. * copies of the Software, and to permit persons to whom the Software is
  6. * furnished to do so, subject to the following conditions:
  7. *
  8. * The above notice and this permission notice shall be included in all copies
  9. * or substantial portions of the Software.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. * SOFTWARE.
  18. */
  19. /* File for "Basic Shapes" lesson of the OpenGL tutorial on
  20. * www.videotutorialsrock.com
  21. */
  22.  
  23.  
  24.  
  25. #include <iostream>
  26. #include <stdlib.h> //Needed for "exit" function
  27.  
  28. //Include OpenGL header files, so that we can use OpenGL
  29. #ifdef __APPLE__
  30. #include <OpenGL/OpenGL.h>
  31. #include <GLUT/glut.h>
  32. #else
  33. #include <GL/glut.h>
  34. #endif
  35.  
  36. using namespace std;
  37.  
  38. float rotateX = 0.1f;
  39. float rotateY = 0.1f;
  40. float rotateZ = -4.0f;
  41. float rotateAngle = 180.0f;
  42.  
  43. //Called when a key is pressed
  44. void handleKeypress(unsigned char key, //The key that was pressed
  45. int x, int y) { //The current mouse coordinates
  46. switch (key) {
  47. case 27: //Escape key
  48. case 'a':
  49. rotateX += 0.1f;
  50. glutPostRedisplay();
  51. break;
  52. case 'd':
  53. rotateY += 0.1f;
  54. glutPostRedisplay();
  55. break;
  56. case 'w':
  57. rotateZ += 0.1f;
  58. glutPostRedisplay();
  59. break;
  60. case 's':
  61. rotateAngle += 10.0f;
  62. glutPostRedisplay();
  63. break;
  64. //Exit the program
  65. }
  66. }
  67.  
  68. //Initializes 3D rendering
  69. void initRendering() {
  70. //Makes 3D drawing work when something is in front of something else
  71. glEnable(GL_DEPTH_TEST);
  72. }
  73.  
  74. //Called when the window is resized
  75. void handleResize(int w, int h) {
  76. //Tell OpenGL how to convert from coordinates to pixel values
  77. glViewport(0, 0, w, h);
  78.  
  79. glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
  80.  
  81. //Set the camera perspective
  82. glLoadIdentity(); //Reset the camera
  83. gluPerspective(45.0, //The camera angle
  84. (double)w / (double)h, //The width-to-height ratio
  85. 1.0, //The near z clipping coordinate
  86. 200.0); //The far z clipping coordinate
  87. }
  88.  
  89. //Draws the 3D scene
  90. void drawScene() {
  91. //Clear information from last draw
  92. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  93.  
  94. glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
  95. glLoadIdentity(); //Reset the drawing perspective
  96.  
  97. glTranslatef(0.0f,0.0f,-3.0f);
  98. glRotatef(rotateAngle, rotateX, rotateY, rotateZ);
  99. glColor3f(0.5f,0.5f,1.0f);
  100.  
  101. glBegin(GL_QUADS);
  102.  
  103. glColor3f(0.5f,0.5f,0.0f);
  104. glVertex3f(0.5f,0.5f,-0.5f);
  105. glVertex3f(-0.5f,0.5f,-0.5f);
  106. glVertex3f(-0.5f,0.5f,0.5f);
  107. glVertex3f(0.5f,0.5f,0.5f);
  108.  
  109. glColor3f(0.5f,0.5f,0.0f);
  110. glVertex3f(0.5f,-0.5f,0.5f);
  111. glVertex3f(-0.5f,-0.5f,0.5f);
  112. glVertex3f(-0.5f,-0.5f,-0.5f);
  113. glVertex3f(0.5f,-0.5f,-0.5f);
  114.  
  115. glColor3f(0.5f,0.0f,0.0f);
  116. glVertex3f(0.5f,0.5f,0.5f);
  117. glVertex3f(-0.5f,0.5f,0.5f);
  118. glVertex3f(-0.5f,-0.5f,0.5f);
  119. glVertex3f(0.5f,-0.5f,0.5f);
  120.  
  121. glColor3f(0.5f,0.5f,0.0f);
  122. glVertex3f(0.5f,-0.5f,-0.5f);
  123. glVertex3f(-0.5f,-0.5f,-0.5f);
  124. glVertex3f(-0.5f,0.5f,-0.5f);
  125. glVertex3f(0.5f,0.5f,-0.5f);
  126.  
  127. glColor3f(0.0f,0.0f,0.5f);
  128. glVertex3f(-0.5f,0.5f,0.5f);
  129. glVertex3f(-0.5f,0.5f,-0.5f);
  130. glVertex3f(-0.5f,-0.5f,-0.5f);
  131. glVertex3f(-0.5f,-0.5f,0.5f);
  132.  
  133. glColor3f(0.5f,0.0f,0.5f);
  134. glVertex3f(0.5f,0.5f,-0.5f);
  135. glVertex3f(0.5f,0.5f,0.5f);
  136. glVertex3f(0.5f,-0.5f,0.5f);
  137. glVertex3f(0.5f,-0.5f,-0.5f);
  138.  
  139. glEnd();
  140.  
  141.  
  142. glutSwapBuffers(); //Send the 3D scene to the screen
  143.  
  144. glutPostRedisplay();
  145. }
  146.  
  147. int main(int argc, char** argv) {
  148. //Initialize GLUT
  149. glutInit(&argc, argv);
  150. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  151. glutInitWindowSize(400, 400); //Set the window size
  152.  
  153. //Create the window
  154. glutCreateWindow("Basic Shapes - videotutorialsrock.com");
  155. initRendering(); //Initialize rendering
  156.  
  157. //Set handler functions for drawing, keypresses, and window resizes
  158. glutDisplayFunc(drawScene);
  159. glutKeyboardFunc(handleKeypress);
  160. glutReshapeFunc(handleResize);
  161.  
  162. glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
  163. return 0; //This line is never reached
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement