Este programa despliega cuatro humanoides formados unicamente por cubos de diferetes tamaños, todos creados a apartir
de un cubo base y aplicando transformadas de traslacion y escalamiento*/
#include
#include
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void cuborelleno(void)
{
glBegin(GL_QUADS);
glVertex3f(0.5,0.5,0.5);//v1
glVertex3f(-0.5,0.5,0.5);//v2
glVertex3f(-0.5,-0.5,0.5);//v3
glVertex3f(0.5,-0.5,0.5);//v4
glEnd();
glBegin(GL_QUADS);
glVertex3f(-0.5,0.5,-0.5);//v5
glVertex3f( 0.5,0.5,-0.5);//v6
glVertex3f(0.5,-0.5,-0.5);//v7
glVertex3f(-0.5,-0.5,-0.5);//v8
glEnd();
glBegin(GL_QUADS);
glVertex3f(-0.5,0.5,0.5);//v2
glVertex3f(-0.5,0.5,-0.5);//v5
glVertex3f(-0.5,-0.5,-0.5);//v8
glVertex3f(-0.5,-0.5,0.5);//v3
glEnd();
glBegin(GL_QUADS);
glVertex3f(0.5,0.5,0.5);//v1
glVertex3f(0.5,-0.5,0.5);//v4
glVertex3f( 0.5,-0.5,-0.5);//v7
glVertex3f(0.5,0.5,-0.5);//v6
glEnd();
glBegin(GL_QUADS);
glVertex3f(-0.5,0.5,-0.5);//v5
glVertex3f(-0.5,0.5,0.5);//v2
glVertex3f(0.5,0.5,0.5);//v1
glVertex3f( 0.5,0.5,-0.5);//v6
glEnd();
glBegin(GL_QUADS);
glVertex3f(0.5,-0.5,-0.5);//v7
glVertex3f(-0.5,-0.5,-0.5);//v8
glVertex3f(-0.5,-0.5,0.5);//v3
glVertex3f(0.5,-0.5,0.5);//v4
glEnd();
}
void cubohueco(void)
{
glBegin(GL_LINE_LOOP);
glVertex3f(0.5,0.5,0.5);//v1
glVertex3f(-0.5,0.5,0.5);//v2
glVertex3f(-0.5,-0.5,0.5);//v3
glVertex3f(0.5,-0.5,0.5);//v4
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5,0.5,-0.5);//v5
glVertex3f( 0.5,0.5,-0.5);//v6
glVertex3f(0.5,-0.5,-0.5);//v7
glVertex3f(-0.5,-0.5,-0.5);//v8
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5,0.5,0.5);//v2
glVertex3f(-0.5,0.5,-0.5);//v5
glVertex3f(-0.5,-0.5,-0.5);//v8
glVertex3f(-0.5,-0.5,0.5);//v3
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5,0.5,0.5);//v1
glVertex3f(0.5,-0.5,0.5);//v4
glVertex3f( 0.5,-0.5,-0.5);//v7
glVertex3f(0.5,0.5,-0.5);//v6
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5,0.5,-0.5);//v5
glVertex3f(-0.5,0.5,0.5);//v2
glVertex3f(0.5,0.5,0.5);//v1
glVertex3f( 0.5,0.5,-0.5);//v6
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5,-0.5,-0.5);//v7
glVertex3f(-0.5,-0.5,-0.5);//v8
glVertex3f(-0.5,-0.5,0.5);//v3
glVertex3f(0.5,-0.5,0.5);//v4
glEnd();
}
static int brazderrot = 0; /*Variable que indica sobre cual eje rotará el brazo derecho.*/
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 brazizqrot = 0; /*Variable que indica sobre cual eje rotará el brazo izquierdo.*/
static int angbrazizq = 0; /*Variable que indica el ángulo de rotación del brazo izquierdo*/
static int pierderrot = 0; /*Variable que indica sobre cual eje rotará la pierna derecha.*/
static int angpierder = 0; /*Variable que indica el ángulo de rotación de la pierna derecha.*/
static int pierizqrot = 0; /*Variable que indica sobre cual eje rotará la pierna izquierda.*/
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.*/
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);
cubohueco();
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;
}
glPushMatrix();
glScalef (2.0,3.0,1.0);
cubohueco();
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);
cubohueco();
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);
cubohueco();
glPopMatrix();
/*mano derecha*/
glTranslatef(2.25,0.0,0.0);
glPushMatrix();
glScalef(0.5,0.5,1.0);
cubohueco();
glPopMatrix();
glPopMatrix();/*brazo derecho completo*/
}
void brazoizq (){
glPushMatrix();
glTranslatef(-2.0,1.25,0.0);
glRotatef((GLfloat)angbrazizq,0.0,0.0,1.0);
glPushMatrix();
glScalef(2.0,0.5,1.0);
cubohueco();
glPopMatrix();
/*antebrazo izquierdo*/
glTranslatef(-2.0,0.0,0.0);
glPushMatrix();
glScalef(2.0,1.0,1.0);
cubohueco();
glPopMatrix();
/*mano izquierdo*/
glTranslatef(-1.25,0.0,0.0);
glPushMatrix();
glScalef(0.5,0.5,1.0);
cubohueco();
glPopMatrix();
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);
cubohueco();
glPopMatrix();
/*pantorrilla derecha*/
glTranslatef(0.0,-1.5,0.0);
glPushMatrix();
glScalef(0.6,1.5,1.0);
cubohueco();
glPopMatrix();
/*pie derecho*/
glTranslatef(0.25,-0.85,0.0);
glPushMatrix();
glScalef(1.5,0.25,1.0);
cubohueco();
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);
cubohueco();
glPopMatrix();
/*pantorrilla izquierda*/
glTranslatef(0.0,-1.5,0.0);
glPushMatrix();
glScalef(0.6,1.5,1.0);
cubohueco();
glPopMatrix();
/*pie izquierdo*/
glTranslatef(-0.25,-0.85,0.0);
glPushMatrix();
glScalef(1.5,0.25,1.0);
cubohueco();
glPopMatrix();
glPopMatrix();/*pierna izquierda completa*/
}
void display()
{
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(0.5,0.5,0.5);
/*torso*/
tronco(5.0,0.0,2.0,1,-45.0);
/*cara*/
cabeza();
/*brazo derecho*/
brazoder();
/*brazo izquierdo*/
brazoizq();
/*muslo derecho*/
pierder();
/*muslo izquierdo*/
pierizq();
glPopMatrix();/*humanoide completo*/
glFlush ();
glScalef(0.5,1.5,0.8);
/*torso*/
tronco(1.0,1.0,1.0,0,15.0);
/*cara*/
cabeza();
/*brazo derecho*/
brazoder();
/*brazo izquierdo*/
brazoizq();
/*muslo derecho*/
pierder();
/*muslo izquierdo*/
pierizq();
glPopMatrix();/*humanoide completo*/
glFlush ();
glScalef(1.0,1.0,1.0);
/*torso*/
tronco(-2.0,-1.0,-3.0,1,20.0);
/*cara*/
cabeza();
/*brazo derecho*/
brazoder();
/*brazo izquierdo*/
brazoizq();
/*muslo derecho*/
pierder();
/*muslo izquierdo*/
pierizq();
glPopMatrix();/*humanoide completo*/
glFlush ();
glScalef(2.0,0.5,1.0);
/*torso*/
tronco(-3.0,1.0,2.0,2,45.0);
/*cara*/
cabeza();
/*brazo derecho*/
brazoder();
/*brazo izquierdo*/
brazoizq();
/*muslo derecho*/
pierder();
/*muslo izquierdo*/
pierizq();
glPopMatrix();/*humanoide completo*/
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);
}
/* ARGSUSED1 */
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 't':
torsoroty = (torsoroty + 5)%360;
glutPostRedisplay();
break;
case 'y':
torsorotx = (torsorotx + 5)%360;
glutPostRedisplay();
break;
case 'c':
cuellorot = (cuellorot + 1)%360;
glutPostRedisplay();
break;
case 'd':
angbrazder = (angbrazder + 5)%180;
glutPostRedisplay();
break;
case 's':
angcododer = (angcododer + 5)%180;
glutPostRedisplay();
break;
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (800, 800);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
No hay comentarios.:
Publicar un comentario