Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. // BOX Movement using up-down-left-right key of the keyboard.
  2. #include <gl/glut.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. // Initial size of graphics window.
  7. int WIDTH = 800;
  8. int HEIGHT = 600;
  9.  
  10. // Current size of window.
  11. int width = WIDTH;
  12. int height = HEIGHT;
  13.  
  14. int noOfDisplayCalls=0;
  15. float offset = 0;
  16. int dir = 0;
  17. int noOfTimer=0;
  18.  
  19. /**
  20. * TIMERSEC int
  21. * the amount of time (in milisecond) after which animate function is called
  22. */
  23. const int TIMERMSECS = 5000;
  24.  
  25. const int BOX_WIDTH = 100;
  26. const int BOX_HEIGHT = 30;
  27.  
  28. int boxleft = 350, boxtop=200;
  29.  
  30.  
  31.  
  32. void display ()
  33. {
  34. // printf("calling display(%d)\n",++noOfDisplayCalls);
  35. // Function that clears the window. If you don't want to clear the view window, don't call glClear function. make it as a comment statement.
  36. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  37.  
  38. // --------------
  39. // YOUR CODE ...
  40. // --------------
  41.  
  42. glColor3f(.5, .7, .1);
  43. glLineWidth(1);
  44. glBegin(GL_TRIANGLES);
  45. //if (dir==1)
  46. glVertex2f(width/(double)WIDTH*100+offset,height/(double)HEIGHT*310);
  47. //else
  48. //glVertex2f(width/(double)WIDTH*30+offset,height/(double)HEIGHT*50+offset);
  49.  
  50. glVertex2f(width/(double)WIDTH*100+offset,height/(double)HEIGHT*290);
  51. glVertex2f(width/(double)WIDTH*350,height/(double)HEIGHT*300);
  52. //glVertex2f(400+offset,600);//
  53.  
  54. glBegin(GL_TRIANGLES);
  55. //if (dir==1)
  56. glVertex2f(width/(double)WIDTH*360,height/(double)HEIGHT*100+offset);
  57. //else
  58. //glVertex2f(width/(double)WIDTH*30+offset,height/(double)HEIGHT*50+offset);
  59.  
  60. glVertex2f(width/(double)WIDTH*340,height/(double)HEIGHT*100+offset);
  61. glVertex2f(width/(double)WIDTH*350,height/(double)HEIGHT*300);
  62.  
  63. glEnd();
  64.  
  65. glutSwapBuffers();
  66. }
  67.  
  68.  
  69. void mouseMovement(int mx, int my)
  70. {
  71. // --------------
  72. // YOUR CODE ...
  73. // --------------
  74. //printf("(%d,%d)\n",mx,my);
  75.  
  76. glutPostRedisplay();
  77. }
  78.  
  79.  
  80. void mouseAction(int button, int state, int x, int z)
  81. // Record cursor position in response to mouse button action
  82. {
  83. switch(button){
  84. case GLUT_LEFT_BUTTON:
  85. if (state == GLUT_DOWN) {
  86. printf("Clicked\n");
  87. }
  88. else if(state==GLUT_UP){
  89. printf("Released\n");
  90. }
  91. break;
  92. }
  93. } // end mouse
  94.  
  95. // Respond to window resizing, preserving proportions.
  96. // Parameters give new window size in pixels.
  97. void reshapeMainWindow (int newWidth, int newHeight)
  98. {
  99. printf("Calling Reshape()\n");
  100. // ----------------------------
  101. // DO NOT ALTER THIS FUNCTION ...
  102. // ----------------------------
  103.  
  104. glMatrixMode( GL_PROJECTION );
  105. glLoadIdentity();
  106. glOrtho( 0, newWidth, 0, newHeight, 0, 1 );
  107. glViewport( 0, 0, newWidth, newHeight );
  108. width = newWidth;
  109. height = newHeight;
  110. glMatrixMode(GL_MODELVIEW);
  111. //glutDisplayFunc(display);//
  112. glutPostRedisplay();
  113. }
  114.  
  115.  
  116. // Respond to graphic character keys.
  117. // Parameters give key code and mouse coordinates.
  118. void graphicKeys (unsigned char key, int x, int y)
  119. {
  120. printf("Calling graphicKeys()\n");
  121. switch (key) {
  122. case 'p':{
  123. offset++;
  124. break;
  125.  
  126. }
  127. case 'h':
  128. // --------------
  129. // YOUR CODE ...
  130. // --------------
  131. break;
  132. case 27: // Escape
  133. exit(0);
  134. default:
  135. printf("%c\n",key);
  136. }
  137. glutPostRedisplay();
  138. }
  139.  
  140. // Respond to function keys.
  141. // Parameters give key code and mouse coordinates.
  142. void functionKeys (int key, int x, int y)
  143. {
  144. switch (key)
  145. {
  146.  
  147. case GLUT_KEY_UP:
  148. boxtop +=10;
  149. break;
  150. case GLUT_KEY_DOWN:
  151. boxtop -=10;
  152. break;
  153. case GLUT_KEY_LEFT:
  154. boxleft -= 10;
  155. break;
  156. case GLUT_KEY_RIGHT:
  157. boxleft += 10;
  158. break;
  159. }
  160. glutPostRedisplay();
  161. }
  162.  
  163.  
  164. void timer(int value) {
  165. printf("calling timer(%d)\n",++noOfTimer);
  166. // --------------
  167. // YOUR CODE ...
  168. // --------------
  169.  
  170. glutTimerFunc(500, timer, 0);
  171. glutPostRedisplay();
  172. }
  173.  
  174.  
  175. void doThisAlways()
  176. {
  177. //printf("Calling doThisAlways()\n");
  178.  
  179. if(dir==1)
  180. {
  181. if(offset==500)
  182. {
  183. dir=0;
  184. }
  185. offset+=2.5;
  186. }
  187. else if(dir==0)
  188. {
  189. if(offset==0)
  190. {
  191. dir=1;
  192. }
  193. offset-=2.5;
  194. }
  195.  
  196. glutPostRedisplay();
  197. }
  198.  
  199. int main()
  200. {
  201. // GLUT initialization.
  202. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  203. glutInitWindowSize(width, height);
  204. glutCreateWindow("GLUT Skeleton Program");
  205.  
  206. // Register call backs.
  207. glutDisplayFunc(display);
  208. glutReshapeFunc(reshapeMainWindow);
  209. glutKeyboardFunc(graphicKeys);
  210. glutSpecialFunc(functionKeys);
  211. glutPassiveMotionFunc(mouseMovement);
  212. glutMouseFunc(mouseAction);
  213. glutIdleFunc(doThisAlways);
  214. glutTimerFunc(500, timer, 0);
  215.  
  216. // Enter GLUT loop.
  217. glutMainLoop();
  218. return 0;
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement