Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.97 KB | None | 0 0
  1. #include<windows.h>
  2. #include <GL/freeglut.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include<iostream>
  6. #include<fstream>
  7. using namespace std;
  8. GLdouble pc;//retine pozitia curenta a primului dreptunghi
  9. GLdouble pcd;//retine pozitia curenta a dreptnghiului care depaseste
  10.  
  11. static GLdouble ww, wh;//dimesiunile ferestrei
  12.  
  13. static GLdouble i = 0.0;
  14. static GLdouble j = 0.0;
  15. static GLdouble alpha1 = 1.0;
  16. static GLdouble alpha2 = 1.0;
  17.  
  18.  
  19. struct Punct
  20. {
  21. int x, y;
  22. }A, C, Adep, Cdep;
  23. enum
  24. {
  25. DESCRIERE, DEPASIRE
  26. };
  27.  
  28. int rendermode = DESCRIERE;
  29.  
  30. void menu(int selection)
  31. {
  32. rendermode = selection;
  33. glutPostRedisplay();
  34. }
  35.  
  36. void init(void)
  37. {
  38. glClearColor(0.2, 1.0, 0.1, 0.0);
  39. gluOrtho2D(0.0, 800.0, 0.0, 600.0);
  40. glShadeModel(GL_FLAT);
  41. }
  42.  
  43. void citire()
  44. {
  45. ifstream f("in.txt");
  46. f >> ww >> wh;
  47. f >> A.x >> A.y >> C.x >> C.y;//coordonatel a 2 punte opuse pt primul dreptunghi
  48. f >> Adep.x >> Adep.y >> Cdep.x >> Cdep.y;//coordonatele a 2 pct opuse ale dreptunghiului care depasaseste
  49. pc = A.x;
  50. pcd = Adep.x;
  51. }
  52.  
  53. void desen_strada()
  54. {
  55.  
  56. glBegin(GL_QUADS);
  57. glColor3f(0.0, 0.0, 0.0);
  58. glVertex2f(0, wh / 2 - 100);
  59. glVertex2f(ww, wh / 2 - 100);
  60. glVertex2f(ww+100, wh / 2 + 100);
  61. glVertex2f(100, wh / 2 + 100);
  62. glEnd();
  63.  
  64. glEnable(GL_LINE_STIPPLE);//desenare linie din mijloc cu linie punctata
  65. glLineStipple(1, 0X00FF);
  66. glLineWidth(15);
  67. glBegin(GL_LINES);
  68. glColor3f(1.0, 1.0, 1.0);
  69. glVertex2i(50, wh / 2);
  70. glVertex2i(ww, wh / 2);
  71. glEnd();
  72. glDisable(GL_LINE_STIPPLE);
  73.  
  74.  
  75.  
  76. glFlush();
  77. }
  78.  
  79. void desen2(void)
  80. {
  81. glClear(GL_COLOR_BUFFER_BIT);
  82. glLineWidth(15);
  83. glBegin(GL_LINES);
  84. glColor3f(0.0, 0.0, 0.0);
  85. glVertex2f(0, wh / 2);
  86. glVertex2f(ww, wh / 2);
  87. glEnd();
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96. glPushMatrix();//primul dreptunghi
  97. glTranslated(i / 3, 0.0, 0.0);
  98. glColor3f(0.0, 0.0, 1.0);
  99. glRecti(A.x, A.y, C.x, C.y);
  100. glPopMatrix();
  101. //pc = A.x + i / 3;
  102. //if(abs(pc - pcd) > C.x - A.x)
  103. //{
  104. //
  105.  
  106.  
  107. // glPushMatrix(); //dreptunghiul care depaseste
  108. // glTranslated(j / 2, 0.0, 0.0);
  109. //
  110. // glColor3f(0.9, 0.1, 0.2);
  111. // glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
  112. // glPopMatrix();
  113. // pcd = Adep.x + j / 2;
  114. //}
  115. //else
  116. //{
  117. //
  118. // glPushMatrix(); //dreptunghiul care depaseste
  119. //
  120. // glTranslated(j / 2, 100.0, 0.0); // dreptunghiul care depaseste este scalat pt ca atunci cad depaseste este mai mic
  121. // glScaled(0.8, 0.8, 0);
  122. // pcd = Adep.x + j / 2;
  123. // glColor3f(0.9, 0.1, 0.2);
  124. // glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
  125. // glPopMatrix();
  126. //}
  127. //
  128. glutSwapBuffers();
  129. glFlush();
  130. }
  131.  
  132. void reshape(int w, int h)
  133. {
  134. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  135. glMatrixMode(GL_PROJECTION);
  136. glLoadIdentity();
  137. glOrtho(0.0, GLdouble(w), 0.0, GLdouble(h), -1.0, 1.0);
  138.  
  139.  
  140. glMatrixMode(GL_MODELVIEW);
  141. glLoadIdentity();
  142. }
  143.  
  144. void miscad1(void)
  145. {
  146. i = i + alpha1;
  147.  
  148. if (i > 3 * ww)
  149. alpha1 = -1.0;
  150. else if (i < 0.0)alpha1 = 1.0;
  151. j = j + alpha2;
  152. if (j > 2 * ww)
  153. alpha2 = -1.0;
  154. else if (j < 0.0)
  155. alpha2 = 1.0;
  156.  
  157. glutPostRedisplay();
  158. }
  159.  
  160. void stop(void)
  161. {
  162. return;
  163. }
  164. void Specialkey(int key, int x, int y)
  165. {
  166. switch (key)
  167. {
  168.  
  169. case GLUT_KEY_RIGHT:
  170. alpha1 = 1.0;
  171. alpha2 = 1.0;
  172. glutIdleFunc(miscad1);
  173.  
  174.  
  175. break;
  176.  
  177. case GLUT_KEY_DOWN:
  178. glutIdleFunc(stop);
  179. break;
  180.  
  181. }
  182.  
  183. glutPostRedisplay();
  184. }
  185.  
  186. void glPrintf(int x, int y, char* sir, int lungime) {
  187.  
  188.  
  189. for (int i = 0; i < lungime; i++) {
  190. glColor3f(0.0, 0.0, 0.0);
  191. glRasterPos2i(x + 15 * i, y);
  192. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, sir[i]);
  193. }
  194.  
  195. }
  196.  
  197. void desen(void)
  198. {
  199. glClear(GL_COLOR_BUFFER_BIT);
  200.  
  201.  
  202. switch (rendermode) {
  203. case DESCRIERE:
  204. {char sir1[] = "Titlu:DEPASIRE";
  205. glPrintf(ww / 2, wh - 50, sir1, strlen(sir1));
  206. char sir2[] = " Dovleac Stefania CTI 252 ";
  207. glPrintf(ww / 2 + 100, 100, sir2, strlen(sir2));
  208. char sir3[] = "Cerinta:Simulati o depasire folosind 2 dreptunghiuri.";
  209. glPrintf(0, wh / 2 + 100, sir3, strlen(sir3));
  210. char sir4[] = "Click dreapta pentru meniu ";
  211. glPrintf(0, wh / 2, sir4, strlen(sir4));
  212.  
  213. }
  214.  
  215. break;
  216.  
  217. case DEPASIRE:
  218. {
  219. char sir5[] = "Apasa tasta 's' pentru a porni dreptunghiurile ";
  220. glPrintf(0, 40, sir5, strlen(sir5));
  221. char sir6[] = "Apasa tasta 'p' pentru pauza ";
  222. glPrintf(0, 20, sir6, strlen(sir6));
  223. char sir7[] = "Apasa tasta 'e' pantru a incheia executia ";
  224. glPrintf(0, 0, sir7, strlen(sir7));
  225. desen_strada();
  226.  
  227.  
  228. glPushMatrix();//primul dreptunghi
  229. glTranslated(i / 3, 0.0, 0.0);
  230. glColor3f(0.0, 0.0, 1.0);
  231. glRecti(A.x, A.y, C.x, C.y);
  232. glPopMatrix();
  233. pc = A.x + i / 3;
  234. if (abs(pc - pcd) > C.x - A.x)
  235. {
  236.  
  237.  
  238.  
  239. glPushMatrix(); //dreptunghiul care depaseste
  240. glTranslated(j / 2, 0.0, 0.0);
  241. glColor3f(0.9, 0.1, 0.2);
  242. glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
  243. glPopMatrix();
  244. pcd = Adep.x + j / 2;
  245. }
  246. else
  247. {
  248.  
  249. glPushMatrix(); //dreptunghiul care depaseste
  250.  
  251. glTranslated(j / 2, 130.0, 0.0); // dreptunghiul care depaseste este scalat pt ca atunci cad depaseste este mai mic
  252. glScaled(0.8, 0.8, 0);
  253. glColor3f(0.9, 0.1, 0.2);
  254. glRecti(Adep.x, Adep.y, Cdep.x, Cdep.y);
  255. glPopMatrix();
  256. pcd = Adep.x + j / 2;
  257. }
  258. }
  259. break;
  260.  
  261.  
  262.  
  263.  
  264. };
  265. glutSwapBuffers();
  266. glFlush();
  267.  
  268.  
  269. }
  270. void Normalkey(unsigned char key, int x, int y)
  271. {
  272. switch (key)
  273. {
  274. case 's': alpha1 = 1.0;
  275. alpha2 = 1.0;
  276. glutIdleFunc(miscad1);
  277. break;
  278. case 'p':
  279. glutIdleFunc(stop);
  280. break;
  281. case 'e':
  282. exit(0);
  283. break;
  284.  
  285.  
  286. }
  287. }
  288.  
  289.  
  290. void main(int argc, char** argv)
  291. {
  292. citire();
  293. glutInit(&argc, argv);
  294. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  295. glutInitWindowSize(ww, wh);
  296. glutInitWindowPosition(0, 0);
  297. glutCreateWindow("DEPASIRE");
  298. init();
  299.  
  300. glutDisplayFunc(desen);
  301. //glutDisplayFunc(desen2);
  302.  
  303.  
  304. glutReshapeFunc(reshape);
  305. glutKeyboardFunc(Normalkey);
  306. glutSpecialFunc(Specialkey);
  307.  
  308.  
  309. glutCreateMenu(menu);
  310. glutAddMenuEntry("DESCRIEREA PROIECTULUI ", DESCRIERE);
  311. glutAddMenuEntry("DEPASIRE ", DEPASIRE);
  312. glutAttachMenu(GLUT_RIGHT_BUTTON);
  313.  
  314. glutMainLoop();
  315. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement