(gl_FragCoord.z / gl_FragCoord.w) for quick depth calculation ...
文章推薦指數: 80 %
You can refer to chapter 2.13 “Coordinate Transformations” of OpenGL specs, and to the fact, that gl_FragCoord.w in fragment shader is always 1/ ... (gl_FragCoord.z/gl_FragCoord.w)forquickdepthcalculation,cameratofragment? OpenGL OpenGL:AdvancedCoding Nick_Wiggill December4,2012,1:10pm #1 Lookingatthistutorial,Inoticedaninterestinglittlecalculationtogetper-fragmentdepthfromthecamera. floatz=gl_FragCoord.z/gl_FragCoord.w;Ifindthisworksforme,andthatitappearstobefairlyaccurateintermsofdistance. Now,certainotherpeopleareveryverycertainthatthisisnotavalidwaytogetdepth/distancefromthecamera. Cananyoneshedanylightontothefollowing: (a)Whetherthisisinfactavalidandaccuratewaytogetdepth; (b)Howthemathematicsbehinditwork. Dark_Photon December4,2012,7:10pm #2 Doesn’tmakemuchsensetome.gl_FragCoord.zshouldbea0…1window-spacedepthvalue.Andgl_FragCoord.w,forPERSPECTIVEprojectionsonly,shouldbe-1/(eye-spaceZ).Dividethoseandwhatdoyouget? Noclue. nuclear_bro December5,2012,1:01am #3 Youcanrefertochapter2.13“CoordinateTransformations”ofOpenGLspecs,andtothefact,thatgl_FragCoord.winfragmentshaderisalways1/clp_w. Thepathofeye_zinpipelineisasfollows: fromeye-spacetoclip-space:clp_z=(gl_ProjectionMatrix*eye_pos).z; fromclip-spacetonormalizeddevicecoordinates:ndc_z=clp_z/clp_w; fromndctowindowspace:wnd_z=ndc_z*(dfar-dnear)/2+(dfar+dnear)/2,wheredfaranddnearareparametersfromglDepthRangeandmostlyequalto0and1,soforsimplicitylet’sassumegl_FragCoord.z=wnd_z=(clp_z/clp_w)/2+0.5.Thus,whenyoudividethisbygl_FragCoord.w(whichisequaltomultiplybyclp_w)youwillgetclp_z/2+clp_w/2; Nowlet’shavealookatwhatisclp_zandclp_w.Ifyoulookatprojectionmatrix,andespeciallyatit’s3rdrow(whichisresponsibleforcalculationofclp_z),it’sclearlyseenthatclp_zistheproductofscaleandshiftoperationsuponeye_z:from[eye_near…eye_far]rangeto[-1…1]range,whileclp_wisequalto-eye_z(4throw);Thatway,youcanwriteyourequation(clp_z/2+clp_w/2)as:(Aeye_z+B)/2-eye_z/2=Ceye_z+D-whichisnotequaltoeye_zincommoncase,butislinearcombinationofitwithconstantcoefficients. Soyoucanuseitasameasuretocomparedepths,orasaparameterinanyformulathathavelineardependencyfromeye_zandetc.,butnotasdirectmeasureofeye_z.Forthatpurposeyouneedinversionofprojectionmatrix,orsimplystoreeye_zinvertexshaderandsendittofragmentshaderasavarying; imported_tonyo_au December5,2012,1:04am #4 ThisisthebestdescriptionIcanfindhttp://www.opengl.org/wiki/Compute_eye_space_from_window_space andhttp://mrl.nyu.edu/~dzorin/igf06/lecture11/lecture11.pdfisagoodread Nick_Wiggill December5,2012,7:20am #5 nuclear_bro;1245290: Soyoucanuseitasameasuretocomparedepths,orasaparameterinanyformulathathavelineardependencyfromeye_zandetc.,butnotasdirectmeasureofeye_z. This.ThisiswhatIwantedtoknow. Thankyou,nuclear_bro.
延伸文章資訊
- 1WebGL2 Fog
In between 0 and 1 you get a percentage of both colors. You could implement mix yourself like ......
- 2Getting the real fragment depth in GLSL - Game Development ...
Alternatively, you can get it recover it from gl_FragCoord... In a fragment shader, gl_FragCoord ...
- 3(gl_FragCoord.z / gl_FragCoord.w) for quick depth calculation ...
You can refer to chapter 2.13 “Coordinate Transformations” of OpenGL specs, and to the fact, that...
- 4View Space From gl_FragCoord - Game Development Stack ...
Space & Sound? Defying Physics? 1 · Why are normal maps in tangent space but not in normal space?...
- 5gl_FragCoord.x, y, z being 1 for all pixels and w being the depth
w be representing depth information while gl_FragCoord.z is always 1.0 for all fragments? glsl th...