lunes, 17 de octubre de 2016

CUBO

CUBO
Proyecciones y Multitextura



#include<stdio.h>
#include<stdlib.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <windows.h>
#include <C:\Program Files (x86)\CodeBlocks\MinGW\include\GL\glut.h>
#include <math.h>

using namespace std;

int HL=1,angx=30,angy=60;
unsigned char * datos;

int an1=128,al1=128;//dim imagenes, ancho, alto
unsigned char * imagen1;
int an2=128,al2=128;
unsigned char * imagen2;

int leerImagen1(){
    FILE *imagen;
    imagen=fopen("/Users/Juan/Desktop/arma.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("/Users/Juan/Desktop/boy.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 dibujaI1(void)
{
    glDisable(GL_LIGHTING);
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    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);
    glEnd();
    glEnable(GL_LIGHTING);
}

void dibujaI2(void)
{
    glDisable(GL_LIGHTING);
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    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);
    glEnd();
    glEnable(GL_LIGHTING);
}

void dibujaI3(void)
{
    glDisable(GL_LIGHTING);
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    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);
    glEnd();
    glEnable(GL_LIGHTING);
}

void dibujaI4(void)
{
    glDisable(GL_LIGHTING);
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    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);
    glEnd();
    glEnable(GL_LIGHTING);
}

void dibujaI5(void)
{
    glDisable(GL_LIGHTING);
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    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);
    glEnd();
    glEnable(GL_LIGHTING);
}

void dibujaI6(void)
{
    glDisable(GL_LIGHTING);
    glColor3f(1,1,1);
    glBegin(GL_QUADS);
    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);
    glEnd();
    glEnable(GL_LIGHTING);
}

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);
        glPushMatrix();
            texturaI1();
            dibujaI1();
            texturaI2();
            dibujaI2();
            texturaI1();
            dibujaI3();
            texturaI2();
            dibujaI4();
            texturaI1();
            dibujaI5();
            texturaI2();
            dibujaI6();
        glPopMatrix();
    //glutWireCube(0.5);
    //glutWireSphere(0.2,10,10);
    glPopMatrix();
    glFlush();
}

void proy(unsigned char key, int x,int y){//proyecciones ortogonal o perspectiva
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    switch (key) {
        case 27:exit(0);
        case 'p':
            gluPerspective(60, 1, 0.1, 10);//(campo de visión en grados, relacion de aspecto, z cercano, z lejano)
            break;
        case 'o':
            glOrtho(-1,1,-1,1,-1,10);
            break;
    }
    glutPostRedisplay();
}


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();
}

void inicio()
{
    glEnable(GL_TEXTURE_2D);
    glMatrixMode(GL_PROJECTION); //Hacer uso de la pantalla
 //   gluOrtho2D(0,500,0,500);
    leerImagen1();
    leerImagen2();
    //Definir el Ancho O DISTRIBUCION DEL AREA DE TRABAJO de -5 a 5 en x, y Largo de -5 a 5 en y
    //glClearColor(0.0,0.0,0.0,0.0);//color de fondo R,G,B,ALFHA en este caso negro
    //glLoadIdentity();
}

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;
}

No hay comentarios:

Publicar un comentario