/** manosrobot.c
Este programa despliega dos manos humanoides formadas únicamente por cubos de diferentes tamaños, todos creados a partir de un cubo base y aplicando transformaciones de traslación y escalamiento. Dichas manos aparecen en una escena donde son capaces de abrirse y cerrarse por sí solas.*/
#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();
}
/*Variables globales utilizadas para los movimientos de las falanges de los dedos y dos variables auxiliares para dar un efecto adecuado a la animación.*/
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;
static int x = 0;
static int y = 0;
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
glPopMatrix();
glPopMatrix(); /*Dedo pulgar completo*/
}
void palma (float x, float y, float z, float ang, float ang2){ /*Función que despliega la palma.*/
glTranslatef(x,y,z);
glTranslatef(-0.25,0.0,0.0);
glRotatef(180,1.0,0.0,0.0);
glRotatef(180,0.0,1.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);
glRotatef(ang,0.0,1.0,0.0);
glRotatef(ang2,0.0,0.0,-1.0);
glPushMatrix();
glTranslatef(0.25,0.0,0.0);
glScalef (1.0,1.0,0.5);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
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);
cubohueco();
glPopMatrix();
glPopMatrix(); /*Índice completo*/
}
void display()
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity (); /* clear the matrix */
gluLookAt (0.0, 1.5, 3.5, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glScalef(-0.5,0.5,0.5);
/*Palma*/
palma(1.5,1.5,0.0,90.0,90.0);
/*Pulgar*/
pulgar();
/*Dedos (índice, medio, anular, meñique)*/
dedos();
glPopMatrix();/*Mano completa*/
glPushMatrix();
glScalef(0.5,0.5,0.5);
/*Palma*/
palma(1.5,1.5,0.0,90.0,90.0);
/*Pulgar*/
pulgar();
/*Dedos (índice, medio, anular, meñique)*/
dedos();
glPopMatrix();
glFlush ();
}
void idle() /*Función para animar las manos.*/
{
for(x = 0; x <>
{
}
if (y <>
{
pulgfalmay = pulgfalmay + 5;
pulgfalmedx = pulgfalmedx + 5;
pulgfalmen = pulgfalmen + 5;
menfalmay = menfalmay + 5;
anufalmay = anufalmay + 5;
medfalmay = medfalmay + 5;
indfalmay = indfalmay + 5;
y = y + 5;
}
else if (y <>
{
pulgfalmay = pulgfalmay - 5;
pulgfalmedx = pulgfalmedx - 5;
pulgfalmen = pulgfalmen - 5;
menfalmay = menfalmay - 5;
anufalmay = anufalmay - 5;
medfalmay = medfalmay - 5;
indfalmay = indfalmay - 5;
y = y +5;
}
if (y == 180)
{
y = 0;
}
display();
}
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 'p':
pulgfalmay = (pulgfalmay+5)%90;
pulgfalmedx = (pulgfalmedx+5)%90;
pulgfalmen = (pulgfalmen+5)%90;
glutPostRedisplay();
break;
case 'a':
pulgfalmay = (pulgfalmay+5)%90;
glutPostRedisplay();
break;
case 'h':
menfalmay = (menfalmay+5)%90;
glutPostRedisplay();
break;
case 'j':
anufalmay = (anufalmay+5)%90;
glutPostRedisplay();
break;
case 'k':
medfalmay = (medfalmay+5)%90;
glutPostRedisplay();
break;
case 'l':
indfalmay = (indfalmay+5)%90;
glutPostRedisplay();
break;
case 'x':
munecax = (munecax+5)%360;
glutPostRedisplay();
break;
case 'y':
munecay = (munecay+5)%360;
glutPostRedisplay();
break;
case 'z':
munecaz = (munecaz+5)%360;
glutPostRedisplay();
break;
case 'v':
pulgfalmedy = (pulgfalmedy+5)%90;
glutPostRedisplay();
break;
case 'c':
pulgfalmedx = (pulgfalmedx+5)%90;
glutPostRedisplay();
break;
case 'b':
pulgfalmen = (pulgfalmen+5)%90;
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 ();
glutIdleFunc(idle);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}