martes, 18 de octubre de 2016

Textura Cubo en 3D



Este ejercicio consta de agregar una textura a un cubo en 3D con las teclas a,s,d,w cambias el sentido de rotación de la figura.
Cubo3D
/**
Garnica Ayala Hector Daniel
Textura a un cubo en 3D
*/

#include 
#include
#include
#include
#include 

int ancho=512,alto=512, angx=0, angy=0, angz=0;

unsigned char * datos,*datos2,*datos3,*datos4,*datos5,*datos6;

void DrawTexturedCube(GLdouble size)
{
    GLdouble HL; // half length for the sides of cube
    HL = size / 2.0f;
    glPushMatrix();

    // Cara frontal
    //glBindTexture(GL_TEXTURE_2D, *datos);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-HL,-HL, HL);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( HL,-HL, HL);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f( HL, HL, HL);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-HL, HL, HL);
    glEnd();
    // Cara tracera
    glBindTexture(GL_TEXTURE_2D, *datos2);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-HL,-HL,-HL);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-HL, HL,-HL);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f( HL, HL,-HL);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( HL,-HL,-HL);
    glEnd();
    // Carra superior o tapa
    glBindTexture(GL_TEXTURE_2D, *datos3);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-HL, HL,-HL);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-HL, HL, HL);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( HL, HL, HL);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f( HL, HL,-HL);
    glEnd();
    // Cara de abajo
    glBindTexture(GL_TEXTURE_2D, *datos4);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-HL,-HL,-HL);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f( HL,-HL,-HL);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( HL,-HL, HL);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-HL,-HL, HL);
    glEnd();
    // cara izquierda
    glBindTexture(GL_TEXTURE_2D, *datos5);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-HL,-HL,-HL);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(-HL,-HL, HL);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(-HL, HL, HL);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-HL, HL,-HL);
    glEnd();
    //cara derecha
    glBindTexture(GL_TEXTURE_2D, *datos6);
    glBegin(GL_QUADS);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f( HL,-HL,-HL);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f( HL, HL,-HL);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f( HL, HL, HL);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f( HL,-HL, HL);
    glEnd();
    glPopMatrix();
}


int leerImagen()
{
    FILE *imagen;
    imagen=fopen("C:/Users/host/Documents/PROG_Graficacion/sdoparcial/TextCube3D/box.raw","r");
    datos=(unsigned char*)malloc(ancho*alto*3);
    datos2=(unsigned char*)malloc(ancho*alto*3);
    datos3=(unsigned char*)malloc(ancho*alto*3);
    datos4=(unsigned char*)malloc(ancho*alto*3);
    datos5=(unsigned char*)malloc(ancho*alto*3);
    datos6=(unsigned char*)malloc(ancho*alto*3);

    if(imagen==NULL)
    {
        printf("Error: No imagen");
        return 0;
    }
    else
    {
        printf("OK: Imagen cargada correctamente");
    }
    fread(datos,ancho*alto*3,1,imagen);
    fread(datos2,ancho*alto*3,1,imagen);
    fread(datos3,ancho*alto*3,1,imagen);
    fread(datos4,ancho*alto*3,1,imagen);
    fread(datos5,ancho*alto*3,1,imagen);
    fread(datos6,ancho*alto*3,1,imagen);
    fclose(imagen);

    return 1;
}
void teclado(unsigned char tecla, int x, int y)
{
    switch(tecla)
    {
    case(27):
        exit(0);
    case 'd':
        angy=5;
        break;
    case 'a':
        angy=-5;
        break;
    case 's':
        angx=5;
        break;
    case 'w':
        angx=-5;
        break;
    default:
        glutPostRedisplay();
        break;
    }
    glutPostRedisplay();
}


void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glRotatef(angz, 0, 0, 1);
    glRotatef(angy, 0, 1, 0);
    glRotatef(angx, 1, 0, 0);
    DrawTexturedCube(1.0);
    glFlush();
}
void Init()
{
    glClearColor(0.0,0.0,0.0,0);
    //________________________________________________________________
    gluOrtho2D(-1.5,1.5,-1.5,1.5);
    gluPerspective(45, 1, 1, 30);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    leerImagen();
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ancho, alto, 0, GL_RGB, GL_UNSIGNED_BYTE, datos);
    glEnable(GL_TEXTURE_2D);

}
int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(10,10);
    glutCreateWindow ("Textura a un cubo en 3D");

    glEnable(GL_LIGHT0); // Enable a single light source
    glEnable(GL_CULL_FACE);

    Init();
    glutDisplayFunc(display);
    glutKeyboardFunc(teclado);
    glutMainLoop();
    return 0;
}

Descargar Archivo.txt Cubo3D.txt

No hay comentarios:

Publicar un comentario