Advertisement
pedro10pierre

Untitled

Jan 20th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.60 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <time.h>
  5.  
  6. #ifdef __APPLE__
  7. #include <GLUT/glut.h>
  8. #else
  9. #include <GL/glut.h>
  10. #endif
  11.  
  12. // Tamanho da box (centrada na origem) onde as figuras são desenhadas.
  13. #define CELL_SIZE 2.0
  14.  
  15. // Tamanho (número de casas) do tabuleiro quadrado.
  16. // Cada casa do tabuleiro tem tamanho CELL_SIZE.
  17. // TAB_SIZE tem que ser par (facilita o alinhamento).
  18. #define TAB_SIZE 10
  19.  
  20.  
  21. GLfloat vertices[][3] = { {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},
  22. {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
  23. {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0} };
  24.  
  25. GLfloat normals[][3] = { {-1.0,-1.0,-1.0}, {1.0,-1.0,-1.0},
  26. {1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
  27. {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0} };
  28.  
  29. GLfloat colors[][3] = { {0.0,0.0,0.0}, {1.0,0.0,0.0},
  30. {1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
  31. {1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0} };
  32.  
  33. typedef struct cubo* Cubo;
  34. struct cubo {
  35. int xTab, yTab; // Posição do cubo nas casas do tabuleiro.
  36. float scale;
  37. float xPos, yPos;
  38. float xRot, yRot; // Rotação necessária em x e em y em graus multiplos de 90.
  39. float xRoti, yRoti; // Rotação actual em x e em y até atingir xRot e yRot, respectivamente.
  40. int posicao;
  41. // Matriz de rotação que define a posição actual do cubo.
  42. GLfloat m[16];
  43.  
  44. float r, g, b;
  45. };
  46.  
  47. Cubo cubo; // cubo actual
  48. Cubo* aCubos; // array de cubos
  49. int nCubos = 5; // 1..9
  50. int ox[10][10];
  51. float distCamara = 5;
  52. float dDistCamara = 0.2;
  53. float alfaLongitude = 0;
  54. float alfaLatitude = 0;
  55. float dAlfa = 2;
  56. float xRato, yRato; // guarda a última posição do rato (para calcular o deslocamento na Lat. e Long.)
  57. float angRato = 0.25; // precisão do rato: número de angulos por pixel.
  58.  
  59. int timeUpdate = 20; // intervalo de tempo (ms) para o update
  60. float velRot = 200.0; // velocidade de rotação dos cubos em graus/s
  61. float angRotMax = 90;
  62.  
  63. void polygon(int a, int b, int c, int d)
  64. {
  65. glBegin(GL_POLYGON);
  66. glNormal3fv(normals[a]);
  67. glVertex3fv(vertices[a]);
  68. glNormal3fv(normals[b]);
  69. glVertex3fv(vertices[b]);
  70. glNormal3fv(normals[c]);
  71. glVertex3fv(vertices[c]);
  72. glNormal3fv(normals[d]);
  73. glVertex3fv(vertices[d]);
  74. glEnd();
  75. }
  76.  
  77. void lineloop(int a, int b, int c, int d)
  78. {
  79. glColor3f(1.0, 1.0, 1.0);
  80. glBegin(GL_LINE_LOOP);
  81. glVertex3fv(vertices[a]);
  82. glVertex3fv(vertices[b]);
  83. glVertex3fv(vertices[c]);
  84. glVertex3fv(vertices[d]);
  85. glEnd();
  86. }
  87.  
  88. void colorcube(void)
  89. {
  90. polygon(0, 3, 2, 1);
  91. polygon(2, 3, 7, 6);
  92. polygon(0, 4, 7, 3);
  93. polygon(1, 2, 6, 5);
  94. polygon(4, 5, 6, 7);
  95. polygon(0, 1, 5, 4);
  96.  
  97. lineloop(0, 3, 2, 1);
  98. lineloop(2, 3, 7, 6);
  99. lineloop(0, 4, 7, 3);
  100. lineloop(1, 2, 6, 5);
  101. lineloop(4, 5, 6, 7);
  102. lineloop(0, 1, 5, 4);
  103. }
  104.  
  105. // Desenha um cubo com diferentes cores nos lados.
  106. void colorcubeWithColor(float r, float g, float b)
  107. {
  108. // Um dos lados começa com uma cor e os outros lados são variações dessa cor.
  109.  
  110. float cor[3] = { r, g, b };
  111.  
  112. glColor3fv(cor);
  113. polygon(0, 3, 2, 1);
  114.  
  115. glColor3fv(cor);
  116. polygon(2, 3, 7, 6);
  117.  
  118. glColor3fv(cor);
  119. polygon(0, 4, 7, 3);
  120.  
  121. glColor3fv(cor);
  122. polygon(1, 2, 6, 5);
  123.  
  124. glColor3fv(cor);
  125. polygon(4, 5, 6, 7);
  126.  
  127. glColor3fv(cor);
  128. polygon(0, 1, 5, 4);
  129.  
  130. lineloop(0, 3, 2, 1);
  131. lineloop(2, 3, 7, 6);
  132. lineloop(0, 4, 7, 3);
  133. lineloop(1, 2, 6, 5);
  134. lineloop(4, 5, 6, 7);
  135. lineloop(0, 1, 5, 4);
  136.  
  137. }
  138.  
  139.  
  140. void TabuleiroA()
  141. {
  142.  
  143. //int ox[12], oy[12];
  144. float color[3] = { 0.0, 0.0, 1.0 };
  145. for (int i = 2; i <= 4; i++) {
  146. ox[i][3] = 1;
  147.  
  148. }
  149.  
  150.  
  151.  
  152. }
  153.  
  154. // Desenha um tabuleiro no plano XY e centrado na origem dos eixos;
  155. // é quadrado, de tamanho TAB_SIZE x TAB_SIZE posições (casas do tabuleiro);
  156. // cada posição que tem lado de tamanho CELL_SIZE.
  157. void desenhaTabuleiro()
  158. {
  159. int i, j;
  160. TabuleiroA();
  161. glPushMatrix();
  162.  
  163. // Centrar o tabuleiro no eixo XY e colocar o topo do tabuleiro no eixo dos Z (Z = 0).
  164. glTranslatef(-(TAB_SIZE * CELL_SIZE) / 2, -(TAB_SIZE * CELL_SIZE) / 2, -CELL_SIZE / 2);
  165.  
  166. for (i = 0; i < TAB_SIZE; i++) //i = Y, j= X
  167.  
  168. for (j = 0; j < TAB_SIZE; j++) {
  169.  
  170. if (ox[j][i] != 1) {
  171.  
  172. glPushMatrix();
  173. glTranslatef(j * CELL_SIZE + CELL_SIZE / 2, i * CELL_SIZE + CELL_SIZE / 2, 0);
  174. glScalef(0.90, 0.90, 0.30);
  175. glColor3f(1.0, 0.0, 0.0);
  176.  
  177.  
  178.  
  179. colorcube(); // Este cubo tem que ser desenhado dentro do CELL_SIZE.
  180. // É o que acontece porque o cubo tem o tamanho do CELL_SIZE.
  181. glPopMatrix();
  182. }
  183.  
  184.  
  185. }
  186.  
  187.  
  188.  
  189.  
  190.  
  191. glPopMatrix();
  192. //glutPostRedisplay();
  193.  
  194. }
  195.  
  196. // Faz os translates necessários para colocar uma forma gráfica (desenhada
  197. // numa box de tamanho CELL_SIZE) em cima de uma posição do tabuleiro.
  198. void translateFormaTabuleiro(int x, int y) {
  199. glTranslatef(-(TAB_SIZE * CELL_SIZE) / 2, -(TAB_SIZE * CELL_SIZE) / 2, CELL_SIZE / 6);
  200. glTranslatef(x * CELL_SIZE + CELL_SIZE / 2, y * CELL_SIZE + CELL_SIZE / 2, 0);
  201. }
  202.  
  203. // aloca a memória para a struct cubo
  204. Cubo criaCubo(int xTab, int yTab, float scale)
  205. {
  206. Cubo c = (Cubo)malloc(sizeof(*c));
  207. c->xTab = xTab;
  208. c->yTab = yTab;
  209. c->scale = scale;
  210. c->xPos = xTab;
  211. c->yPos = yTab;
  212. c->xRot = c->yRot = c->xRoti = c->yRoti = 0.0;
  213.  
  214. c->r = 0;
  215. c->g = 1;
  216. c->b = 0;
  217.  
  218. glMatrixMode(GL_MODELVIEW);
  219. glPushMatrix();
  220. glLoadIdentity();
  221. glGetFloatv(GL_MODELVIEW_MATRIX, c->m);
  222. glPopMatrix();
  223.  
  224. return c;
  225. }
  226.  
  227. // Liberta a memória ocupada pelo Cubo c
  228. void apagaCubo(Cubo c)
  229. {
  230. free(c);
  231. }
  232.  
  233. void actualizaRotCubo(Cubo c, GLfloat ang, GLfloat x, GLfloat y, GLfloat z)
  234. {
  235. glMatrixMode(GL_MODELVIEW);
  236. glPushMatrix();
  237. glLoadIdentity();
  238. glRotatef(ang, x, y, z);
  239. glMultMatrixf(c->m);
  240. glGetFloatv(GL_MODELVIEW_MATRIX, c->m);
  241. glPopMatrix();
  242. }
  243.  
  244. void rodaCuboCima(Cubo c)
  245. {
  246. if (c->yRot == c->yRoti) // não está a rodar em Y
  247. c->xRot += -angRotMax;
  248. }
  249.  
  250. void rodaCuboBaixo(Cubo c)
  251. {
  252. if (c->yRot == c->yRoti) // não está a rodar em Y
  253. c->xRot += angRotMax;
  254. }
  255.  
  256. void rodaCuboEsq(Cubo c)
  257. {
  258. if (c->xRot == c->xRoti) // não está a rodar em X
  259. c->yRot += -angRotMax;
  260. }
  261.  
  262. void rodaCuboDir(Cubo c)
  263. {
  264. if (c->xRot == c->xRoti) // não está a rodar em X
  265. c->yRot += angRotMax;
  266. }
  267.  
  268. void select(Cubo c)
  269. {
  270. c->scale *= 1.1;
  271. }
  272.  
  273. void unselect(Cubo c)
  274. {
  275. c->scale /= 1.1;
  276. }
  277.  
  278. void desenhaCubo(Cubo c)
  279. {
  280. if (c == NULL) return;
  281.  
  282. glPushMatrix();
  283.  
  284. translateFormaTabuleiro(c->xTab, c->yTab);
  285. glScalef(c->scale, c->scale, 1);
  286.  
  287.  
  288. glMultMatrixf(c->m);
  289.  
  290. colorcubeWithColor(c->r, c->g, c->b);
  291.  
  292. glPopMatrix();
  293. }
  294.  
  295. void updateCubo(Cubo c, int t)
  296. { // ToDo: colocar aqui uma condição para só fazer isto se for preciso...
  297. float angMov = velRot * (t / 1000.0);
  298.  
  299. if (c->xRot != 0 || c->xRoti != 0) {
  300. if (c->xRot > c->xRoti) c->xRoti += angMov;
  301. if (c->xRot < c->xRoti) c->xRoti -= angMov;
  302. if (fabs(c->xRot - c->xRoti) < angMov) {
  303. actualizaRotCubo(c, c->xRot, 1, 0, 0);
  304. c->xRot = c->xRoti = 0.0;
  305. }
  306. }
  307.  
  308. if (c->yRot != 0 || c->yRoti != 0) {
  309. if (c->yRot > c->yRoti) c->yRoti += angMov;
  310. if (c->yRot < c->yRoti) c->yRoti -= angMov;
  311. if (fabs(c->yRot - c->yRoti) < angMov) {
  312. actualizaRotCubo(c, c->yRot, 0, 1, 0);
  313. c->yRot = c->yRoti = 0.0;
  314. }
  315. }
  316. }
  317.  
  318. void display(void)
  319. {
  320. int i;
  321. float x, y, z;
  322. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  323. //glShadeModel(GL_FLAT);
  324.  
  325. glMatrixMode(GL_MODELVIEW);
  326. glLoadIdentity();
  327.  
  328. // Camara
  329. x = distCamara * sin(alfaLongitude * 3.14 / 180) * cos(alfaLatitude * 3.14 / 180);
  330. z = distCamara * cos(alfaLongitude * 3.14 / 180) * cos(alfaLatitude * 3.14 / 180);
  331. y = distCamara * sin(alfaLatitude * 3.14 / 180);
  332. gluLookAt(x, y, z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  333.  
  334. // Redimensiona a mundo para caber na janela.
  335. glScalef(0.2, 0.2, 0.2);
  336.  
  337. // Desenha o tabuleiro e os cubos.
  338. desenhaTabuleiro();
  339. for (i = 0; i < nCubos; i++)
  340. desenhaCubo(aCubos[i]);
  341. printf("xTAB: %d, yTAB: %d\n", cubo->xTab, cubo->yTab);
  342.  
  343. //Pinta Cubos
  344.  
  345.  
  346. glFlush();
  347.  
  348. glutSwapBuffers();
  349. }
  350.  
  351.  
  352. void myReshape(int w, int h)
  353. {
  354. glViewport(0, 0, w, h);
  355. glMatrixMode(GL_PROJECTION);
  356. glLoadIdentity();
  357. if (w <= h)
  358. glFrustum(-2.0, 2.0, -2.0 * (GLfloat)h / (GLfloat)w,
  359. 2.0 * (GLfloat)h / (GLfloat)w, 3.0, 20.0);
  360. else
  361. glFrustum(-2.0 * (GLfloat)w / (GLfloat)h,
  362. 2.0 * (GLfloat)w / (GLfloat)h, -2.0, 2.0, 3.0, 20.0);
  363. glMatrixMode(GL_MODELVIEW);
  364.  
  365. }
  366.  
  367. // timer callback
  368. void update(int v)
  369. {
  370. int i;
  371.  
  372. for (i = 0; i < nCubos; i++)
  373. updateCubo(aCubos[i], v);
  374.  
  375. glutPostRedisplay();
  376. glutTimerFunc(v, update, v);
  377. }
  378.  
  379. void selectCubo(int i)
  380. {
  381. printf("Cubo %i seleccionado!!!\n", i);
  382. if (i <= 0 || i > nCubos) return;
  383.  
  384. unselect(cubo);
  385. cubo = aCubos[i - 1];
  386. select(cubo);
  387. }
  388.  
  389. void teclas(unsigned char key, int x, int y)
  390. {
  391. switch (key) {
  392. case 'q':
  393. case 'Q':
  394. exit(0);
  395. break;
  396. }
  397.  
  398. selectCubo(key - '0');
  399.  
  400. glutPostRedisplay();
  401. }
  402.  
  403. void rato(GLint button, GLint state, GLint x, GLint y)
  404. {
  405. xRato = x;
  406. yRato = y;
  407. }
  408.  
  409. // mouse motion callback
  410. void moveRatoPress(int x, int y)
  411. {
  412. alfaLongitude += (x - xRato) * angRato;
  413. alfaLatitude -= (y - yRato) * angRato;
  414. xRato = x;
  415. yRato = y;
  416.  
  417. if (alfaLongitude >= 360) alfaLongitude == 360;
  418. if (alfaLongitude < 0) alfaLongitude == 360;
  419. if (alfaLatitude >= 360) alfaLatitude == 360;
  420. if (alfaLatitude < 0) alfaLatitude == 360;
  421.  
  422. printf("R(Lon,Lat)=(%6.2f,%6.2f)\n", alfaLongitude, alfaLatitude);
  423.  
  424.  
  425. glutPostRedisplay();
  426. }
  427.  
  428. void VerificaLimites() {
  429.  
  430. if (GLUT_KEY_UP) {
  431.  
  432. if (cubo->yTab == TAB_SIZE )
  433. cubo->yTab -= 1;
  434.  
  435. }
  436. if (GLUT_KEY_DOWN) {
  437.  
  438. if (cubo->yTab == -1)
  439. cubo->yTab += 1;
  440. }
  441. if (GLUT_KEY_LEFT) {
  442.  
  443. if (cubo->xTab == -1)
  444. cubo->xTab += 1;
  445. }
  446. if (GLUT_KEY_RIGHT) {
  447.  
  448. if (cubo->xTab == TAB_SIZE)
  449. cubo->xTab -= 1;
  450. }
  451. }
  452.  
  453.  
  454. void teclasEspeciais(int key, int x, int y)
  455. {
  456. switch (key) {
  457. case GLUT_KEY_UP:
  458. cubo->yTab += 1;
  459. if (cubo->xTab == 4 && cubo->yTab == 3 || cubo->xTab == 3 && cubo->yTab == 3 || cubo->xTab == 2 && cubo->yTab == 3)
  460. cubo->yTab -= 1;
  461.  
  462. VerificaLimites();
  463. break;
  464. case GLUT_KEY_DOWN:
  465.  
  466. cubo->yTab -= 1;
  467. if (cubo->xTab == 4 && cubo->yTab == 3 || cubo->xTab == 3 && cubo->yTab == 3 || cubo->xTab == 2 && cubo->yTab == 3)
  468. cubo->yTab += 1;
  469. VerificaLimites();
  470. break;
  471. case GLUT_KEY_LEFT:
  472.  
  473. cubo->xTab -= 1;
  474. if (cubo->xTab == 4 && cubo->yTab == 3)
  475. cubo->xTab += 1;
  476. VerificaLimites();
  477. break;
  478. case GLUT_KEY_RIGHT:
  479.  
  480. cubo->xTab += 1;
  481. if (cubo->xTab == 2 && cubo->yTab == 3)
  482. cubo->xTab -= 1;
  483. VerificaLimites();
  484. break;
  485. }
  486.  
  487. glutPostRedisplay();
  488. }
  489.  
  490. void myInit()
  491. {
  492. int i;
  493.  
  494. // Para que as linhas sejam bem desenhadas à volta dos cubos
  495. glPolygonOffset(1.0, 1.0);
  496. glEnable(GL_POLYGON_OFFSET_FILL);
  497.  
  498. srand(time(0));
  499.  
  500. // Cria cubos em posições aleatórias.
  501. aCubos = (Cubo*)malloc(sizeof(Cubo) * nCubos);
  502. for (i = 0; i < nCubos; i++) {
  503. aCubos[i] = criaCubo(rand() % TAB_SIZE, rand() % TAB_SIZE, 0.8);
  504. aCubos[i]->r = ((float)rand()) / RAND_MAX;
  505. aCubos[i]->g = ((float)rand()) / RAND_MAX;
  506. aCubos[i]->b = ((float)rand()) / RAND_MAX;
  507. }
  508.  
  509. cubo = aCubos[0]; // o primeiro cubo é o selecionado
  510. select(cubo);
  511.  
  512. }
  513.  
  514. void main(int argc, char** argv)
  515. {
  516. glutInit(&argc, argv);
  517.  
  518. /* need both double buffering and z buffer */
  519. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  520. glutInitWindowSize(500, 500);
  521. glutCreateWindow("TP2");
  522. myInit();
  523. glutReshapeFunc(myReshape);
  524. glutDisplayFunc(display);
  525. glutMouseFunc(rato);
  526. glutMotionFunc(moveRatoPress);
  527. glutKeyboardFunc(teclas);
  528. glutSpecialFunc(teclasEspeciais);
  529. glEnable(GL_DEPTH_TEST); /* Enable hidden--surface--removal */
  530. glutTimerFunc(timeUpdate, update, timeUpdate);
  531. glutMainLoop();
  532. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement