c - OpenGL - Frame Buffer Depth Texture differs from Color Depth Texture -
i'm doing shadow mapping in opengl - such i've created frame buffer object render depth of scene view of light.
glbindrenderbuffer(gl_renderbuffer, color_buffer); glrenderbufferstorage(gl_renderbuffer, gl_rgba, width, height); glframebufferrenderbuffer(gl_framebuffer, gl_color_attachment0, gl_renderbuffer, color_buffer); glbindrenderbuffer(gl_renderbuffer, depth_buffer); glrenderbufferstorage(gl_renderbuffer, gl_depth_component24, width, height); glframebufferrenderbuffer(gl_framebuffer, gl_depth_attachment, gl_renderbuffer, depth_buffer); glgentextures(1, &color_texture); glbindtexture(gl_texture_2d, color_texture); glteximage2d(gl_texture_2d, 0, gl_rgba, width, height, 0, gl_rgba, gl_unsigned_byte, null); 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_texture_wrap_s, gl_clamp); gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_clamp); glframebuffertexture2d(gl_framebuffer, gl_color_attachment0, gl_texture_2d, color_texture, 0); glgentextures(1, &depth_texture); glbindtexture(gl_texture_2d, depth_texture); glteximage2d(gl_texture_2d, 0, gl_depth_component24, width, height, 0, gl_depth_component, gl_float, null); 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_texture_wrap_s, gl_clamp); gltexparameteri(gl_texture_2d, gl_texture_wrap_t, gl_clamp); glframebuffertexture2d(gl_framebuffer, gl_depth_attachment, gl_texture_2d, depth_texture, 0);
this renders scene light perspective normal, expect. addition i'm using custom shader render depth of scene "color_texture".
varying vec4 screen_position; void main() { screen_position = gl_modelviewprojectionmatrix * gl_vertex; gl_position = screen_position; } -------------- varying vec4 screen_position; void main() { float depth = screen_position.z / screen_position.w; gl_fragcolor = vec4(depth, depth, depth, 1.0); }
i can write these 2 textures "color_texture" , "depth_texture" screen using full screen quad. both indeed depth maps , correct. weird bit.
when comes rendering shadows on objects sampling "color_texture" depth works fine, when switch sampling "depth_texture", depth different scale , constant.
when added fudge factor numbers sampled depth sort of work, difficult , felt horrible.
i can't tell wrong, technically 2 textures should identical when sampled. can't carry on using "color_texture" due accuracy of rgb. need switch can't life of me work out why depth texture gives different value.
i've programmed shadow mapping several times before , isn't concept new me.
can shed light on this?
there issues shader. first, screen_position
not screen position; that's clip-space vertex position. screen space relative monitor, , therefore change if moved window around. never screen-space positions of anything; opengl goes down window space (relative window's location).
second, this:
float depth = screen_position.z / screen_position.w;
does not compute depth. computes normalized-device coordinate (ndc) space z position, ranges [-1, 1]. depth window-space value, comes after ndc space value transformed viewport transform (specified glviewport , gldepthrange). transform puts depth on [0, 1] range.
most importantly third, you're doing manually; let glsl you:
void main() { gl_position = gl_modelviewprojectionmatrix * gl_vertex; } -------------- void main() { gl_fragcolor = vec4(gl_fragcoord.zzz, 1.0); }
see? better.
now:
when comes rendering shadows on objects sampling "color_texture" depth works fine, when switch sampling "depth_texture", depth different scale , constant.
of course is. that's because believed depth ndc z-value. opengl writes actual window-space z value depth. need work window space. opengl nice enough provide uniforms fragment shader make easier. can use uniforms transform ndc-space z values window space z values.
Comments
Post a Comment