Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <stdlib.h>
  5. #include<math.h>
  6. #ifdef __APPLE__
  7. #include <OpenGL/OpenGL.h>
  8. #include <GLUT/glut.h>
  9. #else
  10. #include <GL/glut.h>
  11. #endif
  12.  
  13. using namespace std;
  14.  
  15. //Called when a key is pressed
  16. void handleKeypress(unsigned char key, int x, int y) {
  17. switch (key) {
  18. case 27: //Escape key
  19. exit(0);
  20. }
  21. }
  22.  
  23. //Initializes 3D rendering
  24. void initRendering() {
  25. glEnable(GL_DEPTH_TEST);
  26. }
  27.  
  28. //Called when the window is resized
  29. void handleResize(int w, int h) {
  30. glViewport(0, 0, w, h);
  31. glMatrixMode(GL_PROJECTION);
  32. glLoadIdentity();
  33. gluPerspective(45.0, (double)w / (double)h, 1.0, 200.0);
  34. }
  35.  
  36. float _angle1 = 40.0f;
  37. //float _angle2 = 30.0f;
  38. //float _angle3 = 30.0f;
  39. float _cameraAngle = 0.0f;
  40. float x=0.0f;
  41. float y=-1.0f;
  42. float z=0.0f;
  43. //Draws the 3D scene
  44. void drawScene() {
  45. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  46.  
  47. glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
  48. glLoadIdentity(); //Reset the drawing perspective
  49. glRotatef(-_cameraAngle, 0.0f, 1.0f, 0.0f); //Rotate the camera
  50. glTranslatef(0.0f, 1.0f, -5.0f); //Move forward 5 units
  51.  
  52. glPushMatrix(); //Save the transformations performed thus far
  53.  
  54.  
  55.  
  56.  
  57. glBegin(GL_LINES);
  58.  
  59. glVertex3f(0.0f, 0.0f, 0.0f);
  60. glVertex3f(1.0, 0.0f, 0.0f);
  61.  
  62. glVertex3f(0.0f, 0.0f, 0.0f);
  63. glVertex3f(0.0f, -1.0f, 0.0f);
  64.  
  65. glVertex3f(0.0, -1.0f, 0.0f);
  66. glVertex3f(1.0, -1.0f, 0.0f);
  67.  
  68. glVertex3f(1.0f, 0.0f, 0.0f);
  69. glVertex3f(1.0f, -1.0f, 0.0f);
  70.  
  71.  
  72.  
  73.  
  74. glEnd();
  75.  
  76. glBegin(GL_QUADS);
  77. glVertex3f(x-0.2f, y-0.2f, z);
  78. glVertex3f(x-0.2f, y+0.2f, z);
  79. glVertex3f(x+0.2f, y+0.2f, z);
  80. glVertex3f(x+0.2f, y-0.2f, z);
  81.  
  82.  
  83.  
  84.  
  85. glEnd();
  86.  
  87. glPopMatrix(); //Undo the move to the center of the triangle
  88.  
  89. glutSwapBuffers();
  90. }
  91.  
  92. void update(int value) {
  93.  
  94. x+=.01;
  95.  
  96. if(x>1.0f && y<0.0f)
  97. {
  98. x=1.0f;
  99. y+=0.01;
  100.  
  101. if(x==1.0f && y>0.0f)
  102. {
  103. x-=0.1;
  104. y=0.0f;
  105. // x=0.01;
  106.  
  107.  
  108.  
  109. if(x<0.0f && y==0.0f)
  110. {
  111. x=0.0f;
  112. y-=0.01;
  113. }
  114. }}
  115.  
  116. glutPostRedisplay(); //Tell GLUT that the display has changed
  117.  
  118. //Tell GLUT to call update again in 25 milliseconds
  119. glutTimerFunc(25, update, 0);
  120. }
  121.  
  122. int main(int argc, char** argv) {
  123. //Initialize GLUT
  124. glutInit(&argc, argv);
  125. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  126. glutInitWindowSize(800, 800);
  127.  
  128. //Create the window
  129. glutCreateWindow("Transformations and Timers ");
  130. initRendering();
  131.  
  132. //Set handler functions
  133. glutDisplayFunc(drawScene);
  134. glutKeyboardFunc(handleKeypress);
  135. glutReshapeFunc(handleResize);
  136.  
  137. glutTimerFunc(25, update, 0); //Add a timer
  138.  
  139. glutMainLoop();
  140. return 0;
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement