Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. /*
  2. Practica 1 Informática Gráfica
  3. */
  4.  
  5. #include <windows.h>
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. //#include <sys/time.h>
  10.  
  11. #include <ctime>
  12.  
  13. #ifdef __APPLE__
  14. #include <GLUT/glut.h>
  15. #else
  16. #include <GL/gl.h>
  17. #include <GL/glut.h>
  18. #endif
  19.  
  20. #include <math.h>
  21.  
  22. using namespace std;
  23.  
  24. #include <GL/glut.h>
  25. #include <iostream>
  26. #include <cstdlib>
  27.  
  28. float xi, yi, zi;
  29.  
  30. float angleZ, angleX;
  31.  
  32. int screenWidth = 640;
  33. int screenHeight = 480;
  34. //DEFINICION DE FUNCIONES DE CALLBACK
  35.  
  36. //Coloca la imagen raster en la posicion del puntero del raton al pulsar boton izdo. Limpia la ventana co el boton derecho
  37. void myMouse(int button, int state, int mx, int my) {
  38.  
  39. if (button == GLUT_LEFT_BUTTON)
  40. {
  41.  
  42. }
  43. if (button == GLUT_RIGHT_BUTTON) {
  44.  
  45. }
  46. glutPostRedisplay();
  47. }
  48.  
  49.  
  50.  
  51.  
  52.  
  53. void dibujarEje() {
  54. //DIBUJA UN CONJUNTO DE PUNTOS
  55.  
  56. glBegin(GL_LINES);
  57.  
  58. glColor3f(1, 0, 0);
  59. glVertex3f(0.0, 0.0, 0.0);
  60. glVertex3f(1.0, 0.0, 0.0);
  61.  
  62. glColor3f(0, 1, 0);
  63. glVertex3f(0.0, 0.0, 0.0);
  64. glVertex3f(0.0, 1.0, 0.0);
  65.  
  66. glColor3f(0, 0, 1);
  67. glVertex3f(-1.0, -1.0, -1.0);
  68. glVertex3f(0.0, 0.0, 1.0);
  69.  
  70. glEnd();
  71.  
  72. }
  73.  
  74.  
  75. //Cambia las dimensiones de la pantalla
  76. void myReshape(int w, int h) {
  77. screenWidth = w;
  78. screenHeight = h;
  79.  
  80. }
  81.  
  82. //Dibuja el raster
  83.  
  84. void myDisplay(void) {
  85.  
  86. glClearColor(0.0, 0.0, 0.0, 0.0);
  87. glClear(GL_COLOR_BUFFER_BIT);
  88.  
  89. glLoadIdentity();
  90. dibujarEje();
  91.  
  92. GLfloat matrizT[16] = { 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0,xi, yi, zi, 1.0 };
  93. GLfloat matrizR[16] = { cos(angleZ), sin(angleZ), 0.0, 0.0, -sin(angleZ), cos(angleZ), 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0 };
  94. GLfloat matrizX[16] = { 1.0, 0.0, 0.0, 0.0, 0.0, cos(angleX), sin(angleX), 0.0, 0.0, -sin(angleX), cos(angleX), 0.0,0.0, 0.0, 0.0, 1.0 };
  95.  
  96. glMultMatrixf(matrizT);
  97. glMultMatrixf(matrizR);
  98. glMultMatrixf(matrizX);
  99.  
  100. glColor3f(1.0f, 1.0f, 1.0f);
  101. glutWireTeapot(1.0f);
  102.  
  103. glFlush();
  104.  
  105. }
  106.  
  107. //Control por teclado
  108.  
  109. void myKeys(unsigned char key, int x, int y) {
  110. switch (key)
  111. {
  112. case 'a':
  113. case 'A':
  114. xi += -0.5;
  115.  
  116. break;
  117. case 'w':
  118. case 'W':
  119. yi += 0.5;
  120.  
  121. break;
  122.  
  123. case 's':
  124. case 'S':
  125. yi += -0.5;
  126. break;
  127. case 'D':
  128. case 'd':
  129. xi += 0.5;
  130. break;
  131. case 'f':
  132. case 'F':
  133. angleZ += 5.0;
  134. break;
  135. case 'g':
  136. case 'G':
  137. angleZ += -5.0;
  138. break;
  139.  
  140. case 'y':
  141. case 'Y':
  142. angleX += 5.0;
  143. break;
  144.  
  145. case 'h':
  146. case 'H':
  147. angleX += -5.0;
  148. break;
  149.  
  150. case 'q': exit(0);
  151.  
  152.  
  153. }
  154. glutPostRedisplay();
  155. }
  156.  
  157. void myIdle()
  158. {
  159. // Número de milisegundos que han pasado desde que se inicio el programa
  160. long int currentTime = glutGet(GLUT_ELAPSED_TIME);
  161. // animacion en funcion del tiempo
  162. glutPostRedisplay();
  163. }
  164.  
  165. int main(int argc, char** argv) {
  166.  
  167. //CREACION DEL ENTORNO GRAFICO
  168. glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  169. glutInitWindowSize(screenWidth, screenHeight);
  170. glutInitWindowPosition(30, 30);
  171. glutInit(&argc, argv);
  172. glutCreateWindow("Lab. Informatica Grafica");
  173. //--------------------
  174.  
  175.  
  176. //Damos de alta a las funciones de Callback
  177. glutKeyboardFunc(myKeys);
  178. glutMouseFunc(myMouse);
  179. glutDisplayFunc(myDisplay);
  180. glutReshapeFunc(myReshape);
  181. //glutIdleFunc(myIdle); //comentada por eficiencia
  182. //--------------------
  183.  
  184. //Funciones de limpieza de la pantalla
  185. glClearColor(0.0, 0.0, 0.0, 0.0);
  186. glClear(GL_COLOR_BUFFER_BIT);
  187. //---------------------
  188.  
  189.  
  190. glFlush();
  191. glMatrixMode(GL_PROJECTION);
  192. //izda,dcha,abajo,arriba
  193. glLoadIdentity();
  194. glOrtho(-5.0, 5.0, -5.0, 5.0, -2.5, 2.5); //Vista en 3D
  195. glMatrixMode(GL_MODELVIEW);
  196. //x,y,anchura,altura
  197. glViewport(0, 0, 640, 480);
  198.  
  199.  
  200.  
  201. //Llamada al bucle principal de recoleccion de eventos
  202. glutMainLoop();
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement