Advertisement
Guest User

Untitled

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