martes, 17 de abril de 2007

Previo - Práctica 7

A continuación se presenta el código el cual despliega, en la ventana principal, una fractal de tipo Sierpinski: con un clic izquierdo, imprime las coordenadas en las que está el Mouse y abre una ventana la cual centra dichas coordenadas y redibuja la fractal a partir de ellas; un clic derecho sobre dicha ventana la cierra. Un clic derecho sobre la ventana principal abre otra en la cual aparece el humanoide previamente hecho animado y un clic derecho sobre dicha ventana la cierra. Oprimiendo la tecla ESC, en cualquier ventana, termina el programa.

#include
#include
#include
#define RAND_MAX 10.0

static int xmouse = 0;
static int ymouse = 0;
static float x = 0.0;
static float y = 0.0;
static float xa = -2.5;
static float ya = -2.165;
static float xb = 2.5;
static float yb = -2.165;
static float xc = 0.0;
static float yc = 2.165;
static float alea = 0.0;
static int cont = 0;
static int win1, win2, win3;
static int bandvent2 = 0;
static int bandvent3 = 0;
static int tiemp;
static int tiemp2 = 0;
static int angbrazder = 0; /*Variable que indica el ángulo de rotación del brazo derecho*/
static int codderrot = 0; /*Variable que indica sobre cual eje rotará el brazo derecho.*/
static int angcododer = 0; /*Variable que indica el ángulo de rotación del brazo derecho*/
static int angbrazizq = 0; /*Variable que indica el ángulo de rotación del brazo izquierdo*/
static int angcodoizq = 0;
static int angpierder = 0; /*Variable que indica el ángulo de rotación de la pierna derecha.*/
static int angpierizq = 0; /*Variable que indica el ángulo de rotación de la pierna izquierda.*/
static int torsoroty = 0; /*Variable que indica el ángulo de rotación del torso en y.*/
static int torsorotx = 0; /*Variable que indica el ángulo de rotación del torso en x.*/
static int cuellorot = 0; /*Variable que indica el ángulo de rotación de la cabeza.*/
static int munecax = 0;
static int munecay = 0;
static int munecaz = 0;
static int pulgfalmay = 0;
static int pulgfalmedx = 0;
static int pulgfalmedy = 0;
static int pulgfalmen = 0;
static int menfalmay = 0;
static int anufalmay = 0;
static int medfalmay = 0;
static int indfalmay = 0;

void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}



void fractal(void)
{
x = rand()%20;
y = rand()%20;
x = x/20;
y = y/20;
glBegin(GL_POINTS);
glVertex2f (xa, ya);
glVertex2f (xb, yb);
glVertex2f (xc, yc);
glVertex2f (x, y);
glEnd();
for (cont = 0; cont <500000 ; cont ++)
{
alea = rand()%3;
if (alea == 0) {
x = (xa+x)/2;
y = (ya+y)/2;
}
else if (alea == 1) {
x = (xb+x)/2;
y = (yb+y)/2;
}
else {
x = (xc+x)/2;
y = (yc+y)/2;
}
glBegin(GL_POINTS);
glVertex2f (x, y);
glEnd();
}
}

void display(void)
{
srand(time(NULL));
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
fractal();
glFlush();
}
void cabeza (){ /* Función que despliega la cabeza. */
glPushMatrix();
glTranslatef(0.0,2.0,0.0);
glRotatef((GLfloat)cuellorot,0.0,0.1,0.0);
glutWireCube(1.0);
glPopMatrix();
}

void tronco (float x, float y, float z, int ejerot, float torsorot){ /*Función que despliega el torso.*/
glTranslatef(x,y,z);
switch (ejerot){
case 0:
glRotatef(torsorot,1.0,0.0,0.0);
break;
case 1:
glRotatef(torsorot,0.0,1.0,0.0);
break;
case 2:
glRotatef(torsorot,0.0,0.0,1.0);
break;
default:
break;
}
glRotatef((GLfloat)torsorotx,1.0,0.0,0.0);
glRotatef((GLfloat)torsoroty,0.0,1.0,0.0);
glPushMatrix();
glScalef (2.0,3.0,1.0);
glutWireCube(1.0);
glPopMatrix();
}

void pulgar (){ /* Función que despliega el pulgar. */
glPushMatrix();
/*Falange mayor*/
glTranslatef(-0.14,-0.6,-0.02);
glTranslatef(0.0,0.4,0.0);
glRotatef((GLfloat)pulgfalmay,-1.0,0.0,0.0);
glPushMatrix();
glTranslatef(0.0,-0.47,0.02);
glScalef(0.25,0.35,0.25);
glutWireCube(1.0);
glPopMatrix();
/*Falange media*/
glTranslatef(0.0,-0.57,-0.385);
glTranslatef(-0.12,0.1,0.37);
glRotatef((GLfloat)pulgfalmedx,-1.0,0.0,0.0);
glRotatef((GLfloat)pulgfalmedy,0.0,0.0,1.0);
glPushMatrix();
glTranslatef(0.12,-0.3,0.03);
glScalef(0.25,0.25,0.25);
glutWireCube(1.0);
glPopMatrix();
/*Falange menor*/
glTranslatef(0.12,-0.5,-0.17);
glTranslatef(0.38,0.15,0.25);
glRotatef((GLfloat)pulgfalmen,-1.0,0.0,0.0);
glPushMatrix();
glTranslatef(-0.38,-0.14,-0.05);
glScalef(0.25,-0.12,0.25);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix(); /*Dedo pulgar completo*/
}

void palma (float x, float y, float z){ /*Función que despliega la palma.*/
glTranslatef(x,y,z);
glTranslatef(-0.25,0.0,0.0);
glRotatef((GLfloat)munecax,1.0,0.0,0.0);
glRotatef((GLfloat)munecay,0.0,1.0,0.0);
glRotatef((GLfloat)munecaz,0.0,0.0,1.0);
glPushMatrix();
glTranslatef(0.25,0.0,0.0);
glScalef (1.0,1.0,0.5);
glutWireCube(1.0);
glPopMatrix(); /*Palma completa*/
}

void dedos (){ /*Función que despliega los dedos índice, medio, anular y meñique.*/
glPushMatrix();
/*Meñique, falange mayor*/
glTranslatef(0.95,0.38,0.0);
glTranslatef(-0.5,0.0,0.0);
glRotatef((GLfloat)menfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.5,0.0,0.0);
glScalef(0.4,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Meñique, falange media*/
glTranslatef(0.9,0.0,0.0);
glTranslatef(-0.4,0.0,-0.04);
glRotatef((GLfloat)menfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.4,0.0,0.04);
glScalef(0.4,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Meñique, falange menor*/
glTranslatef(1.15,0.005,0.04);
glTranslatef(-0.7,0.0,0.02);
glRotatef((GLfloat)menfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.3,0.0,-0.02);
glScalef(0.3,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix();/*Meñique completo*/
glPushMatrix();
/*Anular, falange mayor*/
glTranslatef(0.95,0.13,0.0);
glTranslatef(-0.5,0.0,0.0);
glRotatef((GLfloat)anufalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.5,0.0,0.0);
glScalef(0.4,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Anular, falange media*/
glTranslatef(0.88,0.0,0.0);
glTranslatef(-0.4,0.0,-0.04);
glRotatef((GLfloat)anufalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.5,0.0,0.04);
glScalef(0.55,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Anular, falange menor*/
glTranslatef(1.32,0.005,0.04);
glTranslatef(-0.7,0.0,0.02);
glRotatef((GLfloat)anufalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.3,0.0,-0.02);
glScalef(0.3,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix(); /*Anular completo*/
glPushMatrix();
/*Medio, falange mayor*/
glTranslatef(0.95,-0.12,0.0);
glTranslatef(-0.5,0.0,0.0);
glRotatef((GLfloat)medfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.5,0.0,0.0);
glScalef(0.4,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Medio, falange media*/
glTranslatef(0.88,0.0,0.0);
glTranslatef(-0.4,0.0,-0.04);
glRotatef((GLfloat)medfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.545,0.0,0.04);
glScalef(0.63,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Medio, falange menor*/
glTranslatef(1.4,0.005,0.04);
glTranslatef(-0.7,0.0,0.02);
glRotatef((GLfloat)medfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.3,0.0,-0.02);
glScalef(0.3,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix(); /*Medio completo*/
glPushMatrix();
/*Índice, falange mayor*/
glTranslatef(0.95,-0.37,0.0);
glTranslatef(-0.5,0.0,0.0);
glRotatef((GLfloat)indfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.5,0.0,0.0);
glScalef(0.4,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Índice, falange media*/
glTranslatef(0.81,0.0,0.0);
glTranslatef(-0.4,0.0,-0.04);
glRotatef((GLfloat)indfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.545,0.0,0.04);
glScalef(0.5,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
/*Índice, falange menor*/
glTranslatef(1.35,0.005,0.04);
glTranslatef(-0.7,0.0,0.02);
glRotatef((GLfloat)indfalmay,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(0.3,0.0,-0.02);
glScalef(0.3,0.25,0.4);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix(); /*Índice completo*/
}

void mano (void){
glPushMatrix();
palma(0.0,0.0,0.0);
pulgar();
dedos();
glPopMatrix();
}

void brazoder (){ /*Función que despliega el brazo derecho.*/
glPushMatrix();
glTranslatef(2.0,1.25,0.0);
glTranslatef(-1.0,0.0,0.0);
glRotatef(angbrazder,0.0,-1.0,0.0);
glPushMatrix();
glTranslatef(1.0,0.0,0.0);
glScalef(2.0,0.5,1.0);
glutWireCube(1.0);
glPopMatrix();
/*antebrazo derecho*/
glTranslatef(3.0,0.0,0.0);
glTranslatef(-1.0,0.0,0.0);
glRotatef(angcododer,-1.0,0.0,0.0);
glPushMatrix();
glTranslatef(1.0,0.0,0.0);
glScalef(2.0,1.0,1.0);
glutWireCube(1.0);
glPopMatrix();
/*mano derecha*/
glTranslatef(2.4,0.0,0.0);
glScalef(-1.0,1.0,1.0);
glRotatef(180,0.0,1.0,0.0);
glRotatef(180,1.0,0.0,0.0);
mano();
glPopMatrix();/*brazo derecho completo*/
}

void brazoizq (){
glPushMatrix();
glTranslatef(-2.0,1.25,0.0);
glTranslatef(1.0,0.0,0.0);
glRotatef(angbrazizq,0.0,1.0,0.0);
glPushMatrix();
glTranslatef(-1.0,0.0,0.0);
glScalef(2.0,0.5,1.0);
glutWireCube(1.0);
glPopMatrix();
/*antebrazo izquierdo*/
glTranslatef(-3.0,0.0,0.0);
glTranslatef(1.0,0.0,0.0);
glRotatef(180,1.0,0.0,0.0);
glRotatef(angcodoizq,1.0,0.0,0.0);
glPushMatrix();
glTranslatef(-1.0,0.0,0.0);
glScalef(2.0,1.0,1.0);
glutWireCube(1.0);
glPopMatrix();
/*mano izquierda*/
glTranslatef(-2.4,0.0,0.0);
glRotatef(180,0.0,1.0,0.0);
mano();
glPopMatrix();/*brazo izquierdo completo*/
}

void pierder (){
glPushMatrix();
glTranslatef(0.6,-2.25,0.0);
glRotatef((GLfloat)angpierder,0.0,0.0,1.0);
glPushMatrix();
glScalef(0.75,1.5,1.0);
glutWireCube(1.0);
glPopMatrix();
/*pantorrilla derecha*/
glTranslatef(0.0,-1.5,0.0);
glPushMatrix();
glScalef(0.6,1.5,1.0);
glutWireCube(1.0);
glPopMatrix();
/*pie derecho*/
glTranslatef(0.25,-0.85,0.0);
glPushMatrix();
glScalef(1.5,0.25,1.0);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix();/*pierna derecha completa*/
}

void pierizq (){
glPushMatrix();
glTranslatef(-0.6,-2.25,0.0);
glRotatef((GLfloat)angpierizq,0.0,0.0,1.0);
glPushMatrix();
glScalef(0.75,1.5,1.0);
glutWireCube(1.0);
glPopMatrix();
/*pantorrilla izquierda*/
glTranslatef(0.0,-1.5,0.0);
glPushMatrix();
glScalef(0.6,1.5,1.0);
glutWireCube(1.0);
glPopMatrix();
/*pie izquierdo*/
glTranslatef(-0.25,-0.85,0.0);
glPushMatrix();
glScalef(1.5,0.25,1.0);
glutWireCube(1.0);
glPopMatrix();
glPopMatrix();/*pierna izquierda completa*/
}

void display2()
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
gluLookAt (0.0, 0.0, 9.5, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glScalef(1.0,1.0,1.0);
/*torso*/
tronco(0.0,0.0,0.0,1,0.0);
/*cara*/
cabeza();
/*brazo derecho*/
brazoder();
/*brazo izquierdo*/
brazoizq();
/*muslo derecho*/
pierder();
/*muslo izquierdo*/
pierizq();
glPopMatrix();/*humanoide completo*/
glFlush ();
}

void idle()
{
for(tiemp = 0; tiemp < 10000000; tiemp ++)
{
}
if (tiemp2 < 90)
{
pulgfalmay = pulgfalmay + 5;
pulgfalmedx = pulgfalmedx + 5;
pulgfalmen = pulgfalmen + 5;
menfalmay = menfalmay + 5;
anufalmay = anufalmay + 5;
medfalmay = medfalmay + 5;
indfalmay = indfalmay + 5;
cuellorot = cuellorot + 5;
angcododer = angcododer + 5;
angcodoizq = angcodoizq + 5;
angbrazder = angbrazder + 5;
angbrazizq = angbrazizq + 5;
tiemp2 = tiemp2 + 5;
}
else if (tiemp2 < 180)
{
pulgfalmay = pulgfalmay - 5;
pulgfalmedx = pulgfalmedx - 5;
pulgfalmen = pulgfalmen - 5;
menfalmay = menfalmay - 5;
anufalmay = anufalmay - 5;
medfalmay = medfalmay - 5;
indfalmay = indfalmay - 5;
cuellorot = cuellorot - 5;
angcododer = angcododer + 5;
angcodoizq = angcodoizq + 5;
angbrazder = angbrazder - 5;
angbrazizq = angbrazizq - 5;
tiemp2 = tiemp2 + 5;
}
if (tiemp2 == 180)
{
tiemp2 = 0;
}
display2();
}

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 (xmouse, ymouse, (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 keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

void processMouse(int button, int state, int x, int y) {
if (state == GLUT_DOWN) {
if ((glutGetWindow() == win1) &&amp; (button == GLUT_LEFT_BUTTON) && (bandvent2 == 0)){
bandvent2 = 1;
xmouse = 255 - x;
ymouse = y - 255;
printf("Posicion del Mouse: %d, %d \n",x, y);
glutInitWindowSize (500, 500);
glutInitWindowPosition (400, 100);
win2 = glutCreateWindow ("Ventana 2");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape2);
glutIdleFunc(NULL);
glutSetWindow(win2);
glutKeyboardFunc(keyboard);
glutMouseFunc(processMouse);
}
else if ((glutGetWindow() == win2) &&amp; (button == GLUT_RIGHT_BUTTON)){
bandvent2 = 0;
glutIdleFunc(NULL);
glutDestroyWindow(win2);
}
}
if (state == GLUT_UP){
if ((glutGetWindow() == win1) &&amp; (button == GLUT_RIGHT_BUTTON) && (bandvent3 == 0)){
bandvent3 = 1;
glutInitWindowSize (500, 500);
glutInitWindowPosition (400, 300);
win3 = glutCreateWindow ("Ventana 3");
init();
glutDisplayFunc(display2);
glutReshapeFunc(reshape);
glutSetWindow(win3);
glutKeyboardFunc(keyboard);
glutMouseFunc(processMouse);
}
else if ((glutGetWindow() == win3) &&amp; (button == GLUT_LEFT_BUTTON)){
glutIdleFunc(idle);
}
else if ((glutGetWindow() == win3) &&amp; (button == GLUT_RIGHT_BUTTON)){
bandvent3 = 0;
glutIdleFunc(NULL);
glutDestroyWindow(win3);
}
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
win1 = glutCreateWindow ("Ventana 1");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutSetWindow(win1);
glutIdleFunc(NULL);
glutKeyboardFunc(keyboard);
glutMouseFunc(processMouse);
glutMainLoop();
return 0;
}

2 comentarios:

Antonio Davalos dijo...

TA RA RA RA RA RA RA RA RA BATMAAAAAAAN!!!!!!!!!!
TA RA RA RA RA RA RA RA RA BATMAAAAAAAN!!!!!!!!!!
TA RA RA RA RA RA RA RA RA BATMAAAAAAAN!!!!!!!!!!

BAAAATMAAAAAAN BAAAATMAAAAAAAN!!!!!!

Antonio Davalos dijo...

SANTOS BLOGS BATMAN, AHI VIENE LA NANATÚBELA!