如何从深度纹理中获取深度值?
我有完整填充的深度纹理,但似乎没有从中获得正确的值。
GLSL 代码,片段着色器:
uniform sampler2D depth_tex;
uniform float viewport_w;
uniform float viewport_h;
void main()
{
vec2 coord = vec2(gl_FragCoord.xy);
float depth = texture2D(depth_tex, vec2(coord.x / viewport_w, coord.y / viewport.h)).r;
float c = material_amb * depth; // material_amb is preset by host
out_color = vec4(c, c, c, 1.0f);
}
我预计模型的颜色将根据顶点的深度而改变,但我看不到所有内容。当然,我可以在不增加深度的情况下看到模型。深度纹理的类型是GL_DEPTH_COMPONENT、GL_FLOAT。有什么问题吗?我认为从纹理获取深度值的解决方案是错误的,但我不知道原因。深度值存储在哪里?我怎样才能得到深度值?
I have complete-filled depth texture, but it seems not to get a proper value from it.
GLSL code, fragment shader:
uniform sampler2D depth_tex;
uniform float viewport_w;
uniform float viewport_h;
void main()
{
vec2 coord = vec2(gl_FragCoord.xy);
float depth = texture2D(depth_tex, vec2(coord.x / viewport_w, coord.y / viewport.h)).r;
float c = material_amb * depth; // material_amb is preset by host
out_color = vec4(c, c, c, 1.0f);
}
I expect model's color will be changed by vertices' depth, but I can't see everything. Of course, I can see model without multiplying depth. depth texture's type is GL_DEPTH_COMPONENT, GL_FLOAT. What's the problem? I think a solution to get a depth value from a texture is wrong, but I don't know the reason. Where is a depth value stored in? And How can I get a depth value?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这没有意义。
GL_FLOAT
不是 OpenGL 图像格式。GL_DEPTH_COMPONENT
是一种图像格式,但它是无尺寸的。它指定未指定大小的无符号、标准化整数深度格式。GL_FLOAT
可能是您在 glTexImage 调用中放在倒数第二个参数中的内容,对吧?即像素传输参数;它描述了您复制到纹理的像素数据,而不是像素数据在纹理内的存储方式。 OpenGL 可以在多种不同类型的数据格式之间进行转换。如果您的深度数据来自渲染到深度缓冲区,那么您必须了解如何从非线性后投影空间转换为线性预投影空间,然后才能对该数据执行有意义的操作。 此答案展示了如何进行转换。
This makes no sense.
GL_FLOAT
is not an OpenGL image format.GL_DEPTH_COMPONENT
is an image format, but it is an unsized one. It specifies an unsigned, normalized integer depth format of an unspecified size.GL_FLOAT
is probably what you put in the parameter second from the end in your glTexImage call, right? That is the pixel transfer parameter; it describes the pixel data you're copying to the texture, not the way the pixel data is stored within the texture. OpenGL can convert between many different kinds of data formats.If your depth data comes from rendering to the depth buffer, then you must understand how to convert from the non-linear post-projection space to a linear pre-projection space before you can do something meaningful with that data. This answer shows how to do the conversion.