How to access depth buffer values? - Or: gl_FragCoord.z vs ...

文章推薦指數: 80 %
投票人數:10人

gl_FragCoord.z is the window-space depth value of the current fragment. It has nothing to do with the value stored in the depth buffer. The value ... OpenGL-Howtoaccessdepthbuffervalues?-Or:gl_FragCoord.zvs.Renderingdepthtotexture Opengl Glsl Shader FragmentShader Solution1: [1] Onquestion1:Youcan'tdirectlyreadfromthedepthbufferinthefragmentshader(unlesstherearerecentextensionsI'mnotfamiliarwith).YouneedtorendertoaFrameBufferObject(FBO).Typicalsteps: CreateandbindanFBO.LookupcallslikeglGenFramebuffersandglBindFramebufferifyouhavenotusedFBOsbefore. Createatextureorrenderbuffertobeusedasyourcolorbuffer,andattachittotheGL_COLOR_ATTACHMENT0attachmentpointofyourFBOwithglFramebufferTexture2DorglFramebufferRenderbuffer.Ifyouonlycareaboutthedepthfromthisrenderingpass,youcanskipthisandrenderwithoutacolorbuffer. Createadepthtexture,andattachittotheGL_DEPTH_ATTACHMENTattachmentpointoftheFBO. Doyourrenderingthatcreatesthedepthyouwanttouse. UseglBindFramebuffertoswitchbacktothedefaultframebuffer. Bindyourdepthtexturetoasamplerusedbyyourfragmentshader. Yourfragmentshadercannowsamplefromthedepthtexture. Onquestion2:gl_FragCoord.zisthedepthvalueofthefragmentthatyourshaderisoperatingon,notthecurrentvalueofthedepthbufferatthefragmentposition. Solution2: [2] gl_FragCoord.zisthewindow-spacedepthvalueofthecurrentfragment.Ithasnothingtodowiththevaluestoredinthedepthbuffer.Thevaluemaylaterbewrittentothedepthbuffer,ifthefragmentisnotdiscardedanditpassesastencil/depthtest. Technicallytherearesomehardwareoptimizationsthatwillwrite/testthedepthearly,butforallintentsandpurposesgl_FragCoord.zisnotthevaluestoredinthedepthbuffer. Unlessyourenderinmultiplepasses,youcannotreadandwritetothedepthbufferinafragmentshader.Thatistosay,youcannotuseadepthtexturetoreadthedepthandthenturnaroundandwriteanewdepth.Thisisakintotryingtoimplementblendinginthefragmentshader,unlessyoudosomethingexoticwithDX11classhardwareandimageload/store,itjustisnotgoingtowork. Ifyouonlyneedthedepthofthefinaldrawnsceneforsomethinglikeshadowmapping,thenyoucandoadepth-onlypre-passtofillthedepthbuffer.Inthesecondpass,youwouldreadthedepthbufferbutnotwritetoit. Credits ThisarticlefollowstheattributionrequirementsofStackOverflowandislicensedunderCCBY-SA3.0. Source:StackOverflow Solution Credit Solution1 RetoKoradi Solution2 AndonM.Coleman RelatedQuestions AndroidOpenGLESFramebufferobjects-renderingdepth-buffertotexture OpenGLEStovideoiniOS(renderingtoatexturewithiOS5texturecache) Renderthedepthbufferintoatextureusingaframebuffer HowtoefficientlycopydepthbuffertotextureonOpenGLES Howtousedepthtestingwhenrenderingtoanoffscreenbufferthenontotexture Opengles:RendertoTextureviaFrameBufferisrenderingonlyonecolor WritingtodepthbufferwithoutdepthtestinginOpenGL(nonshader) opengldepthbufferslowwhenpointshavesamedepth InitializingOpenGLwithdepthbufferanddepthtesting OpenGL:Acquiringonlyastencilbufferandnodepthbuffer? WhydowehavetocleardepthbufferinOpenGLduringrendering? Efficientwayofreadingdepthvaluesfromdepthbuffer Applyingpartofatexture(spritesheet/texturemap)toapointspriteiniOSOpenGLES2.0 InopenGL.Howdoyoucopypartofatexturetoanothertexture TilingtexturebmpfileastextureontoarectangleinOpenGL? UsingOpenGLEStexturecachesinsteadofglReadPixelstogettexturedata OpenGLrender-to-texture-via-FBO--incorrectdisplayvs.normalTexture SDLOpenGLinC++,TextureShadermissingthetexture OpenGLESrendertotexture,thendrawtexture texturemappingatrapezoidwithasquaretextureinOpenGL Top



請為這篇文章評分?