我目前正在我的应用程序中实施阴影贴图支持,并且我一直在遵循此链接中演示的过程:http://www.fabiensanglard.net/shadowmapping/index.php
但是,我注意到他正在使用纹理矩阵来投影阴影贴图,但我想在不使用纹理矩阵的情况下进行。
我渲染场景的方式如下:
在我的顶点着色器中,我有一个不同的vec4 shadowCoord变量,它的计算方法是将shadowMatrix制服与gl_ModelViewMatrix和gl_Vertex相乘(shadowCoord = shadowMatrix * gl_ModelViewMatrix * gl_Vertex)
片段着色器与上面链接中提到的完全相同。结果显示场景完全是黑色或阴影设置的阴影/颜色。
我假设我将偏差,lightProjectionMatrix,lightModelViewMatrix与相机的模型视图矩阵的倒数相乘时做错了。有人可以指出我做错了吗?
编辑:我希望有人能够识别问题,而不必粘贴代码,因为复杂性,但我会尝试粘贴我能做的事情。对不起,如果代码是一团糟,我不得不删除很多简单的代码:int vp[4];
kexMatrix lightProj, lightMV;
kexCamera tmpCam;
renderBackend.SetColorMask(0);
// save actual camera viewport
glGetIntegerv(GL_VIEWPORT, vp);
// set viewport to framebuffer object size (512x512)
// smFbo is my framebuffer class used for the shadowmap/depth
glViewport(0, 0, smFbo.Width(), smFbo.Height());
// bind framebuffer
smFbo.Bind();
glClear(GL_DEPTH_BUFFER_BIT);
// setup temp. camera for light source
tmpCam.SetOrigin(kexVec3(-2048, 860, 450)); // x, y, z with y being up, and z being forward/back
tmpCam.SetAngles(kexAngle(1.0f, 0.5f, 0.0f)); // yaw, pitch and roll in radians
tmpCam.SetupMatrices();
lightProj = tmpCam.Projection();
lightMV = tmpCam.ModelView();
// setup matrices for OGL
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(lightProj.ToFloatPtr());
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(lightMV.ToFloatPtr());
bShadowPass = true;
// note that each static actor object has its own matrix so before drawing I do:
// glPushMatrix();
// glMultMatrixf(actor->Matrix());
// draw and pop matrix
DrawStaticActors();
bShadowPass = false;
// unbind framebuffer object and restore viewport
smFbo.UnBind();
glViewport(vp[0], vp[1], vp[2], vp[3]);
renderBackend.SetColorMask(1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
kexMatrix bias;
bias.vectors[0].Set(0.5f, 0, 0, 0);
bias.vectors[1].Set(0, 0.5f, 0, 0);
bias.vectors[2].Set(0, 0, 0.5f, 0);
bias.vectors[3].Set(0.5f, 0.5f, 0.5f, 1.0f);
// setup matrices from world camera. handles gl projection/modelview setup as well
SetCameraView(world->Camera());
// shadowmap matrix for GLSL shader
shadowMatrix = bias * lightProj * lightMV * kexMatrix::Invert(world->Camera()->ModelView());
// draw scene as normal and sets up the shader
// shadowMap sampler2D uniform is binded to 1
// also calls smFbo.BindImage() which binds the actual texture to texture unit # 1
// shadowMatrix is also sent as uniform to shader
DrawStaticActors();
DEPTH FRAMEBUFFER SETUP
// texture
glGenTextures(1, &fboTexId);
glBindTexture(GL_TEXTURE_2D, fboTexId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_DEPTH_COMPONENT,
fboWidth,
fboHeight,
0,
GL_DEPTH_COMPONENT,
GL_FLOAT,
0);
// framebuffer
glGenFramebuffers(1, &fboId);
glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glFramebufferTexture2D(GL_FRAMEBUFFER_EXT,
GL_DEPTH_ATTACHMENT_EXT,
GL_TEXTURE_2D,
fboTexId,
0);
CheckStatus();
glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);
FBO BIND FUNCTION
glBindFramebuffer(GL_FRAMEBUFFER_EXT, fboId);
glReadBuffer(GL_NONE);
glDrawBuffer(GL_NONE);
FBO UNBIND FUNCTION
glBindFramebuffer(GL_FRAMEBUFFER_EXT, 0);
glDrawBuffer(GL_BACK);
glReadBuffer(GL_BACK);
FBO BIND IMAGE FUNCTION
int unit = renderBackend.glState.currentUnit;
dtexture currentTexture = renderBackend.glState.textureUnits[unit].currentTexture;
if(fboTexId == currentTexture) {
return;
}
glBindTexture(GL_TEXTURE_2D, fboTexId);
renderBackend.glState.textureUnits[unit].currentTexture = fboTexId;
VERTEX SHADER
//varying vec3 vertex_normal;
varying vec4 shadowCoord;
uniform mat4 shadowMatrix;
void main() {
gl_FrontColor = gl_Color;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;
gl_TexCoord[0] = gl_MultiTexCoord0;
//vertex_normal = normalize(gl_NormalMatrix * gl_Normal);
shadowCoord = shadowMatrix * (gl_ModelViewMatrix * gl_Vertex);
}
FRAGMENT SHADER
uniform sampler2D diffuse;
uniform sampler2D shadowMap;
//varying vec3 vertex_normal;
varying vec4 shadowCoord;
void main() {
vec4 color = texture2D(diffuse, gl_TexCoord[0].st);
vec4 shadowCoordinateWdivide = shadowCoord / shadowCoord.w;
float shadow = 1.0;
if(shadowCoordinateWdivide.x >= 0.0 && shadowCoordinateWdivide.x < 1.0 &&
shadowCoordinateWdivide.y >= 0.0 && shadowCoordinateWdivide.y < 1.0 &&
shadowCoordinateWdivide.z >= 0.0 && shadowCoordinateWdivide.z < 1.0) {
// Used to lower moiré pattern and self-shadowing
shadowCoordinateWdivide.z += 0.0005;
float distanceFromLight = texture2D(shadowMap, shadowCoordinateWdivide.st).z;
if(shadowCoord.w > 0.0) {
shadow = distanceFromLight < shadowCoordinateWdivide.z ? 0.0 : 1.0;
}
}
gl_FragColor = vec4((color * shadow).xyz, color.a);
}