martes, 11 de marzo de 2008

PREVIO #4

// OLIVERA RODRÍGUEZ GUSTAVO

#include

GLfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0}; /* Red diffuse light. */
GLfloat light_position[] = {0.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. */

int hazPerspectiva = 0;

int shoulder=0;
int joint=0;
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 brazo(int shoulder, int joint, int wrist)
{
//Empieza el modelado jerarquico de un robot

glPushMatrix();
glTranslatef(0.0,-0.0,0.0);
glPushMatrix();
glScalef(5.0,2.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,1.5,0.0);
glPushMatrix();
glScalef(2.5,1.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,1.5,0.0);
glPushMatrix();
glScalef(5.0,2.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(-3.5,0.0,0.0);
glPushMatrix();
glScalef(2.0,1.0,2.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.0,0.0,0.0);
glPushMatrix();
glScalef(1.0,1.5,2.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.5,0.0,0.0);
glPushMatrix();
glScalef(2.5,1.0,2.0);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(3.5,0.0,0.0);
glPushMatrix();
glScalef(2.0,1.0,2.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(1.0,0.0,0.0);
glPushMatrix();
glScalef(1.0,1.5,2.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(1.5,0.0,0.0);
glPushMatrix();
glScalef(2.5,1.0,2.0);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,1.5,0.0);
glPushMatrix();
glScalef(1.0,1.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,2.0,0.0);
glPushMatrix();
glScalef(3.0,3.0,3.0);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-1.75,-3.5,0.0);
glPushMatrix();
glScalef(1.5,2.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,-2.0,0.0);
glPushMatrix();
glScalef(2.0,2.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,-2.0,0.0);
glPushMatrix();
glScalef(2.0,2.0,4.0);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(1.75,-3.5,0.0);
glPushMatrix();
glScalef(1.5,2.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,-2.0,0.0);
glPushMatrix();
glScalef(2.0,2.0,3.0);
drawBox();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0,-2.0,0.0);
glPushMatrix();
glScalef(2.0,2.0,4.0);
drawBox();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();
glPopMatrix();

}
display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
brazo(shoulder,joint,wrist);
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 */ 110.0,
/* aspect ratio */ 1.0,
/* Z near */ 1.0, /* Z far */ 20.0);
glMatrixMode(GL_MODELVIEW);
gluLookAt(0.0, 0.0, 10.0, /* eye is at (0,0,5) */
0.0, 0.0, 0.0, /* center is at (0,0,0) */
0.0, 1.0, 0.0); /* up is in positive Y direction */

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


int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("ROBOT");
glutDisplayFunc(display);
init();
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}

REPORTE DE PRÁCTICA #3

En está práctica aprendimos como usar las instrucciones glPushMatrix() y glPopMatrix() para manejar el stack y almacenar matrices de transformación para poder modelar de manera jerarquica un objeto.



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


//empieza el dibujo del primer brazo robot


glPushMatrix();


glTranslatef(-0.75,-0.5, 0.0);


glScalef(1.5,2.0,0.8);


drawBox();


glPopMatrix();


glPushMatrix();


glTranslatef(-0.75,1.0, 0.0);


drawBox();


glPopMatrix();


glPushMatrix();


//empieza el brazo


glRotatef(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(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(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(-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();

glPushMatrix();

glTranslatef(0.0,-1.0,0.0);

glPopMatrix();

//termina el segundo dedo

glPopMatrix();

//termina el grip

glPopMatrix();

//termina el dibujo del primer brazo robot

//empieza el brazo

glRotatef(shoulder, 0.0, 0.0, 1.0);

glTranslatef(-2.0, 0.0, 0.0);

glPushMatrix();

glScalef(2.0,0.5,0.5);

drawBox();

glPopMatrix();

//termina brazo

//empieza antebrazo

glTranslatef(-3.0, 0.0, 0.0);

glRotatef(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(wrist,0.0,0.0,1.0);

glTranslatef(-2.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(-wrist,0.0,0.0,1.0);

glTranslatef(-2.35,-0.125,0.0);

glPushMatrix();

glScalef(0.7,0.25,0.25);

drawBox();

glPopMatrix();

glPopMatrix();

glPushMatrix();

glTranslatef(0.0,-1.0,0.0);

glPopMatrix();

//termina el segundo dedo

glPopMatrix();

//termina el grip

glPopMatrix();

//termina el dibujo del segundo brazo robot



El fragmento de codigo anterior es un ejemplo claro de como podemos utilizar el stack para modelar jerarquicamente