我在OpenGL中遇到阴影贴图的问题。 嗯,阴影看起来像这样:
http://i.stack.imgur.com/7wWm2.png
我的顶点着色器:
#version 330 core
layout(location = 0) in vec3 vertex;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 UV;
out vec2 oUV;
out vec3 oNormal;
out vec4 shadowCoord;
uniform mat4 MVP;
uniform mat4 DepthMVP;
void main(){
gl_Position = MVP * vec4(vertex, 1);
shadowCoord = DepthMVP * vec4(vertex, 1);
oUV = UV;
oNormal = normal;
}
片段着色器:
#version 330 core
in vec2 oUV;
in vec3 oNormal;
in vec4 shadowCoord;
out vec3 color;
uniform sampler2D textureSampler;
uniform sampler2DShadow shadowMap;
void main(){
float v = 1.0;
if(texture(shadowMap,
vec3(shadowCoord.xy, shadowCoord.z /
shadowCoord.w)) < shadowCoord.z )
v = 0;
color = v * texture2D(textureSampler, oUV).rgb*
vec3(1, 1, 1);
}
以下是我创建帧缓冲区的方法:
glGenFramebuffers(1, &ld.fb);
glBindFramebuffer(GL_FRAMEBUFFER, ld.fb);
glGenTextures(1, &ld.dtext);
glBindTexture(GL_TEXTURE_2D, ld.dtext);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1024, 768, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, ld.dtext, 0);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
有什么问题?为什么物体上的阴影看起来那么糟糕?