Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.60 KB | None | 0 0
  1. #include <windows.h>
  2. #include "stdlib.h"
  3. #include "math.h"
  4. #include "stdio.h"
  5. #include <GL\freeGlut.h>
  6.  
  7.  
  8. //---------------------------------------------------------------------------
  9. //------------------------------------------------------- Variaveis globais
  10. //---------------------------------------------------------------------------
  11.  
  12. GLfloat wC = 10.0, hC = 10.0; //.. coordenadas reais - definição do mundo
  13.  
  14. GLfloat wSup = 4.0, hSup = 1.0; //.. dimensoes do suporte
  15. GLfloat xSup = 0.0, incSup = 0.45; //.. posicao horizontal do suporte / incremento para deslocamento
  16.  
  17. GLfloat wLig = 0.8, hLig = 3.0;
  18. GLfloat agLig = 90.0, incLig = 10.0;
  19.  
  20. GLfloat wExt = 3.0, hExt = 0.8;
  21. GLfloat agExt = 0.0, incExt = 10.0;
  22.  
  23. GLint msec = 20; //.. definicao do timer (actualizacao)
  24. char texto[30]; //.. texto a ser escrito no ecran
  25. GLint tentativas = 0; //.. numero de blocos gerados
  26.  
  27.  
  28.  
  29. //++++++++++++++++++++++++++++++++++++++++++++ fazer pelos alunos
  30. GLfloat Matriz[4][4]; // para obter a posição Px, Py
  31. GLfloat Px, Py; // posição final da extremidade
  32. //++++++++++++++++++++++++++++++++++++++++++++ fazer pelos alunos
  33.  
  34.  
  35. //---------------------------------------------------------------------------
  36. //---------------------------------------------------- Inicializa parâmetros
  37. //---------------------------------------------------------------------------
  38.  
  39. void inicializa(void) {
  40. glClearColor(0, 0, 0, 1);
  41. glShadeModel(GL_SMOOTH);
  42. gluOrtho2D(-wC, wC, -hC, hC); // coordenadas mundo
  43. srand(1); // para gerar posicao aleatória
  44. }
  45.  
  46. float aleatorio(GLint minimo, GLint maximo) {
  47. GLfloat y;
  48. y = rand() % 1000;
  49. return (minimo + 0.001 * y * (maximo - minimo));
  50. }
  51.  
  52. //---------------------------------------------------------------------------
  53. //----------------------------------------------------------------- Desenhar
  54. //---------------------------------------------------------------------------
  55. void desenhaQuadrado(GLfloat tam, GLfloat r, GLfloat g, GLfloat b) {
  56. //------------------------------------ desenhado na origem
  57. glColor3f(r, g, b);
  58. glBegin(GL_QUADS);
  59. glVertex2f(0 - tam, 0 - tam);
  60. glVertex2f(0 - tam, 0 + tam);
  61. glVertex2f(0 + tam, 0 + tam);
  62. glVertex2f(0 + tam, 0 - tam);
  63. glEnd();
  64. }
  65.  
  66. void desenhaTexto(char* string, GLfloat x, GLfloat y) {
  67. glRasterPos2f(x, y);
  68. while (*string)
  69. glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, *string++);
  70. }
  71.  
  72.  
  73. void desenhaRobot(GLfloat xSup, GLfloat agLig, GLfloat agExt) {
  74. int i, j;
  75. glPushMatrix();
  76.  
  77. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Suporte vermelho
  78. glTranslatef(xSup, 0.0, 0.0);
  79. glGetFloatv(GL_MODELVIEW_MATRIX, &Matriz[0][0]); // valor final do ponto 'extremidade'
  80. glPushMatrix();
  81. glScalef(wSup, hSup, 1.0);
  82. desenhaQuadrado(1.0, 1, 0, 0);
  83. glPopMatrix();
  84.  
  85. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Ligacao verde
  86. glRotatef(agLig, 0.0, 0.0, 1.0);
  87. glTranslatef(0.0, hLig, 0.0);
  88. glPushMatrix();
  89. glScalef(wLig, hLig, 1.0);
  90. desenhaQuadrado(1.0, 0, 1, 0);
  91. glPopMatrix();
  92.  
  93. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Extremidade Azul
  94. glTranslatef(0, hLig, 0.0);
  95. glRotatef(agExt, 0.0, 0.0, 1.0);
  96. glPushMatrix();
  97. glScalef(wExt, hExt, 1.0);
  98. desenhaQuadrado(1.0, 0, 0, 1);
  99. glPopMatrix();
  100.  
  101.  
  102. glPopMatrix();
  103. }
  104.  
  105.  
  106.  
  107. //---------------------------------------------------------------------------
  108. //.............................................................. Desenha Cena
  109. //---------------------------------------------------------------------------
  110. void desenhaJanela(void) {
  111. int i, j;
  112.  
  113. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~limpa
  114. glClear(GL_COLOR_BUFFER_BIT);
  115.  
  116. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~BraçoArticulado
  117. desenhaRobot(xSup, agLig, agExt);
  118.  
  119. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Quadrado a cair
  120.  
  121.  
  122. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Apanhou ou nao??
  123. //+++++++++++++++++++++++++++++++++++++ posicao final da extremidade
  124. // o glGetFloatv está no local pretendido !!!!!!!
  125. // onde se quer conher a posicao final
  126. //glGetFloatv(GL_MODELVIEW_MATRIX, &Matriz[0][0]); // valor final do ponto 'extremidade '
  127. Px = wC * Matriz[3][0];
  128. Py = hC * Matriz[3][1];
  129. printf(" Px=%4.3f Py=%4.3f\n ", Px, Py);
  130.  
  131.  
  132.  
  133.  
  134. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX A fazer pelos alunos
  135.  
  136. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~Actualizar tentativas e apanhados
  137. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX A fazer pelos alunos
  138. sprintf_s(texto, "Tentativas = %d", tentativas);
  139. desenhaTexto(texto, -wC + 1, hC - 3);
  140. //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX A fazer pelos alunos
  141.  
  142.  
  143. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Informacao
  144. glColor3f(0, 0, 0.97);
  145. char t[] = "{jh,ep}@dei.uc.pt";
  146. desenhaTexto(t, -wC + 0.2, -hC + 0.1);
  147.  
  148.  
  149. //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Refresh
  150. glutSwapBuffers();
  151. }
  152.  
  153.  
  154. //---------------------------------------------------------------------------
  155. //......................................................................Timer
  156. //---------------------------------------------------------------------------
  157. void Timer(int value) {
  158. glutPostRedisplay();
  159. glutTimerFunc(msec, Timer, 1);
  160. }
  161.  
  162.  
  163. //---------------------------------------------------------------------------
  164. //....................................................................Eventos
  165. //---------------------------------------------------------------------------
  166.  
  167. void teclasNotAscii(int key, int x, int y) {
  168. if (key == GLUT_KEY_LEFT) {
  169. xSup = xSup - incSup;
  170. glutPostRedisplay();
  171. }
  172. if (key == GLUT_KEY_RIGHT) {
  173. xSup = xSup + incSup;
  174. glutPostRedisplay();
  175. }
  176. if (key == GLUT_KEY_UP) {
  177. agLig = agLig + incLig;
  178. glutPostRedisplay();
  179. }
  180. if (key == GLUT_KEY_DOWN) {
  181. agLig = agLig - incLig;
  182. glutPostRedisplay();
  183. }
  184. }
  185.  
  186. void teclado(unsigned char key, int x, int y) {
  187.  
  188. switch (key) {
  189.  
  190. case 'A':
  191. case 'a':
  192. agExt = agExt + incExt;
  193. glutPostRedisplay();
  194. break;
  195. case 'Q':
  196. case 'q':
  197. agExt = agExt - incExt;
  198. glutPostRedisplay();
  199. break;
  200.  
  201. case 27:
  202. exit(0);
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208.  
  209.  
  210. //-----------------------------------------------------
  211. int main(int argc, char** argv) {
  212. glutInit(&argc, argv);
  213. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  214. glutInitWindowSize(500, 500);
  215. glutInitWindowPosition(300, 100);
  216. glutCreateWindow("Teclas: left, right, up, down, q, a ");
  217.  
  218. inicializa();
  219.  
  220. glutDisplayFunc(desenhaJanela);
  221. glutKeyboardFunc(teclado);
  222. glutSpecialFunc(teclasNotAscii);
  223. glutTimerFunc(msec, Timer, 1);
  224.  
  225. glutMainLoop();
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement