c++ - Weird behaviour of Z-Buffer -
i'm using deferred shading method render scene, have problem skybox technique due weird behaviour of z-buffer. i've created additional framebuffer , attached 5 textures, 1 of being used depth buffer. first pass geometry pass, fills textures needed data, second pass renders final objects textures , lightning applied main framebuffer, last pass renders skybox. problem skybox being ignored (z-buffer check fails, invisible).
when rendering skybox, map secondary framebuffer gl_read_framebuffer , main framebuffer (screen) gl_draw_framebuffer, depth buffer of secondary framebuffer can used, set depth function gl_lequal. , if change depth function gl_greater (or gl_always) skybox can seen, being rendered on top of objects.
the vertex shader:
#version 330 layout (location = 0) in vec3 position; uniform mat4 m; uniform mat4 v; uniform mat4 p; out vec3 texcoord0; void main() { vec4 mvp_pos = p * v * m * vec4(position, 1.0); gl_position = mvp_pos.xyww; texcoord0 = position; }
model matrix product of translation of skybox camera coordinates.
gldepthfunction set gl_lequal:
gldepthfunction set gl_greater:
contents of z-buffer:
update 1: i've tried use glblitframebuffer that:
glbindframebuffer(gl_read_framebuffer, m_fbo); //additional framebuffer glbindframebuffer(gl_draw_framebuffer, 0); gldepthmask(gl_true); glblitframebuffer(0, 0, window_width, window_height, 0, 0, window_width, window_height, gl_depth_buffer_bit, gl_nearest);
but doesn't work. main depth buffer contains array of black pixels.
update 2:
//fbo initialization glgenframebuffers(1, &m_fbo); glbindframebuffer(gl_draw_framebuffer, m_fbo); // create gbuffer textures glgentextures(array_size_in_elements(m_textures), m_textures); glgentextures(1, &m_depthtexture); (unsigned int = 0; < array_size_in_elements(m_textures); i++) { glbindtexture(gl_texture_2d, m_textures[i]); gltexparameterf(gl_texture_2d, gl_texture_min_filter, gl_nearest); gltexparameterf(gl_texture_2d, gl_texture_mag_filter, gl_nearest); glteximage2d(gl_texture_2d, 0, gl_rgb32f, windowwidth, windowheight, 0, gl_rgb, gl_float, null); glframebuffertexture2d(gl_draw_framebuffer, gl_color_attachment0 + i, gl_texture_2d, m_textures[i], 0); } // depth buffer glbindtexture(gl_texture_2d, m_depthtexture); glteximage2d(gl_texture_2d, 0, gl_depth_component32f, windowwidth, windowheight, 0, gl_depth_component, gl_float, null); glframebuffertexture2d(gl_draw_framebuffer, gl_depth_attachment, gl_texture_2d, m_depthtexture, 0); glenum drawbuffers[] = { gl_color_attachment0, gl_color_attachment1, gl_color_attachment2, gl_color_attachment3 }; gldrawbuffers(array_size_in_elements(drawbuffers), drawbuffers); glenum status = glcheckframebufferstatus(gl_framebuffer); if (status != gl_framebuffer_complete) { fprintf(stderr, "fb error, status: 0x%x\n", status); return false; }
skybox buffers:
glfloat cube_vertices[] = { // front -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0, // -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, -1.0, -1.0, 1.0, -1.0, }; glushort cube_elements[] = { // front 0, 1, 2, 2, 3, 0, // top 3, 2, 6, 6, 7, 3, // 7, 6, 5, 5, 4, 7, // bottom 4, 5, 1, 1, 0, 4, // left 4, 0, 3, 3, 7, 4, // right 1, 5, 6, 6, 2, 1, };
when rendering skybox, map secondary framebuffer
gl_read_framebuffer
, main framebuffer (screen)gl_draw_framebuffer
, depth buffer of secondary framebuffer can used,
that not how things work. gl_read_framebuffer
not relevant depth test. used source opeations glreadpixels
or glblitframebuffer
. kind of rendering, gl_draw_framebuffer
relevant. includes depth test.
unfortuanly, can't mix user-defined render targets render buffers or textures window-system provided buffers, can't render final screen while using specific depth test. either have additional render pass fbo using custom depth buffer, , blit result screen, or can blit contents of custom depth buffer window-system provided 1 , render that.
Comments
Post a Comment