阴影贴图与定向光 - 阴影太小

时间:2017-04-11 12:07:10

标签: opengl graphics glsl

我无法为定向灯实施简单的阴影贴图算法。从地平面上的立方体对象投射的阴影太小,我无法弄清楚原因!请帮帮我们!

这是从灯光的“位置”A看到的场景图像,这里是我的立方体的阴影B。图像B上的阴影就好像只有立方体的顶部正面是投射阴影,完全忽略立方体的顶面!

这是我的投影矩阵和光线位置:

glm::vec3 worldSpace_lightPos  = glm::vec3( 0.0f, 4.0f, -1.0f );

glm::vec3 worldSpace_planePos  = glm::vec3( 0.0f, 0.0f, 0.0f );
glm::vec3 worldSpace_cubePos   = glm::vec3( 0.0f, 1.5f, 1.0);

glm::vec3 lightSpace_cameraDir = worldSpace_cubePos + glm::vec3(0.5f);

glm::mat4 V = glm::lookAt( glm::vec3(-8.0f, 7.0f, 0.0f), glm::vec3(0,0,0), glm::vec3(0,1,0) );
glm::mat4 P = glm::perspective(45.0f, 4.0f/3.0f, 0.01f, 100.0f);

glm::mat4 depthV = glm::lookAt(worldSpace_lightPos, worldSpace_cubePos + glm::vec3(0.5f), glm::vec3(0,1,0));
glm::mat4 depthP = glm::ortho<float>(-10,10,-20,20,1,9.5);

这是我的纹理参数:

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);

我的第二遍片段着色器:

#version 330 core

in vec3 eyeSpace_coordinates;
in vec3 eyeSpace_normals;
in vec4 lightSpace_coordinates;

uniform sampler2D ShadowMap;

uniform vec3 lightDir;
uniform mat4 view;
uniform mat4 projection;


float calculateShadow()
{
    float bias = 0.0005f;
    float isShadowed = 1.0f;

    vec3 nuevo = lightSpace_coordinates.xyz;

float closestDepth = texture(ShadowMap, nuevo.xy).r;
float currentDepth = nuevo.z - bias;

if( currentDepth > closestDepth )
    isShadowed = 0.3f;

return isShadowed;
}

void main()
{ 
float shadow = calculateShadow();

gl_FragColor = vec4( vec3(shadow), 1.0f ) * vec4( 1.0f, 0.0f, 0.0f, 1.0f);
}

0 个答案:

没有答案