Reflejo | Diana Vargas

#include <windows.h>
#include <math.h>
#include <GL/glut.h>


float salto = 0.0;//esferita q rebota
float anguloLuz = 0.0, alturaLuz = 15;

float c=1,dia=-0.1;

float angle = 0,angle2 = 0;
int moving, startx, starty;

static GLdouble tamanio = 4.0;//radio de la esfera

static GLfloat posicionLuz[4];
static GLfloat colorLuz[] = {1, 1, 1, 1.0};

static GLfloat planoPiso[4];
static GLfloat sombraPiso[4][4];

enum {X, Y, Z, W};
enum {A, B, C, D};


char *textura[] = {//textura dibujada
    "oooooooooooooooo",
    "o...............",
    "o..xxxxxxxxxxxx.",
    "o..xxxxxxxxxxxx.",
    "o..xxxxxxxxxxxx.",
    "o..xxx......xxx.",
    "o..xxx......xxx.",
    "o..xxx.o.o..xxx.",
    "o..xxx..o.o.xxx.",
    "o..xxx......xxx.",
    "o..xxx......xxx.",
    "o..xxxxxxxxxxxx.",
    "o..xxxxxxxxxxxx.",
    "o..xxxxxxxxxxxx.",
    "o...............",
    "o...............",
};


void texturaPiso(void)
{
    GLubyte floorTexture[16][16][3];
    GLubyte *color;

    int x,y;
    // Crear RGB para textura
    color = (GLubyte*) floorTexture;

    for (x = 0; x < 16; x++) {//le asigna colores para pintar el pisito
        for (y = 0; y < 16; y++) {
            if (textura[x][y] == 'x') {
                color[0] = 0;
                color[1] = 120;
                color[2] = 128;
            } else if (textura[x][y] == 'o') {
                color[0] = 64;
                color[1] = 0;
                color[2] = 0;
            }else {
                color[0] =255;
                color[1] =255;
                color[2] =255;
            }
            color += 3;
        }
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexImage2D(GL_TEXTURE_2D, 0, 3, 16,16, 0,
                     GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
}

static GLfloat verticesPiso[4][3] = {
    { -50.0, 0.0,  50.0 },
    {  50.0, 0.0,  50.0 },
    {  50.0, 0.0, -50.0 },
    { -50.0, 0.0, -50.0 },
};

void dibujarPiso(void)
{
    glDisable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);

    glBegin(GL_QUADS);
    glTexCoord2f(0.0, 0.0);
    glVertex3fv(verticesPiso[0]);
    glTexCoord2f(0.0, 16.0);
    glVertex3fv(verticesPiso[1]);
    glTexCoord2f(16.0, 16.0);
    glVertex3fv(verticesPiso[2]);
    glTexCoord2f(16.0, 0.0);
    glVertex3fv(verticesPiso[3]);
    glEnd();

    glDisable(GL_TEXTURE_2D);
    glEnable(GL_LIGHTING);
}

/* Matriz de proyeccion de la sombra */
void shadowMatrix(float matrizSombra[4][4],
             float ecPlano[4],
             float posLuz[4])
{
    GLfloat dot;

    // Producto punto entre vector light position y la normal de ground plane
    dot = ecPlano[X] * posLuz[X] +
    ecPlano[Y] * posLuz[Y] +
    ecPlano[Z] * posLuz[Z] +
    ecPlano[W] * posLuz[W];

    matrizSombra[0][0] = dot - posLuz[X] * ecPlano[X];
    matrizSombra[1][0] = 0.f - posLuz[X] * ecPlano[Y];
    matrizSombra[2][0] = 0.f - posLuz[X] * ecPlano[Z];
    matrizSombra[3][0] = 0.f - posLuz[X] * ecPlano[W];

    matrizSombra[X][1] = 0.f - posLuz[Y] * ecPlano[X];
    matrizSombra[1][1] = dot - posLuz[Y] * ecPlano[Y];
    matrizSombra[2][1] = 0.f - posLuz[Y] * ecPlano[Z];
    matrizSombra[3][1] = 0.f - posLuz[Y] * ecPlano[W];

    matrizSombra[X][2] = 0.f - posLuz[Z] * ecPlano[X];
    matrizSombra[1][2] = 0.f - posLuz[Z] * ecPlano[Y];
    matrizSombra[2][2] = dot - posLuz[Z] * ecPlano[Z];
    matrizSombra[3][2] = 0.f - posLuz[Z] * ecPlano[W];

    matrizSombra[X][3] = 0.f - posLuz[W] * ecPlano[X];
    matrizSombra[1][3] = 0.f - posLuz[W] * ecPlano[Y];
    matrizSombra[2][3] = 0.f - posLuz[W] * ecPlano[Z];
    matrizSombra[3][3] = dot - posLuz[W] * ecPlano[W];

}

/* Ecuación del plano dados 3 puntos. */
void defPlano(float plano[4], float v0[3], float v1[3], float v2[3])
{
    GLfloat vec0[3], vec1[3];
    /* Producto cruz entre 2 vectores. */
    vec0[X] = v1[X] - v0[X];
    vec0[Y] = v1[Y] - v0[Y];
    vec0[Z] = v1[Z] - v0[Z];

    vec1[X] = v2[X] - v0[X];
    vec1[Y] = v2[Y] - v0[Y];
    vec1[Z] = v2[Z] - v0[Z];

    /* Encontrar producto cruz para A, B, y C en la ec. del plano */
    plano[A] = vec0[Y] * vec1[Z] - vec0[Z] * vec1[Y];
    plano[B] = -(vec0[X] * vec1[Z] - vec0[Z] * vec1[X]);
    plano[C] = vec0[X] * vec1[Y] - vec0[Y] * vec1[X];

    plano[D] = -(plano[A] * v0[X] + plano[B] * v0[Y] + plano[C] * v0[Z]);
}

void dibujaObjeto(void)
{
    glPushMatrix();
        glTranslatef(0, 4, 0);
        glTranslatef(0.0, salto, 0.0);

        GLfloat mat_p_ambient[] = {0.4725,0.4245,0.8645,1};
        GLfloat mat_p_diffuse[] = {0.34615,0.5143,0.8903,1};
        GLfloat mat_pspecular[] = {1.797357,1.723991,1.708006,1};
        GLfloat mat_pshininess[] = {18.2};

        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_pspecular);
        glMaterialfv(GL_FRONT, GL_SHININESS, mat_pshininess);
        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_p_ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_p_diffuse);

        //gluQuadricDrawStyle(figura, GLU_FILL);
    //gluSphere(figura,10,32,32);
    //gluCylinder(figura,10,10,20,32,32);
    //gluDisk(figura,3,1,32,32);
    //gluPartialDisk(figura,5,10,32,32,0,180);
    //glClear( GL_COLOR_BUFFER_BIT );
    //glColor3f(1.0, 1.0, 1.0);

    glPushMatrix();
    //glRotatef(angulo+2, 0, 1, 0);
    //HEAD
    glPushMatrix();
    glTranslatef(0,1.8,0);
    glScalef(1.1,0.7,1);
    //glutWireSphere(1,20,20);
    glutSolidSphere(1,20,20);
    glPopMatrix();

    //BODY
    glPushMatrix();
    glTranslatef(0,-1.2,0);
    glScalef(2,1.5,1.5);

    //glutWireSphere(1,20,20);
    glutSolidSphere(1,20,20);
    glPopMatrix();

    glPushMatrix();
    glTranslatef(0,0.6,0);
    glScalef(1.4,0.6,1);
    //glutWireSphere(1,20,20);
    glutSolidSphere(1,20,20);

    glPopMatrix();

    // PIE DER
    glPushMatrix();
    glTranslatef(0.6,-2.8,0);
    glScalef(1.1,1.3,1);
    //glutWireSphere(0.5,20,20);
    glutSolidSphere(0.5,20,20);
    glPopMatrix();

    // PIE IZQ
    glPushMatrix();
    glTranslatef(-0.6,-2.8,0);
    glScalef(1.1,1.3,1);
    //glutWireSphere(0.5,20,20);
    glutSolidSphere(0.5,20,20);
    glPopMatrix();

     // BRAZO
    glPushMatrix();
    //glColor3f(1.0, 0.0, 1.0);
    glTranslatef(1.9,-0.5,0);
    glRotatef(40, 0, 0, 1);
    glScalef(1.1,2.5,1);
    //glutWireSphere(0.5,20,20);
    glutSolidSphere(0.5,20,20);
    glPopMatrix();

    // EL OTRO BRAZO
    glPushMatrix();
    glTranslatef(-1.9,-0.5,0);
    glRotatef(-40, 0, 0, 1);
    glScalef(-1.1,2.5,1);
    //glutWireSphere(0.5,20,20);
    glutSolidSphere(0.5,20,20);
    glPopMatrix();

    //OJO IZQ
    glPushMatrix();
    glColor3f(0, 0, 0);
    glTranslatef(-0.5,1.8,0);
    glutSolidSphere(0.2,20,20);
    glPopMatrix();

    //OJO DER
    glPushMatrix();
    glColor3f(0, 0, 0);
    glTranslatef(0.5,1.8,0);
    glutSolidSphere(0.2,20,20);
    glPopMatrix();


    glPopMatrix();
    //glutSolidSphere(tamanio, 128, 128);
    //glutSolidTeapot(tamanio);
    //dibujaObjeto1();

    glPopMatrix();
}



void display(void)
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

    c=c+(dia*0.02);
    if (c > 1 || c < 0) dia=-dia;//aclarado y obscurecimiento
    glClearColor(c, c, c, 1);
    //mueve la luz x,y,z |0,1,2,
    posicionLuz[0] = 12*cos(anguloLuz);
    posicionLuz[1] = alturaLuz;
    posicionLuz[2] = 12*sin(anguloLuz);
    //posicionLuz[3] = 0.8;//luz posicional o direccional
    posicionLuz[3] = 0.95;

    shadowMatrix(sombraPiso, planoPiso, posicionLuz);

    glPushMatrix();
        glRotatef(angle2, 1.0, 0.0, 0.0);/* mover mouse */
        glRotatef(angle, 0.0, 1.0, 0.0);

        glLightfv(GL_LIGHT0, GL_POSITION, posicionLuz); /*light position. */

        glPushMatrix();
            glScalef(1.0, -1.0, 1.0);
            glLightfv(GL_LIGHT0, GL_POSITION, posicionLuz);//Reflejar la luz tmb
          //reflejo
        dibujaObjeto();
        glPopMatrix();

        glLightfv(GL_LIGHT0, GL_POSITION, posicionLuz);//Reacomodar la luz

//*************************************************************

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glColor4f(1.0, 1.0, 1.0, 0.6);
        dibujarPiso();


        glDisable(GL_LIGHTING);
        glDisable(GL_DEPTH_TEST);
        glPushMatrix();
            glColor4f(0.0, 0.0, 0.0, 0.7);
            //glColor4f(0.0, 0.0, 0.0, 0.2);//esfera
            glMultMatrixf((GLfloat *) sombraPiso);
               //Sombra
            dibujaObjeto();
            glPopMatrix();

        glDisable(GL_BLEND);
        glEnable(GL_LIGHTING);
        glEnable(GL_DEPTH_TEST);//

           //dibujo objeto solito
    dibujaObjeto();
        glDisable(GL_LIGHTING);
            glColor3f(1.0, 1.0, 0.7);
            glTranslatef(posicionLuz[0], posicionLuz[1], posicionLuz[2]);
            glutSolidSphere(0.5, 20, 20);  //simulo la pos. de la luz con una esfera
        glEnable(GL_LIGHTING);

    glPopMatrix();
glutSwapBuffers();
}

void mouse(int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON) {
        if (state == GLUT_DOWN) {
            moving = 1;
            startx = x;
            starty = y;
        }
        if (state == GLUT_UP) {
            moving = 0;
        }
    }
}

void mover(int x, int y)
{
    if (moving) {
        angle = angle + (x - startx);
        angle2 = angle2 + (y - starty);
        startx = x;
        starty = y;
        glutPostRedisplay();
    }
}
void idle(void)
{
    float time = 0.0;
    time = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
    salto =10.0 * fabs(sin(time)*0.7);
    anguloLuz += 0.001;
    glutPostRedisplay();
}


void init(void){
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_TEXTURE_2D);

    glMatrixMode(GL_PROJECTION);
    gluPerspective(40.0,1.0,20.0,500.0);
    glMatrixMode(GL_MODELVIEW);
    gluLookAt(0.0, 8.0, 60.0,  /* vista en (0,8,60) */
              0.0, 8.0, 0.0,      /* centro en (0,8,0) */
              0.0, 1.0, 0.);      /* altura en Y */

    glLightfv(GL_LIGHT0, GL_DIFFUSE, colorLuz);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    texturaPiso();
    defPlano(planoPiso, verticesPiso[1], verticesPiso[2], verticesPiso[3]);//Plano para sombra}
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_STENCIL | GLUT_MULTISAMPLE);

    glutInitWindowSize(1024, 800);

    glutCreateWindow("BAYMAX SALTANDO <3 | DV");

    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(mover);
    glutIdleFunc(idle);

    init();
    glutMainLoop();
    return 0;
}



Comentarios

Entradas populares de este blog

Código Capitán América Diana Vargas

Cámara | Israel González de la Peña