[英]opengl framebuffer depth texture not working
I've been trying to get shadow mapping to work , but the the depth isn't even writing to the texture . 我一直在尝试使阴影映射起作用,但是深度甚至都没有写入纹理。 Here's my code .
这是我的代码。
//this is outside the main loop to generate the depth texture and the framebuffer
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
GLuint depthTexture;
glGenTextures(1, &depthTexture);
glBindTexture(GL_TEXTURE_2D, depthTexture);
glTexImage2D(GL_TEXTURE_2D, 0,GL_DEPTH_COMPONENT, 1024,1024,0,GL_DEPTH_COMPONENT,GL_FLOAT,0); 0,GL_DEPTH_COMPONENT,GL_FLOAT,0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
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);
glBindTexture(GL_TEXTURE_2D, 0);
GLuint FramebufferName ;
glGenFramebuffers(1, &FramebufferName);
glBindFramebuffer(GL_FRAMEBUFFER , FramebufferName);
glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0);
glDrawBuffer(GL_NONE);
glBindFramebuffer(GL_FRAMEBUFFER , 0);
/////////////////this is inside the loop
glBindFramebuffer(GL_FRAMEBUFFER , FramebufferName);
glClearDepth(1.0f);
glClear(GL_DEPTH_BUFFER_BIT);
glViewport(0,0,1024,1024);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
cout<<" WARNING !! ";
Matrix DephtViewMatrix = IDENTITY_MATRIX;
DephtViewMatrix = MultiplyMatrices(DephtViewMatrix , TRANSLATION_MATRIX(0,-25,0)); //my own math header
DephtViewMatrix = MultiplyMatrices(DephtViewMatrix , XROTATION_MATRIX(-1.6));
shadow.UseProgram();
shadow.CreateProjection(60,1024,1024,0.1 ,70);//also tried to change to orthographic projection , but it didn't help
shadow.setMVP(DephtViewMatrix , IDENTITY_MATRIX);
sphere.draws();
terrain.draws();
glBindFramebuffer(GL_FRAMEBUFFER ,0);
//vetrex shader
#version 330
layout(location=0) in vec3 in_Position;
uniform mat4 ViewMatrix;
uniform mat4 ProjectionMatrix;
void main(void)
{
gl_Position = ProjectionMatrix * ViewMatrix * vec4(in_Position.xyz ,1) ;
}
//fragment shader
#version 330
layout(location = 0) out float depth;
void main()
{
depth = gl_FragCoord.z;
}
I'm drawing the texture on a cube , the scene draws correctly when drawing it with colors . 我在立方体上绘制纹理,用颜色绘制时场景正确绘制。
Can you please edit your question to show the shader you are using to sample the depth texture? 您可以编辑问题以显示用于采样深度纹理的着色器吗?
Immediately, I notice that you have texture comparison set to GL_COMPARE_R_TO_TEXTURE
; 马上,我注意到您已将纹理比较设置为
GL_COMPARE_R_TO_TEXTURE
; this means that unless you sample the texture using sampler2DShadow
, samplerCubeShadow
, etc. that the results will be undefined. 这意味着除非您使用
sampler2DShadow
, samplerCubeShadow
等对纹理进行sampler2DShadow
, samplerCubeShadow
结果将是不确定的。 Textures with comparison mode != GL_NONE
must be sampled using a ...Shadow
sampler, likewise textures with comparison mode == GL_NONE
must not be sampled with a ...Shadow
sampler. 具有比较模式!=
GL_NONE
纹理必须使用...Shadow
采样器采样,同样,具有比较模式== GL_NONE
纹理也不能使用...Shadow
采样器采样。
And even if you use the appropriate sampler type for your depth texture, if you have texture comparison enabled then the result of sampling the texture is going to be a binary pass/fail ( 1.0 or 0.0 ) result of performing a depth test on the stored depth, rather than the depth itself. 即使为深度纹理使用了适当的采样器类型,如果启用了纹理比较,则对纹理进行采样的结果将是对存储的图像执行深度测试的二进制通过/失败( 1.0或0.0 )结果深度,而不是深度本身。 You really cannot visualize the depth stored in a shadow map using texture comparison.
您实际上无法使用纹理比较来可视化存储在阴影贴图中的深度。 But the good news is that you can change the comparison mode on-the-fly depending on how you intend to use the depth texture, it just needs to match your sampler type in the shader you are using.
但是好消息是,您可以根据打算使用深度纹理的方式即时更改比较模式,它只需要在使用的着色器中匹配采样器类型即可。
You might want to take a look at this related answer that I wrote, which explains the differences between shadow and non-shadow samplers. 您可能想看看我写的这个相关答案 ,它解释了阴影采样器和非阴影采样器之间的区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.