Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.66 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 = 2600;
  36. const long int TAM = 100;
  37.  
  38. typedef float VTiempo[TAM];
  39. VTiempo tiempo;
  40.  
  41. typedef punto vector[MAX_TAM];
  42. vector PUNTOS;
  43. typedef punto vectors[26];
  44.  
  45. const double NEARX = -1.0;  //left
  46. const double NEARY = -1.0;  //bottom
  47. const double NEARZ = 1.0;   //near
  48.  
  49. const double FARX = 1.0;    //right
  50. const double FARY = 1.0;    //top  
  51. const double FARZ = 5.0;    //far
  52.  
  53. //En coordenadas del mundo
  54. const double CAMX = 0.0;
  55. const double CAMY = 0.0;
  56. const double CAMZ = 2.0;
  57.  
  58. bool perspectiva = false;
  59. bool ortografica = false;
  60.  
  61. int origen = 0;
  62.  
  63. int screenWidth = 640;
  64. int screenHeight = 480;
  65.  
  66. void LeerFicheroPuntos();
  67. void silueta(vectors);
  68. void frame(vectors, int);
  69.  
  70. //DEFINICION DE FUNCIONES DE CALLBACK
  71. void LeerFicheroPuntos()
  72. {
  73.     ifstream f;
  74.     int i = 0, t = 0;
  75.     float prueba = 0;
  76.     //float x, y, z;
  77.     int cont = 0;
  78.     f.open("posiciones.txt");
  79.  
  80.     if (f.is_open())
  81.     {
  82.         while (f >> prueba)
  83.         {
  84.             if (cont < 26)
  85.             {
  86.                 PUNTOS[i].x = prueba;
  87.                 f >> PUNTOS[i].y;
  88.                 f >> PUNTOS[i].z;
  89.                 //cout << PUNTOS[i].x << " " << PUNTOS[i].y << " " << PUNTOS[i].z << endl;
  90.  
  91.                 i++;
  92.                 cont++;
  93.             }
  94.             else
  95.             {
  96.                
  97.                 cont = 0;
  98.                 tiempo[t] = prueba;
  99.                 //cout << t << "   " << tiempo[t] << endl;
  100.                 t++;
  101.             }
  102.  
  103.         }  
  104.         glEnd();
  105.     }
  106.     else
  107.         cout << "Error" << endl;
  108.  
  109.     f.close();
  110.  
  111. }
  112.  
  113.  void frame(vectors linea, int origen)
  114.  {
  115.  
  116.      for (int i = 0; i < 26; i++)
  117.      {
  118.          linea[i].x = PUNTOS[i + origen].x;
  119.          linea[i].y = PUNTOS[i + origen].y;
  120.          linea[i].z = PUNTOS[i + origen].z;
  121.      }
  122.  
  123. }
  124.  
  125.  
  126.  
  127.  void m_ortho()
  128.  {
  129.      float N[16] = { 2 / (FARX - NEARX),            0,                      0,              0,
  130.                          0,                2 / (FARY - NEARY),              0,              0,
  131.                          0,                         0,              -2 / (FARZ - NEARZ),        0,
  132.                      -(FARX + NEARX) / (FARX - NEARX),
  133.                      -(FARY + NEARY) / (FARY - NEARY),
  134.                      -(FARZ + NEARZ) / (FARZ - NEARZ),
  135.                                      1 };
  136.  
  137.      glMultMatrixf(N);
  138.  
  139.  }
  140. void silueta(vectors linea)
  141. {
  142.     glColor3f(1.0, 1.0, 1.0);
  143.  
  144.     glBegin(GL_LINES);
  145.         //Pierna izquierda
  146.         glVertex3f(linea[2].x, linea[2].y, linea[2].z);
  147.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  148.  
  149.         glVertex3f(linea[2].x, linea[2].y, linea[2].z);
  150.         glVertex3f(linea[3].x, linea[3].y, linea[3].z);
  151.  
  152.         glVertex3f(linea[3].x, linea[3].y, linea[3].z);
  153.         glVertex3f(linea[4].x, linea[4].y, linea[4].z);
  154.  
  155.         glVertex3f(linea[4].x, linea[4].y, linea[4].z);
  156.         glVertex3f(linea[5].x, linea[5].y, linea[5].z);
  157.  
  158.         glVertex3f(linea[5].x, linea[5].y, linea[5].z);
  159.         glVertex3f(linea[6].x, linea[6].y, linea[6].z);
  160.  
  161.         glVertex3f(linea[6].x, linea[6].y, linea[6].z);
  162.         glVertex3f(linea[7].x, linea[7].y, linea[7].z);
  163.  
  164.  
  165.         //Pierna derecha
  166.         glVertex3f(linea[20].x, linea[20].y, linea[20].z);
  167.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  168.  
  169.         glVertex3f(linea[20].x, linea[20].y, linea[20].z);
  170.         glVertex3f(linea[21].x, linea[21].y, linea[21].z);
  171.  
  172.         glVertex3f(linea[21].x, linea[21].y, linea[21].z);
  173.         glVertex3f(linea[22].x, linea[22].y, linea[22].z);
  174.  
  175.         glVertex3f(linea[22].x, linea[22].y, linea[22].z);
  176.         glVertex3f(linea[23].x, linea[23].y, linea[23].z);
  177.  
  178.         glVertex3f(linea[23].x, linea[23].y, linea[23].z);
  179.         glVertex3f(linea[24].x, linea[24].y, linea[24].z);
  180.  
  181.         glVertex3f(linea[24].x, linea[24].y, linea[24].z);
  182.         glVertex3f(linea[25].x, linea[25].y, linea[25].z);
  183.  
  184.         //Brazo izquierdo
  185.         glVertex3f(linea[0].x, linea[0].y, linea[0].z);
  186.         glVertex3f(linea[1].x, linea[1].y, linea[1].z);
  187.  
  188.         glVertex3f(linea[0].x, linea[0].y, linea[0].z);
  189.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  190.  
  191.  
  192.         //Brazo derecho
  193.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  194.         glVertex3f(linea[18].x, linea[18].y, linea[18].z);
  195.  
  196.         glVertex3f(linea[18].x, linea[18].y, linea[18].z);
  197.         glVertex3f(linea[19].x, linea[19].y, linea[19].z);
  198.  
  199.         //Cabeza
  200.         glVertex3f(linea[8].x, linea[8].y, linea[8].z);
  201.         glVertex3f(linea[9].x, linea[9].y, linea[9].z);
  202.  
  203.         glVertex3f(linea[9].x, linea[9].y, linea[9].z);
  204.         glVertex3f(linea[10].x, linea[10].y, linea[10].z);
  205.  
  206.  
  207.         //Columna
  208.         glVertex3f(linea[9].x, linea[9].y, linea[9].z);
  209.         glVertex3f(linea[11].x, linea[11].y, linea[11].z);
  210.  
  211.         glVertex3f(linea[11].x, linea[11].y, linea[11].z);
  212.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  213.  
  214.         glVertex3f(linea[12].x, linea[12].y, linea[12].z);
  215.         glVertex3f(linea[13].x, linea[13].y, linea[13].z);
  216.  
  217.         glVertex3f(linea[13].x, linea[13].y, linea[13].z);
  218.         glVertex3f(linea[14].x, linea[14].y, linea[14].z);
  219.  
  220.         glVertex3f(linea[14].x, linea[14].y, linea[14].z);
  221.         glVertex3f(linea[15].x, linea[15].y, linea[15].z);
  222.  
  223.         glVertex3f(linea[15].x, linea[15].y, linea[15].z);
  224.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  225.  
  226.         glVertex3f(linea[16].x, linea[16].y, linea[16].z);
  227.         glVertex3f(linea[17].x, linea[17].y, linea[17].z);
  228.     glEnd();
  229.  
  230. }
  231.  
  232.  
  233. //Función del callback del ratón
  234. void myMouse(int button, int state, int mx, int  my)
  235. {
  236.     if (state == GLUT_DOWN)
  237.     {
  238.         if (button == GLUT_LEFT_BUTTON)
  239.         {
  240.         }
  241.  
  242.         else if (button == GLUT_RIGHT_BUTTON)
  243.         {
  244.  
  245.         }
  246.         glutPostRedisplay();
  247.     }
  248. }
  249. //Función de callback de cambio del tamaño de la ventana
  250. void myReshape(int w, int h)
  251. {
  252.     screenWidth = w;
  253.     screenHeight = h;
  254. }
  255.  
  256. //Función de callback del teclado
  257. void myKeys(unsigned char key, int x, int y)
  258. {
  259.     switch (key)
  260.     {
  261.     case 'p':
  262.     case 'P':
  263.         if (perspectiva == true)
  264.             perspectiva = false;
  265.         else
  266.             perspectiva = true;
  267.        
  268.         glutPostRedisplay();
  269.         break;
  270.  
  271.     case 'o':
  272.     case 'O':
  273.         if (ortografica == true)
  274.             ortografica = false;
  275.         else
  276.             ortografica = true;
  277.         glutPostRedisplay();
  278.        
  279.  
  280.         break;
  281.     }
  282.  
  283.     glutPostRedisplay();
  284. }
  285.  
  286. void parallel() {
  287.     float angle = 135 * 3.14 / 180;
  288.  
  289.     //Tercera proyección (3.3.3)
  290.     float N[16] = { 1,              0,                  0,                  0,
  291.                     0,              1,                  0,                  0,
  292.                     0,      -sin(angle) / cos(angle),       0,                  0,
  293.                     0-1 * (sin(angle) / cos(angle)),    -1,                  1 };
  294.  
  295.     glMultMatrixf(N);
  296.  
  297. }
  298.  
  299. void myDisplay(void)
  300. {
  301.     /*glClearColor(0.0, 0.0, 0.0, 0.0);
  302.     glClear(GL_COLOR_BUFFER_BIT);
  303.  
  304.     vectors linea;
  305.  
  306.     origen += 25;
  307.     frame(linea, origen);
  308.  
  309.     glPushMatrix();
  310.     glLoadIdentity();
  311.     glViewport(0, 0, screenWidth / 2, screenHeight / 2);
  312.     if (perspectiva == false)
  313.         gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  314.     else
  315.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  316.     silueta(linea);
  317.     glPopMatrix();
  318.  
  319.     //Abajo izquierda
  320.     /*glPushMatrix();
  321.     glLoadIdentity();
  322.     glViewport(0, 0, screenWidth / 2, screenHeight / 2);
  323.     gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  324.     silueta(linea);
  325.     glPopMatrix();
  326.  
  327.  
  328.     //Arriba derecha
  329.     glPushMatrix();
  330.     glViewport(screenWidth / 2, screenHeight / 2, screenWidth / 2, screenHeight / 2);
  331.     if (perspectiva == false)
  332.         gluLookAt(0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  333.     else
  334.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  335.     silueta(linea);
  336.     glPopMatrix();
  337.  
  338.     //Abajo derecha
  339.     glPushMatrix();
  340.     glViewport(screenWidth / 2, 0, screenWidth / 2, screenHeight / 2);
  341.     if (perspectiva == false)
  342.         gluLookAt(-100.0, 0.0, 00.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  343.     else
  344.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  345.     silueta(linea);
  346.     glPopMatrix();
  347.  
  348.     //Arriba izquierda
  349.     glPushMatrix();
  350.     glViewport(0, screenHeight / 2, screenWidth / 2, screenHeight / 2);
  351.     if (perspectiva == false)
  352.         gluLookAt(100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  353.     else
  354.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  355.     silueta(linea);
  356.     glPopMatrix();*/
  357.  
  358.     glFlush();
  359. }
  360.  
  361.  
  362. //Funcion de actualizacion automatica
  363. //Es la función que se ejecuta siempre que no ocurran otros eventos
  364.  
  365. void myIdle()
  366. {
  367.     // Número de milisegundos que han pasado desde que se inicio el programa
  368.     long int currentTime = glutGet(GLUT_ELAPSED_TIME);
  369.     glClearColor(0.0, 0.0, 0.0, 0.0);
  370.     glClear(GL_COLOR_BUFFER_BIT);
  371.  
  372.     int i = 0;
  373.     vectors linea;
  374.  
  375.    
  376.     frame(linea, origen);
  377.     origen += 26;
  378.  
  379.  
  380.     glPushMatrix();
  381.     glLoadIdentity();
  382.     glViewport(0, 0, screenWidth / 2, screenHeight / 2);
  383.     if (perspectiva == false)
  384.         gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  385.     else
  386.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  387.     silueta(linea);
  388.     glPopMatrix();
  389.  
  390.     //Arriba derecha
  391.     glPushMatrix();
  392.     glViewport(screenWidth / 2, screenHeight / 2, screenWidth / 2, screenHeight / 2);
  393.     if (perspectiva == false)
  394.         gluLookAt(0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
  395.     else
  396.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  397.     silueta(linea);
  398.     glPopMatrix();
  399.  
  400.     //Abajo derecha
  401.     glPushMatrix();
  402.     glViewport(screenWidth / 2, 0, screenWidth / 2, screenHeight / 2);
  403.     if (perspectiva == false)
  404.         gluLookAt(-100.0, 0.0, 00.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  405.     else
  406.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  407.     silueta(linea);
  408.     glPopMatrix();
  409.  
  410.     //Arriba izquierda
  411.     glPushMatrix();
  412.     glViewport(0, screenHeight / 2, screenWidth / 2, screenHeight / 2);
  413.     if (perspectiva == false)
  414.         gluLookAt(100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  415.     else
  416.         gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  417.     silueta(linea);
  418.     glPopMatrix();
  419.  
  420.  
  421.     glutPostRedisplay();
  422.     Sleep(tiempo[i] / 10);  // Esto si no tienes habilitado el vSync
  423.  
  424.  
  425.  
  426.     //Para que el personaje vuelva al punto inicial y se mueva infinitamente
  427.     if (origen <= MAX_TAM- 1)
  428.     {
  429.         i++;
  430.     }
  431.     else
  432.     {
  433.         origen = 0;
  434.         i = 0;
  435.     }
  436.        
  437. }
  438.  
  439.  
  440. int main(int argc, char ** argv)
  441. {
  442.     //Inicialización de GLUT
  443.     glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  444.     glutInitWindowSize(screenWidth, screenHeight);
  445.     glutInitWindowPosition(30, 30);
  446.     glutInit(&argc, argv);
  447.     glutCreateWindow("Uso Basico OpenGl. Lab. Informatica Grafica");
  448.     LeerFicheroPuntos();
  449.     //--------------------
  450.  
  451.     //Damos de alta a las funciones de Callback
  452.     glutKeyboardFunc(myKeys);
  453.     glutMouseFunc(myMouse);
  454.     glutDisplayFunc(myDisplay);
  455.     glutReshapeFunc(myReshape);
  456.     glutIdleFunc(myIdle);
  457.     //--------------------
  458.  
  459.     //Configurar el color de borrado de la pantalla y borrar la pantalla
  460.     glClearColor(0.0, 0.0, 0.0, 0.0);
  461.     glClear(GL_COLOR_BUFFER_BIT);
  462.     //---------------------
  463.  
  464.     glFlush();
  465.  
  466.     //Configuración de la vista
  467.     glMatrixMode(GL_PROJECTION);
  468.     glLoadIdentity();
  469.     glOrtho(-1000.0, 1000.0, 1000.0, -1000.0, 1000, -1000);
  470.     glMatrixMode(GL_MODELVIEW);
  471.  
  472.  
  473.  
  474.     //x,y,anchura,altura
  475.     glViewport(0, 0, screenWidth, screenHeight);
  476.  
  477.     //Llamada al bucle principal de recoleccion de eventos
  478.     glutMainLoop();
  479. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement