java - JOGL 2.0, render depth buffer to texture -
i trying implement simple shadow mapping technique in jogl 2.0 , struggle rendering depth values texture. maybe doing wrong weird rendering scene in color works properly. have found similar question here @ stackoverflow, asked here: render depth buffer texture using frame buffer
and problem solved calling
gl.gldrawbuffer(gl2.gl_none); gl.glreadbuffer(gl2.gl_none);
however, not in case. when render scene in texture in color normally, function works properly. here result:
however, after trying render depth values, renders white color (and doesn't correspond scene @ all)
---- updated code, working now:
private void initializefbo3(gl2 gl) { //create frame buffer gl.glgenframebuffers(1, framebufferid, 0); gl.glbindframebuffer(gl2.gl_framebuffer, framebufferid[0]); // ------------- depth buffer texture -------------- gl.glgentextures(1,depthbufferid,0); gl.glbindtexture(gl2.gl_texture_2d, depthbufferid[0]); gl.glteximage2d(gl2.gl_texture_2d, // target texture type 0, // mipmap lod level gl2.gl_depth_component, // internal pixel format //gl2.gl_depth_component shadowmapwidth, // width of generated image shadowmapheight, // height of generated image 0, // border of image gl2.gl_depth_component, // external pixel format gl2.gl_unsigned_int, // datatype each value null); // buffer store texture in memory //some parameters gl.gltexparameteri(gl2.gl_texture_2d, gl2.gl_texture_min_filter, gl2.gl_nearest); gl.gltexparameteri(gl2.gl_texture_2d, gl2.gl_texture_mag_filter, gl2.gl_nearest); gl.gltexparameteri(gl2.gl_texture_2d, gl2.gl_texture_wrap_s, gl2.gl_clamp_to_edge); gl.gltexparameteri(gl2.gl_texture_2d, gl2.gl_texture_wrap_t, gl2.gl_clamp_to_edge); //attach 2d texture fbo gl.glframebuffertexture2d(gl2.gl_framebuffer, gl2.gl_depth_attachment, gl2.gl_texture_2d, depthbufferid[0],0); gl.glbindtexture(gl2.gl_texture_2d, 0); //disable color buffer //https://stackoverflow.com/questions/12546368/render-the-depth-buffer-into-a-texture-using-a-frame-buffer gl.gldrawbuffer(gl2.gl_none); gl.glreadbuffer(gl2.gl_none); //set pixels ((width*2)* (height*2)) //it has have twice size of shadowmap size pixels = glbuffers.newdirectbytebuffer(shadowmapwidth*shadowmapheight*4); //set default frame buffer before doing check //http://www.opengl.org/wiki/fbo#completeness_rules gl.glbindframebuffer(gl2.gl_framebuffer, 0); int status = gl.glcheckframebufferstatus(gl2.gl_framebuffer); // check our framebuffer ok if(gl.glcheckframebufferstatus(gl2.gl_framebuffer) != gl2.gl_framebuffer_complete) { system.err.println("can not use fbo! status error:" + status); } } public void display(glautodrawable drawable) { gl2 gl = drawable.getgl().getgl2(); // opengl graphics context gl.glclear(gl2.gl_color_buffer_bit | gl2.gl_depth_buffer_bit); gl.glloadidentity(); // reset model-view matrix //render scene frame buffer first gl.glbindframebuffer(gl2.gl_framebuffer, framebufferid[0]); rendersmallscene(gl); //read pixels buffer gl.glbindframebuffer(gl2.gl_read_framebuffer, framebufferid[0]); //read pixels gl.glreadpixels(0, 0, shadowmapwidth, shadowmapheight, gl2.gl_depth_component , gl2.gl_unsigned_byte, pixels); //switch default fbo gl.glbindframebuffer(gl2.gl_framebuffer, 0); drawsceneobjects(gl); //draw pixels, format has have 1 gl.gldrawpixels(shadowmapwidth, shadowmapheight, gl2.gl_luminance , gl2.gl_unsigned_byte, pixels); }
working result:
you must read using fbo , opengl in general.
in code create fbo , attachments in each frame. that's wrong.it's huge overhead.construct fbos on init once.second, must bind fbo in order draw (or read it), otherwise opengl draw default fbo.take here , here
so ,once fbo ready render this:
glbindframebuffer((gl_draw_framebuffer, yourfbo); drawsceneobjects(gl); glbindframebuffer((gl_read_framebuffer, yourfbo); readpixelshere() glbindframebuffer((gl_framebuffer, 0);///switch default fbo
in fact , in case ,as leave fbo bound,just call
glbindframebuffer((gl_read_framebuffer, yourfbo);
after drawing geometry.
also , if not using shaders there no reason use textures fbo attachments.create render buffer instead.
Comments
Post a Comment