我的阴影贴图似乎到处投射阴影,我不知道为什么。
这是我设置FBO和深度纹理的地方
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
// Depth texture. Slower than a depth buffer, but you can sample it later in your shader
glGenTextures(1, &shadowMap);
glBindTexture(GL_TEXTURE_2D, shadowMap);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT16, 800, 600, 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_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowMap, 0);
glDrawBuffer(GL_NONE); // No color buffer is drawn to.
// Always check that our framebuffer is ok
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout << "shadowMap fail" << endl;
这是我的两次传球
// clear the screen
glEnable(GL_CULL_FACE);
glClearColor(0.5f,0.5f,0.5f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 projection(1.0);
projection = glm::perspective(float(60.0f*DEG_TO_RADIAN),800.0f/600.0f,1.0f,150.0f);
rt3d::setUniformMatrix4fv(shaderProgram, "projection", glm::value_ptr(projection));
GLfloat scale(1.0f); // just to allow easy scaling of complete scene
glm::mat4 modelview(1.0); // set base position for scene
mvStack.push(modelview);
for (int pass = 0; pass < 2; pass++) {
if (pass == 0) {
glUseProgram(shadowMapProgram);
glm::vec3 lightInvDir = glm::vec3(lightPos.x, lightPos.y, lightPos.z);
oldeye = eye;
eye = lightInvDir;
at = glm::vec3(0, 0, 0);
up = glm::vec3(0, 1, 0);
// Compute the MVP matrix from the light's point of view
glm::mat4 depthViewMatrix = glm::lookAt(eye, at ,up);
depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
glBindFramebuffer(GL_FRAMEBUFFER, FramebufferName);
uniformIndex = glGetUniformLocation(shadowMapProgram, "depthMVP");
glUniformMatrix4fv(uniformIndex, 1, GL_FALSE, &depthMVP[0][0]);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, shadowMap, 0);
glDrawBuffer(GL_NONE); // No color buffer is drawn to.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
rt3d::setUniformMatrix4fv(shadowMapProgram, "projection", glm::value_ptr(depthProjectionMatrix));
uniformIndex = glGetUniformLocation(shadowMapProgram, "modelMatrix");
glUniformMatrix4fv(uniformIndex, 1, GL_FALSE, glm::value_ptr(depthModelMatrix));
cout << uniformIndex << " DepthModelMatrix" << endl;
}
if (pass == 1) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
eye = oldeye;
at = moveForward(eye, r, 1.0f);
up = glm::vec3(0, 1, 0);
glUseProgram(toonShaderProgram);
glm::mat4 depthBiasMVP = biasMatrix*depthMVP;
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
//uniformIndex = glGetUniformLocation(toonShaderProgram, "depthBiasMVP");
// glUniformMatrix4fv(uniformIndex, 1, GL_FALSE, &depthBiasMVP[0][0]);
//cout << uniformIndex << " depthBiasMVP" << endl;
rt3d::setUniformMatrix4fv(toonShaderProgram, "depthBiasMVP", glm::value_ptr(depthBiasMVP));
uniformIndex = glGetUniformLocation(toonShaderProgram, "shadowMap");
glUniform1i(uniformIndex, shadowMap);
//cout << uniformIndex << " shadowMap" << endl;
rt3d::setUniformMatrix4fv(toonShaderProgram, "projection", glm::value_ptr(projection));
uniformIndex = glGetUniformLocation(toonShaderProgram, "modelMatrix");
glUniformMatrix4fv(uniformIndex, 1, GL_FALSE, glm::value_ptr(depthModelMatrix));
cout << uniformIndex << " modelMatrix" << endl;
}
mvStack.top() = glm::lookAt(eye, at, up);
//render scene
我的阴影贴图着色器 .vert
#version 330
// Input vertex data, different for all executions of this shader.
in vec3 in_Position;
// Values that stay constant for the whole mesh.
uniform mat4 depthMVP;
uniform mat4 modelMatrix;
uniform mat4 projection;
void main(){
vec4 vertexPosition = modelMatrix * vec4(in_Position,1.0);
gl_Position = depthMVP * vertexPosition;
}
.frag
#version 330
void main(){
// Not really needed, OpenGL does it anyway
gl_FragColor = vec4(gl_FragCoord.z);
}
和我的Phong着色器 .vert
...
out vec4 ShadowCoord;
uniform mat4 modelMatrix;
void main(void) {
...
gl_Position = projection * vertexPosition;
vec4 shadowPos = (modelMatrix * vec4(in_Position,1.0));
ShadowCoord = depthBiasMVP * shadowPos;
ShadowCoord = ShadowCoord/ ShadowCoord.w;
.frag
....
if ((ShadowCoord.x < 0 || ShadowCoord.x > 1 || ShadowCoord.y < 0 || ShadowCoord.y > 1 || ShadowCoord.z < 0 || ShadowCoord.z > 1)){
visibility = 0.5f;
}else{
float shadowDepth = texture(shadowMap, ShadowCoord.xy).r;
if(shadowDepth<ShadowCoord.z-bias)
visibility = 0.0f;
}
// Ambient intensity
vec4 ambientI = light.ambient * material.ambient;
// Diffuse intensity
vec4 diffuseI = light.diffuse * material.diffuse *visibility;
diffuseI = diffuseI * max(dot(normalize(ex_N),normalize(ex_L)),0);
// Specular intensity
// Calculate R - reflection of light
vec3 R = normalize(reflect(normalize(-ex_L),normalize(ex_N)));
vec4 specularI = light.specular * material.specular *visibility;
specularI = specularI * pow(max(dot(R,ex_V),0), material.shininess);
// Fragment colour
out_Color = (ambientI + diffuseI + specularI) * texture(textureUnit0, ex_TexCoord);
我真的无法弄清楚我做错了什么。我所有的场景都投射在阴影中。 我尝试了各种各样的事情,包括在屏幕上渲染我的阴影贴图(如图所示),并使用不同的矩阵进行计算,但我仍然无法弄明白。
我计算我的ShadowCoords的空间是否重要,我一直在使用modelview,模型会更好吗?