Programa que dibuja a la hormiga jerarquicamente en OPenGL:
#include
#include
float Ang=0;
void Idle(void);
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void cubo(void) //construcción del cubo , cara por cara
{
glBegin(GL_LINE_LOOP);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f( 0.5f,0.5f,-0.5f);
glVertex3f(0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);
glVertex3f(0.5f,0.5f,-0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f( 0.5f,0.5f,-0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glEnd();
}
void pataHormiga(void)
{
glPushMatrix(); //parte 1
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
glPushMatrix(); //parte 2
glTranslatef(0.3,0,0);
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
glPushMatrix(); //parte 3
glTranslatef(0.6,0,0);
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
glPushMatrix(); //parte 4
glTranslatef(0.9,0,0);
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glColor3f (1.0, 0.0, 0.0); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (0.5, 0.8, 0.2); /* modeling transformation */
glRotatef(Ang,1.0f,1.0f,1.0f);
glPushMatrix(); //caja central de la hormiga
glScalef (0.8, 0.5, 0.5);
cubo();
glPopMatrix();
glPushMatrix(); //cabeza
glTranslatef(0,0.4,0);
glScalef (0.3, 0.3, 0.5);
cubo();
glPopMatrix();
glPushMatrix(); //torzo
glTranslatef(0,-0.7,0);
glScalef (0.3, 0.8, 0.5);
cubo();
glPopMatrix();
glPushMatrix(); //pata 1 derecha
glTranslatef(0.3,-0.4,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 1 izq
glTranslatef(-1.2,-0.4,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 2 derecha
glTranslatef(0.3,-0.6,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 2 izquierda
glTranslatef(-1.2,-0.6,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 3 derecha
glTranslatef(0.3,-0.8,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 3 izquierda
glTranslatef(-1.2,-0.8,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //caja final de la hormiga
glTranslatef(0,-1.3,0);
glScalef (0.8, 0.4, 0.5);
cubo();
glPopMatrix();
glutSwapBuffers();
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 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (700, 700);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
// glutDisplayFunc(Render);
glutIdleFunc(Idle);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void Idle(void)
{
Ang++;
display();
}
#include
float Ang=0;
void Idle(void);
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void cubo(void) //construcción del cubo , cara por cara
{
glBegin(GL_LINE_LOOP);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f( 0.5f,0.5f,-0.5f);
glVertex3f(0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glVertex3f( 0.5f,-0.5f,-0.5f);
glVertex3f(0.5f,0.5f,-0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(-0.5f,0.5f,-0.5f);
glVertex3f(-0.5f,0.5f,0.5f);
glVertex3f(0.5f,0.5f,0.5f);
glVertex3f( 0.5f,0.5f,-0.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,-0.5f);
glVertex3f(-0.5f,-0.5f,0.5f);
glVertex3f(0.5f,-0.5f,0.5f);
glEnd();
}
void pataHormiga(void)
{
glPushMatrix(); //parte 1
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
glPushMatrix(); //parte 2
glTranslatef(0.3,0,0);
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
glPushMatrix(); //parte 3
glTranslatef(0.6,0,0);
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
glPushMatrix(); //parte 4
glTranslatef(0.9,0,0);
glScalef (0.3, 0.1, 0.3);
cubo();
glPopMatrix();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
//glColor3f (1.0, 0.0, 0.0); /* clear the matrix */
/* viewing transformation */
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef (0.5, 0.8, 0.2); /* modeling transformation */
glRotatef(Ang,1.0f,1.0f,1.0f);
glPushMatrix(); //caja central de la hormiga
glScalef (0.8, 0.5, 0.5);
cubo();
glPopMatrix();
glPushMatrix(); //cabeza
glTranslatef(0,0.4,0);
glScalef (0.3, 0.3, 0.5);
cubo();
glPopMatrix();
glPushMatrix(); //torzo
glTranslatef(0,-0.7,0);
glScalef (0.3, 0.8, 0.5);
cubo();
glPopMatrix();
glPushMatrix(); //pata 1 derecha
glTranslatef(0.3,-0.4,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 1 izq
glTranslatef(-1.2,-0.4,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 2 derecha
glTranslatef(0.3,-0.6,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 2 izquierda
glTranslatef(-1.2,-0.6,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 3 derecha
glTranslatef(0.3,-0.8,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //pata 3 izquierda
glTranslatef(-1.2,-0.8,0);
pataHormiga();
glPopMatrix();
glPushMatrix(); //caja final de la hormiga
glTranslatef(0,-1.3,0);
glScalef (0.8, 0.4, 0.5);
cubo();
glPopMatrix();
glutSwapBuffers();
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 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (700, 700);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
// glutDisplayFunc(Render);
glutIdleFunc(Idle);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void Idle(void)
{
Ang++;
display();
}
No hay comentarios:
Publicar un comentario