lunes, 15 de abril de 2013

FIGURA GEOMETRICA EN 3D

CODIGO:

#include "stdafx.h"
#include <iostream>
#include <gl\glut.h>

GLfloat x=0.0,y=0.0,z=0.0,angulo=0.0,an=0.0,angu=0.0;
int actual=0;


float ver1[]={0,0,0};
float ver2[]={0,0,4};
float ver3[]={4,0,4};
float ver4[]={4,0,0};
float ver5[]={2,6,2};


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1,1,1);
gluLookAt(0, 5, 15,     0, 0, 0,     0, 1, 0);

glTranslatef(x,y,z);
glRotatef(angulo,0,1,0);
glRotatef(an,1,0,0);
glRotatef(angu,0,0,1);

glBegin(GL_TRIANGLES);//cara derecha
glColor3f(1,0,1);//rojo y azul
glVertex3fv(ver3);
glVertex3fv(ver4);
glVertex3fv(ver5);
//cara trasera
glColor3f(1,0,0);//rojo
glVertex3fv(ver1);
glVertex3fv(ver4);
glVertex3fv(ver5);
//cara frontal
glColor3f(0,0,1);//azul
glVertex3fv(ver2);
glVertex3fv(ver3);
glVertex3fv(ver5);
//cara izquierda
glColor3f(1,1,0);//rojo y verde
glVertex3fv(ver1);
glVertex3fv(ver2);
glVertex3fv(ver5);
glEnd();
//cara base
glBegin(GL_QUADS);
glColor3f(0,1,1);//verde y azul
glVertex3fv(ver1);
glVertex3fv(ver2);
glVertex3fv(ver3);
glVertex3fv(ver4);
glEnd();

glFlush();
   glutSwapBuffers(); //Se hace el cambio de buffer
}

void Redimensionar(GLsizei ancho, GLsizei alto)
{
if(alto == 0)
{
 alto = 1;
}
 
glViewport(0, 0, ancho, alto);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
if(ancho>alto)
{
gluPerspective(45.0f,ancho/alto,1,150.0f);
}
else
{
gluPerspective(45.0f,alto/ancho,1,150.0f);
}
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void teclado(unsigned char key,int inX, int inY)
{
if(key == 'w')
{
y=y+0.2f;
glutPostRedisplay();
}
if(key == 's')
{
y=y-0.2f;
glutPostRedisplay();
}
if(key == 'a')
{
x=x+0.2f;
glutPostRedisplay();
}
if(key == 'd')
{
x=x-0.2f;
glutPostRedisplay();
}
if(key == 'q')
{
z=z+0.2f;
glutPostRedisplay();
}
if(key == 'z')
{
z=z-0.2f;
glutPostRedisplay();
}
if(key == 'r')
{
angulo=angulo+10;
glutPostRedisplay();
}
if(key == 'f')
{
angulo=angulo-10;
glutPostRedisplay();
}
if(key == 't')
{
an=an+10;
glutPostRedisplay();
}
if(key == 'y')
{
an=an-10;
glutPostRedisplay();
}
if(key == 'g')
{
angu=angu+10;
glutPostRedisplay();
}
if(key == 'h')
{
angu=angu-10;
glutPostRedisplay();
}
}

void myinit()
{
/*glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);*/
glClearColor(0.0, 1.0, 1.0, 1.0);

}

void InicializarGL(void)
{
glEnable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
}

void main(void)
{
glutInitWindowSize(640,480);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //En este punto se hace la solicitud de doble buffer
glutInitWindowPosition(50,50);
glutCreateWindow("PIRAMIDE");
glutDisplayFunc(display);
    glutReshapeFunc(Redimensionar);
glutKeyboardFunc(teclado);
InicializarGL();
    glutMainLoop();
myinit();
glutDestroyWindow(glutGetWindow());
}

ROTACION:




TRASLACION:





ESCALACION:


No hay comentarios:

Publicar un comentario