c++ - How to fill the deph buffer in OpenGL 1 with a depth texture? -
i separate moveable 3d object , non moveable 3d. can generate 2 textures non moveables : background color texture , background depth texture . have no problem. values ok both. check value gray gradient texture depth.
and write / fill color buffer , depth buffer in opengl texture depth doesn't work (the color buffer fine on screen).
my goal never recompute non movable 3d objects when camera doesn't move.
(rem : depth buffer bits size in 16 bits)
this code generate depth texture :
u16 m_backgrounddepthbuffer[4096 * 4096]; // it's member of class glreadpixels(0,0, width(), height(), gl_depth_component, gl_unsigned_short, (void*)m_backgrounddepthbuffer); // allocate gpu-memory depth-texture. gldeletetextures(1, &m_backgrounddepthtextureid); glgentextures(1, &m_backgrounddepthtextureid); glbindtexture(gl_texture_2d, m_backgrounddepthtextureid); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_nearest); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_nearest); glteximage2d(gl_texture_2d, 0, gl_depth_component16, width(), height(), 0, gl_depth_component, gl_unsigned_short, m_backgrounddepthbuffer);
this code filling color buffer :
void application::render() { glenable(gl_depth_test); gldepthfunc(gl_less); glclear(gl_depth_buffer_bit|gl_color_buffer_bit); // render background render::renderimage(m_backgroundtextureid, 0,0, width(), height()); // debug // render::renderimage(m_backgrounddepthgraytextureid, 0,0, width(), height()); // how fill depth buffer : it's work slow gldrawpixels(width(), height(), gl_depth_component, gl_unsigned_short, (glvoid*) m_backgrounddepthbuffer); // // doesn't work // render::renderdepth(m_backgrounddepthtextureid, 0,0, width(), height()); }
this render::renderimage() : works
void render::renderimage(u32 tex, int x, int y, int w, int h, float anchorx, float anchory) { glclear(gl_depth_buffer_bit); glboolean depth = glisenabled(gl_depth_test); if (depth) gldisable(gl_depth_test); glmatrixmode(gl_projection); glpushmatrix(); glloadidentity(); gluortho2d(0, s_width, s_height, 0); glmatrixmode(gl_modelview); glpushmatrix(); glloadidentity(); glcolor3ub(255, 255, 255); glenable(gl_texture_2d); glbindtexture(gl_texture_2d, tex); glenableclientstate(gl_vertex_array); glenableclientstate(gl_texture_coord_array); gldisableclientstate(gl_color_array); x -= (int)(anchorx * w); y -= (int)(anchory * h); gvertices[0] = vector3f(x, y, 0); gvertices[1] = vector3f(x + w - 1, y, 0); gvertices[2] = vector3f(x + w - 1, y + h - 1, 0); gvertices[3] = vector3f(x, y + h - 1, 0); gtexcoords[0] = vector2f(0, 1); gtexcoords[1] = vector2f(1, 1); gtexcoords[2] = vector2f(1, 0); gtexcoords[3] = vector2f(0, 0); gindexes[0] = 2; gindexes[1] = 1; gindexes[2] = 0; gindexes[3] = 0; gindexes[4] = 3; gindexes[5] = 2; glvertexpointer(3, gl_float, 0, gvertices); gltexcoordpointer(2, gl_float, 0, gtexcoords); gldrawelements(gl_triangles, 3 * 2, gl_unsigned_short, gindexes); gldisable(gl_texture_2d); glpopmatrix(); glmatrixmode(gl_projection); glpopmatrix(); glmatrixmode(gl_modelview); if (depth) glenable(gl_depth_test); }
this render::renderdepth() : doesn't work / have texture depth = 0.0f instead use depth of each texel. want change depth buffer , nothing color buffer render::renderdepth() similar render::renderimage() : render 2 triangles
void render::renderdepth(u32 tex, int x, int y, int w, int h, float anchorx, float anchory) { glclear(gl_depth_buffer_bit); /*gltexparameteri(gl_texture_2d_array, gl_texture_compare_mode, gl_compare_ref_depth_to_texture_ext); gltexparameteri(gl_texture_2d_array, gl_texture_compare_func, gl_always); gltexparameteri(gl_texture_2d_array, gl_depth_texture_mode, gl_alpha); */ glboolean depth = glisenabled(gl_depth_test); //if (depth) glenable(gl_depth_test); glmatrixmode(gl_projection); glpushmatrix(); glloadidentity(); gluortho2d(0, s_width, s_height, 0); glmatrixmode(gl_modelview); glpushmatrix(); glloadidentity(); glcolor3ub(255, 255, 255); glenable(gl_texture_2d); glbindtexture(gl_texture_2d, tex); glenableclientstate(gl_vertex_array); glenableclientstate(gl_texture_coord_array); gldisableclientstate(gl_color_array); x -= (int)(anchorx * w); y -= (int)(anchory * h); gvertices[0] = vector3f(x, y, 0.0); gvertices[1] = vector3f(x + w - 1, y, 0.0); gvertices[2] = vector3f(x + w - 1, y + h - 1, 0.0); gvertices[3] = vector3f(x, y + h - 1, 0.0); gtexcoords[0] = vector2f(0, 1); gtexcoords[1] = vector2f(1, 1); gtexcoords[2] = vector2f(1, 0); gtexcoords[3] = vector2f(0, 0); gindexes[0] = 2; gindexes[1] = 1; gindexes[2] = 0; gindexes[3] = 0; gindexes[4] = 3; gindexes[5] = 2; glvertexpointer(3, gl_float, 0, gvertices); gltexcoordpointer(2, gl_float, 0, gtexcoords); gldrawelements(gl_triangles, 3 * 2, gl_unsigned_short, gindexes); gldisable(gl_texture_2d); glpopmatrix(); glmatrixmode(gl_projection); glpopmatrix(); glmatrixmode(gl_modelview); }
Comments
Post a Comment