martes, 11 de marzo de 2008

Reporte práctica 3

Reporte práctica 3
Agregar un brazo, tronco y cabeza al código.

____________




//Programa brazo.c
//4 de marzo de 2008

/* Granados Chavarria Eduardo
* 302621127
* reporte practica 3
*/
//Igor Valiente - igor at servidor dot unam dot mx

#include

GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */
GLfloat light_poseybition[] = {1.0, 1.0, -1.0, 0.0}; /* Infinite light location. */
GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */

static int shoulder=0;
static int joint=0;
static int wrist=0;

void drawBox(void)
{
int i;

for (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}

void troncoCabeza()
{
glPushMatrix();
// tronco
glTranslatef(0,1.10,0);
glPushMatrix();
glScalef(2.6,2.2,0.5);
drawBox();
glPopMatrix();

glTranslatef(0,1.45,0);
glPushMatrix();
glScalef(0.7,0.7,0.5);
drawBox();
glPopMatrix();


glPopMatrix();
}

void brazoIzquierdo(int shoulder, int joint, int wrist)
{
glPushMatrix();
glTranslatef(-1.3,2.0,0);
//empieza el dibujo del brazo robot
glPushMatrix();
//empieza el brazo
glRotatef(-(GLfloat)shoulder, 0.0, 0.0, 1.0);
glTranslatef(-1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0,0.5,0.5);
drawBox();
glPopMatrix();
//termina brazo
//empieza antebrazo
glTranslatef(-1.0, 0.0, 0.0);
glRotatef(-(GLfloat)joint, 0.0, 0.0, 1.0);
glTranslatef(-1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0,0.5,0.5);
drawBox();
glPopMatrix();
//termina antebrazo-- M6
//empieza el grip
glPushMatrix();
//empieza el primer dedo
glPushMatrix();
glTranslatef(-1.0,0.0,0.0);
glRotatef((GLfloat)wrist,0.0,0.0,1.0);
glTranslatef(-0.35,0.125,0.0);
glPushMatrix();
glScalef(0.7,0.25,0.25);
drawBox();
glPopMatrix();
glPopMatrix();
//termina el primer dedo
//empieza el segundo dedo
glPushMatrix();
glTranslatef(-1.0,0.0,0.0);
glRotatef((GLfloat)-wrist,0.0,0.0,1.0);
glTranslatef(-0.35,-0.125,0.0);
glPushMatrix();
glScalef(0.7,0.25,0.25);
drawBox();
glPopMatrix();
glPopMatrix();
//termina el segundo dedo
glPopMatrix();
//termina el grip
glPopMatrix();
//termina el dibujo del brazo robot
glPopMatrix();
}

void brazo(int shoulder, int joint, int wrist)
{
//empieza el dibujo del brazo robot
glPushMatrix();
glTranslatef(1.3,2.0,0);
glPushMatrix();
//empieza el brazo
glRotatef((GLfloat)shoulder, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0,0.5,0.5);
drawBox();
glPopMatrix();
//termina brazo
//empieza antebrazo
glTranslatef(1.0, 0.0, 0.0);
glRotatef((GLfloat)joint, 0.0, 0.0, 1.0);
glTranslatef(1.0, 0.0, 0.0);
glPushMatrix();
glScalef(2.0,0.5,0.5);
drawBox();
glPopMatrix();
//termina antebrazo-- M6
//empieza el grip
glPushMatrix();
//empieza el primer dedo
glPushMatrix();
glTranslatef(1.0,0.0,0.0);
glRotatef((GLfloat)wrist,0.0,0.0,1.0);
glTranslatef(0.35,0.125,0.0);
glPushMatrix();
glScalef(0.7,0.25,0.25);
drawBox();
glPopMatrix();
glPopMatrix();
//termina el primer dedo
//empieza el segundo dedo
glPushMatrix();
glTranslatef(1.0,0.0,0.0);
glRotatef((GLfloat)-wrist,0.0,0.0,1.0);
glTranslatef(0.35,-0.125,0.0);
glPushMatrix();
glScalef(0.7,0.25,0.25);
drawBox();
glPopMatrix();
glPopMatrix();
//termina el segundo dedo
glPopMatrix();
//termina el grip
glPopMatrix();
//termina el dibujo del brazo robot
glPopMatrix();
}
display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
brazoIzquierdo(shoulder,joint,wrist);
brazo(shoulder,joint,wrist);
troncoCabeza();
glutSwapBuffers();
}

void
init(void)
{
/* Setup cube vertex data. */
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -0.5;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 0.5;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -0.5;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 0.5;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 0.5;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -0.5;

/* Enable a single OpenGL light. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

/* Use depth buffering for hidden surface elimination. */
glEnable(GL_DEPTH_TEST);

/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);
gluPerspective( /* field of view in degree */ 120.0,
/* aspect ratio */ 1.0,
/* Z near */ 1.0, /* Z far */ 20.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.); /* up is in positive Y direction */

/* Adjust cube position to be asthetic angle. */
//glTranslatef(0.0, 0.0, -1.0);
glRotatef(20, 1.0, 0.0, 0.0);
//glRotatef(-20, 0.0, 0.0, 1.0);
}

int keyboard(char key, int x, int y)
{
switch(key){
case 27: exit(0); //tecla de escape
break;
case 'h': shoulder=(shoulder+5)%360;
glutPostRedisplay();
break;
case 'H': shoulder=(shoulder-5)%360;
glutPostRedisplay();
break;
case 'c': joint=(joint+5)%360;
glutPostRedisplay();
break;
case 'C': joint=(joint-5)%360;
glutPostRedisplay();
break;
case 'm': wrist=(wrist+5)%360;
glutPostRedisplay();
break;
case 'M': wrist=(wrist-5)%360;
glutPostRedisplay();
break;
default: break;
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("Brazo robot rojo");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}

Previo Práctica 4

Crear un humanoide

____________




/*
* Eduardo Granados Chavarria
*
* Computación Gráfica
* Programa Humanoide
* Previo y reporte práctica 4
*/

/*
* Teclas para mover minusculas y mayusculas son sentidos opuestos, para restaurar 'p'
* q,w,e,r,t,y,u,i,a,s,d,f,g,h,j,k,z,x,c,v,b,n
*
*/


#include

GLfloat light_diffuse[] = {1.0, 0.00, 0.0, 1.0}; /* Red diffuse light. */
GLfloat light_position[] = {1.0, 1.0, -1.0, 0.0}; /* Infinite light location. */
GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */
{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},
{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };
GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */
{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},
{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };
GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */

GLfloat Xmano=0.0,Zmano=0.0,Ybrazo=0.0, Yantebrazo=0.0, Zantebrazo=0.0;
GLfloat Xcabeza = 0, Ycabeza = 0, Zcabeza=0;
GLfloat Yabdomen=0, Zabdomen=0,Ypecho=0,Zpecho=0;
GLfloat Xpie=0, Ypie=0, Xpierna=0, Xmuslo=0,Ymuslo=0, Zmuslo=0;
GLfloat Zpie=0, Ycadera=0,Zcadera=0;
int i;

void drawBox(void)
{
int i;

for (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}

void brazoIzquierdo(float x_mano, float z_mano, float y_brazo, float y_antebrazo, float z_antebrazo)
{
glPushMatrix();

//antebrazo
glTranslatef(-2.5,1.5,0);
glRotatef(y_antebrazo,0,1,0);
glRotatef(z_antebrazo,0,0,1);
glTranslatef(-1.5,0,0);
glPushMatrix();
glScalef(3,1,0.5);
drawBox();
glPopMatrix();

glPushMatrix();
//brazo
glTranslatef(-1.5,0,0);
glRotatef(y_brazo,0,1,0);
glTranslatef(-1.5,0,0);
glPushMatrix();
glScalef(3,1.5,.5);
drawBox();
glPopMatrix();

glPushMatrix();
// mano
glTranslatef(-1.5,0,0);
glRotatef(x_mano,1,0,0);
glRotatef(z_mano,0,0,1);
glTranslatef(-0.75,0,0);
glPushMatrix();
glScalef(1.5,0.5,0.5);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}

void brazoDerecho(float x_mano,float z_mano,float y_brazo, float y_antebrazo, float z_antebrazo)
{
glPushMatrix();

//antebrazo
glTranslatef(2.5,1.5,0);
glRotatef(y_antebrazo,0,1,0);
glRotatef(z_antebrazo,0,0,1);
glTranslatef(1.5,0,0);
glPushMatrix();
glScalef(3,1,0.5);
drawBox();
glPopMatrix();

glPushMatrix();
//brazo
glTranslatef(1.5,0,0);
glRotatef(y_brazo,0,1,0);
glTranslatef(1.5,0,0);
glPushMatrix();
glScalef(3,1.5,.5);
drawBox();
glPopMatrix();

glPushMatrix();
// mano
glTranslatef(1.5,0,0);
glRotatef(x_mano,1,0,0);
glRotatef(z_mano,0,0,1);
glTranslatef(0.75,0,0);
glPushMatrix();
glScalef(1.5,0.5,0.5);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}

void cabezaCuello(float x_cabeza,float y_cabeza,float z_cabeza)
{
glPushMatrix();

glTranslatef(0,2.5,0);
glPushMatrix();
glScalef(2,.5,.5);
drawBox();
glPopMatrix();

glPushMatrix();
glTranslatef(0,.5,0);
glRotatef(x_cabeza,1,0,0);
glRotatef(y_cabeza,0,1,0);
glRotatef(z_cabeza,0,0,1);
glTranslatef(0,2.5/2,0.5);
glPushMatrix();
glScalef(3,2.5,.5);
drawBox();
glPopMatrix();

glPopMatrix();
glPopMatrix();
}

void pechoAbdomen(float z_pecho,float y_pecho,float y_abdomen,float z_abdomen)
{
glPushMatrix();
glTranslatef(0,.50,0);
glRotatef(z_abdomen,0,0,1);
glRotatef(y_abdomen,0,1,0);
glTranslatef(0,1.5/2,0);
glPushMatrix();
glScalef(3,1.5,0.5);
drawBox();
glPopMatrix();

glPushMatrix();
glTranslatef(0,1.5/2,0);
glRotatef(z_pecho,0,0,1);
glRotatef(y_pecho,0,1,0);
glTranslatef(0,2,0);
glPushMatrix();
glScalef(5,4,0.5);
drawBox();
glPopMatrix();

brazoIzquierdo(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);
brazoDerecho(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);
cabezaCuello(Xcabeza,Ycabeza,Zcabeza);
glPopMatrix();
glPopMatrix();
}


void pieIzquierdo(float x_pie,float z_pie,float x_pierna,float x_muslo,float y_muslo,float z_muslo)
{
glPushMatrix();

//muslo
glTranslatef(-2,-.75,0);
glRotatef(x_muslo,1,0,0);
glRotatef(y_muslo,0,1,0);
glRotatef(z_muslo,0,0,1);
glTranslatef(0,-2.5/2,0);
glPushMatrix();
glScalef(1,2.5,0.5);
drawBox();
glPopMatrix();

glPushMatrix();
//pierna
glTranslatef(0,-2.5/2,0);
glRotatef(x_pierna,1,0,0);
glTranslatef(0,-2.5/2,0);
glPushMatrix();
glScalef(2,2.5,0.5);
drawBox();
glPopMatrix();

glPushMatrix();
//pie
glTranslatef(0,-1.5,0);
glRotatef(x_pie,1,0,0);
glRotatef(z_pie,0,0,1);
glTranslatef(0,-.5,0);
glPushMatrix();
glScalef(3,1,0.5);
drawBox();
glPopMatrix();

glPopMatrix();
glPopMatrix();
glPopMatrix();
}


void pieDerecho(float x_pie,float z_pie,float x_pierna,float x_muslo,float y_muslo,float z_muslo)
{
glPushMatrix();

//muslo
glTranslatef(2,-.75,0);
glRotatef(x_muslo,1,0,0);
glRotatef(y_muslo,0,1,0);
glRotatef(z_muslo,0,0,1);
glTranslatef(0,-2.5/2,0);
glPushMatrix();
glScalef(1,2.5,0.5);
drawBox();
glPopMatrix();

glPushMatrix();
//pierna
glTranslatef(0,-2.5/2,0);
glRotatef(x_pierna,1,0,0);
glTranslatef(0,-2.5/2,0);
glPushMatrix();
glScalef(2,2.5,0.5);
drawBox();
glPopMatrix();

glPushMatrix();
//pie
glTranslatef(0,-1.5,0);
glRotatef(x_pie,1,0,0);
glRotatef(z_pie,0,0,1);
glTranslatef(0,-.5,0);
glPushMatrix();
glScalef(3,1,0.5);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
}

void cadera(float y_cadera, float z_cadera)
{
glPushMatrix();
glTranslatef(0,0,0);
glRotatef(y_cadera,0,1,0);
glRotatef(z_cadera,0,0,1);
glTranslatef(0,-1.5/2,0);
glPushMatrix();
glScalef(5,1.5,0.5);
drawBox();
glPopMatrix();

pechoAbdomen(Zpecho,Ypecho,Yabdomen,Zabdomen);
pieIzquierdo(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);
pieDerecho(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);
glPopMatrix();
}

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
cadera(Ycadera, Zcadera);
//pechoAbdomen(Zpecho,Ypecho,Yabdomen,Zabdomen);
//brazoIzquierdo(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);
//brazoDerecho(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);
//cabezaCuello(Xcabeza,Ycabeza,Zcabeza);
//pieIzquierdo(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);
//pieDerecho(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);


glPopMatrix();
glutSwapBuffers();
}


void init(void)
{
/* Setup cube vertex data. */
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -0.5;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 0.5;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -0.5;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 0.5;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 0.5;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -0.5;

/* Enable a single OpenGL light. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);

/* Use depth buffering for hidden surface elimination. */
glEnable(GL_DEPTH_TEST);

/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);

//gluPerspective( /* field of view in degree */ 120.0,
///* aspect ratio */ 1.0,
// /* Z near */ 1.0, /* Z far */ 20.0);

glOrtho(-11,11,-11,11,-11,11);

glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 8.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.); /* up is in positive Y direction */

/* Adjust cube position to be asthetic angle. */
//glTranslatef(0.0, 0.0, -1.0);
glRotatef(20, 1.0, 0.0, 0.0);
//glRotatef(-20, 0.0, 0.0, 1.0);

}

int keyboard(char key, int x, int y)
{
switch(key){
case 27: exit(0); break;

case 'p': Xmano+=1; Xmano=0.0; Zmano=0.0; Ybrazo=0.0; Yantebrazo=0.0;
Zantebrazo=0.0; Xcabeza = 0; Ycabeza = 0; Zcabeza=0; Yabdomen=0;
Zabdomen=0; Ypecho=0; Zpecho=0; Xpie=0; Ypie=0; Xpierna=0;
Xmuslo=0; Ymuslo=0; Zmuslo=0;
Zpie=0; Ycadera=0;Zcadera=0;
break;

case 'q': Xmano+=1; break;
case 'Q': Xmano-=1; break;

case 'w': Zmano+=1; break;
case 'W': Zmano-=1; break;

case 'e': Ybrazo+=1; break;
case 'E': Ybrazo-=1; break;

case 'r': Yantebrazo+=1; break;
case 'R': Yantebrazo-=1; break;

case 't': Zantebrazo+=1; break;
case 'T': Zantebrazo-=1; break;

case 'y': Xcabeza+=1; break;
case 'Y': Xcabeza-=1; break;

case 'u': Ycabeza+=1; break;
case 'U': Ycabeza-=1; break;

case 'i': Zcabeza+=1; break;
case 'I': Zcabeza-=1; break;

case 'a': Yabdomen+=1; break;
case 'A': Yabdomen-=1; break;

case 's': Zabdomen+=1; break;
case 'S': Zabdomen-=1; break;

case 'd': Ypecho+=1; break;
case 'D': Ypecho-=1; break;

case 'f': Zpecho+=1; break;
case 'F': Zpecho-=1; break;

case 'g': Ycadera+=1; break;
case 'G': Ycadera-=1; break;

case 'h': Zcadera+=1; break;
case 'H': Zcadera-=1; break;

case 'z': Xpie+=1; break;
case 'Z': Xpie-=1; break;

case 'x': Ypie+=1; break;
case 'X': Ypie-=1; break;

case 'c': Xpierna+=1; break;
case 'C': Xpierna-=1; break;

case 'v': Xmuslo+=1; break;
case 'V': Xmuslo-=1; break;
case 'b': Ymuslo+=1; break;
case 'B': Ymuslo-=1; break;
case 'n': Zmuslo+=1; break;
case 'N': Zmuslo-=1; break;

default: break;
}
glutPostRedisplay();
}


int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition(850, 0); // posicion de la ventana
glutInitWindowSize(500,500);
glutCreateWindow("Brazo Robot Rojo");
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}






Previo Práctica 4

Crear un humanoide




___________________________________



/*

* Eduardo Granados Chavarria

*

* Computación Gráfica

* Programa Humanoide

* Previo y reporte práctica 4

*/


/*

* Teclas para mover minusculas y mayusculas son sentidos opuestos, para restaurar 'p'

* q,w,e,r,t,y,u,i,a,s,d,f,g,h,j,k,z,x,c,v,b,n

*

*/



#include


GLfloat light_diffuse[] = {1.0, 0.00, 0.0, 1.0}; /* Red diffuse light. */

GLfloat light_position[] = {1.0, 1.0, -1.0, 0.0}; /* Infinite light location. */

GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */

{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},

{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };

GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */

{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},

{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };

GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */


GLfloat Xmano=0.0,Zmano=0.0,Ybrazo=0.0, Yantebrazo=0.0, Zantebrazo=0.0;

GLfloat Xcabeza = 0, Ycabeza = 0, Zcabeza=0;

GLfloat Yabdomen=0, Zabdomen=0,Ypecho=0,Zpecho=0;

GLfloat Xpie=0, Ypie=0, Xpierna=0, Xmuslo=0,Ymuslo=0, Zmuslo=0;

GLfloat Zpie=0, Ycadera=0,Zcadera=0;

int i;


void drawBox(void)

{

int i;


for (i = 0; i < 6; i++) {

glBegin(GL_QUADS);

glNormal3fv(&n[i][0]);

glVertex3fv(&v[faces[i][0]][0]);

glVertex3fv(&v[faces[i][1]][0]);

glVertex3fv(&v[faces[i][2]][0]);

glVertex3fv(&v[faces[i][3]][0]);

glEnd();

}

}


void brazoIzquierdo(float x_mano, float z_mano, float y_brazo, float y_antebrazo, float z_antebrazo)

{

glPushMatrix();


//antebrazo

glTranslatef(-2.5,1.5,0);

glRotatef(y_antebrazo,0,1,0);

glRotatef(z_antebrazo,0,0,1);

glTranslatef(-1.5,0,0);

glPushMatrix();

glScalef(3,1,0.5);

drawBox();

glPopMatrix();


glPushMatrix();

//brazo

glTranslatef(-1.5,0,0);

glRotatef(y_brazo,0,1,0);

glTranslatef(-1.5,0,0);

glPushMatrix();

glScalef(3,1.5,.5);

drawBox();

glPopMatrix();


glPushMatrix();

// mano

glTranslatef(-1.5,0,0);

glRotatef(x_mano,1,0,0);

glRotatef(z_mano,0,0,1);

glTranslatef(-0.75,0,0);

glPushMatrix();

glScalef(1.5,0.5,0.5);

drawBox();

glPopMatrix();

glPopMatrix();

glPopMatrix();

glPopMatrix();

}


void brazoDerecho(float x_mano,float z_mano,float y_brazo, float y_antebrazo, float z_antebrazo)

{

glPushMatrix();


//antebrazo

glTranslatef(2.5,1.5,0);

glRotatef(y_antebrazo,0,1,0);

glRotatef(z_antebrazo,0,0,1);

glTranslatef(1.5,0,0);

glPushMatrix();

glScalef(3,1,0.5);

drawBox();

glPopMatrix();


glPushMatrix();

//brazo

glTranslatef(1.5,0,0);

glRotatef(y_brazo,0,1,0);

glTranslatef(1.5,0,0);

glPushMatrix();

glScalef(3,1.5,.5);

drawBox();

glPopMatrix();


glPushMatrix();

// mano

glTranslatef(1.5,0,0);

glRotatef(x_mano,1,0,0);

glRotatef(z_mano,0,0,1);

glTranslatef(0.75,0,0);

glPushMatrix();

glScalef(1.5,0.5,0.5);

drawBox();

glPopMatrix();

glPopMatrix();

glPopMatrix();

glPopMatrix();

}


void cabezaCuello(float x_cabeza,float y_cabeza,float z_cabeza)

{

glPushMatrix();


glTranslatef(0,2.5,0);

glPushMatrix();

glScalef(2,.5,.5);

drawBox();

glPopMatrix();


glPushMatrix();

glTranslatef(0,.5,0);

glRotatef(x_cabeza,1,0,0);

glRotatef(y_cabeza,0,1,0);

glRotatef(z_cabeza,0,0,1);

glTranslatef(0,2.5/2,0.5);

glPushMatrix();

glScalef(3,2.5,.5);

drawBox();

glPopMatrix();


glPopMatrix();

glPopMatrix();

}


void pechoAbdomen(float z_pecho,float y_pecho,float y_abdomen,float z_abdomen)

{

glPushMatrix();

glTranslatef(0,.50,0);

glRotatef(z_abdomen,0,0,1);

glRotatef(y_abdomen,0,1,0);

glTranslatef(0,1.5/2,0);

glPushMatrix();

glScalef(3,1.5,0.5);

drawBox();

glPopMatrix();


glPushMatrix();

glTranslatef(0,1.5/2,0);

glRotatef(z_pecho,0,0,1);

glRotatef(y_pecho,0,1,0);

glTranslatef(0,2,0);

glPushMatrix();

glScalef(5,4,0.5);

drawBox();

glPopMatrix();


brazoIzquierdo(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);

brazoDerecho(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);

cabezaCuello(Xcabeza,Ycabeza,Zcabeza);

glPopMatrix();

glPopMatrix();

}



void pieIzquierdo(float x_pie,float z_pie,float x_pierna,float x_muslo,float y_muslo,float z_muslo)

{

glPushMatrix();


//muslo

glTranslatef(-2,-.75,0);

glRotatef(x_muslo,1,0,0);

glRotatef(y_muslo,0,1,0);

glRotatef(z_muslo,0,0,1);

glTranslatef(0,-2.5/2,0);

glPushMatrix();

glScalef(1,2.5,0.5);

drawBox();

glPopMatrix();


glPushMatrix();

//pierna

glTranslatef(0,-2.5/2,0);

glRotatef(x_pierna,1,0,0);

glTranslatef(0,-2.5/2,0);

glPushMatrix();

glScalef(2,2.5,0.5);

drawBox();

glPopMatrix();


glPushMatrix();

//pie

glTranslatef(0,-1.5,0);

glRotatef(x_pie,1,0,0);

glRotatef(z_pie,0,0,1);

glTranslatef(0,-.5,0);

glPushMatrix();

glScalef(3,1,0.5);

drawBox();

glPopMatrix();


glPopMatrix();

glPopMatrix();

glPopMatrix();

}



void pieDerecho(float x_pie,float z_pie,float x_pierna,float x_muslo,float y_muslo,float z_muslo)

{

glPushMatrix();


//muslo

glTranslatef(2,-.75,0);

glRotatef(x_muslo,1,0,0);

glRotatef(y_muslo,0,1,0);

glRotatef(z_muslo,0,0,1);

glTranslatef(0,-2.5/2,0);

glPushMatrix();

glScalef(1,2.5,0.5);

drawBox();

glPopMatrix();


glPushMatrix();

//pierna

glTranslatef(0,-2.5/2,0);

glRotatef(x_pierna,1,0,0);

glTranslatef(0,-2.5/2,0);

glPushMatrix();

glScalef(2,2.5,0.5);

drawBox();

glPopMatrix();


glPushMatrix();

//pie

glTranslatef(0,-1.5,0);

glRotatef(x_pie,1,0,0);

glRotatef(z_pie,0,0,1);

glTranslatef(0,-.5,0);

glPushMatrix();

glScalef(3,1,0.5);

drawBox();

glPopMatrix();

glPopMatrix();

glPopMatrix();

glPopMatrix();

}


void cadera(float y_cadera, float z_cadera)

{

glPushMatrix();

glTranslatef(0,0,0);

glRotatef(y_cadera,0,1,0);

glRotatef(z_cadera,0,0,1);

glTranslatef(0,-1.5/2,0);

glPushMatrix();

glScalef(5,1.5,0.5);

drawBox();

glPopMatrix();


pechoAbdomen(Zpecho,Ypecho,Yabdomen,Zabdomen);

pieIzquierdo(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);

pieDerecho(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);

glPopMatrix();

}


void display(void)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


glPushMatrix();

cadera(Ycadera, Zcadera);

//pechoAbdomen(Zpecho,Ypecho,Yabdomen,Zabdomen);

//brazoIzquierdo(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);

//brazoDerecho(Xmano,Zmano,Ybrazo, Yantebrazo, Zantebrazo);

//cabezaCuello(Xcabeza,Ycabeza,Zcabeza);

//pieIzquierdo(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);

//pieDerecho(Xpie,Zpie,Xpierna,Xmuslo,Ymuslo,Zmuslo);



glPopMatrix();

glutSwapBuffers();

}



void init(void)

{

/* Setup cube vertex data. */

v[0][0] = v[1][0] = v[2][0] = v[3][0] = -0.5;

v[4][0] = v[5][0] = v[6][0] = v[7][0] = 0.5;

v[0][1] = v[1][1] = v[4][1] = v[5][1] = -0.5;

v[2][1] = v[3][1] = v[6][1] = v[7][1] = 0.5;

v[0][2] = v[3][2] = v[4][2] = v[7][2] = 0.5;

v[1][2] = v[2][2] = v[5][2] = v[6][2] = -0.5;


/* Enable a single OpenGL light. */

glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHT0);

glEnable(GL_LIGHTING);


/* Use depth buffering for hidden surface elimination. */

glEnable(GL_DEPTH_TEST);


/* Setup the view of the cube. */

glMatrixMode(GL_PROJECTION);


//gluPerspective( /* field of view in degree */ 120.0,

///* aspect ratio */ 1.0,

// /* Z near */ 1.0, /* Z far */ 20.0);


glOrtho(-11,11,-11,11,-11,11);


glMatrixMode(GL_MODELVIEW);

gluLookAt(0.0, 0.0, 8.0, /* eye is at (0,0,5) */

0.0, 0.0, 0.0, /* center is at (0,0,0) */

0.0, 1.0, 0.); /* up is in positive Y direction */


/* Adjust cube position to be asthetic angle. */

//glTranslatef(0.0, 0.0, -1.0);

glRotatef(20, 1.0, 0.0, 0.0);

//glRotatef(-20, 0.0, 0.0, 1.0);


}


int keyboard(char key, int x, int y)

{

switch(key){

case 27: exit(0); break;


case 'p': Xmano+=1; Xmano=0.0; Zmano=0.0; Ybrazo=0.0; Yantebrazo=0.0;

Zantebrazo=0.0; Xcabeza = 0; Ycabeza = 0; Zcabeza=0; Yabdomen=0;

Zabdomen=0; Ypecho=0; Zpecho=0; Xpie=0; Ypie=0; Xpierna=0;

Xmuslo=0; Ymuslo=0; Zmuslo=0;

Zpie=0; Ycadera=0;Zcadera=0;

break;


case 'q': Xmano+=1; break;

case 'Q': Xmano-=1; break;


case 'w': Zmano+=1; break;

case 'W': Zmano-=1; break;


case 'e': Ybrazo+=1; break;

case 'E': Ybrazo-=1; break;


case 'r': Yantebrazo+=1; break;

case 'R': Yantebrazo-=1; break;


case 't': Zantebrazo+=1; break;

case 'T': Zantebrazo-=1; break;


case 'y': Xcabeza+=1; break;

case 'Y': Xcabeza-=1; break;


case 'u': Ycabeza+=1; break;

case 'U': Ycabeza-=1; break;


case 'i': Zcabeza+=1; break;

case 'I': Zcabeza-=1; break;


case 'a': Yabdomen+=1; break;

case 'A': Yabdomen-=1; break;


case 's': Zabdomen+=1; break;

case 'S': Zabdomen-=1; break;


case 'd': Ypecho+=1; break;

case 'D': Ypecho-=1; break;


case 'f': Zpecho+=1; break;

case 'F': Zpecho-=1; break;


case 'g': Ycadera+=1; break;

case 'G': Ycadera-=1; break;


case 'h': Zcadera+=1; break;

case 'H': Zcadera-=1; break;


case 'z': Xpie+=1; break;

case 'Z': Xpie-=1; break;


case 'x': Ypie+=1; break;

case 'X': Ypie-=1; break;


case 'c': Xpierna+=1; break;

case 'C': Xpierna-=1; break;


case 'v': Xmuslo+=1; break;

case 'V': Xmuslo-=1; break;

case 'b': Ymuslo+=1; break;

case 'B': Ymuslo-=1; break;

case 'n': Zmuslo+=1; break;

case 'N': Zmuslo-=1; break;


default: break;

}

glutPostRedisplay();

}



int main(int argc, char **argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowPosition(850, 0); // posicion de la ventana

glutInitWindowSize(500,500);

glutCreateWindow("Brazo Robot Rojo");

glutDisplayFunc(display);

glutKeyboardFunc(keyboard);

init();

glutMainLoop();

return 0; /* ANSI C requires main to return int. */

}

Reporte de practica 3

Reporte de la practica tres, el programa dado por el profesor se debe agregar otro brazo, tronco y cabeza:


___________________________________________



//Programa brazo.c

//4 de marzo de 2008


/* Granados Chavarria Eduardo

*

* reporte practica 3

*/

//Igor Valiente - igor at servidor dot unam dot mx


#include


GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0}; /* Red diffuse light. */

GLfloat light_poseybition[] = {1.0, 1.0, -1.0, 0.0}; /* Infinite light location. */

GLfloat n[6][3] = { /* Normals for the 6 faces of a cube. */

{-1.0, 0.0, 0.0}, {0.0, 1.0, 0.0}, {1.0, 0.0, 0.0},

{0.0, -1.0, 0.0}, {0.0, 0.0, 1.0}, {0.0, 0.0, -1.0} };

GLint faces[6][4] = { /* Vertex indices for the 6 faces of a cube. */

{0, 1, 2, 3}, {3, 2, 6, 7}, {7, 6, 5, 4},

{4, 5, 1, 0}, {5, 6, 2, 1}, {7, 4, 0, 3} };

GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */


static int shoulder=0;

static int joint=0;

static int wrist=0;


void drawBox(void)

{

int i;


for (i = 0; i < 6; i++) {

glBegin(GL_QUADS);

glNormal3fv(&n[i][0]);

glVertex3fv(&v[faces[i][0]][0]);

glVertex3fv(&v[faces[i][1]][0]);

glVertex3fv(&v[faces[i][2]][0]);

glVertex3fv(&v[faces[i][3]][0]);

glEnd();

}

}


void troncoCabeza()

{

glPushMatrix();

// tronco

glTranslatef(0,1.10,0);

glPushMatrix();

glScalef(2.6,2.2,0.5);

drawBox();

glPopMatrix();


glTranslatef(0,1.45,0);

glPushMatrix();

glScalef(0.7,0.7,0.5);

drawBox();

glPopMatrix();



glPopMatrix();

}


void brazoIzquierdo(int shoulder, int joint, int wrist)

{

glPushMatrix();

glTranslatef(-1.3,2.0,0);

//empieza el dibujo del brazo robot

glPushMatrix();

//empieza el brazo

glRotatef(-(GLfloat)shoulder, 0.0, 0.0, 1.0);

glTranslatef(-1.0, 0.0, 0.0);

glPushMatrix();

glScalef(2.0,0.5,0.5);

drawBox();

glPopMatrix();

//termina brazo

//empieza antebrazo

glTranslatef(-1.0, 0.0, 0.0);

glRotatef(-(GLfloat)joint, 0.0, 0.0, 1.0);

glTranslatef(-1.0, 0.0, 0.0);

glPushMatrix();

glScalef(2.0,0.5,0.5);

drawBox();

glPopMatrix();

//termina antebrazo-- M6

//empieza el grip

glPushMatrix();

//empieza el primer dedo

glPushMatrix();

glTranslatef(-1.0,0.0,0.0);

glRotatef((GLfloat)wrist,0.0,0.0,1.0);

glTranslatef(-0.35,0.125,0.0);

glPushMatrix();

glScalef(0.7,0.25,0.25);

drawBox();

glPopMatrix();

glPopMatrix();

//termina el primer dedo

//empieza el segundo dedo

glPushMatrix();

glTranslatef(-1.0,0.0,0.0);

glRotatef((GLfloat)-wrist,0.0,0.0,1.0);

glTranslatef(-0.35,-0.125,0.0);

glPushMatrix();

glScalef(0.7,0.25,0.25);

drawBox();

glPopMatrix();

glPopMatrix();

//termina el segundo dedo

glPopMatrix();

//termina el grip

glPopMatrix();

//termina el dibujo del brazo robot

glPopMatrix();

}


void brazo(int shoulder, int joint, int wrist)

{

//empieza el dibujo del brazo robot

glPushMatrix();

glTranslatef(1.3,2.0,0);

glPushMatrix();

//empieza el brazo

glRotatef((GLfloat)shoulder, 0.0, 0.0, 1.0);

glTranslatef(1.0, 0.0, 0.0);

glPushMatrix();

glScalef(2.0,0.5,0.5);

drawBox();

glPopMatrix();

//termina brazo

//empieza antebrazo

glTranslatef(1.0, 0.0, 0.0);

glRotatef((GLfloat)joint, 0.0, 0.0, 1.0);

glTranslatef(1.0, 0.0, 0.0);

glPushMatrix();

glScalef(2.0,0.5,0.5);

drawBox();

glPopMatrix();

//termina antebrazo-- M6

//empieza el grip

glPushMatrix();

//empieza el primer dedo

glPushMatrix();

glTranslatef(1.0,0.0,0.0);

glRotatef((GLfloat)wrist,0.0,0.0,1.0);

glTranslatef(0.35,0.125,0.0);

glPushMatrix();

glScalef(0.7,0.25,0.25);

drawBox();

glPopMatrix();

glPopMatrix();

//termina el primer dedo

//empieza el segundo dedo

glPushMatrix();

glTranslatef(1.0,0.0,0.0);

glRotatef((GLfloat)-wrist,0.0,0.0,1.0);

glTranslatef(0.35,-0.125,0.0);

glPushMatrix();

glScalef(0.7,0.25,0.25);

drawBox();

glPopMatrix();

glPopMatrix();

//termina el segundo dedo

glPopMatrix();

//termina el grip

glPopMatrix();

//termina el dibujo del brazo robot

glPopMatrix();

}

display(void)

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

brazoIzquierdo(shoulder,joint,wrist);

brazo(shoulder,joint,wrist);

troncoCabeza();

glutSwapBuffers();

}


void

init(void)

{

/* Setup cube vertex data. */

v[0][0] = v[1][0] = v[2][0] = v[3][0] = -0.5;

v[4][0] = v[5][0] = v[6][0] = v[7][0] = 0.5;

v[0][1] = v[1][1] = v[4][1] = v[5][1] = -0.5;

v[2][1] = v[3][1] = v[6][1] = v[7][1] = 0.5;

v[0][2] = v[3][2] = v[4][2] = v[7][2] = 0.5;

v[1][2] = v[2][2] = v[5][2] = v[6][2] = -0.5;


/* Enable a single OpenGL light. */

glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);

glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHT0);

glEnable(GL_LIGHTING);


/* Use depth buffering for hidden surface elimination. */

glEnable(GL_DEPTH_TEST);


/* Setup the view of the cube. */

glMatrixMode(GL_PROJECTION);

gluPerspective( /* field of view in degree */ 120.0,

/* aspect ratio */ 1.0,

/* Z near */ 1.0, /* Z far */ 20.0);

glMatrixMode(GL_MODELVIEW);

gluLookAt(0.0, 0.0, 5.0, /* eye is at (0,0,5) */

0.0, 0.0, 0.0, /* center is at (0,0,0) */

0.0, 1.0, 0.); /* up is in positive Y direction */


/* Adjust cube position to be asthetic angle. */

//glTranslatef(0.0, 0.0, -1.0);

glRotatef(20, 1.0, 0.0, 0.0);

//glRotatef(-20, 0.0, 0.0, 1.0);

}


int keyboard(char key, int x, int y)

{

switch(key){

case 27: exit(0); //tecla de escape

break;

case 'h': shoulder=(shoulder+5)%360;

glutPostRedisplay();

break;

case 'H': shoulder=(shoulder-5)%360;

glutPostRedisplay();

break;

case 'c': joint=(joint+5)%360;

glutPostRedisplay();

break;

case 'C': joint=(joint-5)%360;

glutPostRedisplay();

break;

case 'm': wrist=(wrist+5)%360;

glutPostRedisplay();

break;

case 'M': wrist=(wrist-5)%360;

glutPostRedisplay();

break;

default: break;

}

glutPostRedisplay();

}

int main(int argc, char **argv)

{

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(500,500);

glutCreateWindow("Brazo robot rojo");

glutDisplayFunc(display);

glutKeyboardFunc(keyboard);

init();

glutMainLoop();

return 0; /* ANSI C requires main to return int. */

}