opengl阴影映射阴影移动与相机

时间:2014-03-18 05:57:18

标签: opengl shadow

只有当我进入场景并调整场景中的摄像机视图时,才能显示阴影。但它没有意义,因为如果光源移动,阴影应该只改变或移动,在我的情况下,如果我改变我的相机视图,阴影会移动。

然而,阴影形状正确映射,位置是奇数和错误,我很确定当我从灯光视图渲染场景时,我没有将相机视图作为灯光视图传递

这是我如何设置第一个传球制服

void Renderer::setShadowUniform(GLuint program_id)
{
    glUseProgram(program_id);
    auto light_vec = vec3(data.directional * 30.0f);
    Matrices.light_view = glm::lookAt(light_vec, vec3(1.0f), vec3(0.0f, 1.0f, 0.0f));
    Matrices.light_proj = Matrices.projection;

GlobalUniformLocations.light_projc = glGetUniformLocation(program_id, "light_proj");
GlobalUniformLocations.light_view = glGetUniformLocation(program_id, "light_view");

glUniformMatrix4fv(GlobalUniformLocations.light_view, 1, GL_FALSE, &Matrices.light_view[0][0]);
glUniformMatrix4fv(GlobalUniformLocations.light_projc, 1, GL_FALSE, &Matrices.light_proj[0][0]);
}

这是我渲染场景时设置制服的地方

void Renderer::setNormalRenderUniform(GLuint program_id)
{   


glUseProgram(program_id);
    //view related uniforms
    auto light_vec = vec3(data.directional * 30.0f);
    Matrices.light_view = glm::lookAt(light_vec, vec3(1.0f), vec3(0.0f, 1.0f, 0.0f));
    Matrices.light_proj = Matrices.projection;
    //Matrices.light_proj =  glm::ortho<float>(-20,20,-20,20,-20,40);
    Matrices.world_view = cams[0]->GetWorldToViewMat();
    GlobalUniformLocations.eye =        glGetUniformLocation(program_id, "camera_position");    
    GlobalUniformLocations.projection = glGetUniformLocation(program_id, "projection");
    GlobalUniformLocations.world_view = glGetUniformLocation(program_id, "world_view");

    glUniformMatrix4fv(GlobalUniformLocations.world_view, 1, GL_FALSE, &Matrices.world_view[0][0]);
    glUniformMatrix4fv(GlobalUniformLocations.projection, 1, GL_FALSE, &Matrices.projection[0][0]);
    glUniform3fv(GlobalUniformLocations.eye, 1, &cams[0]->position.x);

    //shadow related uniforms
    GlobalUniformLocations.shadow_map      = glGetUniformLocation(program_id, "shadow_map");
    GlobalUniformLocations.biased_matrix   = glGetUniformLocation(program_id, "biased_mat");    
    GlobalUniformLocations.light_projc     = glGetUniformLocation(program_id, "light_proj");
    GlobalUniformLocations.light_view      = glGetUniformLocation(program_id, "light_view");
    glUniformMatrix4fv(GlobalUniformLocations.biased_matrix, 1, GL_FALSE, &Matrices.biased_matrix[0][0]);
    glUniformMatrix4fv(GlobalUniformLocations.light_view, 1, GL_FALSE, &Matrices.light_view[0][0]);
    glUniformMatrix4fv(GlobalUniformLocations.light_projc, 1, GL_FALSE, &Matrices.light_proj[0][0]);

    //vertex color
    GlobalUniformLocations.enable_vertex_color = glGetUniformLocation(program_id, "vertex_color_on");

    //texture related uniforms
    GlobalUniformLocations.texture = glGetUniformLocation(program_id, "textSample");
    }

it's my paintGL method where I draw everything

    glBindFramebuffer(GL_FRAMEBUFFER, renderer->depth_framebuffer_id);  
    glViewport(0, 0, DEPTH_TEXTURE_WIDTH, DEPTH_TEXTURE_HEIGHT);
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glClearDepth(1.0f); 
    glCullFace(GL_FRONT); 
    glEnable(GL_DEPTH_TEST);
    renderer->setShadowUniform(renderer->shaders[0]->program_id);
    renderer->Draw();   

    ////render scene
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glViewport(0, 0, width(), height());
    glDisable(GL_DEPTH_TEST);
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glCullFace(GL_BACK);
    glActiveTexture(GL_TEXTURE0 + renderer->depth_texture_id);
    glBindTexture(GL_TEXTURE_2D, renderer->depth_texture_id);
    glUniform1i(renderer->GlobalUniformLocations.shadow_map, renderer->depth_texture_id);
    renderer->setNormalRenderUniform(renderer->shaders[1]->program_id); 
    renderer->Draw();

0 个答案:

没有答案