domingo, 15 de abril de 2007

Previo Práctica 7 Laboratorio

Este es el codigo del previo de la practica 7, el despliegue del humanoide se muestra con la instruccion humanoide.

//Fractal de alguien cuyo nombre no se escribir --> Sarpinski
#include GL/glut.h
#include stdlib.h
#include stdio.h
#include time.h

static int valor_prueba = 1;
static float px1=-2.5;
static float py1=-2.165;
static float px2=2.5;
static float py2=-2.165;
static float px3=0;
static float py3=2.165;
static float px = 0;
static float py = 0;
static float pz = 0;
static int ventana2, ventana1, ventana3;
static float movX = 0;
static float movY = 0;
void humanoide(void);

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
int i= 0;
srand(time(NULL));
glClear (GL_COLOR_BUFFER_BIT);
px = rand()%20;
py = rand()%20;
pz = rand()%20;
pz = pz/10;
px = px/10;
py = py/10;
glColor3f (0.7, 0.7, 0.7);
glLoadIdentity ();
gluLookAt (0.0,0.0,5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

//Despliegue de los puntos iniciales de los triangulos
//Al desplegar los puntos se cambia de color haciendo
//un efecto de coloreado
glBegin(GL_POINTS);
glVertex2f(px1,py1);
glEnd();
glColor3f (pz, px, py);
glBegin(GL_POINTS);
glVertex2f(px2,py2);
glEnd();
glBegin(GL_POINTS);
glVertex2f(px3,py3);
glEnd();
glColor3f (px, py, pz);
glBegin(GL_POINTS);
glVertex2f(px,py);

glEnd();
//calculo de los puntos medios
do
{
valor_prueba = rand()%3;
if(valor_prueba == 0)
{
px = (px1 + px)/2;
py = (py1 + py)/2;
}
if(valor_prueba == 1)
{
px = (px2 + px)/2;
py = (py2 + py)/2;
}
if(valor_prueba == 2)
{
px = (px3 + px)/2;
py = (py3 + py)/2;
}
glBegin(GL_POINTS);
glVertex2f(px,py);
glEnd();
i++;
}while (i < 77777);
glFlush ();
}

//Esta funcion es igual a display pero se hace un "zoom" al fractal, que es un escalamiento
void display2(void)
{
int i= 0;
srand(time(NULL));
glClear (GL_COLOR_BUFFER_BIT);
px = rand()%20;
py = rand()%20;
pz = rand()%20;
pz = pz/10;
px = px/10;
py = py/10;
glColor3f (0.7, 0.7, 0.7);
glLoadIdentity ();
gluLookAt (0.0,0.0,5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

//Despliegue de los puntos iniciales de los triangulos
//Al desplegar los puntos se cambia de color haciendo
//un efecto de coloreado
glBegin(GL_POINTS);
glVertex2f(px1,py1);
glEnd();
glColor3f (pz, px, py);
glScalef(4,4,1);
glBegin(GL_POINTS);
glVertex2f(px2,py2);
glEnd();
glBegin(GL_POINTS);
glVertex2f(px3,py3);
glEnd();
glColor3f (px, py, pz);
glBegin(GL_POINTS);
glVertex2f(px,py);

glEnd();
//calculo de los puntos medios
do
{
valor_prueba = rand()%3;
if(valor_prueba == 0)
{
px = (px1 + px)/2;
py = (py1 + py)/2;
}
if(valor_prueba == 1)
{
px = (px2 + px)/2;
py = (py2 + py)/2;
}
if(valor_prueba == 2)
{
px = (px3 + px)/2;
py = (py3 + py)/2;
}
glBegin(GL_POINTS);
glVertex2f(px,py);
glEnd();
i++;
}while (i < 77777);
glFlush ();
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}
void reshape2 (int w, int h)
{
glViewport (movX, movY, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glFrustum (-1.0, 1.0, -1.0, 1.0, 1.5, 20.0);
glMatrixMode (GL_MODELVIEW);
}

//Funcion del teclado, al apretar ESC se cierra el programa
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case 'q':
glutPostRedisplay();
break;
}
}

//Funcion mouse para mandar a imprimir la coordenada
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{ //Despliegue para la ventana del punto del fractal
if ((button == GLUT_LEFT_BUTTON) && (glutGetWindow() == ventana1))
{
printf("Hiciste click en la posicion: %d, %d \n", x,y);
movX = 400 - x;
movY = y - 300;

glutInitWindowSize (800,600);
glutInitWindowPosition (400, 100);
ventana2 = glutCreateWindow ("Centrado del Fractal");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape2);
glutSetWindow(ventana2);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);

}
//Condicion para el cierre de la ventana con el click derecho
else if ((glutGetWindow() == ventana2) && (button == GLUT_RIGHT_BUTTON))
{
glutDestroyWindow(ventana2);
glutSetWindow(ventana1);
printf("Se ha cerrado la ventana nueva\n");
}
//Despliegue para la ventana del humanoide
else if ((button == GLUT_RIGHT_BUTTON) && (glutGetWindow() == ventana1))
{
printf("Hiciste click en la posicion: %d, %d \n", x,y);
glutInitWindowSize (300,300);
glutInitWindowPosition (200, 100);
ventana3 = glutCreateWindow ("Animacion del Humanoide");
//Aqui se tiene que desplegar al humanoide bailando
init();
humanoide();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSetWindow(ventana3);
glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);


}
//Condicion para cerrar la ventana con el click derecho
else if ((glutGetWindow() == ventana3) && (button == GLUT_RIGHT_BUTTON))
{
glutDestroyWindow(ventana3);
glutSetWindow(ventana1);
printf("Se ha cerrado la ventana nueva\n");
}
}
}
int main(int argc, char** argv)
{
//Configuracion de la primera ventana
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (800, 600);
glutInitWindowPosition (50, 50);
ventana1 = glutCreateWindow ("Click Izquierdo: Da coordenada y nueva ventana - Click Derecho: Ventana con humanoide - ESC: Cierra");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

No hay comentarios.: