lunes, 12 de marzo de 2012

CODIGO CUBO 3D

 // cubolulu.cpp : Defines the entry point for the console application.
//

#include <GL/glut.h>
GLfloat anguloCuboX = 0.0f;
GLfloat anguloCuboY = 0.0f;
GLfloat anguloEsfera = 0.0f;
GLfloat x,y,z, ang, paso, dtx, dtz,pasoc,pasoa;
GLfloat pasob=-0.5;
GLint ancho=400;
GLint alto=400;
GLfloat trasy=0.0;
GLfloat a,b,c=2;

int hazPerspectiva = 0;
void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  glOrtho(-4,4, -4, 4, 1, 10);
  glMatrixMode(GL_MODELVIEW);
  ancho = width;
  alto = height;
}
void dibujarContorno()
{
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);  //cara frontal
    glVertex3f(a-1.0f, b-1.0f,  c+1.0f);
    glVertex3f(a+ 1.0f,b-1.0f, c+1.0f);
    glVertex3f( a+1.0f, b+1.0f,  c+1.0f);
    glVertex3f(a-1.0f,  b+1.0f,  c+1.0f);
 glEnd();
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);       //cara trasera
    glVertex3f( a+1.0f, b-1.0f, c-1.0f);
    glVertex3f(a-1.0f, b-1.0f, c-1.0f);
    glVertex3f(a-1.0f,  b+1.0f, c-1.0f);
    glVertex3f( a+1.0f,  b+1.0f, c-1.0f);
    glEnd();
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);      //cara lateral izq
    glVertex3f(a-1.0f,b-1.0f, c-1.0f);
    glVertex3f(a-1.0f,b-1.0f,  c+1.0f);
    glVertex3f(a-1.0f, b+1.0f,  c+1.0f);
    glVertex3f(a-1.0f, b+1.0f, c-1.0f);
    glEnd();
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);       //cara lateral dcha
    glVertex3f(a+1.0f, b-1.0f,  c+1.0f);
    glVertex3f(a+1.0f, b-1.0f, c-1.0f);
    glVertex3f(a+1.0f,  b+1.0f, c-1.0f);
    glVertex3f(a+1.0f,  b+1.0f,  c+1.0f);
    glEnd();
      glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);       //cara arriba
    glVertex3f(a-1.0f, b+1.0f,  c+1.0f);
    glVertex3f( a+1.0f, b+1.0f,  c+1.0f);
    glVertex3f( a+1.0f, b+1.0f, c-1.0f);
    glVertex3f(a-1.0f, b+1.0f, c-1.0f);
    glEnd();
    glColor3f(0.0f, 0.0f, 0.0f);
    glBegin(GL_LINE_STRIP);       //cara abajo
    glVertex3f( a+1.0f,b-1.0f, c-1.0f);
    glVertex3f( a+1.0f,b-1.0f,  c+1.0f);
    glVertex3f(a-1.0f,b-1.0f,  c+1.0f);
    glVertex3f(a-1.0f,b-1.0f, c-1.0f);
    glEnd();
}
void dibujarCube()
{
    glColor3f(0.0f, 1.0f, 0.0f);
    glBegin(GL_POLYGON);  //cara frontal
    glVertex3f(a-1.0f, b-1.0f,  c+1.0f);
    glVertex3f(a+ 1.0f,b-1.0f, c+1.0f);
    glVertex3f( a+1.0f, b+1.0f,  c+1.0f);
    glVertex3f(a-1.0f,  b+1.0f,  c+1.0f);
 glEnd();
    glColor3f(1.0f, 0.0f, 0.0f);
    glBegin(GL_POLYGON);       //cara trasera
    glVertex3f( a+1.0f, b-1.0f, c-1.0f);
    glVertex3f(a-1.0f, b-1.0f, c-1.0f);
    glVertex3f(a-1.0f,  b+1.0f, c-1.0f);
    glVertex3f( a+1.0f,  b+1.0f, c-1.0f);
    glEnd();
    glColor3f(1.0f, 0.0f, 1.0f);
 glBegin(GL_POLYGON);       //cara lateral izq
    glVertex3f(a-1.0f,b-1.0f, c-1.0f);
    glVertex3f(a-1.0f,b-1.0f,  c+1.0f);
    glVertex3f(a-1.0f, b+1.0f,  c+1.0f);
    glVertex3f(a-1.0f, b+1.0f, c-1.0f);
    glEnd();
    glColor3f(1.0f, 0.0f, 1.0f);
 glBegin(GL_POLYGON);       //cara lateral dcha
    glVertex3f(a+1.0f, b-1.0f,  c+1.0f);
    glVertex3f(a+1.0f, b-1.0f, c-1.0f);
    glVertex3f(a+1.0f,  b+1.0f, c-1.0f);
    glVertex3f(a+1.0f,  b+1.0f,  c+1.0f);
    glEnd();
    glColor3f(1.0f,0.6f, 1.0f);
 glBegin(GL_POLYGON);       //cara arriba
    glVertex3f(a-1.0f, b+1.0f,  c+1.0f);
    glVertex3f( a+1.0f, b+1.0f,  c+1.0f);
    glVertex3f( a+1.0f, b+1.0f, c-1.0f);
    glVertex3f(a-1.0f, b+1.0f, c-1.0f);
    glEnd();
 glColor3f(1.0f, 1.0f, 0.0f);
    glBegin(GL_POLYGON);       //cara abajo
    glVertex3f( a+1.0f,b-1.0f, c-1.0f);
    glVertex3f( a+1.0f,b-1.0f,  c+1.0f);
    glVertex3f(a-1.0f,b-1.0f,  c+1.0f);
    glVertex3f(a-1.0f,b-1.0f, c-1.0f);
    glEnd();
}
void gldrawCube()
{
    float xMark = -1.0f;
    float yMark = -1.0f;
    float zMark = 1.0f;
    for (int i = 0; i < 2; i++)
    //moves back one unit on z-axis
    {
        for (int j = 0; j < 2; j++)
        //moves up one unit on y-axis
        {
            for (int k = 0; k < 2; k++)
            //builds 3 cubes along x-axis
            {
                dibujarCube();
    dibujarContorno();
    a=xMark;
    b=yMark;
    c=zMark;
                xMark++;
            }
            xMark = -1.0f;
            yMark++;
        }
        yMark = -1.0f;
        zMark--;
    }
}

void display()
{  
 glShadeModel(GL_SMOOTH);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 glLoadIdentity();
 glTranslatef(0.0f, 0.0f, -5.0f);
 glRotatef(anguloCuboX, 1.0f, 0.0f, 0.0f);
    glRotatef(anguloCuboY, 0.0f, 1.0f, 0.0f);
 //glScalef(2.0f,2.0f,2.0f);
 gldrawCube();
 glLoadIdentity();
 glTranslatef(0.0f, 0.0f, -5.0f);
   // glRotatef(anguloEsfera, 1.0f, 0.0f, 0.0f);
 glColor3f(1.0f, 1.0f, 1.0f);   
 glFlush();
    glutSwapBuffers();
 anguloCuboX+=0.1f;
    anguloCuboY+=0.1f;
  //  anguloEsfera+=0.2f;
}
void init()
{
    glClearColor(0,0,0,0);
    glEnable(GL_DEPTH_TEST);
    ancho = 400;
    alto = 400;
}
void idle()
{
    display();
}
 void specialkeyevent( int key,  int Xx, int Yy )
{
 //float angrad;
  switch ( key )
  {
  case GLUT_KEY_UP:
   if(pasoa<.55)
    pasoa=pasoa+0.5;
    break;
   case GLUT_KEY_DOWN:
    if(pasoa>=-0.4)
    pasoa=pasoa-0.5;
   break;
  case GLUT_KEY_LEFT:
   if(pasoc>-.55)
    pasoc=pasoc-.5;
   break;
  case GLUT_KEY_RIGHT:
   if(pasoc<0.5)
    pasoc=pasoc+.5;
   break;
  }
   glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y)

 switch(key)  
 {
  case 'z':
   if(pasob<.1)
    pasob+=0.5;
   break;
  case 'Z':
   if(pasob>-.1)
    pasob-=0.5;
   break;
 }
 glutPostRedisplay();
}

 int main(int argc, char **argv)
 {
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  glutInitWindowPosition(100, 100);
  glutInitWindowSize(ancho, alto);
  glutCreateWindow("Cubo Rubik");
  init();
  glutDisplayFunc(display);
  glutReshapeFunc(reshape);
  glutIdleFunc(idle);
  glutKeyboardFunc(keyboard);
  glutSpecialFunc( specialkeyevent );
  glutMainLoop();
  return 0;
 }

No hay comentarios:

Publicar un comentario