How can I get depth value from depth texture?
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 * de开发者_开发问答pth; // 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?
depth texture's type is GL_DEPTH_COMPONENT, GL_FLOAT
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.
精彩评论