Advertisement
IcaroPeretti

Colisão

Jun 20th, 2021
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. /// colisao de quadrados
  2. //////////////////////////////////////////////
  3.  
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <GL/glut.h>
  7.  
  8. #define janela_altura 400
  9. #define janela_largura 600
  10.  
  11. float trans = 0;
  12.  
  13. void display(void);
  14. void tela(GLsizei w, GLsizei h);
  15. void keyboard(unsigned char tecla, int x, int y);
  16.  
  17. int main(int argc, char** argv)
  18. {
  19. glutInit(&argc, argv); // suporte a janelas
  20.  
  21. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  22. // PADRAO DE CORES
  23.  
  24. glutInitWindowSize(janela_largura, janela_altura); // tamanho da janela
  25. glutInitWindowPosition(100, 100); // posicao que surge a janela
  26. glutCreateWindow("colisao_quadrado"); // cria janela
  27.  
  28. //glutFullScreen();
  29. glutKeyboardFunc(&keyboard); // chama teclado
  30. glutReshapeFunc(tela); // configura a tela
  31. glutDisplayFunc(display);
  32. glutMainLoop(); // redesenhar
  33.  
  34. return(0);
  35. }
  36.  
  37.  
  38. void keyboard(unsigned char tecla, int x, int y)
  39. {
  40. printf("\ntecla %c\n", tecla);
  41. printf("\n\nDigite 1 para esquerda: ");
  42. printf("\ntecla %c\n", tecla);
  43. printf("\no mouse estava em %d x %d\n", x, y);
  44. if (tecla == '1')
  45. {
  46. trans = trans - 1;
  47. printf("\n o valor de translacao e %.2f\n", trans);
  48. }
  49. if (tecla == '2')
  50. {
  51. trans = trans + 1;
  52. printf("\n o valor de translacao e %.2f\n", trans);
  53. }
  54.  
  55.  
  56.  
  57. glutPostRedisplay();
  58.  
  59.  
  60. }
  61.  
  62.  
  63.  
  64. void desenhar()
  65. {
  66.  
  67. glTranslatef(0, 0, 0);
  68.  
  69. glBegin(GL_QUADS);
  70.  
  71. glColor3f(1.0, 0.0, 0.0); // cor
  72. glVertex2f(-100, 100);
  73. glVertex2f(-5, 100);
  74. glVertex2f(-5, 0);
  75. glVertex2f(-100, 0);
  76.  
  77. glEnd();
  78.  
  79. glTranslatef(trans, 0, 0);
  80.  
  81. glBegin(GL_QUADS);
  82. glColor3f(1.0, 1.0, 0.0); // cor
  83. glVertex2f(100, 100);
  84. glVertex2f(5, 100);
  85. glVertex2f(5, 0);
  86. glVertex2f(100, 0);
  87.  
  88. glEnd();
  89.  
  90. }
  91.  
  92. void display()
  93. {
  94. glMatrixMode(GL_MODELVIEW); //coordenadas de desenho
  95. glLoadIdentity();
  96.  
  97. if (trans > -10)
  98. {
  99. glClearColor(0.0f, 0.0f, 1.0f, 1.0f); // cor do fundo
  100.  
  101. }
  102. else
  103. {
  104. glClearColor(1.0f, 1.0f, 1.0f, 1.0f); // cor do fundo
  105. }
  106.  
  107. glClear(GL_COLOR_BUFFER_BIT); // EXECUTA LIMPESA
  108.  
  109. // Especificar o local aonde o desenho acontece: bem no centro da janela
  110. glTranslatef(janela_largura / 2, janela_altura / 2, 0.0f);
  111.  
  112. glViewport(0, 0, janela_largura, janela_altura);
  113. desenhar();
  114.  
  115.  
  116. glFlush(); // execute o desenho
  117.  
  118. }
  119.  
  120. void tela(GLsizei w, GLsizei h)
  121. {
  122.  
  123.  
  124. glMatrixMode(GL_PROJECTION);
  125. glLoadIdentity();
  126.  
  127. // cria a janela (esq, direita, embaixo, em cima)
  128. gluOrtho2D(0, janela_largura, 0, janela_altura);
  129.  
  130.  
  131. glMatrixMode(GL_MODELVIEW);
  132.  
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement