opengl - Rendering in Framebuffer does not effect while GL_DEPTH_TEST enabled -
i trying render in frame buffer.
so made framebuffer depth texture , color texture attached shown here : http://www.opengl.org/wiki/framebuffer_object_examples#color_texture.2c_depth_texture
but nothing rendered if gl_depth_test enabled. doing wrong?
the whole code long...
frame buffer class :
class framebuffer { gluint id; size_t width, height, ncolorattachments; gluint colortexture[gl_max_color_attachments_ext]; gluint depthtexture; private: void attachdepthtexture(); void attachcolortexture(gluint); public: void create(size_t, size_t, size_t); void binddepthtexture() const; void bindcolortexture(gluint) const; void bind() const; void unbind() const; size_t getwidth() const; size_t getheight() const; }; void framebuffer::attachdepthtexture() { glgentextures(1, &depthtexture); glbindtexture(gl_texture_2d, depthtexture); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp); gltexparameteri(gl_texture_2d, gl_texture_compare_mode, gl_compare_r_to_texture); gltexparameteri(gl_texture_2d, gl_texture_compare_func, gl_lequal); gltexparameteri(gl_texture_2d, gl_depth_texture_mode, gl_intensity); glteximage2d(gl_texture_2d, 0, gl_depth_component24, width, height, 0, gl_depth_component, gl_unsigned_byte, null); glbindtexture(gl_texture_2d, 0); glframebuffertexture2dext(gl_framebuffer_ext, gl_depth_attachment_ext, gl_texture_2d, depthtexture, 0); } void framebuffer::attachcolortexture(gluint i) { i=clamp(i, 0, gl_max_color_attachments_ext-1); glgentextures(1, &colortexture[i]); glbindtexture(gl_texture_2d, colortexture[i]); gltexparameteri(gl_texture_2d, gl_texture_min_filter, gl_linear); gltexparameteri(gl_texture_2d, gl_texture_mag_filter, gl_linear); gltexparameterf(gl_texture_2d, gl_texture_wrap_s, gl_clamp); gltexparameterf(gl_texture_2d, gl_texture_wrap_t, gl_clamp); glteximage2d(gl_texture_2d, 0, gl_rgba8, width, height, 0, gl_bgra, gl_unsigned_byte, null); glbindtexture(gl_texture_2d, 0); glframebuffertexture2dext(gl_framebuffer_ext, gl_color_attachment0_ext+i, gl_texture_2d, colortexture[i], 0); } void framebuffer::create(size_t width, size_t height, size_t ncolorattachments=1) { this->width=width; this->height=height; ncolorattachments=clamp(ncolorattachments, 0, gl_max_color_attachments_ext); this->ncolorattachments=ncolorattachments; glenum fbostatus; glgenframebuffersext(1, &id); glbindframebufferext(gl_framebuffer_ext, id); if(ncolorattachments==0) { gldrawbuffer(gl_none); glreadbuffer(gl_none); } attachdepthtexture(); for(int i=0; i<ncolorattachments; i++) attachcolortexture(i); fbostatus = glcheckframebufferstatusext(gl_framebuffer_ext); if(fbostatus != gl_framebuffer_complete_ext) showmessage("gl_framebuffer_complete_ext failed, cannot use fbo", "fbo create error"); glbindframebufferext(gl_framebuffer_ext, 0); } void framebuffer::binddepthtexture() const { glbindtexture(gl_texture_2d, depthtexture); } void framebuffer::bindcolortexture(gluint i=0) const { i=clamp(i, 0, gl_max_color_attachments_ext-1); glbindtexture(gl_texture_2d, colortexture[i]); } void framebuffer::bind() const { glbindframebufferext(gl_framebuffer_ext, id); } void framebuffer::unbind() const { glbindframebufferext(gl_framebuffer_ext, 0); } size_t framebuffer::getwidth() const { return width; } size_t framebuffer::getheight() const { return height; }
here used :
void game::init() { if(loaded) { terrain::init(); mainframebuffer.create(settings::screenwidth, settings::screenheight, 1); ... } void game::render() { glmatrixmode(gl_projection); glpushmatrix(); glloadidentity(); gluperspective(45.0, (float)game::settings::screenwidth/game::settings::screenheight, 0.1, 1000.0); glmatrixmode(gl_modelview); glpushmatrix(); glloadidentity(); setcamera(); frustum.updatefrustum(); if(settings::shadowon) makeshadow(); mainframebuffer.bind(); if(settings::renderskyon) rendersky(); if(settings::reflectionon) renderreflection(settings::terrainshaderon, settings::objectshaderon); if(settings::renderterrainon) renderterrain(settings::terrainshaderon, settings::shadowon); if(settings::renderobjectson) renderobjectsunderwater(); if(settings::renderwateron) renderwater(true); drawcursor(); if(settings::renderobjectson) renderobjects(settings::objectshaderon); drawmarkers(); renderprojectiles(); renderparticles(); mainframebuffer.unbind(); glmatrixmode(gl_projection); glloadidentity(); glortho(-1, 1, -1, 1, 1, -1); glmatrixmode(gl_modelview); glloadidentity(); glenable(gl_texture_2d); mainframebuffer.bindcolortexture(); gldrawrectangle(-1, 1, 1, -1); texture2d::bindnone(); gldisable(gl_texture_2d); glmatrixmode(gl_modelview); glpopmatrix(); glmatrixmode(gl_projection); glpopmatrix(); }
the reflection rendered turning off depth test. appears.
sorry, forgot clear new framebuffer.
Comments
Post a Comment