How to access depth buffer values? - Or: gl_FragCoord.z vs ...
文章推薦指數: 80 %
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
延伸文章資訊
- 1gl_FragCoord.z - OpenGL: Advanced Coding - Khronos Forums
gl_FragCoord is window-relative (screen) coordinates. .xy is pixel units of the center of the fra...
- 2opengl glsl 深度計算 - w3c學習教程
gl_fragcoord和gl_fragdepth分別是片元著色器的輸入和輸出變數。 gl_fragcoord是個vec4,四個分量分別對應x, y, z和1/w。其中,x和y ...
- 3LearnOpenGL - Depth Testing
The x and y components of gl_FragCoord represent the fragment's screen-space coordinates (with (0...
- 4How to access depth buffer values? - Or: gl_FragCoord.z vs ...
gl_FragCoord.z is the window-space depth value of the current fragment. It has nothing to do with...
- 5关于GLSL的gl_FragCoord、gl_FragDepth以及深度计算
gl_FragCoord和gl_FragDepth分别是片元着色器的输入和输出变量。 gl_FragCoord是个vec4,四个分量分别对应x, y, z和1/w。其中,x和y是当前片元的窗口 ...