Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.30 KB | None | 0 0
  1. // TRIUNGHI ROTIT / STATIC
  2.  
  3. #include <windows.h>
  4. #include <math.h>
  5. #include <gl/freeglut.h>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. double PI = 3.1415;
  11. double angle = 0;
  12. double scl = 0;
  13. int n = 4; int radius = 20;
  14.  
  15. double theta = 0, theta_inc;
  16. double truck_speed = 0.15, q = 1;
  17. double car_speed = 0.2;
  18. float truck_start_x = 300;
  19. float car_start_x = 10; float x_center = car_start_x + radius, y_center = 230;
  20. double k = 0.0;
  21. double angle1 = 0;
  22. double angle2 = 0;
  23. void init(void) // initializare fereastra de vizualizare
  24. {
  25. glClearColor(1.0, 1.0, 1.0, 0.0); // precizeaza culoarea de fond a ferestrei de vizualizare
  26. glMatrixMode(GL_MODELVIEW);
  27. glLoadIdentity();
  28.  
  29. glMatrixMode(GL_PROJECTION); // se precizeaza este vorba de o reprezentare 2D, realizata prin proiectie ortogonala
  30. gluOrtho2D(0.0, 1200.0, 0, 800.0); // sunt indicate coordonatele extreme ale ferestrei de vizualizare
  31. glClear(GL_COLOR_BUFFER_BIT); // reprezentare si colorare fereastra de vizualizare
  32.  
  33. }
  34.  
  35. void circle(float x, float y, float r, int segments)
  36. {
  37. glBegin(GL_TRIANGLE_FAN);
  38. glVertex2f(x, y);
  39. for (int n = 0; n <= segments; ++n) {
  40. float const t = 2 * 3.1415 * (float)n / (float)segments;
  41. glVertex2f(x + sin(t) * r, y + cos(t) * r);
  42. }
  43. glEnd();
  44.  
  45. glBegin(GL_LINES);
  46. glColor3f(1.0, 0.0, 0.0);
  47. glVertex3f(x, y, 0.0);
  48. glVertex3f(x+ r/2, y+ r/2, 0);
  49. glEnd();
  50. }
  51.  
  52.  
  53. void rotirePoligon(void)
  54. {
  55.  
  56. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  57.  
  58. //DESENARE DRUM
  59. glLineWidth(2.5);
  60. glColor3f(0.0, 0.0, 0.0);
  61. glBegin(GL_LINES);
  62. glVertex3f(0.0, 200.0, 0.0);
  63. glVertex3f(1200, 200, 0);
  64.  
  65. glVertex3f(0.0, 275.0, 0.0);
  66. glVertex3f(90, 275, 0);
  67.  
  68. glVertex3f(200.0, 275.0, 0.0);
  69. glVertex3f(300, 275, 0);
  70.  
  71. glVertex3f(400.0, 275.0, 0.0);
  72. glVertex3f(500, 275, 0);
  73.  
  74. glVertex3f(600.0, 275.0, 0.0);
  75. glVertex3f(700, 275, 0);
  76.  
  77. glVertex3f(800.0, 275.0, 0.0);
  78. glVertex3f(900, 275, 0);
  79.  
  80. glVertex3f(1000.0, 275.0, 0.0);
  81. glVertex3f(1100, 275, 0);
  82.  
  83. glVertex3f(1180.0, 275.0, 0.0);
  84. glVertex3f(1200, 275, 0);
  85.  
  86.  
  87. glVertex3f(0.0, 350.0, 0.0);
  88. glVertex3f(1200, 350, 0);
  89. glEnd();
  90.  
  91. angle += 0.05;
  92. truck_speed += 0.2;
  93. car_speed += 1;
  94. k++;
  95. //DESENARE MASINA
  96. glPushMatrix();
  97.  
  98. if (car_start_x + 100 + car_speed < truck_start_x + truck_speed || car_start_x + 100 + car_speed > truck_start_x + truck_speed + 450)
  99. glTranslated(car_speed, 1, 0);
  100. else
  101. glTranslated(car_speed, 100, 0);
  102.  
  103. glColor3f(0.3 , 0.0, 0.6);
  104. angle1 -= 1;
  105. angle2 -=2;
  106. glPushMatrix();
  107. glTranslatef(x_center, y_center, 0); // Moves rotated circle to desired location.
  108. glRotatef(angle2, 0, 0, 1);
  109. glTranslatef(-x_center, -y_center, 0); // Centers circle at origin.
  110. circle(car_start_x + radius, 230, radius / 2, 100);
  111.  
  112. glPopMatrix();
  113. glColor3f(0.3, 0.0, 0.6);
  114. glPushMatrix();
  115. glTranslatef(car_start_x + 130 - radius, y_center, 0);
  116. glRotatef(angle2, 0, 0, 1);
  117. glTranslatef(-(car_start_x + 130 - radius), -y_center, 0);
  118. circle(car_start_x + 130 - radius, 230, radius / 2, 100);
  119. glPopMatrix();
  120.  
  121. glBegin(GL_POLYGON);
  122. glColor3f(1.0, 1.0, 0.0);
  123. glVertex2f(car_start_x, 230);
  124. glVertex2f(car_start_x + 130, 230);
  125. glVertex2f(car_start_x + 130, 250);
  126.  
  127. glColor3f(0.0, 1.0, 1.0);
  128. glVertex2f(car_start_x + 110, 250);
  129. glVertex2f(car_start_x + 90, 280);
  130.  
  131. glColor3f(1.0, 0.0, 1.0);
  132. glVertex2f(car_start_x + 30, 280);
  133. glVertex2f(car_start_x + 30, 250);
  134. glVertex2f(car_start_x, 250);
  135.  
  136. glEnd();
  137. glPopMatrix();
  138.  
  139. // DESENARE CAMION
  140. glPushMatrix();
  141. glTranslated(truck_speed, q, 0.0);
  142. glTranslated(x_center, y_center, 0);
  143. //glRotated(angle, 0, 0, 1);
  144. glTranslated(-x_center, -y_center, 0);
  145.  
  146. glPushMatrix();
  147.  
  148.  
  149. glLineWidth(6.0);
  150. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  151. glColor3f(0.5, 0.2, 0.8);
  152. glPushMatrix();
  153. glTranslatef(truck_start_x + 110 + radius, y_center, 0);
  154. glRotatef(angle1, 0, 0, 1);
  155. glTranslatef(-(truck_start_x + 110 + radius), -y_center, 0);
  156. circle(truck_start_x + 110 + radius, 230, radius, 100);
  157. glPopMatrix();
  158. glColor3f(0.5, 0.2, 0.8);
  159. glPushMatrix();
  160. glTranslatef(truck_start_x + 290 - radius, y_center, 0);
  161. glRotatef(angle1, 0, 0, 1);
  162. glTranslatef(-(truck_start_x + 290 - radius), -y_center, 0);
  163. circle(truck_start_x + 290 - radius, 230, radius, 100);
  164. glPopMatrix();
  165.  
  166.  
  167. glBegin(GL_POLYGON);
  168.  
  169. glColor3f(0, 0, 1);
  170. glVertex2f(truck_start_x + 100, 230);
  171. glVertex2f(truck_start_x + 300, 230);
  172. glVertex2f(truck_start_x + 300, 300);
  173. glVertex2f(truck_start_x + 240, 300);
  174. glVertex2f(truck_start_x + 240, 320);
  175. glVertex2f(truck_start_x + 100, 320);
  176.  
  177. glEnd();
  178.  
  179. glPopMatrix();
  180.  
  181. glPopMatrix();
  182.  
  183. glutSwapBuffers();
  184. glutPostRedisplay();
  185.  
  186.  
  187. glFlush();
  188.  
  189. }
  190. void main(int argc, char** argv)
  191. {
  192.  
  193.  
  194. glutInit(&argc, argv); // initializare GLUT
  195. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // se utilizeaza un singur buffer | modul de colorare RedGreenBlue (= default)
  196. glutInitWindowPosition(100, 100); // pozitia initiala a ferestrei de vizualizare (in coordonate ecran)
  197. glutInitWindowSize(1200, 800); // dimensiunile ferestrei
  198. glutCreateWindow("Proiect DEPASIRE - Cojocaru Andrei & Tordoi Laurentiu"); // creeaza fereastra
  199. init();
  200. glutDisplayFunc(rotirePoligon); // procedura desen este invocata ori de cate ori este nevoie
  201. glutMainLoop(); // ultima instructiune a programului, asteapta (eventuale) noi date de intrare
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement