Esempio n. 1
0
void
myOrthoReshape(int w, int h) {
    glViewport(0, 0, w, h);

    GLfloat aspect = (GLfloat) w / (GLfloat) h;
    GLfloat left = ss->lens[0];
    GLfloat right = ss->lens[1];
    GLfloat bottom = ss->lens[2];
    GLfloat top = ss->lens[3];
    GLfloat zNear = ss->lens[4];
    GLfloat zFar = ss->lens[5];

    if (w > h) {
        ss->proj = Ortho(left*aspect, right*aspect, bottom, top, zNear, zFar);
    } else {
        ss->proj = Ortho(left, right, bottom/aspect, top/aspect, zNear, zFar);
    }
}
Esempio n. 2
0
void
display( void )
{
    glClearColor( 1.0, 1.0, 1.0, 1.0 );
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    GLfloat time = (GLfloat) (glutGet(GLUT_ELAPSED_TIME)/1000.0f);

    light_pos.x = 1.5f + (sin(time) * 2.0f);
    light_pos.z = 2.0f + (cos(time) / 0.5f);
    glUniform4fv(glGetUniformLocation(cur_program, "vLight"), 1, light_pos);

    // Enable the Shadow Framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, shadow_buffer);
    glBindTexture(GL_TEXTURE_2D, shadow_texture);

    // Move camera to light position, do an orthographic projection.
    SceneState ls;
    ls.proj = Ortho(-2.0, 2.0, -2.0, 2.0, 0.01, 10.0);
    ls.at = vec3(0.0, 0.0, 0.0);
    ls.up = vec3(0.0, 1.0, 0.0);
    ls.eye = vec3(light_pos.x, light_pos.y, light_pos.z);

    ls.mv = LookAt(
        light_pos,
        vec4(ls.at, 1.0f),
        vec4(ls.up, 0.0f)
    );
    //printSceneState(&ls);

    // Render scene with all objects.
    for (unsigned int i = 0; i < objects.size(); ++i) {
        objects[i].ss = &ls;
        objects[i].draw(shadow_shader);
        objects[i].ss = ss;
    }

    // Disable the Shadow Framebuffer
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    //glBindTexture(GL_TEXTURE_2D, 0);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
    glClearColor( 1.0, 1.0, 1.0, 1.0 );

    // Pass ortho proj of light to shader. Multiplied by bias matrix.
    //  See we want to look up information about vertices in a texture,
    //  but that texture is defined from 0 to height and 0 to width. We
    //  want this in projection bounds.
    // Pass texture to shader.
    mat4 bias(
        0.5, 0.0, 0.0, 0.5,
        0.0, 0.5, 0.0, 0.5,
        0.0, 0.0, 0.5, 0.5,
        0.0, 0.0, 0.0, 1.0
    );
    glUniformMatrix4fv( glGetUniformLocation(cur_program, "ShadowView"),
            1, GL_TRUE, ls.mv );
    glUniformMatrix4fv( glGetUniformLocation(cur_program, "BiasedShadowProjection"),
            1, GL_TRUE, bias*ls.proj );
    glUniform1i(glGetUniformLocation(cur_program, "shadowMap"),
            shadow_texture);

    ss->mv = LookAt(
        vec4(ss->eye, 1.0f),
        vec4(ss->at, 1.0f),
        vec4(ss->up, 0.0f)
    );

    glUniform1i(glGetUniformLocation(cur_program, "disks" ), disks);


    // Render each loaded object file.
    for (unsigned int i = 0; i < objects.size(); ++i) {
        if (wireframe_vao == i) {
            objects[i].wireframe(uniform_color);
            // Draw manipulator
            for (unsigned int j = 0; j < manipulator.size(); ++j) {
                manipulator[j].translate = objects[i].translate;
                manipulator[j].draw(uniform_color);
            }
        } else {
            objects[i].draw(cur_program);
        }
    }

    glutSwapBuffers();
}