En computación gráfica, shading se refiere al proceso de alterar el color basado en el ángulo de incidencia de la luz y la distancia de la fuente de luz, lo anterior con el fin de producir efectos visuales parecidos a los que en la realidad se producen. El proceso de shading se lleva acabo durante el rendering.
2. ¿Qué es, como se declara y se utiliza en OpenGL/GLUT?
Una fuente de luz
Es el origen de donde provendrá la iluminación de nuestra escena en OpenGL. Para crear una fuente de luz primero se debe activar la iluminación en OpenGL esto se logra con la función glEnable(GL_LIGHTING) y después se debe activar una fuente de luz por separado con la posibilidad de tener hasta 8, esto con el comando glEnable(GL_LIGHTn) donde n es el índice de la luz que se quiere activar.
Un material
Es la forma en la que se va interactuar con la luz reflejando las diferentes componentes roja, verde y azul, en diferente intensidad.
Para declarar un material se utiliza la función:
void glMaterial{if}(GLenum face, GLenum pname, TYPE param);
En su primer parámetro recibe una bandera la cual indica que caras de nuestro objeto tendrán las características de nuestro material ya sean las caras frontales, las de atrás o todo el objeto, el siguiente parámetro es otra bandera que indica el tipo de atributo de nuestro material, ejemplo la componente difusa, la componente ambiental, el tercer parámetro es el color que tendrá ese atributo de nuestro material.
Una textura
En principio para trabajar con texturas se necesita poder cargar imágenes en el formato de nuestra elección. Ya que OpenGL no tiene implementado algún método para esto, se tiene que realizar esta tarea de manera independiente. Resuelto lo anterior se debe proceder a activar las texturas, esto se logra con glEnable(GL_TEXTURE_2D). Antes de poder asignar una textura a cualquier objeto es necesario que el objeto tenga asignadas sus propias coordenadas de textura. Después de cargar la imagen, se tiene que generar un identificador con el cual OpenGL hará referencia a dicha textura, esto se logra con la función void glGenTextures( GLsizei n, GLuint *texturas ). Con este identificador ahora podemos referenciar la textura para poder generarla y asignarle algunos atributos. Para generar la textura se utiliza glTexImage2D(), y glTexParameteri() para asignar algunos características.
3. ¿Cúales son los atributos y características que se pueden declarar en OpenGL/GLUT para manejo de?
Luces
Los principales atributos de una luz son la posición, la componente difusa que representa el color de la luz, la componente ambiental que es el color de la iluminación global, y la componente specular que es el color de la luz que será reflejada por los objetos.
Materiales
Las características de los materiales son la difusa que es el color mate del objeto, la componente ambiental que es la iluminación ambiental que afecta al color del objeto, la componente specular que será el color que reflejará y el brillo que representa la intensidad de la componente specular.
Texturas
En cuanto texturas algunos atributos referentes son los filtros que se utilizaran para tratar la imagen, texturas ambientales, texturas cúbicas, otro parámetro es el número de repeticiones de dicha textura, entre otros.
4. Escriba un programa donde:
Dibuje un cubo y asigne características de material que lo hagan verse como de madera:
Aplique una textura de madera al cubo (bitmap)
Aplique como textura su fotografía
El cambio de modo se hará con un clic de Mouse
#include
#include
#include
#include
#include
#include
#include
// Constantes -----------------------------------------------------------------
#define kWindowWidth 800
#define kWindowHeight 600
// Estructuras ----------------------------------------------------------------
typedef struct // Se crea una estructura
{
GLubyte *imageData; // Datos de la imagen
GLuint bpp; // Profundidad del color de la imagen en bits por pixel
GLuint width; // Ancho de la imagen
GLuint height; // Alto de la imagen
GLuint texID; // Numero identificador de la textura
} TextureImage; // Nombre de la estructura
// Prototipos -------------------------------------------------------
bool loadTGA(TextureImage *texture, char *filename);
GLvoid initGL(GLvoid);
GLvoid display(GLvoid);
GLvoid resize(int width, int height);
GLvoid mouse(int button,int state,int x,int y);
GLvoid idle(GLvoid);
GLvoid loadGLTextures(void);
GLvoid keyboard(unsigned char key, int x, int y);
void setMaterial( GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb,
GLfloat shine);
void inline drawString (char *s);
void setOrthographicProjection();
void resetPerspectiveProjection();
// Variables globales ----------------------------------------------------------
bool lp;
GLfloat xrot=45.0f;
GLfloat yrot=15.0f;
GLfloat z =- 10.0f;
GLfloat lightAmbient[] = { 0.4f,0.4f,0.4f,0.0f };
GLfloat lightDiffuse[] = { 1.0f,0.0f,0.0f,1.0f };
GLfloat lightSpecular[]= {1.0, 1.0, 1.0, 0.0};
GLfloat lightPosition[] = {0.0f, 40.0f, 0.0f, 1.0f };
GLfloat lmodelAmbient[] = {0.2, 0.2, 0.2, 1.0};
GLfloat localView[] = {0.0};
GLfloat fogColor[4]= {0.5f, 0.5f, 0.5f, 1.0f};
GLfloat spotDirection[] = { 0.0, -1.0, 0.0 };
TextureImage texture[10];
char *textureName[]={"crate1.tga","crate2.tga","crate3.tga","crate4.tga","crate5.tga"};
int cambiaTextura = 3;
static char label[100];
int w,h;
static int frame=0, timer = 0, timebase=0;
float fps;
float lpos[4] = { 0.0, 5.0, 0.0, 0.0};
// InitGL -------------------------------------------------------------------
GLvoid initGL(GLvoid)
{
loadGLTextures(); // Carga las texturas
glEnable(GL_TEXTURE_2D); // Habilita el mapeo de texturas
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glDepthMask(GL_TRUE);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient );
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse );
//glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 2.0);
//glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 1.0);
//glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.001);
//glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 0.2);
//glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 45.0);
//glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodelAmbient);
glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, localView);
glEnable(GL_FOG);
glFogfv(GL_FOG_COLOR,fogColor);
glFogf(GL_FOG_DENSITY,0.03f);
glFogi(GL_FOG_MODE, GL_EXP);
glFogf(GL_FOG_START, 0.0f);
glFogf(GL_FOG_END, 150.f);
}
// DrawGLScene -----------------------------------------------------
GLvoid display(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,z);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glLightfv(GL_LIGHT0,GL_POSITION,lpos);
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glTranslatef(lpos[0],lpos[1],lpos[2]);
glutWireSphere(0.2f,10,10);
glPopMatrix();
glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D, texture[cambiaTextura].texID);
glPushMatrix();
glRotatef(12,0,1,0);
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-2.5,0.0,0.0);
glRotatef(67,0,1,0);
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(2.5,0.0,0.0);
glRotatef(95,0,-1,0);
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();
glPushMatrix();
glRotatef(35,0,1,0);
glTranslatef(0.0,1.8,0.0);
glScalef(0.8,0.8,0.8);
glBindTexture(GL_TEXTURE_2D, texture[6].texID);
glBegin(GL_QUADS);
// Front Face
glNormal3f( 0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[8].texID);
glBegin(GL_QUADS);
// Top Face
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glNormal3f( 0.0f,-1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[7].texID);
glBegin(GL_QUADS);
// Back Face
glNormal3f( 0.0f, 0.0f,-1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Right face
glNormal3f( 1.0f, 0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glNormal3f(-1.0f, 0.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, texture[5].texID);
glColor3f(0.4,0.4,0.4);
glBegin(GL_QUADS);
glNormal3f( 0.0f, 1.0f, 0.0f);
glTexCoord2f(0.0, 1.0); glVertex3f(-5.0, -1.0, -5.0);
glTexCoord2f(0.0, 0.0); glVertex3f(-5.0, -1.0, 5.0);
glTexCoord2f(1.0, 0.0); glVertex3f(5.0, -1.0, 5.0);
glTexCoord2f(1.0, 1.0); glVertex3f(5.0, -1.0, -5.0);
glEnd();
glBindTexture(GL_TEXTURE_2D, texture[9].texID);
glBegin(GL_QUADS);
glNormal3f( 0.0f,0.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-5.0f, -1.0f, -5.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 5.0f, -1.0f, -5.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 5.0f, 6.0f, -5.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-5.0f, 6.0f, -5.0f);
glEnd();
glBegin(GL_QUADS);
glNormal3f( 0.0f,0.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( -5.0f, 6.0f, 5.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(5.0f, 6.0f, 5.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(5.0f, -1.0f, 5.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( -5.0f, -1.0f, 5.0f);
glEnd();
glBegin(GL_QUADS);
// Bottom Face
glNormal3f( -1.0f,0.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(5.0f, 6.0f, 5.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 5.0f, 6.0f, -5.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 5.0f, -1.0f, -5.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(5.0f, -1.0f, 5.0f);
glEnd();
glBegin(GL_QUADS);
// Bottom Face
glNormal3f( 1.0f,0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-5.0f, -1.0f, 5.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( -5.0f, -1.0f, -5.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( -5.0f, 6.0f, -5.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-5.0f, 6.0f, 5.0f);
glEnd();
setOrthographicProjection();
glPushMatrix();
glPushAttrib( GL_LIGHTING_BIT );
glPushAttrib( GL_TEXTURE_BIT );
glDisable(GL_TEXTURE_2D);
glDisable( GL_LIGHTING );
glLoadIdentity();
glColor3f(1.0f,0.0f,0.0f);
sprintf(label,"FPS: %5.2f",fps);
glRasterPos2f(30.0,30.0);
glColor3f(1.0f,1.0f,1.0f);
drawString (label);
glRasterPos2f(30.0,50.0);
drawString ("Controles");
glRasterPos2f(30.0,70.0);
drawString ("Iluminacion on/off: b");
glRasterPos2f(30.0,85.0);
drawString ("Cambio de texturas: clic izquierdo");
glRasterPos2f(30.0,100.0);
drawString ("Zoom in: w");
glRasterPos2f(30.0,115.0);
drawString ("Zoom out: s");
glPopAttrib();
glPopAttrib();
glPopMatrix();
resetPerspectiveProjection();
glutSwapBuffers();
glFlush();
}
// ReSizeGLScene ------------------------------------------------------------
void resize( int width , int height )
{
if (height==0)
{
height=1;
}
w=width;
h=height;
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,150.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Keyboard --------------------------------------------------------------
GLvoid keyboard(unsigned char Key, int x, int y)
{
#pragma unused (x, y)
switch(Key)
{
case 'b':
if (!lp)
{
lp = true;
glEnable(GL_LIGHTING);
}
else
{
lp = false;
glDisable(GL_LIGHTING);
}
break;
case 'w':
z+=2.0f;
break;
case 's':
z -= 2.0f;
break;
case 'q':
xrot -= 3.0f;
break;
case 'e':
xrot += 3.0f;
break;
case 'a':
yrot += 3.0f;
break;
case 'd':
yrot -= 3.0f;
break;
default:
break;
}
if (Key == 'i')
lpos[2] += 0.2f;
if (Key == 'k')
lpos[2] -= 0.2f;
if (Key == 'l')
lpos[0] += 0.2f;
if (Key == 'j')
lpos[0] -= 0.2f;
if (Key == 'u')
lpos[1] += 0.2f;
if (Key == 'o')
lpos[1] -= 0.2f;
glutPostRedisplay();
}
//Funcion callback para manejar eventos con el mouse
GLvoid mouse(int button,int state,int x,int y)
{
if(state == GLUT_DOWN)
{
switch(button)
{
case GLUT_LEFT_BUTTON:
if(cambiaTextura < 4)
cambiaTextura++;
else
cambiaTextura = 0;
break;
case GLUT_RIGHT_BUTTON:
break;
default:
break;
}
}
glutPostRedisplay();
}
// Idle -------------------------------------------------------------------
GLvoid idle(GLvoid)
{
frame++;
timer = glutGet(GLUT_ELAPSED_TIME);
if (timer - timebase > 1000)
{
fps = frame*1000.0/(timer-timebase);
timebase = timer;
frame = 0;
}
display();
}
// Main ---------------------------------------------------------------------
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(kWindowWidth, kWindowHeight);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
initGL();
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutIdleFunc(idle);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
// LoadGLTextures -----------------------------------------------------------
GLvoid loadGLTextures(GLvoid)
{
//Se cargan las texturas
loadTGA(&texture[0], textureName[0]);
loadTGA(&texture[1], textureName[1]);
loadTGA(&texture[2], textureName[2]);
loadTGA(&texture[3], textureName[3]);
loadTGA(&texture[4], textureName[4]);
loadTGA(&texture[5], "piso3.tga");
loadTGA(&texture[6], "caja1.tga");
loadTGA(&texture[7], "caja2.tga");
loadTGA(&texture[8], "caja3.tga");
loadTGA(&texture[9], "muro3.tga");
// Se crean las texturas
glBindTexture(GL_TEXTURE_2D, texture[0].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[0].width, texture[0].height, GL_RGB, GL_UNSIGNED_BYTE, texture[0].imageData);
glBindTexture(GL_TEXTURE_2D, texture[1].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[1].width, texture[1].height, GL_RGB, GL_UNSIGNED_BYTE, texture[1].imageData);
glBindTexture(GL_TEXTURE_2D, texture[2].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[2].width, texture[2].height, GL_RGB, GL_UNSIGNED_BYTE, texture[2].imageData);
glBindTexture(GL_TEXTURE_2D, texture[3].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[3].width, texture[3].height, GL_RGB, GL_UNSIGNED_BYTE, texture[3].imageData);
glBindTexture(GL_TEXTURE_2D, texture[4].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[4].width, texture[4].height, GL_RGB, GL_UNSIGNED_BYTE, texture[4].imageData);
glBindTexture(GL_TEXTURE_2D, texture[5].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[5].width, texture[5].height, GL_RGB, GL_UNSIGNED_BYTE, texture[5].imageData);
glBindTexture(GL_TEXTURE_2D, texture[6].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[6].width, texture[6].height, GL_RGB, GL_UNSIGNED_BYTE, texture[6].imageData);
glBindTexture(GL_TEXTURE_2D, texture[7].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[7].width, texture[7].height, GL_RGB, GL_UNSIGNED_BYTE, texture[7].imageData);
glBindTexture(GL_TEXTURE_2D, texture[8].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[8].width, texture[8].height, GL_RGB, GL_UNSIGNED_BYTE, texture[8].imageData);
glBindTexture(GL_TEXTURE_2D, texture[9].texID);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texture[9].width, texture[9].height, GL_RGB, GL_UNSIGNED_BYTE, texture[9].imageData);
}
/********************> LoadTGA() <*****/
bool loadTGA(TextureImage *texture, char *filename) // Loads A TGA File Into Memory
{
GLubyte TGAheader[12]={0,0,2,0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header
GLubyte TGAcompare[12]; // Used To Compare TGA Header
GLubyte header[6]; // First 6 Useful Bytes From The Header
GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File
GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram
GLuint temp; // Temporary Variable
GLuint type=GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)
FILE *file = fopen(filename, "rb"); // Open The TGA File
if( file==NULL || // Does File Even Exist?
fread(TGAcompare,1,sizeof(TGAcompare),file)!=sizeof(TGAcompare) || // Are There 12 Bytes To Read?
memcmp(TGAheader,TGAcompare,sizeof(TGAheader))!=0 || // Does The Header Match What We Want?
fread(header,1,sizeof(header),file)!=sizeof(header)) // If So Read Next 6 Header Bytes
{
fclose(file); // If Anything Failed, Close The File
return false; // Return False
}
texture->width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture->height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
if( texture->width <=0 || // Is The Width Less Than Or Equal To Zero
texture->height <=0 || // Is The Height Less Than Or Equal To Zero
(header[4]!=24 && header[4]!=32)) // Is The TGA 24 or 32 Bit?
{
fclose(file); // If Anything Failed, Close The File
return false; // Return False
}
texture->bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture->bpp/8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = texture->width*texture->height*bytesPerPixel; // Calculate The Memory Required For The TGA Data
texture->imageData=(GLubyte *)malloc(imageSize); // Reserve Memory To Hold The TGA Data
if( texture->imageData==NULL || // Does The Storage Memory Exist?
fread(texture->imageData, 1, imageSize, file)!=imageSize) // Does The Image Size Match The Memory Reserved?
{
if(texture->imageData!=NULL) // Was Image Data Loaded
free(texture->imageData); // If So, Release The Image Data
fclose(file); // Close The File
return false; // Return False
}
for(GLuint i=0; i
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
temp=texture->imageData[i]; // Temporarily Store The Value At Image Data 'i'
texture->imageData[i] = texture->imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
texture->imageData[i + 2] = temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}
fclose (file); // Close The File
if (texture[0].bpp==24) // Was The TGA 24 Bits
{
type=GL_RGB; // If So Set The 'type' To GL_RGB
}
// Build A Texture From The Data
// We're doing this in a different function in this tutorial
glGenTextures(1, &texture[0].texID); // Generate OpenGL texture IDs
return true; // Texture Building Went Ok, Return True
}
void setMaterial( GLfloat ambr, GLfloat ambg, GLfloat ambb,
GLfloat difr, GLfloat difg, GLfloat difb,
GLfloat specr, GLfloat specg, GLfloat specb,
GLfloat shine)
{
GLfloat mat[4];
mat[0] = ambr; mat[1] = ambg; mat[2] = ambb; mat[3] = 1.0;
glMaterialfv(GL_FRONT, GL_AMBIENT, mat);
mat[0] = difr; mat[1] = difg; mat[2] = difb;
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat);
mat[0] = specr; mat[1] = specg; mat[2] = specb;
glMaterialfv(GL_FRONT, GL_SPECULAR, mat);
glMaterialf(GL_FRONT, GL_SHININESS, shine * 128.0);
}
void inline drawString (char *s)
{
unsigned int i;
for (i = 0; i < strlen (s); i++)
glutBitmapCharacter (GLUT_BITMAP_HELVETICA_12, s[i]);
}
void setOrthographicProjection()
{
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glScalef(1, -1, 1);
glTranslatef(0, -h, 0);
glMatrixMode(GL_MODELVIEW);
}
void resetPerspectiveProjection()
{
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
No hay comentarios:
Publicar un comentario