Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <time.h>
  6.  
  7.  
  8. #ifdef _APPLE_
  9. #include <GLUT/glut.h>
  10. #else
  11. #include <GL/glut.h>
  12. #endif
  13.  
  14.  
  15. #ifndef M_PI
  16. #define M_PI 3.1415926535897932384626433832795
  17. #endif
  18.  
  19. #define RAD(x) (M_PI*(x)/180)
  20. #define GRAUS(x) (180*(x)/M_PI)
  21.  
  22. #define DEBUG 1
  23.  
  24. #define DELAY_MOVIMENTO 20
  25. #define RAIO_ROTACAO 20
  26.  
  27. #define LARGURA_BASE 4
  28. #define COMPRIMENTO_BASE 7
  29. #define ALTURA_BASE 1
  30.  
  31. #define LARGURA_TORRE 2
  32. #define COMPRIMENTO_TORRE 2
  33. #define ALTURA_TORRE 0.5
  34.  
  35. #define COMPRIMENTO_CANHAO 4
  36. #define RAIO_CANHAO 0.2
  37.  
  38.  
  39.  
  40. /* VARIAVEIS GLOBAIS */
  41.  
  42. typedef struct {
  43. GLboolean q, a, z, x, up, down, left, right;
  44. }Teclas;
  45.  
  46. typedef struct {
  47. GLfloat x, y, z;
  48. }Pos;
  49.  
  50. typedef struct {
  51. Pos eye, center, up;
  52. GLfloat fov;
  53. }Camera;
  54.  
  55.  
  56. typedef struct {
  57. GLboolean doubleBuffer;
  58. GLint delayMovimento;
  59. Teclas teclas;
  60. GLuint menu_id;
  61. GLboolean menuActivo;
  62. Camera camera;
  63. GLboolean debug;
  64. GLboolean ortho;
  65. }Estado;
  66.  
  67. typedef struct {
  68. GLfloat x, y;
  69. GLint pontuacao;
  70. }Raquete;
  71.  
  72. typedef struct {
  73. GLfloat x, y;
  74. GLfloat velocidade;
  75. GLfloat direccao;
  76. GLfloat direccaoRodas;
  77. GLfloat angTorre;
  78. GLfloat angCanhao;
  79. }Tanque;
  80.  
  81. typedef struct {
  82. Tanque tanque;
  83. GLboolean parado;
  84. }Modelo;
  85.  
  86.  
  87. Estado estado;
  88. Modelo modelo;
  89.  
  90. void inicia_modelo()
  91. {
  92. modelo.tanque.x = 0;
  93. modelo.tanque.y = -20;
  94. modelo.tanque.velocidade = 0;
  95. modelo.tanque.direccao = 0;
  96. modelo.tanque.direccaoRodas = 0;
  97. modelo.tanque.angTorre = 0;
  98. modelo.tanque.angCanhao = 0;
  99.  
  100. }
  101.  
  102. /* Inicialização do ambiente OPENGL */
  103. void Init(void)
  104. {
  105.  
  106. srand((unsigned)time(NULL));
  107.  
  108. modelo.parado = GL_FALSE;
  109.  
  110. estado.debug = DEBUG;
  111. estado.menuActivo = GL_FALSE;
  112. estado.delayMovimento = DELAY_MOVIMENTO;
  113. estado.camera.eye.x = 40;
  114. estado.camera.eye.y = 40;
  115. estado.camera.eye.z = 40;
  116. estado.camera.center.x = 0;
  117. estado.camera.center.y = 0;
  118. estado.camera.center.z = 0;
  119. estado.camera.up.x = 0;
  120. estado.camera.up.y = 0;
  121. estado.camera.up.z = 1;
  122. estado.ortho = GL_TRUE;
  123. estado.camera.fov = 60;
  124.  
  125. estado.teclas.a = estado.teclas.q = estado.teclas.z = estado.teclas.x = \
  126. estado.teclas.up = estado.teclas.down = estado.teclas.left = estado.teclas.right = GL_FALSE;
  127.  
  128. inicia_modelo();
  129.  
  130. glClearColor(0.0, 0.0, 0.0, 0.0);
  131.  
  132. glEnable(GL_POINT_SMOOTH);
  133. glEnable(GL_LINE_SMOOTH);
  134. glEnable(GL_POLYGON_SMOOTH);
  135. glEnable(GL_DEPTH_TEST);
  136. //glutIgnoreKeyRepeat(GL_TRUE);
  137.  
  138. }
  139.  
  140. /**************************************
  141. * callbacks de janela/desenho *
  142. **************************************/
  143.  
  144. // CALLBACK PARA REDIMENSIONAR JANELA
  145.  
  146. void Reshape(int width, int height)
  147. {
  148. // glViewport(botom, left, width, height)
  149. // define parte da janela a ser utilizada pelo OpenGL
  150.  
  151. glViewport(0, 0, (GLint)width, (GLint)height);
  152.  
  153.  
  154. // Matriz Projeccao
  155. // Matriz onde se define como o mundo e apresentado na janela
  156. glMatrixMode(GL_PROJECTION);
  157. glLoadIdentity();
  158.  
  159. // gluOrtho2D(left,right,bottom,top);
  160. // projeccao ortogonal 2D, com profundidade (Z) entre -1 e 1
  161. if (estado.debug)
  162. printf("Reshape %s\n", (estado.ortho) ? "ORTHO" : "PERSPECTIVE");
  163.  
  164. if (estado.ortho)
  165. {
  166. if (width < height)
  167. glOrtho(-20, 20, -20 * (GLdouble)height / width, 20 * (GLdouble)height / width, -100, 100);
  168. else
  169. glOrtho(-20 * (GLdouble)width / height, 20 * (GLdouble)width / height, -20, 20, -100, 100);
  170. }
  171. else
  172. gluPerspective(estado.camera.fov, (GLfloat)width / height, 1, 100);
  173.  
  174. // Matriz Modelview
  175. // Matriz onde são realizadas as tranformacoes dos modelos desenhados
  176. glMatrixMode(GL_MODELVIEW);
  177. }
  178.  
  179.  
  180. void desenhaPoligono(GLfloat a[], GLfloat b[], GLfloat c[], GLfloat d[], GLfloat cor[])
  181. {
  182.  
  183. glBegin(GL_POLYGON);
  184. glColor3fv(cor);
  185. glVertex3fv(a);
  186. glVertex3fv(b);
  187. glVertex3fv(c);
  188. glVertex3fv(d);
  189. glEnd();
  190. }
  191.  
  192.  
  193. void desenhaCubo()
  194. {
  195. GLfloat vertices[][3] = { {-0.5,-0.5,-0.5},
  196. {0.5,-0.5,-0.5},
  197. {0.5,0.5,-0.5},
  198. {-0.5,0.5,-0.5},
  199. {-0.5,-0.5,0.5},
  200. {0.5,-0.5,0.5},
  201. {0.5,0.5,0.5},
  202. {-0.5,0.5,0.5} };
  203.  
  204. GLfloat cores[][3] = { {0.0,1.0,1.0},
  205. {1.0,0.0,0.0},
  206. {1.0,1.0,0.0},
  207. {0.0,1.0,0.0},
  208. {1.0,0.0,1.0},
  209. {0.0,0.0,1.0},
  210. {1.0,1.0,1.0} };
  211.  
  212. desenhaPoligono(vertices[1], vertices[0], vertices[3], vertices[2], cores[0]);
  213. desenhaPoligono(vertices[2], vertices[3], vertices[7], vertices[6], cores[1]);
  214. desenhaPoligono(vertices[3], vertices[0], vertices[4], vertices[7], cores[2]);
  215. desenhaPoligono(vertices[6], vertices[5], vertices[1], vertices[2], cores[3]);
  216. desenhaPoligono(vertices[4], vertices[5], vertices[6], vertices[7], cores[4]);
  217. desenhaPoligono(vertices[5], vertices[4], vertices[0], vertices[1], cores[5]);
  218. }
  219.  
  220.  
  221. void strokeString(char* str, double x, double y, double z, double s)
  222. {
  223. int i, n;
  224.  
  225. n = strlen(str);
  226. glPushMatrix();
  227. glColor3d(0.0, 0.0, 0.0);
  228. glTranslated(x, y, z);
  229. glScaled(s, s, s);
  230. for (i = 0; i < n; i++)
  231. glutStrokeCharacter(GLUT_STROKE_ROMAN, (int)str[i]);
  232.  
  233. glPopMatrix();
  234.  
  235. }
  236.  
  237. void bitmapString(char* str, double x, double y)
  238. {
  239. int i, n;
  240.  
  241. // fonte pode ser:
  242. // GLUT_BITMAP_8_BY_13
  243. // GLUT_BITMAP_9_BY_15
  244. // GLUT_BITMAP_TIMES_ROMAN_10
  245. // GLUT_BITMAP_TIMES_ROMAN_24
  246. // GLUT_BITMAP_HELVETICA_10
  247. // GLUT_BITMAP_HELVETICA_12
  248. // GLUT_BITMAP_HELVETICA_18
  249. //
  250. // int glutBitmapWidth ( void *font , int character);
  251. // devolve a largura de um carácter
  252. //
  253. // int glutBitmapLength ( void *font , const unsigned char *string );
  254. // devolve a largura de uma string (soma da largura de todos os caracteres)
  255.  
  256. n = strlen(str);
  257. glRasterPos2d(x, y);
  258. for (i = 0; i < n; i++)
  259. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)str[i]);
  260. }
  261.  
  262. void bitmapCenterString(char* str, double x, double y)
  263. {
  264. int i, n;
  265.  
  266. n = strlen(str);
  267. glRasterPos2d(x - glutBitmapLength(GLUT_BITMAP_HELVETICA_18, (const unsigned char*)str) * 0.5, y);
  268. for (i = 0; i < n; i++)
  269. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, (int)str[i]);
  270. }
  271.  
  272.  
  273. // ... definicao das rotinas auxiliares de desenho ...
  274.  
  275.  
  276. void desenhaTanque(Tanque t)
  277. {
  278. glPushMatrix();
  279. glTranslatef(0, 0, ALTURA_BASE * 0.5);
  280. glPushMatrix();
  281. glScalef(COMPRIMENTO_BASE, LARGURA_BASE, ALTURA_BASE);
  282. desenhaCubo();
  283. glPopMatrix();
  284.  
  285.  
  286. GLUquadric* quad;
  287. quad = gluNewQuadric();
  288. //front left
  289. glPushMatrix();
  290. glTranslatef(COMPRIMENTO_BASE * 0.4, LARGURA_BASE*0.6, 0);
  291. glRotatef(90,1,0,0);
  292. gluCylinder(quad,0.5,0.5,0.4,10,2);
  293. glPopMatrix();
  294. //rear left
  295. glPushMatrix();
  296. glTranslatef(-COMPRIMENTO_BASE * 0.4, LARGURA_BASE * 0.6, 0);
  297. glRotatef(90, 1, 0, 0);
  298. gluCylinder(quad, 0.5, 0.5, 0.4, 10, 2);
  299. glPopMatrix();
  300.  
  301. //front right
  302.  
  303. glPushMatrix();
  304. glTranslatef(COMPRIMENTO_BASE * 0.4, -LARGURA_BASE * 0.5, 0);
  305. glRotatef(90, 1, 0, 0);
  306. gluCylinder(quad, 0.5, 0.5, 0.4, 10, 2);
  307. glPopMatrix();
  308.  
  309. //rear right
  310.  
  311. glPushMatrix();
  312. glTranslatef(-COMPRIMENTO_BASE * 0.4, -LARGURA_BASE * 0.5, 0);
  313. glRotatef(90, 1, 0, 0);
  314. gluCylinder(quad, 0.5, 0.5, 0.4, 10, 2);
  315. glPopMatrix();
  316.  
  317.  
  318.  
  319.  
  320. glTranslatef(0, 0, ALTURA_BASE * 0.5 + ALTURA_TORRE * 0.5);
  321. glRotatef(t.angTorre, 0, 0, 1);
  322. glPushMatrix();
  323. glScalef(COMPRIMENTO_TORRE, LARGURA_TORRE, ALTURA_TORRE);
  324. desenhaCubo();
  325. glPopMatrix();
  326.  
  327. //glTranslatef(COMPRIMENTO_TORRE * 0.5,0, 0);
  328. glRotatef(-t.angCanhao, 0, 1, 0);
  329. glTranslatef(COMPRIMENTO_CANHAO * 0.5, 0, 0);
  330. glPushMatrix();
  331. glScalef(COMPRIMENTO_CANHAO, RAIO_CANHAO * 2, RAIO_CANHAO * 2);
  332. desenhaCubo();
  333. glPopMatrix();
  334.  
  335. glPopMatrix();
  336.  
  337. }
  338.  
  339. void desenhaChao(GLfloat dimensao)
  340. {
  341.  
  342. glBegin(GL_POLYGON);
  343. glVertex3f(dimensao, dimensao, 0);
  344. glVertex3f(-dimensao, dimensao, 0);
  345. glVertex3f(-dimensao, -dimensao, 0);
  346. glVertex3f(dimensao, -dimensao, 0);
  347. glEnd();
  348.  
  349. }
  350.  
  351. // Callback de desenho
  352.  
  353. void Draw(void)
  354. {
  355. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  356.  
  357. glLoadIdentity();
  358.  
  359. gluLookAt(estado.camera.eye.x, estado.camera.eye.y, estado.camera.eye.z, \
  360. estado.camera.center.x, estado.camera.center.y, estado.camera.center.z, \
  361. estado.camera.up.x, estado.camera.up.y, estado.camera.up.z);
  362.  
  363. // ... chamada das rotinas auxiliares de desenho ...
  364.  
  365. glColor3f(0.5f, 0.5f, 0.5f);
  366. desenhaChao(RAIO_ROTACAO + 5);
  367.  
  368.  
  369. glPushMatrix();
  370. glRotatef(modelo.tanque.direccao, 0, 0, 1);
  371. glTranslatef(modelo.tanque.x, modelo.tanque.y, 0);
  372.  
  373. desenhaTanque(modelo.tanque);
  374.  
  375. glPopMatrix();
  376.  
  377.  
  378. glFlush();
  379. if (estado.doubleBuffer)
  380. glutSwapBuffers();
  381. }
  382.  
  383. /*******************************
  384. * callbacks timer/idle *
  385. *******************************/
  386.  
  387. void Timer(int value)
  388. {
  389. glutTimerFunc(estado.delayMovimento, Timer, 0);
  390. if (estado.teclas.z)
  391. {
  392. modelo.tanque.angTorre += 2;
  393.  
  394. }
  395. if (estado.teclas.x)
  396. {
  397. modelo.tanque.angTorre -= 2;
  398.  
  399. }
  400. if (estado.teclas.q && modelo.tanque.angCanhao < 75)
  401. {
  402. modelo.tanque.angCanhao += 2;
  403.  
  404. }
  405. if (estado.teclas.a && modelo.tanque.angCanhao > 0)
  406. {
  407. modelo.tanque.angCanhao -= 2;
  408.  
  409. }
  410. if (estado.menuActivo || modelo.parado) // sair em caso de o jogo estar parado ou menu estar activo
  411. {
  412. glutPostRedisplay();
  413. return;
  414. }
  415.  
  416. modelo.tanque.direccao += 1;
  417.  
  418. if (estado.menuActivo || modelo.parado) // sair em caso de o jogo estar parado ou menu estar activo
  419. return;
  420.  
  421. // redesenhar o ecra
  422. glutPostRedisplay();
  423. }
  424.  
  425.  
  426.  
  427. void imprime_ajuda(void)
  428. {
  429. printf("\n\nDesenho de um quadrado\n");
  430. printf("h,H - Ajuda \n");
  431. printf("z,Z - Roda torre para a esquerda\n");
  432. printf("x,X - Roda torre para a direita\n");
  433. printf("q,Q - Levantar canhao\n");
  434. printf("a,A - Baixar canhao\n");
  435. printf("i,I - Reinicia modelo\n");
  436. printf("o,O - Alterna entre projecãoo Ortografica e Perspectiva\n");
  437. printf("f,F - Poligono Fill \n");
  438. printf("l,L - Poligono Line \n");
  439. printf("p,P - Poligono Point \n");
  440. printf("s,S - Inicia/para movimento\n");
  441. printf("ESC - Sair\n");
  442. }
  443.  
  444. /*******************************
  445. * callbacks de teclado *
  446. *******************************/
  447.  
  448. // Callback para interaccao via teclado (carregar na tecla)
  449.  
  450. void Key(unsigned char key, int x, int y)
  451. {
  452. switch (key) {
  453.  
  454. case 27:
  455. exit(0);
  456. // ... accoes sobre outras teclas ...
  457.  
  458. case 'h':
  459. case 'H':
  460. imprime_ajuda();
  461. break;
  462. case 'i':
  463. case 'I':
  464. inicia_modelo();
  465. break;
  466. case 'o':
  467. case 'O':
  468. estado.ortho = !estado.ortho;
  469. Reshape(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
  470. break;
  471. case 'Q':
  472. case 'q': estado.teclas.q = GL_TRUE;
  473. break;
  474. case 'A':
  475. case 'a': estado.teclas.a = GL_TRUE;
  476. break;
  477. case 'Z':
  478. case 'z': estado.teclas.z = GL_TRUE;
  479. break;
  480. case 'X':
  481. case 'x': estado.teclas.x = GL_TRUE;
  482. break;
  483. case 'D':
  484. case 'd': estado.debug = !estado.debug;
  485. if (estado.menuActivo || modelo.parado)
  486. glutPostRedisplay();
  487. printf("DEBUG is %s\n", (estado.debug) ? "ON" : "OFF");
  488. break;
  489. case 'f':
  490. case 'F':
  491. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  492. break;
  493. case 'p':
  494. case 'P':
  495. glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
  496. break;
  497. case 'l':
  498. case 'L':
  499. glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
  500. break;
  501.  
  502. /* case 's' :
  503. case 'S' :
  504. modelo.parado=!modelo.parado;
  505. break;*/
  506.  
  507.  
  508. }
  509.  
  510. if (estado.debug)
  511. printf("Carregou na tecla %c\n", key);
  512.  
  513. }
  514.  
  515. // Callback para interaccao via teclado (largar a tecla)
  516.  
  517. void KeyUp(unsigned char key, int x, int y)
  518. {
  519. switch (key) {
  520. // ... accoes sobre largar teclas ...
  521.  
  522. case 'Q':
  523. case 'q': estado.teclas.q = GL_FALSE;
  524. break;
  525. case 'A':
  526. case 'a': estado.teclas.a = GL_FALSE;
  527. break;
  528. case 'Z':
  529. case 'z': estado.teclas.z = GL_FALSE;
  530. break;
  531. case 'X':
  532. case 'x': estado.teclas.x = GL_FALSE;
  533. break;
  534.  
  535. }
  536.  
  537. if (estado.debug)
  538. printf("Largou a tecla %c\n", key);
  539. }
  540.  
  541. // Callback para interaccao via teclas especiais (carregar na tecla)
  542.  
  543. void SpecialKey(int key, int x, int y)
  544. {
  545. // ... accoes sobre outras teclas especiais ...
  546. // GLUT_KEY_F1 ... GLUT_KEY_F12
  547. // GLUT_KEY_UP
  548. // GLUT_KEY_DOWN
  549. // GLUT_KEY_LEFT
  550. // GLUT_KEY_RIGHT
  551. // GLUT_KEY_PAGE_UP
  552. // GLUT_KEY_PAGE_DOWN
  553. // GLUT_KEY_HOME
  554. // GLUT_KEY_END
  555. // GLUT_KEY_INSERT
  556.  
  557. switch (key) {
  558.  
  559. // redesenhar o ecra
  560. //glutPostRedisplay();
  561. }
  562.  
  563.  
  564. if (estado.debug)
  565. printf("Carregou na tecla especial %d\n", key);
  566. }
  567.  
  568. // Callback para interaccao via teclas especiais (largar na tecla)
  569.  
  570. void SpecialKeyUp(int key, int x, int y)
  571. {
  572. switch (key) {
  573. }
  574. if (estado.debug)
  575. printf("Largou a tecla especial %d\n", key);
  576.  
  577. }
  578.  
  579. int main(int argc, char** argv)
  580. {
  581. // char str[]=" makefile MAKEFILE Makefile ";
  582. estado.doubleBuffer = 1;
  583.  
  584. glutInit(&argc, argv);
  585. glutInitWindowPosition(0, 0);
  586. glutInitWindowSize(640, 480);
  587. glutInitDisplayMode(((estado.doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) | GLUT_RGB | GLUT_DEPTH);
  588. if (glutCreateWindow("Exemplo") == GL_FALSE)
  589. exit(1);
  590.  
  591. Init();
  592.  
  593. imprime_ajuda();
  594.  
  595. // Registar callbacks do GLUT
  596.  
  597. // callbacks de janelas/desenho
  598. glutReshapeFunc(Reshape);
  599. glutDisplayFunc(Draw);
  600.  
  601. // Callbacks de teclado
  602. glutKeyboardFunc(Key);
  603. glutKeyboardUpFunc(KeyUp);
  604. glutSpecialFunc(SpecialKey);
  605. glutSpecialUpFunc(SpecialKeyUp);
  606.  
  607. // callbacks timer/idle
  608. glutTimerFunc(estado.delayMovimento, Timer, 0);
  609.  
  610. // COMECAR...
  611. glutMainLoop();
  612. return 0;
  613. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement