Código:
#include <windows.h>
#include <C:\GLUT\include\GL\glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
/** Descripcion: Humanoide con materiales
*
* Autores:
* Edgar Villa Miguel.
* Fecha de creacion: 25/04/17
* Revisiones: ---
* programa.
* Fecha de ultima actualizacion: 30/04/17
*/
int angx = 0, angy = 0; // Con estas se esta rotando sobre el eje x y sobre el eje y
/* Prototipos de las funciones
tipo-valor-retorno nombre-funcion ( lista-parametros )*/
void iluminacion (int encendido); // Establece el tipo de iluminación que se tendrá
void aplicarMaterialOro(int encendido); // Establece un material tipo oro para usarse
void aplicarMaterialLaton(int encendido); // Establece un material tipo latón para usarse
void display( void ); // Dibuja los elementos sobre la pantalla
void proy( unsigned char key, int x, int y); // Controla la interacción/respuesta entre el teclado y la ejecución del programa
void girar(int key, int x, int y); // Controla la interacción/respuesta entre el teclado y la ejecución del programa con teclas especiales
/**
* Inicia la ejecución del programa
*/
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH|GLUT_STENCIL);
glutInitWindowSize( 400, 800);
glutInitWindowPosition( 50, 50);
glutCreateWindow ("Humanoide con materiales");
/* 1.- La luz default es una luz direccional desde la camara */
glEnable(GL_DEPTH_TEST); // Habilita un test de profundidad
//glEnable(GL_LIGHTING); // Habilitar iluminacion
//glEnable(GL_LIGHT0);
glMatrixMode(GL_PROJECTION); // Se apila una proyección, indica que se va a tener un tipo de proyección y abajo se indica en el caso p y o
glLoadIdentity();
gluPerspective( 90, 0.5, 0.1, 100);
glutDisplayFunc(display);
glutKeyboardFunc(proy);
glutSpecialFunc(girar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutMainLoop();
return 0;
}
/**
* Estable el tipo de iluminación a usar
* @param encendido Para activar/desactivar la iluminación
*/
void iluminacion (int encendido)
{
if( encendido == 1 )
{
const GLfloat pos[4] = { 10, 10, 10}; //poner afuera
static GLfloat colorLuz[] = { 1, 1, 1, 1}; // El 1 es como iluminacion global, como el sol, si es 0 es como un foquito creo
// Lo anterior es una luz blanca
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, colorLuz); // La luz 0 va a ser difusa del color blanco, arriva
glLightfv(GL_LIGHT0, GL_POSITION, pos);
} else
{
glDisable(GL_LIGHTING);
}
}
/**
* Establece el tipo de material a usar, oro en este caso
* @param encendido Para activar/desactivar el uso del material
*/
void aplicarMaterialOro(int encendido)
{
if( encendido == 1 ) {
GLfloat mat_green_ambient[] = { 0.24725,0.1995,0.0745,0.75};//ambiente y la ultima es alfa transaparencia
GLfloat mat_green_diffuse[] = { 0.75164,0.60648,0.22648,0.75};//difuso
GLfloat mat_specular[] = { 0.628281,0.555802,0.366065,0.5};//especular r,g,b y alfa
GLfloat mat_shininess[] = { 0.4*128}; //128 mucho brillo y 0 poco brillo
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //se aplica solo en la cara frontal gl_front and back
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_green_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
}
}
/**
* Establece el tipo de material a usar, latón en este caso
* @param encendido Para activar/desactivar el uso del material
*/
void aplicarMaterialLaton(int encendido) //es color verde esmeralda
{
if(encendido==1) {
//estos primeros son los parametros
GLfloat mat_green_ambient[] = { 0.329412, 0.223529, 0,027451, 0.75};//ambiente y la ultima es alfa transaparencia
GLfloat mat_green_diffuse[] = { 0.780392, 0.568627, 0.113725, 0.75};//difuso
GLfloat mat_specular[] = { 0.992157, 0.941176, 0.807843, 0.5};//especular r,g,b y alfa
GLfloat mat_shininess[] = { 0.21794872*128}; //128 mucho brillo y 0 poco brillo
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //se aplica solo en la cara frontal gl_front and back
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_green_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_green_diffuse);
}
}
/**
* Dibuja los elementos sobre la pantalla
*/
void display( void )
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); // Aqui solo se le limpia el buffer de pixeles, ahora tambien se debe de hacer el buffer de profundidad que es siguiente
glColor3f( 0, 1, 0);
iluminacion(1);
glPushMatrix();
glTranslatef( 0, 0.5, -4);
glRotatef( angx, 1, 0, 0);
glRotatef( angy, 0, 1, 0);
// Cabeza
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( 0, 2.1, 0);
glScalef( 1, 1.2, 1 );
glutSolidSphere( 0.6, 28, 28 );
glPopMatrix();
// Cuerpo
glPushMatrix();
glTranslatef( 0, 0.2, 0);
glScalef( 1.2, 2.3, 0.8 );
glutSolidSphere( 0.6, 28, 28 );
glPopMatrix();
// Hombro derecho
glPushMatrix();
glTranslatef( 0.55, 1.2, 0);
glutSolidSphere( 0.21, 28, 28 );
glPopMatrix();
// Brazo derecho, vista usuario
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( 0.75, 0.73, 0);
glRotatef( -75, 0, 0, 1 );
glScalef( 4, 0.7, 0.7 );
glutSolidCube( 0.28 );
glPopMatrix();
// Union con antebrazo derecho
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( 0.86, 0.32, 0);
glutSolidSphere( 0.21, 28, 28 );
glPopMatrix();
// Antebrazo derecho
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( 0.86, 0.0, 0);
glScalef( 0.65, 2.8, 0.65 );
glutSolidCube( 0.28 );
glPopMatrix();
// Mano derecha
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( 0.86, -0.38, 0);
glScalef( 1, 2, 1 );
glutSolidSphere( 0.15, 28, 28 );
glPopMatrix();
// Hombro izquierdo ///////////////////////////
glPushMatrix();
glTranslatef( -0.55, 1.2, 0);
glutSolidSphere( 0.21, 28, 28 );
glPopMatrix();
// Brazo izquierdo, vista usuario
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( -0.75, 0.73, 0);
glRotatef( 75, 0, 0, 1 );
glScalef( 4, 0.7, 0.7 );
glutSolidCube( 0.28 );
glPopMatrix();
// Union con antebrazo izquierdo
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( -0.86, 0.32, 0);
glutSolidSphere( 0.21, 28, 28 );
glPopMatrix();
// Antebrazo izquierdo
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( -0.86, 0.0, 0);
glScalef( 0.65, 2.8, 0.65 );
glutSolidCube( 0.28 );
glPopMatrix();
// Mano izquierda
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( -0.86, -0.38, 0);
glScalef( 1, 2, 1 );
glutSolidSphere( 0.15, 28, 28 );
glPopMatrix();
// Pierna izquierda /////////////////////////////
glPushMatrix();
glTranslatef( -0.45, -1, 0);
glutSolidSphere( 0.26, 28, 28 );
glPopMatrix();
// Muslo izquierdo
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( -0.56, -1.6, 0);
glRotatef( -6, 0, 0, 1);
glScalef( 0.9, 3.2, 0.9);
glutSolidCube( 0.32 );
glPopMatrix();
// Rodilla
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( -0.62, -2.2, 0);
glutSolidSphere( 0.23, 28, 28 );
glPopMatrix();
// Pantorrilla izquierda
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( -0.62, -2.6, 0);
glScalef( 0.9, 3, 0.9);
glutSolidCube( 0.32 );
glPopMatrix();
// Pie izquierdo
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( -0.62, -3.35, 0);
glRotatef( 90, 0, 1, 0 );
glRotatef( -90, 1, 0, 0 );
glutSolidCone( 0.32, 0.6, 28, 28 );
glPopMatrix();
// Pierna derecha /////////////////////////////
glPushMatrix();
glTranslatef( 0.45, -1, 0);
glutSolidSphere( 0.26, 28, 28 );
glPopMatrix();
// Muslo derecho
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( 0.56, -1.6, 0);
glRotatef( 6, 0, 0, 1);
glScalef( 0.9, 3.2, 0.9);
glutSolidCube( 0.32 );
glPopMatrix();
// Rodilla derecha
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( 0.62, -2.2, 0);
glutSolidSphere( 0.23, 28, 28 );
glPopMatrix();
// Pantorrilla derecha
glPushMatrix();
aplicarMaterialLaton(1);
glTranslatef( 0.62, -2.6, 0);
glScalef( 0.9, 3, 0.9);
glutSolidCube( 0.32 );
glPopMatrix();
// Pie derecho
glPushMatrix();
aplicarMaterialOro(1);
glTranslatef( 0.62, -3.35, 0);
glRotatef( 90, 0, 1, 0 );
glRotatef( -90, 1, 0, 0 );
glutSolidCone( 0.32, 0.6, 28, 28 );
glPopMatrix();
glPopMatrix();
glFlush();
}
/**
* Controla la interacción/respuesta entre el teclado y la ejecución del programa
* Parámetros necesarios y obligatorios
* @param c Tecla que fue presionada del teclado, generada en código ASCII
* @param x Coordenada x del mouse cuando fue presionada la tecla respectiva
* @param y Coordenada y del mouse cuando fue presionada la tecla respectiva
*/
void proy( unsigned char key, int x, int y)
{
glMatrixMode(GL_PROJECTION); // Se apila una proyección, indica que se va a tener un tipo de proyección y abajo se indica en el caso p y o
glLoadIdentity();
switch (key){
case 27:
exit(0);
case 'p':
gluPerspective( 90, 0.5, 0.1, 100); // zNear es como desde donde se coloca la camara o mira de la persona, mientras que zFar es hasta donde verá y por tanto cuantos objetos se veran
break;
case 'o':
glOrtho( -2, 2, -4, 4, -2, 100);
break;
}
/* 2.- Se agrega una matriz para la presentacion de un modelo */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay();
}
/**
* Controla la interacción/respuesta entre el teclado y la ejecución del programa con teclas especiales
* Parámetros necesarios y obligatorios
* @param c Tecla que fue presionada del teclado, generada en código ASCII
* @param x Coordenada x del mouse cuando fue presionada la tecla respectiva
* @param y Coordenada y del mouse cuando fue presionada la tecla respectiva
*/
void girar(int key, int x, int y)
{
switch(key){
case GLUT_KEY_UP:
angx=angx+3;
break;
case GLUT_KEY_DOWN:
angx=angx-3;
break;
case GLUT_KEY_LEFT:
angy=angy+3;
break;
case GLUT_KEY_RIGHT:
angy=angy-3;
break;
}
glutPostRedisplay();
}








