David Gómez Jaramillo
El ejercicio consiste en el dibujo de un cubo en 3D aplicando texturas con lo cual se lo implemente a manera de que al presionar la tecla "o", usa una textura y "p", utiliza otra textura.
También se aplica que al utilizar las teclas izquierda y derecha se realicen rotaciones.
Capturas de Pantalla:
Imágenes Utilizadas:
Ladrillo |
Madera |
Código:
/*
Texturas 3D
David Gómez Jaramillo
*/
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#include<stdio.h>
#include<stdlib.h>
int opcion =0;
int HL=1,angx=30,angy=60;
unsigned char * datos;
int an1=128,al1=128;
unsigned char * imagen1;
int an2=128,al2=128;
unsigned char * imagen2;
int leerImagen1(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/ladrillo.RAW","r");
imagen1=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen1,an1*al1*3,1,imagen);
fclose(imagen);
return 1;
}
int leerImagen2(){
FILE *imagen;
imagen=fopen("C:/ImagenesC/madera.RAW","r");
imagen2=(unsigned char*)malloc(an1*al1*3);
if(imagen==NULL){
printf("Error: No imagen");
return 0;
}
fread(imagen2,an2*al2*3,1,imagen);
fclose(imagen);
return 1;
}
void texturaI1(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an1, al1, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen1);
}
void texturaI2(void)
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, an2, al2, 0, GL_RGB, GL_UNSIGNED_BYTE, imagen2);
}
void dibujaCara(int j, int img)
{
glDisable(GL_LIGHTING);
glColor3f(1,1,1);
glBegin(GL_QUADS);
switch(j){
case 1:
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(0,0.5,0);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(0,0,0);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(0.5,0,0);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(0.5,0.5,0);
break;
case 2:
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(0,0.5,-0.5);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(0,0,-0.5);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(0,0,0);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(0,0.5,0);
break;
case 3:
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(0.5,0.5,-0.5);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(0.5,0,-0.5);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(0,0,-0.5);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(0,0.5,-0.5);
break;
case 4:
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(0.5,0.5,0);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(0.5,0,0);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(0.5,0,-0.5);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(0.5,0.5,-0.5);
break;
case 5:
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(0,0.5,-0.5);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(0,0.5,0);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(0.5,0.5,0);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(0.5,0.5,-0.5);
break;
case 6:
glTexCoord3f(0.0, 1.0,0.0);
glVertex3f(0,0,-0.5);
glTexCoord3f(0.0, 0.0,0.0);
glVertex3f(0,0,0);
glTexCoord3f(1.0, 0.0,0.0);
glVertex3f(0.5,0,0);
glTexCoord3f(1.0, 1.0,0.0);
glVertex3f(0.5,0,-0.5);
break;
}
glEnd();
glEnable(GL_LIGHTING);
}
void construyeCubo1(int t){
if(t==0){
texturaI1();
}else{
texturaI2();
}
dibujaCara(1,1);
dibujaCara(4,1);
dibujaCara(2,1);
dibujaCara(3,1);
dibujaCara(6,1);
dibujaCara(5,1);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0, 1, 0);
glLineWidth(3);
glPushMatrix();
glTranslatef(0, 0, -2);
glRotatef(angx, 1, 0, 0);
glRotatef(angy, 0, 1, 0);
if (opcion==0){
construyeCubo1(0);
}else{
construyeCubo1(1);
}
glPopMatrix();
glFlush();
}
void proy(unsigned char key, int x,int y){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
switch (key) {
case 27:exit(0);
case 'p':
gluPerspective(60, 1, 0.1, 10); //se genera el modo de perspectiva 3D
//field of view campo de vision 60°
//relacion de aspecto 1 a 1
//Znear lo mas cerca en z (0.1)
//Zfar lo mas lejano es 10
opcion=1;
break;
case 'o':
opcion=0;
gluPerspective(60, 1, 0.1, 10); //se genera el modo de perspectiva 3D
//glOrtho(-1,1,-1,1,-1,10); //se genera el modo ortogonal 2D (x1,x2, y1,y2, z1,z2)
break;
}
glutPostRedisplay();
}
void inicio()
{
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION); //Hacer uso de la pantalla
leerImagen1();
leerImagen2();
}
void girar(int key, int x, int y){
switch(key){
case GLUT_KEY_LEFT:
angx=angx+3;
break;
case GLUT_KEY_RIGHT:
angx=angx-3;
break;
}
glutPostRedisplay();
}
int main(int argc, char **argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600,600);
glutInitWindowPosition(10,10);
glutCreateWindow ("Proyecciones y Multi-Textura");
inicio();
glutDisplayFunc(display);
glutKeyboardFunc(proy);
glutSpecialFunc(girar);
glutMainLoop();
return 0;
}
Descargar Código:
No hay comentarios:
Publicar un comentario