miércoles, 14 de marzo de 2012

Cubo en 3D

#include <gl/glut.h>
GLfloat angr, delta;
GLfloat z;

void idleevent()
{
 angr += delta;
 glutPostRedisplay();
}
void displayevent(void)
{
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glEnable( GL_DEPTH_TEST );
 glLoadIdentity();
 glTranslatef( -0.5, -0.5, z );
 glRotatef( angr, 0,1,0 );
 glColor3f(0,0,1);
 glBegin( GL_QUADS ); // frontal
  glVertex3f( 0,  0,  0 );
  glVertex3f( 1,  0,  0 );
  glVertex3f( 1,  1,  0 );
  glVertex3f( 0,  1,  0 );
 glEnd();
 glColor3f(0,1,0);
 glBegin( GL_QUADS ); // lateral derecha
  glVertex3f( 1, 0,  0 );
  glVertex3f( 1, 0, -1 );
  glVertex3f( 1, 1, -1 );
  glVertex3f( 1, 1,  0 );
 glEnd();
 glColor3f(1,0,0);
 glBegin( GL_QUADS ); // superior
  glVertex3f( 0, 1,  0 );
  glVertex3f( 1, 1,  0 );
  glVertex3f( 1, 1, -1 );
  glVertex3f( 0, 1, -1 );
 glEnd();
 glColor3f(0,1,1);
 glBegin ( GL_QUADS ); //lateral izquierda
  glVertex3f(  0,  0,  0 );
  glVertex3f(  0,  0, -1 );
  glVertex3f(  0,  1, -1 );
  glVertex3f(  0,  1,  0 );
 glEnd();
 glColor3f(1,0,1);
 glBegin ( GL_QUADS ); //tracera
  glVertex3f( 0,  0, -1 );
  glVertex3f( 1,  0, -1 );
  glVertex3f( 1,  1, -1 );
  glVertex3f( 0,  1, -1 );
 glEnd();
 glColor3f(1,1,0);
 glBegin ( GL_QUADS ); //inferior
  glVertex3f( 0,  0,  0 );
  glVertex3f( 1,  0,  0 );
  glVertex3f( 1,  0, -1 );
  glVertex3f( 0,  0, -1 );
 glEnd();
 glutSwapBuffers();
}
void specialkeyevent( int key, int Xx, int Yy )
{

 switch ( key ) {
 case GLUT_KEY_UP:    z += 0.1;
  break;
 case GLUT_KEY_DOWN:  z -= 0.1;
  break;
  case GLUT_KEY_F1:    delta += 0.1;
  break;
 case GLUT_KEY_F2:    delta -= 0.1;
  break;
 }
 glutPostRedisplay();
}
void reshapeevent(GLsizei width, GLsizei height)
{
 glViewport(0,0,width,height);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 gluPerspective(40,(GLfloat)width/(GLfloat)height,  0.01, 20);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}

int main(int argc, char** argv)
{
 glutInit( &argc, argv );
 glutInitWindowSize( 300, 300 );
 glutInitWindowPosition( 100, 100 );
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
    glutCreateWindow( "" );
 glutSetWindowTitle( "Cubo" );
 delta  = 0;
 angr  = 0;
 z = -5;
    glutReshapeFunc (reshapeevent);
 glutDisplayFunc( displayevent );
 glutSpecialFunc( specialkeyevent );
 glutIdleFunc( idleevent );
 glutMainLoop();
 return 0;
}

No hay comentarios:

Publicar un comentario