Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <stdlib.h>
- #include <stdio.h>
- //#include <sys/time.h>
- #include <ctime>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/gl.h>
- #include <GL/glut.h>
- #endif
- #include <math.h>
- #include <iostream>
- #include <fstream>
- //#include <vector>
- using namespace std;
- string FICHERO = "movimiento.txt";
- struct punto
- {
- float x;
- float y;
- float z;
- };
- //Estaticos//
- const long int MAX_TAM = 2600;
- const long int TAM = 100;
- typedef float VTiempo[TAM];
- VTiempo tiempo;
- typedef punto vector[MAX_TAM];
- vector PUNTOS;
- typedef punto vectors[26];
- const double NEARX = -1.0; //left
- const double NEARY = -1.0; //bottom
- const double NEARZ = 1.0; //near
- const double FARX = 1.0; //right
- const double FARY = 1.0; //top
- const double FARZ = 5.0; //far
- //En coordenadas del mundo
- const double CAMX = 0.0;
- const double CAMY = 0.0;
- const double CAMZ = 2.0;
- bool perspectiva = false;
- bool ortografica = false;
- int origen = 0;
- int screenWidth = 640;
- int screenHeight = 480;
- void LeerFicheroPuntos();
- void silueta(vectors);
- void frame(vectors, int);
- //DEFINICION DE FUNCIONES DE CALLBACK
- void LeerFicheroPuntos()
- {
- ifstream f;
- int i = 0, t = 0;
- float prueba = 0;
- //float x, y, z;
- int cont = 0;
- f.open("posiciones.txt");
- if (f.is_open())
- {
- while (f >> prueba)
- {
- if (cont < 26)
- {
- PUNTOS[i].x = prueba;
- f >> PUNTOS[i].y;
- f >> PUNTOS[i].z;
- //cout << PUNTOS[i].x << " " << PUNTOS[i].y << " " << PUNTOS[i].z << endl;
- i++;
- cont++;
- }
- else
- {
- cont = 0;
- tiempo[t] = prueba;
- //cout << t << " " << tiempo[t] << endl;
- t++;
- }
- }
- glEnd();
- }
- else
- cout << "Error" << endl;
- f.close();
- }
- void frame(vectors linea, int origen)
- {
- for (int i = 0; i < 26; i++)
- {
- linea[i].x = PUNTOS[i + origen].x;
- linea[i].y = PUNTOS[i + origen].y;
- linea[i].z = PUNTOS[i + origen].z;
- }
- }
- void m_ortho()
- {
- float N[16] = { 2 / (FARX - NEARX), 0, 0, 0,
- 0, 2 / (FARY - NEARY), 0, 0,
- 0, 0, -2 / (FARZ - NEARZ), 0,
- -(FARX + NEARX) / (FARX - NEARX),
- -(FARY + NEARY) / (FARY - NEARY),
- -(FARZ + NEARZ) / (FARZ - NEARZ),
- 1 };
- glMultMatrixf(N);
- }
- void silueta(vectors linea)
- {
- glColor3f(1.0, 1.0, 1.0);
- glBegin(GL_LINES);
- //Pierna izquierda
- glVertex3f(linea[2].x, linea[2].y, linea[2].z);
- glVertex3f(linea[16].x, linea[16].y, linea[16].z);
- glVertex3f(linea[2].x, linea[2].y, linea[2].z);
- glVertex3f(linea[3].x, linea[3].y, linea[3].z);
- glVertex3f(linea[3].x, linea[3].y, linea[3].z);
- glVertex3f(linea[4].x, linea[4].y, linea[4].z);
- glVertex3f(linea[4].x, linea[4].y, linea[4].z);
- glVertex3f(linea[5].x, linea[5].y, linea[5].z);
- glVertex3f(linea[5].x, linea[5].y, linea[5].z);
- glVertex3f(linea[6].x, linea[6].y, linea[6].z);
- glVertex3f(linea[6].x, linea[6].y, linea[6].z);
- glVertex3f(linea[7].x, linea[7].y, linea[7].z);
- //Pierna derecha
- glVertex3f(linea[20].x, linea[20].y, linea[20].z);
- glVertex3f(linea[16].x, linea[16].y, linea[16].z);
- glVertex3f(linea[20].x, linea[20].y, linea[20].z);
- glVertex3f(linea[21].x, linea[21].y, linea[21].z);
- glVertex3f(linea[21].x, linea[21].y, linea[21].z);
- glVertex3f(linea[22].x, linea[22].y, linea[22].z);
- glVertex3f(linea[22].x, linea[22].y, linea[22].z);
- glVertex3f(linea[23].x, linea[23].y, linea[23].z);
- glVertex3f(linea[23].x, linea[23].y, linea[23].z);
- glVertex3f(linea[24].x, linea[24].y, linea[24].z);
- glVertex3f(linea[24].x, linea[24].y, linea[24].z);
- glVertex3f(linea[25].x, linea[25].y, linea[25].z);
- //Brazo izquierdo
- glVertex3f(linea[0].x, linea[0].y, linea[0].z);
- glVertex3f(linea[1].x, linea[1].y, linea[1].z);
- glVertex3f(linea[0].x, linea[0].y, linea[0].z);
- glVertex3f(linea[12].x, linea[12].y, linea[12].z);
- //Brazo derecho
- glVertex3f(linea[12].x, linea[12].y, linea[12].z);
- glVertex3f(linea[18].x, linea[18].y, linea[18].z);
- glVertex3f(linea[18].x, linea[18].y, linea[18].z);
- glVertex3f(linea[19].x, linea[19].y, linea[19].z);
- //Cabeza
- glVertex3f(linea[8].x, linea[8].y, linea[8].z);
- glVertex3f(linea[9].x, linea[9].y, linea[9].z);
- glVertex3f(linea[9].x, linea[9].y, linea[9].z);
- glVertex3f(linea[10].x, linea[10].y, linea[10].z);
- //Columna
- glVertex3f(linea[9].x, linea[9].y, linea[9].z);
- glVertex3f(linea[11].x, linea[11].y, linea[11].z);
- glVertex3f(linea[11].x, linea[11].y, linea[11].z);
- glVertex3f(linea[12].x, linea[12].y, linea[12].z);
- glVertex3f(linea[12].x, linea[12].y, linea[12].z);
- glVertex3f(linea[13].x, linea[13].y, linea[13].z);
- glVertex3f(linea[13].x, linea[13].y, linea[13].z);
- glVertex3f(linea[14].x, linea[14].y, linea[14].z);
- glVertex3f(linea[14].x, linea[14].y, linea[14].z);
- glVertex3f(linea[15].x, linea[15].y, linea[15].z);
- glVertex3f(linea[15].x, linea[15].y, linea[15].z);
- glVertex3f(linea[16].x, linea[16].y, linea[16].z);
- glVertex3f(linea[16].x, linea[16].y, linea[16].z);
- glVertex3f(linea[17].x, linea[17].y, linea[17].z);
- glEnd();
- }
- //Función del callback del ratón
- void myMouse(int button, int state, int mx, int my)
- {
- if (state == GLUT_DOWN)
- {
- if (button == GLUT_LEFT_BUTTON)
- {
- }
- else if (button == GLUT_RIGHT_BUTTON)
- {
- }
- glutPostRedisplay();
- }
- }
- //Función de callback de cambio del tamaño de la ventana
- void myReshape(int w, int h)
- {
- screenWidth = w;
- screenHeight = h;
- }
- //Función de callback del teclado
- void myKeys(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 'p':
- case 'P':
- if (perspectiva == true)
- perspectiva = false;
- else
- perspectiva = true;
- glutPostRedisplay();
- break;
- case 'o':
- case 'O':
- if (ortografica == true)
- ortografica = false;
- else
- ortografica = true;
- glutPostRedisplay();
- break;
- }
- glutPostRedisplay();
- }
- void parallel() {
- float angle = 135 * 3.14 / 180;
- //Tercera proyección (3.3.3)
- float N[16] = { 1, 0, 0, 0,
- 0, 1, 0, 0,
- 0, -sin(angle) / cos(angle), 0, 0,
- 0, -1 * (sin(angle) / cos(angle)), -1, 1 };
- glMultMatrixf(N);
- }
- void myDisplay(void)
- {
- /*glClearColor(0.0, 0.0, 0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- vectors linea;
- origen += 25;
- frame(linea, origen);
- glPushMatrix();
- glLoadIdentity();
- glViewport(0, 0, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- //Abajo izquierda
- /*glPushMatrix();
- glLoadIdentity();
- glViewport(0, 0, screenWidth / 2, screenHeight / 2);
- gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- //Arriba derecha
- glPushMatrix();
- glViewport(screenWidth / 2, screenHeight / 2, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- //Abajo derecha
- glPushMatrix();
- glViewport(screenWidth / 2, 0, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(-100.0, 0.0, 00.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- //Arriba izquierda
- glPushMatrix();
- glViewport(0, screenHeight / 2, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();*/
- glFlush();
- }
- //Funcion de actualizacion automatica
- //Es la función que se ejecuta siempre que no ocurran otros eventos
- void myIdle()
- {
- // Número de milisegundos que han pasado desde que se inicio el programa
- long int currentTime = glutGet(GLUT_ELAPSED_TIME);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- int i = 0;
- vectors linea;
- frame(linea, origen);
- origen += 26;
- glPushMatrix();
- glLoadIdentity();
- glViewport(0, 0, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- //Arriba derecha
- glPushMatrix();
- glViewport(screenWidth / 2, screenHeight / 2, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- //Abajo derecha
- glPushMatrix();
- glViewport(screenWidth / 2, 0, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(-100.0, 0.0, 00.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- //Arriba izquierda
- glPushMatrix();
- glViewport(0, screenHeight / 2, screenWidth / 2, screenHeight / 2);
- if (perspectiva == false)
- gluLookAt(100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- else
- gluLookAt(100, 100, 100, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- silueta(linea);
- glPopMatrix();
- glutPostRedisplay();
- Sleep(tiempo[i] / 10); // Esto si no tienes habilitado el vSync
- //Para que el personaje vuelva al punto inicial y se mueva infinitamente
- if (origen <= MAX_TAM- 1)
- {
- i++;
- }
- else
- {
- origen = 0;
- i = 0;
- }
- }
- int main(int argc, char ** argv)
- {
- //Inicialización de GLUT
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
- glutInitWindowSize(screenWidth, screenHeight);
- glutInitWindowPosition(30, 30);
- glutInit(&argc, argv);
- glutCreateWindow("Uso Basico OpenGl. Lab. Informatica Grafica");
- LeerFicheroPuntos();
- //--------------------
- //Damos de alta a las funciones de Callback
- glutKeyboardFunc(myKeys);
- glutMouseFunc(myMouse);
- glutDisplayFunc(myDisplay);
- glutReshapeFunc(myReshape);
- glutIdleFunc(myIdle);
- //--------------------
- //Configurar el color de borrado de la pantalla y borrar la pantalla
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT);
- //---------------------
- glFlush();
- //Configuración de la vista
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-1000.0, 1000.0, 1000.0, -1000.0, 1000, -1000);
- glMatrixMode(GL_MODELVIEW);
- //x,y,anchura,altura
- glViewport(0, 0, screenWidth, screenHeight);
- //Llamada al bucle principal de recoleccion de eventos
- glutMainLoop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement