Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.54 KB | None | 0 0
  1.  
  2. #include <windows.h>
  3.  
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. //#include <sys/time.h>
  7.  
  8. #include <ctime>
  9.  
  10. #ifdef __APPLE__
  11. #include <GLUT/glut.h>
  12. #else
  13. #include <GL/gl.h>
  14. #include <GL/glut.h>
  15. #endif
  16.  
  17. #include <math.h>
  18. #include <iostream>
  19. #include <fstream>
  20. //#include <vector>
  21.  
  22. using namespace std;
  23.  
  24.  
  25. string FICHERO = "movimiento.txt";
  26.  
  27. struct punto
  28. {
  29.     float x;
  30.     float y;
  31.     float z;
  32. };
  33.  
  34. //Estaticos//
  35. const long int MAX_TAM = 10000;
  36. typedef punto vector[MAX_TAM];
  37. vector PUNTOS;
  38.  
  39. typedef punto vectors[26];
  40.  
  41. //Dinamicos//
  42. //vector<punto> PUNTOS;
  43.  
  44.  
  45.  
  46.  
  47.  
  48. int screenWidth = 640;
  49. int screenHeight = 480;
  50.  
  51. void LeerFicheroPuntos();
  52. void silueta(vectors);
  53. void frame(vectors);
  54. //DEFINICION DE FUNCIONES DE CALLBACK
  55. void LeerFicheroPuntos()
  56. {
  57.     ifstream f;
  58.     int i = 0;
  59.     float prueba = 0;
  60.     //float x, y, z;
  61.     int cont = 0;
  62.     f.open("posiciones.txt");
  63.  
  64.     if (f.is_open())
  65.     {
  66.         while (f >> prueba)
  67.         {
  68.             if (cont < 26)
  69.             {
  70.                 PUNTOS[i].x = prueba;
  71.                 f >> PUNTOS[i].y;
  72.                 f >> PUNTOS[i].z;
  73.                 //cout << PUNTOS[i].x << " " << PUNTOS[i].y << " " << PUNTOS[i].z << endl;
  74.  
  75.                 i++;
  76.                 cont++;
  77.             }
  78.             else
  79.                 cont = 0;
  80.         }  
  81.         glEnd();
  82.     }
  83.     else
  84.         cout << "Error" << endl;
  85.  
  86.     f.close();
  87.  
  88. }
  89.  
  90.  void frame(vectors linea)
  91.  {
  92.  
  93.  
  94.      for (int i = 0; i < 26; i++)
  95.      {
  96.          linea[i].x = PUNTOS[i].x;
  97.          linea[i].y = PUNTOS[i].y;
  98.          linea[i].z = PUNTOS[i].z;
  99.      }
  100.  
  101. }
  102. void silueta(vectors linea)
  103. {
  104.     glColor3f(1.0, 1.0, 1.0);
  105.  
  106.     glBegin(GL_LINES);
  107.         //Pierna izquierda
  108.         glVertex3f(linea[2].x, linea[2].y, linea[2].z);
  109.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  110.  
  111.         glVertex3f(linea[2].x, linea[2].y, linea[2].z);
  112.         glVertex3f(linea[3].x, linea[3].y, linea[3].z);
  113.  
  114.         glVertex3f(linea[3].x, linea[3].y, linea[3].z);
  115.         glVertex3f(linea[4].x, linea[4].y, linea[4].z);
  116.  
  117.         glVertex3f(linea[4].x, linea[4].y, linea[4].z);
  118.         glVertex3f(linea[5].x, linea[5].y, linea[5].z);
  119.  
  120.         glVertex3f(linea[5].x, linea[5].y, linea[5].z);
  121.         glVertex3f(linea[6].x, linea[6].y, linea[6].z);
  122.  
  123.         glVertex3f(linea[6].x, linea[6].y, linea[6].z);
  124.         glVertex3f(linea[7].x, linea[7].y, linea[7].z);
  125.  
  126.  
  127.         //Pierna derecha
  128.         glVertex3f(linea[20].x, linea[20].y, linea[20].z);
  129.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  130.  
  131.         glVertex3f(linea[20].x, linea[20].y, linea[20].z);
  132.         glVertex3f(linea[21].x, linea[21].y, linea[21].z);
  133.  
  134.         glVertex3f(linea[21].x, linea[21].y, linea[21].z);
  135.         glVertex3f(linea[22].x, linea[22].y, linea[22].z);
  136.  
  137.         glVertex3f(linea[22].x, linea[22].y, linea[22].z);
  138.         glVertex3f(linea[23].x, linea[23].y, linea[23].z);
  139.  
  140.         glVertex3f(linea[23].x, linea[23].y, linea[23].z);
  141.         glVertex3f(linea[24].x, linea[24].y, linea[24].z);
  142.  
  143.         glVertex3f(linea[24].x, linea[24].y, linea[24].z);
  144.         glVertex3f(linea[25].x, linea[25].y, linea[25].z);
  145.  
  146.         //Brazo izquierdo
  147.         glVertex3f(linea[0].x, linea[0].y, linea[0].z);
  148.         glVertex3f(linea[1].x, linea[1].y, linea[1].z);
  149.  
  150.         glVertex3f(linea[0].x, linea[0].y, linea[0].z);
  151.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  152.  
  153.  
  154.         //Brazo derecho
  155.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  156.         glVertex3f(linea[18].x, linea[18].y, linea[18].z);
  157.  
  158.         glVertex3f(linea[18].x, linea[18].y, linea[18].z);
  159.         glVertex3f(linea[19].x, linea[19].y, linea[19].z);
  160.  
  161.         //Cabeza
  162.         glVertex3f(linea[8].x, linea[8].y, linea[8].z);
  163.         glVertex3f(linea[9].x, linea[9].y, linea[9].z);
  164.  
  165.         glVertex3f(linea[9].x, linea[9].y, linea[9].z);
  166.         glVertex3f(linea[10].x, linea[10].y, linea[10].z);
  167.  
  168.  
  169.         //Columna
  170.         glVertex3f(linea[9].x, linea[9].y, linea[9].z);
  171.         glVertex3f(linea[11].x, linea[11].y, linea[11].z);
  172.  
  173.         glVertex3f(linea[11].x, linea[11].y, linea[11].z);
  174.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  175.  
  176.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  177.         glVertex3f(linea[13].x, linea[13].y, linea[13].z);
  178.  
  179.         glVertex3f(linea[13].x, linea[13].y, linea[13].z);
  180.         glVertex3f(linea[14].x, linea[14].y, linea[14].z);
  181.  
  182.         glVertex3f(linea[14].x, linea[14].y, linea[14].z);
  183.         glVertex3f(linea[15].x, linea[15].y, linea[15].z);
  184.  
  185.         glVertex3f(linea[15].x, linea[15].y, linea[15].z);
  186.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  187.  
  188.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  189.         glVertex3f(linea[17].x, linea[17].y, linea[17].z);
  190.     glEnd();
  191.  
  192. }
  193.  
  194.  
  195. //Función del callback del ratón
  196. void myMouse(int button, int state, int mx, int  my)
  197. {
  198.     if (state == GLUT_DOWN)
  199.     {
  200.         if (button == GLUT_LEFT_BUTTON)
  201.         {
  202.         }
  203.  
  204.         else if (button == GLUT_RIGHT_BUTTON)
  205.         {
  206.  
  207.         }
  208.         glutPostRedisplay();
  209.     }
  210. }
  211.  
  212.  
  213. //Función de callback de cambio del tamaño de la ventana
  214. void myReshape(int w, int h)
  215. {
  216.     screenWidth = w;
  217.     screenHeight = h;
  218. }
  219.  
  220. //Función de callback del teclado
  221. void myKeys(unsigned char key, int x, int y)
  222. {
  223.     switch (key)
  224.     {
  225.     case 'p':
  226.     case 'P':
  227.        
  228.         break;
  229.  
  230.     case 'o':
  231.     case 'O':
  232.        
  233.         break;
  234.     }
  235.  
  236.     glutPostRedisplay();
  237. }
  238.  
  239. void parallel() {
  240.     float angle = 135 * 3.14 / 180;
  241.  
  242.     //Tercera proyección (3.3.3)
  243.     float N[16] = { 1,              0,                  0,                  0,
  244.                     0,              1,                  0,                  0,
  245.                     0,      -sin(angle) / cos(angle),       0,                  0,
  246.                     0-1 * (sin(angle) / cos(angle)),    -1,                  1 };
  247.  
  248.     glMultMatrixf(N);
  249.  
  250. }
  251.  
  252. void myDisplay(void)
  253. {
  254.     glClearColor(0.0, 0.0, 0.0, 0.0);
  255.     glClear(GL_COLOR_BUFFER_BIT);
  256.  
  257.     glBegin(GL_LINES);
  258.     vectors linea;
  259.     frame(linea);
  260.  
  261.     /*En OpenGl existen dos instrucciones para configurar las ventanas de la transformación:
  262.     void gluOrtho2D(Gldouble left, Gldouble right, Gldouble bottom, Gldouble top);
  263.     Configura la Ventana del Mundo
  264.  
  265.     void glViewport(Glint x, Glint y, Glint width, Glint height);
  266.     Configura el Puerto de Vista
  267.     Estas instrucciones deben ser llamadas en la función de callback que se activa cada vez que se
  268.     modifica el tamaño de la ventana de visualización (conocida generalmente con el nombre de
  269.     MyReshape())
  270.     void myReshape(int w, int h){
  271.      glViewport(0,0,w,h);
  272.      glMatrixMode(GL_PROJECTION);
  273.      glLoadIdentity();
  274.      if(w <=h) //ventana grafica más ancha que alta
  275.      gluOrtho2D(-2.0,2.0,-2.0* (float)h/w, 2.0* (float)h/w,2.0* (float)h/w, 2.0* (float)h/w );
  276.      else
  277.      gluOrtho2D(-2.0* (float)w/h, 2.0* (float)w/h,-2.0,2.0);
  278.      glMatrixMode(GL_MODELVIEW);
  279.     }*/
  280.  
  281.  
  282.     silueta(linea);
  283.  
  284.  
  285.     glEnd();
  286.     glFlush();
  287. }
  288.  
  289. //Funcion de actualizacion automatica
  290. //Es la función que se ejecuta siempre que no ocurran otros eventos
  291.  
  292. void myIdle()
  293. {
  294.     // Número de milisegundos que han pasado desde que se inicio el programa
  295.     long int currentTime = glutGet(GLUT_ELAPSED_TIME);
  296.     glutPostRedisplay();
  297.     Sleep(1);  // Esto si no tienes habilitado el vSync
  298. }
  299.  
  300.  
  301. int main(int argc, char ** argv)
  302. {
  303.     //Inicialización de GLUT
  304.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  305.     glutInitWindowSize(screenWidth, screenHeight);
  306.     glutInitWindowPosition(30, 30);
  307.     glutInit(&argc, argv);
  308.     glutCreateWindow("Uso Basico OpenGl. Lab. Informatica Grafica");
  309.     LeerFicheroPuntos();
  310.     //--------------------
  311.  
  312.     //Damos de alta a las funciones de Callback
  313.     glutKeyboardFunc(myKeys);
  314.     glutMouseFunc(myMouse);
  315.     glutDisplayFunc(myDisplay);
  316.     glutReshapeFunc(myReshape);
  317.     glutIdleFunc(myIdle);
  318.     //--------------------
  319.  
  320.     //Configurar el color de borrado de la pantalla y borrar la pantalla
  321.     glClearColor(0.0, 0.0, 0.0, 0.0);
  322.     glClear(GL_COLOR_BUFFER_BIT);
  323.     //---------------------
  324.  
  325.     glFlush();
  326.  
  327.     //Configuración de la vista
  328.     glMatrixMode(GL_PROJECTION);
  329.     glLoadIdentity();
  330.     glOrtho(-1000.0, 1000.0, 1000.0, -1000.0, 1000, -1000);
  331.     glMatrixMode(GL_MODELVIEW);
  332.  
  333.  
  334.  
  335.     //x,y,anchura,altura
  336.     glViewport(0, 0, screenWidth, screenHeight);
  337.  
  338.     //Llamada al bucle principal de recoleccion de eventos
  339.     glutMainLoop();
  340. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement