Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.51 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdio.h> // printf
  4. #include "RgbImage.h"
  5. #include <math.h>
  6. #include <time.h>
  7. #include <OpenGL/gl.h>
  8. #include <OpenGL/glu.h>
  9. #include <GLUT/glut.h> // openGL
  10.  
  11. //==================================================================== Definir cores
  12. #define AZUL 0.0, 0.0, 1.0, 1.0
  13. #define VERMELHO 1.0, 0.0, 0.0, 1.0
  14. #define AMARELO 1.0, 1.0, 0.0, 1.0
  15. #define VERDE 0.0, 1.0, 0.0, 1.0
  16. #define LARANJA 0.8, 0.6, 0.1, 1.0
  17. #define WHITE 1.0, 1.0, 1.0, 1.0
  18. #define BLACK 0.0, 0.0, 0.0, 1.0
  19. #define GRAY1 0.2, 0.2, 0.2, 1.0
  20. #define GRAY2 0.8, 0.8, 0.8, 1.0
  21. #define PRETO 0.0, 0.0, 0.0, 1.0
  22.  
  23. #include "RgbImage.h"
  24. #define PI 3.14159
  25.  
  26. //================================================================================
  27. //===========================================================Variaveis e constantes
  28.  
  29. //------------------------------------------------------------ Sistema Coordenadas
  30. GLfloat xC = 16.0, zC = 15.0;
  31. GLint wScreen = 800, hScreen = 500;
  32. GLint msec = 100; //.. definicao do timer (actualizacao)
  33.  
  34. //------------------------------------------------------------ Observador
  35. GLfloat rVisao = 3.0, aVisao = 0.5 * PI, incVisao = 0.1;
  36. GLfloat angPersp = 109.0;
  37. GLfloat obsPini[] = {1, 1.0, 0.5 * xC};
  38. GLfloat obsPfin[] = {obsPini[0] - rVisao * cos(aVisao), obsPini[1], obsPini[2] - rVisao *sin(aVisao)};
  39.  
  40. RgbImage imag;
  41.  
  42. //--------------------------------------------- Escada
  43. int y = 0, z = 0;
  44. //------------------------------
  45. int verticesESC = 3 * 52; // 53 vertices (x,y,z) = 53*3
  46. int facesESC = 13; // 53/4
  47. int plataformas = 2;
  48.  
  49. static GLuint poligono[] = {};
  50. static GLfloat vertices[] = {
  51. -2.000000, -0.000000, -0.960000, 2.000000, -0.000000, -0.960000, 2.000000, 0.000000, -0.000000, -2.000000, 0.000000, -0.000000, -2.000000, 0.000000, -0.960000, 2.000000, 0.000000, -0.960000, 2.000000, 0.160000, -0.960000, -2.000000, 0.160000, -0.960000, -2.000000, 0.160000, -1.280000, 2.000000, 0.160000, -1.280000, 2.000000, 0.160000, -0.960000, -2.000000, 0.160000, -0.960000, -2.000000, 0.160000, -1.280000, 2.000000, 0.160000, -1.280000, 2.000000, 0.320000, -1.280000, -2.000000, 0.320000, -1.280000, -2.000000, 0.320000, -1.600000, 2.000000, 0.320000, -1.600000, 2.000000, 0.320000, -1.280000, -2.000000, 0.320000, -1.280000, -2.000000, 0.320000, -1.600000, 2.000000, 0.320000, -1.600000, 2.000000, 0.480000, -1.600000, -2.000000, 0.480000, -1.600000, -2.000000, 0.480000, -2.560000, 2.000000, 0.480000, -2.560000, 2.000000, 0.480000, -1.600000, -2.000000, 0.480000, -1.600000, -2.000000, 0.480000, -2.560000, 2.000000, 0.480000, -2.560000, 2.000000, 0.640000, -2.560000, -2.000000, 0.640000, -2.560000, -2.000000, 0.640000, -2.880000, 2.000000, 0.640000, -2.880000, 2.000000, 0.640000, -2.560000, -2.000000, 0.640000, -2.560000, -2.000000, 0.640000, -2.880000, 2.000000, 0.640000, -2.880000, 2.000000, 0.800000, -2.880000, -2.000000, 0.800000, -2.880000, -2.000000, 0.800000, -3.200000, 2.000000, 0.800000, -3.200000, 2.000000, 0.800000, -2.880000, -2.000000, 0.800000, -2.880000, -2.000000, 0.800000, -3.200000, 2.000000, 0.800000, -3.200000, 2.000000, 0.960000, -3.200000, -2.000000, 0.960000, -3.200000, -2.000000, 0.960000, -4.160000, 2.000000, 0.960000, -4.160000, 2.000000, 0.960000, -3.200000, -2.000000, 0.960000, -3.200000};
  52.  
  53. static GLfloat normais[] = {
  54. 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000, 0.000000, 0.000000, 1.000000};
  55.  
  56. // -------------- objectos
  57. void drawEscada()
  58. {
  59.  
  60. //------------------------- ESCADA
  61. glColor4f(GRAY2);
  62. glPushMatrix();
  63. glTranslatef(5, -4.0, 0.0);
  64. glScalef(20, 20, 19);
  65.  
  66. for (int p = 0; p < facesESC; p++)
  67. {
  68. poligono[0] = p * 4 + 0;
  69. poligono[1] = p * 4 + 1;
  70. poligono[2] = p * 4 + 2;
  71. poligono[3] = p * 4 + 3;
  72.  
  73. glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, poligono);
  74. }
  75.  
  76. glPopMatrix();
  77.  
  78. }
  79.  
  80.  
  81. void plataforma2(){
  82. //------------------------- ESCADA
  83. glColor4f(GRAY1);
  84. glPushMatrix();
  85. glTranslatef(15, 18.0, -100.0);
  86. glScalef(20, 20, 19);
  87.  
  88. for (int p = 0; p < facesESC; p++)
  89. {
  90. poligono[0] = p * 4 + 0;
  91. poligono[1] = p * 4 + 1;
  92. poligono[2] = p * 4 + 2;
  93. poligono[3] = p * 4 + 3;
  94.  
  95. glDrawElements(GL_POLYGON, 4, GL_UNSIGNED_INT, poligono);
  96. }
  97.  
  98. glPopMatrix();
  99. }
  100.  
  101. void drawColuna(){
  102. glColor4f(0.5f, 0.35f, 0.05f, 1.0f);
  103.  
  104. glPushMatrix();
  105. glBegin(GL_QUADS);
  106. glVertex3i(-5, 0, 2);
  107. glVertex3i(10, 0, 2);
  108. glVertex3i(10, 0, -5);
  109. glVertex3i(-5, 0, -5);
  110. glEnd();
  111. glPopMatrix();
  112. }
  113.  
  114.  
  115. void init(void)
  116. {
  117. glClearColor(WHITE);
  118. glShadeModel(GL_SMOOTH);
  119.  
  120. glEnable(GL_DEPTH_TEST);
  121.  
  122. //-------------------------------
  123. glVertexPointer(3, GL_FLOAT, 0, vertices);
  124.  
  125. glEnableClientState(GL_VERTEX_ARRAY);
  126. glNormalPointer(GL_FLOAT, 0, normais);
  127. glEnableClientState(GL_NORMAL_ARRAY);
  128. }
  129.  
  130. GLvoid resize(GLsizei width, GLsizei height)
  131. {
  132. wScreen = width;
  133. hScreen = height;
  134. glViewport(0, 0, wScreen, hScreen);
  135. glutPostRedisplay();
  136. }
  137.  
  138. void drawOrientacao()
  139. {
  140.  
  141. glDisable(GL_TEXTURE_2D);
  142. //----------------------- OBSERVADOR
  143. glPushMatrix();
  144. glColor4f(VERDE);
  145. glTranslatef(obsPini[0], obsPini[1], obsPini[2]);
  146. glutSolidCube(1);
  147. glPopMatrix();
  148. glPushMatrix();
  149. glColor4f(PRETO);
  150. glTranslatef(obsPfin[0], obsPfin[1], obsPfin[2]);
  151. glutSolidCube(1.0);
  152. glPopMatrix();
  153. }
  154.  
  155. void display(void)
  156. {
  157.  
  158. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  159.  
  160. //----------------------------------------- Viewport
  161. glViewport(wScreen / 5, 0, 0.75 * wScreen, hScreen);
  162. glMatrixMode(GL_PROJECTION);
  163. glLoadIdentity();
  164. gluPerspective(angPersp, (float)wScreen / hScreen, 0.1, 100.0);
  165. glMatrixMode(GL_MODELVIEW);
  166. glLoadIdentity();
  167. gluLookAt(obsPini[0], obsPini[1], obsPini[2], obsPfin[0], obsPfin[1], obsPfin[2], 0, 1, 0);
  168.  
  169. drawEscada();
  170. plataforma2();
  171. drawColuna();
  172.  
  173. glutSwapBuffers();
  174. }
  175.  
  176. void updateVisao()
  177. {
  178.  
  179. obsPfin[0] = obsPini[0] + rVisao * cos(aVisao);
  180. obsPfin[2] = obsPini[2] - rVisao * sin(aVisao);
  181. glutPostRedisplay();
  182. }
  183.  
  184. void keyboard(unsigned char key, int x, int y)
  185. {
  186. switch (key)
  187. {
  188. //--------------------------- Escape
  189. case 27:
  190. exit(0);
  191. break;
  192. }
  193. }
  194.  
  195. void teclasNotAscii(int key, int x, int y)
  196. {
  197. if (key == GLUT_KEY_UP)
  198. {
  199. obsPini[0] = obsPini[0] + incVisao * cos(aVisao)*10;
  200. obsPini[2] = obsPini[2] - incVisao * sin(aVisao)*10;
  201. }
  202. if (key == GLUT_KEY_DOWN)
  203. {
  204. obsPini[0] = obsPini[0] - incVisao * cos(aVisao)*10;
  205. obsPini[2] = obsPini[2] + incVisao * sin(aVisao)*10;
  206. }
  207. if (key == GLUT_KEY_LEFT)
  208. aVisao = (aVisao + 0.01);
  209. if (key == GLUT_KEY_RIGHT)
  210. aVisao = (aVisao - 0.01);
  211. updateVisao();
  212. }
  213.  
  214. void Timer(int value)
  215. {
  216. glutPostRedisplay();
  217. glutTimerFunc(msec, Timer, 1);
  218. }
  219.  
  220. //======================================================= MAIN
  221. int main(int argc, char **argv)
  222. {
  223.  
  224. glutInit(&argc, argv);
  225. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  226. glutInitWindowSize(wScreen, hScreen);
  227. glutInitWindowPosition(400, 100);
  228. glutCreateWindow("Escadas Moumentais - Ines & David");
  229.  
  230. init();
  231. glutSpecialFunc(teclasNotAscii);
  232. glutReshapeFunc(resize);
  233. glutDisplayFunc(display);
  234. glutKeyboardFunc(keyboard);
  235. glutTimerFunc(msec, Timer, 1);
  236.  
  237. glutMainLoop();
  238.  
  239. return 0;
  240. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement