Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include "extras.h"
  6. #include <math.h>
  7. #include <iostream>
  8.  
  9. using namespace std;
  10.  
  11. // Globals
  12. int width = 1000;
  13. int height = 500;
  14. float rotationX = 0.0, rotationY = 0.0;
  15. int last_x, last_y;
  16. float x=0, y=0, a=400;
  17. bool R = true, L = false, troca1 = true, troca2 = true, troca3 = true;
  18. void idle(){
  19.  
  20. float t, dt;
  21. static float tLast = 0.0;
  22.  
  23. /* Get elapsed time and convert to s */
  24. t = glutGet(GLUT_ELAPSED_TIME);
  25. t /= 1000.0;
  26.  
  27. /* Calculate delta t */
  28. dt = t - tLast;
  29.  
  30. if (R)
  31. x = x + dt;
  32.  
  33. if (L)
  34. x = x - dt;
  35.  
  36. if (x > 10){
  37. R = false;
  38. L = true;
  39. }
  40.  
  41. if (x>180.0){
  42. a=500;
  43. }
  44.  
  45. if (x<0){
  46. R = true;
  47. L = false;
  48. }
  49.  
  50. y = a*cos(x);
  51. if (y<0){
  52. y = y * (-1);
  53. }
  54.  
  55. if (x > 90*(3.14/180) && troca1 == true){
  56. a = 300;
  57. troca1 = false;
  58. }
  59.  
  60. if (x > 270*(3.14/180) && troca2 == true){
  61. a = 200;
  62. troca2 = false;
  63. }
  64.  
  65. if (x > 450*(3.14/180) && troca3 == true){
  66. a = 100;
  67. troca3 = false;
  68. }
  69.  
  70. printf ("x=%f y=%f a=%f\n", x*100, y*400, a);
  71. tLast = t;
  72. glutPostRedisplay();
  73. }
  74.  
  75. void init(void)
  76. {
  77. glClearColor (0.5, 0.5, 0.5, 0.0);
  78. glShadeModel (GL_SMOOTH);
  79. glEnable(GL_DEPTH_TEST); // Habilita Z-buffer
  80. initLight(width, height);
  81. }
  82.  
  83. void display(void)
  84. {
  85. float sphereSize = 30.0;
  86. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  87.  
  88. glPopMatrix();
  89. glBegin(GL_LINES);
  90. glColor3f (1.0, 1.0, 0.0);
  91. glVertex2f (0.0, 400.0);
  92. glVertex2f (1000.0, 400.0);
  93. glEnd();
  94. glPushMatrix();
  95.  
  96. glMatrixMode (GL_PROJECTION);
  97. glLoadIdentity ();
  98. glOrtho(0.0, width, 0.0, height, -sphereSize, sphereSize);
  99.  
  100. glMatrixMode(GL_MODELVIEW);
  101. glLoadIdentity ();
  102.  
  103. setMaterial();
  104.  
  105. glTranslatef(x*100, y, 0.0); // Posicionamento inicial da esfera
  106. glutSolidSphere(20.0, 100, 100);
  107.  
  108. glutSwapBuffers();
  109. }
  110.  
  111. void reshape (int w, int h)
  112. {
  113. width = w;
  114. height = h;
  115. glViewport (0, 0, (GLsizei) w, (GLsizei) h);
  116. }
  117.  
  118. void keyboard (unsigned char key, int x, int y)
  119. {
  120. if(tolower(key) == 27) exit(0);
  121. }
  122.  
  123. // Motion callback
  124. void motion(int x, int y )
  125. {
  126. glutPostRedisplay();
  127. }
  128.  
  129. int main(int argc, char** argv){
  130.  
  131. int b;
  132. cin >> b;
  133. glutInit(&argc, argv);
  134. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  135. glutInitWindowSize (width, height);
  136. glutInitWindowPosition (100, 100);
  137. glutCreateWindow("Animation Base");
  138. init ();
  139. glutDisplayFunc(display);
  140. glutReshapeFunc(reshape);
  141. glutMotionFunc( motion );
  142. glutKeyboardFunc(keyboard);
  143. glutIdleFunc(idle);
  144. glutMainLoop();
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement