我正试着在遗留的OpenGL中实现阴影贴图(是的,我知道它被弃用了,但是),但它不能正常工作。几乎所有的东西都是黑色的(见图,在左下角我添加了一个光源的视角)。我检查了深度、纹理和投影,一切似乎都很适合我。
我在纹理单元0中有一个漫反射纹理,深度纹理在纹理单元1中。
我的初始化代码:
void InitScene()
{
light1_projection = glm::ortho(-10.0f, 10.0f, -10.0f, 10.0f, 1.0f, 100.0f);
light1_view = glm::lookAt(glm::vec3(30.0f, 35.0f, 30.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
camera_projection = glm::perspective(glm::radians(45.0f), 1.333f, 1.0f, 300.0f);
camera_view = glm::lookAt(glm::vec3(0.0f, 30.0f, 20.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
bias = glm::mat4(
0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 5.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f
);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glClearColor(0.2f, 0.2f, 1.0f, 1.0f);
LoadTexture(); // Loads the diffuse texture in TMU 0
InitShadowmapTexture();
}
void InitShadowmapTexture()
{
glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &shadowmap_texture);
glBindTexture(GL_TEXTURE_2D, shadowmap_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
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_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE, GL_LUMINANCE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT,
shadowmap_width, shadowmap_heigth, 0,
GL_DEPTH_COMPONENT, GL_FLOAT, nullptr);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR);
}
我的呈现代码:
void Render()
{
RenderShadowMap();
RenderNormalScene();
RenderNormalSceneLightView();
}
void DrawScene(const glm::mat4 &view, const glm::mat4 &projection)
{
const auto plane = glm::translate(view, glm::vec3(0.0f, 0.0f, 0.0f));
const auto cube1 = glm::translate(view, glm::vec3(0.0f, 3.0f, z)) *
glm::rotate(glm::mat4(1.0f), glm::radians(rotx), glm::vec3(1.0f, 0.0f, 0.0f)) *
glm::rotate(glm::mat4(1.0f), glm::radians(roty), glm::vec3(0.0f, 1.0f, 0.0f));
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(glm::value_ptr(projection));
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(view));
glActiveTexture(GL_TEXTURE1);
const auto planes = bias * light1_projection * light1_view;
glTexGenfv(GL_S, GL_EYE_PLANE, glm::value_ptr(glm::row(planes, 0)));
glTexGenfv(GL_T, GL_EYE_PLANE, glm::value_ptr(glm::row(planes, 1)));
glTexGenfv(GL_R, GL_EYE_PLANE, glm::value_ptr(glm::row(planes, 2)));
glTexGenfv(GL_Q, GL_EYE_PLANE, glm::value_ptr(glm::row(planes, 3)));
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(plane));
DrawPlane(20.0f, 20.0f);
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(glm::value_ptr(cube1));
DrawCube(size, size, size);
}
void RenderNormalScene()
{
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glCullFace(GL_BACK);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glViewport(0, 0, screen_width, screen_heigth);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
DrawScene(camera_view, camera_projection);
}
void RenderNormalSceneLightView()
{
glActiveTexture(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, screen_width / 6, screen_heigth / 6);
glCullFace(GL_BACK);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glViewport(0, 0, screen_width / 6, screen_heigth / 6);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
DrawScene(light1_view, light1_projection);
glDisable(GL_SCISSOR_TEST);
}
void RenderShadowMap()
{
glActiveTexture(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D);
glCullFace(GL_FRONT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glViewport(0, 0, shadowmap_width, shadowmap_heigth);
glClear(GL_DEPTH_BUFFER_BIT);
DrawScene(light1_view, light1_projection);
glActiveTexture(GL_TEXTURE1);
glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowmap_width, shadowmap_heigth);
}
你有什么办法解决这个问题吗?
1条答案
按热度按时间gupuwyp21#
您已将
bias
值设置为在第2行、第2列中,您已将值设置为
5.0f
,而不是0.5f
。这样做的效果是将z
值调整为5,而不是0.5;因此您的错误。正确的
bias
矩阵为