如何在视空间中指向阴影?

时间:2018-06-21 12:01:53

标签: c++ opengl shadows

我正在尝试在视空间中编程点光源阴影,但是当前阴影已切换到屏幕,如下图所示。

Image of the Problem 我在定向阴影方面遇到了这个问题,我只需要简单地将相机视图矩阵与模型矩阵和光空间矩阵相乘,但是对于点阴影,它们不使用lightSpaceMatrix。

GBuffer.v main

void main()
{
    vec4 view_space = view * mod * vec4(position, 1.0); // VIEW SPACE

    _frag_pos = view_space.xyz;
    _texcoord = texcoord;

    _normal = ((view * mod) * vec4(normal, 0.0)).xyz;
    _tangent = (mod * vec4(tangent, 0.0)).xyz;

    gl_Position = proj * world_space;
}

light.f主要计算

vec3 lightColor = vec3(0.3);

// ambient
vec3 ambient = 0.0 * Diffuse;

// diffuse
vec3 lightDir = normalize(vec3(0.0, 1.0, 0.0) - FragPos);
float diff = max(dot(lightDir, Normal), 0.0);
vec3 diffuse = diff * lightColor;

// specular
vec3 viewDir = normalize(camera_pos - FragPos);
vec3 reflectDir = reflect(-lightDir, Normal);
float spec = 0.0;
vec3 halfwayDir = normalize(lightDir + viewDir);  
spec = pow(max(dot(Normal, halfwayDir), 0.0), 64.0);
vec3 specular = spec * lightColor;    

// calculate shadow
float shadow = calculate_point_shadows(FragPos);                      
vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular));    

FragColor = vec4(lighting, 1.0);

C ++点阴影设置

glDisable(GL_BLEND);        // Disable blending for opique materials
        glEnable(GL_DEPTH_TEST);    // Enable depth test
        glDisable(GL_CULL_FACE);

        glm::mat4 model;

        glm::mat4 shadowProj = glm::perspective(glm::radians(90.0f), (float)1024 / (float)1024, 1.0f, 25.0f);
        shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
        shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(-1.0f, 0.0f, 0.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
        shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)));
        shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, -1.0f, 0.0f), glm::vec3(0.0f, 0.0f, -1.0f)));
        shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, 1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));
        shadowTransforms.push_back(shadowProj * glm::lookAt(lightPos, lightPos + glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, -1.0f, 0.0f)));

        glViewport(0, 0, 2048, 2048);
        glBindFramebuffer(GL_FRAMEBUFFER, _shadow_fbo);
        glClear(GL_DEPTH_BUFFER_BIT);

        glUseProgram(pointshadow_program);
        for (unsigned int i = 0; i < 6; ++i)
            glUniformMatrix4fv(glGetUniformLocation(pointshadow_program, ("shadowMatrices[" + std::to_string(i) + "]").c_str()), 1, GL_FALSE, glm::value_ptr(shadowTransforms[i]));

        for (unsigned int i = 0; i < _actors.size(); i++)
        {
            model = _actors[i]->GetModelMatrix();

            glUniformMatrix4fv(glGetUniformLocation(pointshadow_program, "model"), 1, GL_FALSE, glm::value_ptr(model)); // set the model matrix uniform

            _actors[i]->Render();
        }

        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        glViewport(0, 0, 1920, 1080);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // glclear the gbuffer before rendering to it
        glEnable(GL_CULL_FACE);

0 个答案:

没有答案