Dentro de las ventahas esta de que los efectos sobre objetos relacionados se propaguen automáticamente a través de ciertos grupos. Esté método (jerárquico) es muy útil cuando se asocia a transformaciones geométricas ya que sería demasiado complicado rotar, trasladar y escalar una y otra vez algunos objetos que están relacionados, en vez de esto es más fácil encontrar la relación que existe entre dichos objetos y solo tener que hacer pocas transformaciones para lograr los resultados deseados.
2. Escriba un programa que construya de manera jerárquica una escena en la que haya 3 humanoides.
void init(void){
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void cubohum(void){
glBegin(GL_LINE_LOOP);
glVertex3f(0.5,0.5,0.5);
glVertex3f(-0.5,0.5,0.5);
glVertex3f(-0.5,-0.5,0.5);
glVertex3f(0.5,-0.5,0.5);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5,0.5,-0.5);
glVertex3f( 0.5,0.5,-0.5);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5,0.5,0.5);
glVertex3f(-0.5,0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,0.5);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5,0.5,0.5);
glVertex3f(0.5,-0.5,0.5);
glVertex3f( 0.5,-0.5,-0.5);
glVertex3f(0.5,0.5,-0.5);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5,0.5,-0.5);
glVertex3f(-0.5,0.5,0.5);
glVertex3f(0.5,0.5,0.5);
glVertex3f( 0.5,0.5,-0.5);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,-0.5);
glVertex3f(-0.5,-0.5,0.5);
glVertex3f(0.5,-0.5,0.5);
glEnd();
}
void torso (int posx,int posy,int posz){
glTranslatef(0.0,1.5,0.0);
glScalef (0.5, 0.5, 0.5);
glRotatef(posx,1.0,0.0,0.0);
glRotatef(posy,0.0,1.0,0.0);
glRotatef(posz,0.0,0.0,1.0);
glTranslatef(0.0,0.0,0.0);
glPushMatrix();
glScalef (2.0, 3.0, 1.0);
cubohum();
glPopMatrix();
}
void rostro (int cuey){
glPushMatrix();
glRotatef(cuey,0.0,1.0,0.0);
glTranslatef(0.0,2.0,0.0);
cubohum();
glPopMatrix();
}
void brazoder (int hombrodx,int hombrody,int hombrodz,int cododz){
glPushMatrix();
glTranslatef(1.0,1.0,0.0);
glRotatef(hombrodx,1.0,0.0,0.0);
glRotatef(hombrody,0.0,1.0,0.0);
glRotatef(hombrodz,0.0,0.0,1.0);
glTranslatef(1.0,0.25,0.0);
glPushMatrix();
glScalef(2.0,0.5,1.0);
cubohum();
glPopMatrix();
glTranslatef(1.0,0.0,0.0);
glRotatef(cododz,0.0,0.0,1.0);
glTranslatef(1.0,0.0,0.0);
glPushMatrix();
glScalef(2.0,1.0,1.0);
cubohum();
glPopMatrix();
glTranslatef(1.25,0.0,0.0);
glPushMatrix();
glScalef(0.5,0.5,1.0);
cubohum();
glPopMatrix();
glPopMatrix();
}
void brazoizq (int hombroix,int hombroiy,int hombroiz,int codoiz){
glPushMatrix();
glTranslatef(-1.0,1.0,0.0);
glRotatef(hombroix,1.0,0.0,0.0);
glRotatef(hombroiy,0.0,1.0,0.0);
glRotatef(hombroiz,0.0,0.0,1.0);
glTranslatef(-1.0,0.25,0.0);
glPushMatrix();
glScalef(2.0,0.5,1.0);
cubohum();
glPopMatrix();
glTranslatef(-1.0,0.0,0.0);
glRotatef(codoiz,0.0,0.0,1.0);
glTranslatef(-1.0,0.0,0.0);
glPushMatrix();
glScalef(2.0,1.0,1.0);
cubohum();
glPopMatrix();
glTranslatef(-1.25,0.0,0.0);
glPushMatrix();
glScalef(0.5,0.5,1.0);
cubohum();
glPopMatrix();
glPopMatrix();
}
void piernader(int muslodx,int muslody,int muslodz,int rodilladx,int piedy){
glPushMatrix();
glTranslatef(0.665,-1.5,0.0);
glRotatef(muslodx,1.0,0.0,0.0);
glRotatef(muslody,0.0,1.0,0.0);
glRotatef(muslodz,0.0,0.0,1.0);
glTranslatef(0.0,-1.2,0.0);
glPushMatrix();
glScalef(0.68,2.4,1.0);
cubohum();
glPopMatrix();
glTranslatef(0.0,-1.2,0.0);
glRotatef(rodilladx,1.0,0.0,0.0);
glTranslatef(0.0,-1.2,0.0);
glPushMatrix();
glScalef(1.0,2.4,1.0);
cubohum();
glPopMatrix();
glTranslatef(0.0,0.-1.25,0.0);
glRotatef(piedy,0.0,1.0,0.0);
glTranslatef(0.25,-0.35,0.0);
glPushMatrix();
glScalef(1.5,0.75,1.0);
cubohum();
glPopMatrix();
glPopMatrix();
}
void piernaizq(int musloix,int musloiy,int musloiz,int rodillaix,int pieiy){
glPushMatrix();
glTranslatef(-0.665,-1.5,0.0);
glRotatef(musloix,1.0,0.0,0.0);
glRotatef(musloiy,0.0,1.0,0.0);
glRotatef(musloiz,0.0,0.0,1.0);
glTranslatef(0.0,-1.2,0.0);
glPushMatrix();
glScalef(0.68,2.4,1.0);
cubohum();
glPopMatrix();
glTranslatef(0.0,-1.2,0.0);
glRotatef(rodillaix,1.0,0.0,0.0);
glTranslatef(0.0,-1.2,0.0);
glPushMatrix();
glScalef(1.0,2.4,1.0);
cubohum();
glPopMatrix();
glTranslatef(0.0,0.-1.25,0.0);
glRotatef(pieiy,0.0,1.0,0.0);
glTranslatef(-0.25,-0.35,0.0);
glPushMatrix();
glScalef(1.5,0.75,1.0);
cubohum();
glPopMatrix();
glPopMatrix();
}
void humanoides (int px,int py,int pz,int cy,int hdx,int hdy,int hdz,int cdz,int hix,
int hiy,int hiz,int ciz,int mdx,int mdy,int mdz,int rdx,
int pdy,int mix,int miy,int miz,int rix,int piy)
{
torso(px,py,pz);
rostro(cy);
brazoder(hdx,hdy,hdz,cdz);
brazoizq(hix,hiy,hiz,ciz);
piernader(mdx,mdy,mdz,rdx,pdy);
piernaizq(mix,miy,miz,rix,piy);
}
/*Aqui se colocan los tres humanoides*/
void display(void){
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
gluLookAt (0.0,0.0,10.0,0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glScalef(0.5,0.5,0.5);
glRotatef(-45,0.0,1.0,0.0);
glTranslatef(5.0,0.0,2.0);
humanoides(posicionx,posiciony,posicionz,cuelloy,hombroderx,hombrodery,hombroderz,cododerz,
hombroizqx,hombroizqy,hombroizqz,codoizqz,musloderx,muslodery,musloderz,rodilladerx,
piedery,musloizqx,musloizqy,musloizqz,rodillaizqx,pieizqy);
glPopMatrix();
glPushMatrix();
glTranslatef(1.0,1.0,1.0);
glRotatef(15,1.0,0.0,0.0);
glScalef(0.5,-1.5,0.8);
humanoides(posicionx,posiciony,posicionz,cuelloy,hombroderx,hombrodery,hombroderz,cododerz,
hombroizqx,hombroizqy,hombroizqz,codoizqz,musloderx,muslodery,musloderz,rodilladerx,
piedery,musloizqx,musloizqy,musloizqz,rodillaizqx,pieizqy);
glPopMatrix();
glPushMatrix();
glRotatef(20,0.0,1.0,0.0);
glTranslatef(-2.0,-1.0,-3.0);
humanoides(posicionx,posiciony,posicionz,cuelloy,hombroderx,hombrodery,hombroderz,cododerz,
hombroizqx,hombroizqy,hombroizqz,codoizqz,musloderx,muslodery,musloderz,rodilladerx,
piedery,musloizqx,musloizqy,musloizqz,rodillaizqx,pieizqy);
glPopMatrix();
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);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE GLUT_RGB);
glutInitWindowSize (800, 800);
glutInitWindowPosition (90, 90);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
No hay comentarios:
Publicar un comentario