我正在我的程序中实现阴影贴图,作为中间步骤,我希望能够看到第一次光通过后生成的深度纹理,但是我在屏幕上看到的只是红色,相关代码:< / p>
@Override
protected void render(final double msDelta) {
super.render(msDelta);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
//Light pass
glViewport(0, 0, 4096, 4096);
...
//Draw pass
glViewport(0, 0, screenWidth, screenHeight);
...
//Visualize depth texture
if (visualDepthTexture) {
glViewport(100, screenHeight - (screenHeight / 5) - 100, screenWidth / 5, screenHeight / 5);
glClearDepthf(1f);
glClear(GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
depthTexture.bind();
glDrawBuffer(GL_BACK);
depthBox.draw(0);
depthTexture.unbind();
}
}
其中depthTexture
:
//shadow FBO and texture
depthFBO = new FrameBufferObject().create().bind();
depthTexture = new Texture2D().create().bind()
.storage2D(11, GL_DEPTH_COMPONENT32F, 4096, 4096) //11 is log2(4096)
.minFilter(GL_LINEAR)
.magFilter(GL_LINEAR)
.compareMode(GL_COMPARE_REF_TO_TEXTURE)
.compareFunc(GL_LEQUAL);
depthFBO.texture2D(GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, depthTexture, 0)
.unbind();
depthTexture.unbind();
depthBox.draw(0)
将使用以下着色器在GL_TRIANGLES
模式下绘制6个顶点:
depth-box.vs.glsl
#version 430 core
void main(void) {
const vec4 vertices[6] = vec4[](
vec4(-1.0, -1.0, 1.0, 1.0),
vec4(-1.0, 1.0, 1.0, 1.0),
vec4(1.0, 1.0, 1.0, 1.0),
vec4(1.0, 1.0, 1.0, 1.0),
vec4(1.0, -1.0, 1.0, 1.0),
vec4(-1.0, -1.0, 1.0, 1.0)
);
gl_Position = vertices[gl_VertexID];
}
depth-box.fs.glsl
#version 430 core
layout(binding = 0) uniform sampler2D shadow_tex;
out vec4 color;
void main(void) {
color = vec4(texture2D(shadow_tex, gl_FragCoord.xy));
}
我想要实现的是深度纹理被转换为灰度(以查看阴影应该出现的位置),并且小视口中的两个三角形会得到该结果的纹理。