What does (gl_FragCoord.z / gl_FragCoord.w) represent?

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

z / gl_FragCoord.w is not the camera-space (or world-space) distance to the camera. Nor is it the camera-space planar distance to the ... 2022DeveloperSurveyisopen!Takesurvey. Home Public Questions Tags Users Companies Collectives ExploreCollectives Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Collectives™onStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore Whatdoes(gl_FragCoord.z/gl_FragCoord.w)represent? AskQuestion Asked 9years,5monthsago Modified 1year,11monthsago Viewed 17ktimes 16 16 Iwantactualworldspacedistance,andIgetthefeelingfromexperimentationthat (gl_FragCoord.z/gl_FragCoord.w) isthedepthinworldspace?ButI'mnottoosure. EDITI'vejustfoundwhereIhadoriginallylocatedthissnippetofcode.Apparentlyitistheactualdepthfromthecamera? openglglsl Share Improvethisquestion Follow editedDec4,2012at20:49 Engineer askedDec4,2012at20:24 EngineerEngineer 7,99077goldbadges6060silverbadges9999bronzebadges 2 I'mnotsurewhatyourquestionisexactly.Areyouaskingwhatgl_FragCoord.z'svalueis?Orwhatgl_FragDepth'svalueshouldbe?Orsimplyhowtogetaworld-spacedepth?Thelatterrequiresexplainingwhatexactlyyoumeanbyworld-spacedepth,since"depth"isusuallydefinedastheplanardistancetothecamera. – NicolBolas Dec4,2012at20:41 1 Asstatedinthequestion,Iwanttheactualdistance,inworldspaceunits,fromthecamera(orit'snearplane,ifthatismoreappropriate). – Engineer Dec4,2012at20:45 Addacomment  |  3Answers 3 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 29 Thiswasasked(bythesameperson)andansweredelsewhere.I'mparaphrasingandembellishingtheanswerhere: Asstatedinsection15.2.2oftheOpenGL4.3coreprofilespecification(PDF),gl_FragCoord.wis1/clip.w,whereclip.wistheWcomponentoftheclip-spaceposition(ie:whatyouwrotetogl_Position). gl_FragCoord.zisgeneratedbythefollowingprocess,assumingtheusualtransforms: Camera-spacetoclip-spacetransform,viaprojectionmatrixmultiplicationinthevertexshader.clip.z=(projectionMatrix*cameraPosition).z Transformtonormalizeddevicecoordinates.ndc.z=clip.z/clip.w Transformtowindowcoordinates,usingtheglDepthRangenear/farvalues.win.z=((dfar-dnear)/2)*ndc.z+(dfar+dnear)/2. Now,usingthedefaultdepthrangeofnear=0,far=1,wecandefinewin.zintermsofclip-space:(clip.z/clip.w)/2+0.5.Ifwethendividethisbygl_FragCoord.w,thatistheequivalentofmultiplyingbyclip.w,thusgivingus: (gl_FragCoord.z/gl_FragCoord.w)=clip.z/2+clip.w/2=(clip.z+clip.w)/2 Usingthestandardprojectionmatrix,clip.zrepresentsascaleandoffsetfromcamera-spaceZcomponent.Thescaleandoffsetaredefinedbythecamera'snear/fardepthvalues.clip.wis,againinthestandardprojectionmatrix,justthenegationofthecamera-spaceZ.Therefore,wecanredefineourequationinthoseterms: (gl_FragCoord.z/gl_FragCoord.w)=(A*cam.z+B-cam.z)/2=(C*cam.z+D) WhereAandBrepresenttheoffsetandscalebasedonnear/far,andC=(A-1)/2andD=B/2. Therefore,gl_FragCoord.z/gl_FragCoord.wisnotthecamera-space(orworld-space)distancetothecamera.Norisitthecamera-spaceplanardistancetothecamera.Butitisalineartransformofthecamera-spacedepth.Youcoulduseitasawaytocomparetwodepthvaluestogether,iftheycamefromthesameprojectionmatrixandsoforth. Toactuallycomputethecamera-spaceZ,youneedtoeitherpassthecameranear/farfromyourmatrix(OpenGLalreadygivesyoutherangenear/far)andcomputethoseAandBvaluesfromthem,oryouneedtousetheinverseoftheprojectionmatrix.Alternatively,youcouldjustusetheprojectionmatrixdirectlyyourself,sincefragmentshaderscanusethesameuniformsavailabletovertexshaders.YoucanpicktheAandBtermsdirectlyfromthatmatrix.A=projectionMatrix[2][2],andB=projectionMatrix[3][2]. Share Improvethisanswer Follow editedJun15,2020at0:01 communitywiki 2revsNicolBolas Addacomment  |  3 Accordingtothedocs: Availableonlyinthefragmentlanguage,gl_FragDepthisanoutputvariablethat isusedtoestablishthedepthvalueforthecurrentfragment.Ifdepthbuffering isenabledandnoshaderwritestogl_FragDepth,thenthefixedfunctionvalue fordepthwillbeused(thisvalueiscontainedinthezcomponentof gl_FragCoord)otherwise,thevaluewrittentogl_FragDepthisused. So,itlookslikegl_FragDepthshouldjustbegl_FragCoord.zunlessyou'vesetitsomewhereelseinyourshaders. Share Improvethisanswer Follow answeredDec4,2012at20:38 XymostechXymostech 9,39033goldbadges3333silverbadges4444bronzebadges 8 1 Nope,itisn't.That'swhatIoriginallythought.Ijustswappedoutthegl_FragCoord.zinmyequationwithgl_FragDepth(fortheproductionoflinearfog)tocheckyouranswer,andsuddenlymyentiresceneisblackirrespectiveofdistance.WhereaswhenIleaveintheoriginalgl_FragCoord.z,Iseefogaftersomespecifieddistance. – Engineer Dec4,2012at20:42 @NickWiggill:YoucannotarguewiththeOpenGLspecification.Ifyourcodeisproducingthatresult,thenitiseitherduetoabuginyourcodeorabugintheimplementation.That'swhatiswritteninthespec,andthereforethatishowOpenGLbehaves. – NicolBolas Dec4,2012at20:51 1 @NickWiggill:Idon'tunderstandthemathsbehindit?Idon'tunderstandthemathsbehindit?Idon'tneedtogoarounddisprovingnonsense;thespecsayswhatitsays,andthatpageiswrong. – NicolBolas Dec4,2012at20:57 6 @NickWiggill:Yes,peoplepromotingnon-informationasrealanddownvotingrealfactswindsmeup. – NicolBolas Dec4,2012at21:00 1 @EngineerYouaremixingINPUTvariableswithOUTPUTvalues.'gl_FragCoord.z'isavailabletoyouasINPUT.Thisisnottruefor'gl_FragDepth'.ItisanOUTPUTvaluethatshallbefilledbyyou.Ifyoudon'tfillit,specssaythatitwillbefilledautomaticallywith'gl_FragCoord.z'.Youshouldnotuse'gl_FragDepth'asINPUT. – JoãoMonteiro Apr29,2019at3:00  |  Show3morecomments 0 As gl_FragCoord.w=1.0/gl_Position.w And(likely)yourprojectionmatrixgetswfrom-z(ifthelastcolumnis0,0,-1,0)then; floatdistanceToCamera=1.0/gl_FragCoord.w; Share Improvethisanswer Follow answeredMay18,2013at14:32 MarcusOPlattsMarcusOPlatts 44744silverbadges99bronzebadges Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedopenglglsloraskyourownquestion. TheOverflowBlog Makeyouropen-sourceprojectpublicbeforeyou’reready(Ep.444) Thescienceofinterviewingdevelopers FeaturedonMeta AnnouncingthearrivalofValuedAssociate#1214:Dalmarus Improvementstositestatusandincidentcommunication RetiringOurCommunity-SpecificClosureReasonsforServerFaultandSuperUser Temporarilypausingthesitesatisfactionsurvey StagingGround:ReviewerMotivation,Scaling,andOpenQuestions Linked 2 AccessingtheDepthBufferfromafragmentshader 0 Reconstructingworldpositionfromlineardepth Related 283 Onceuponatime,when>wasfasterthan<...wait glslgl_fragcoord.zcalculationandsettinggl_fragdepth cascadedshadowmappinglookupdecision depthasdistancetocameraplaneinglsl glslsampler2dshadowandshadow2dclarification glslcomputeworldcoordinatefromeyedepthandscreenposition rangebasedfoginglsl world-spacepositionfromlogarithmicdepthbuffer x-y-distancefromcameratoobjectinvertexshader hotnetworkquestions aquestionaboutcubenuroadracefe provingarecursiveformulaisindeedafunction unabletomountalinuxraidarray howdoeshebrews11:27saymoseswasnotafraid using covidtestrequirementfordubaitransitfromirelandtoindonesia doesgravitationreallyexistattheparticlelevel selectonlyonepointinpolygonusingqgis qgisreadsblankcellsas howdoyougagafish-personwithouttape howcanaverbalreasoningquestionbesolvedwithmathematica meaningofclappingattheendofmass signalprocessinginpythonvsc whydoesbendersay whydoesthex86nothaveaninstructiontoobtainitsinstructionpointer isitdangeroustoconsume30mgofzinc isitillegaltorideadrunkhorse whatmakesaluminumaerospacegrade howtomodelasimpleshoe whatdoestheidiomaticphrase colourina5by5grid whatproblemscoulde6introducewhenusedfordungeonsanddragonsfifthedition whattapeformatdidthenintendofamicomdatarecorderuse morehotquestions questionfeed subscribetorss tosubscribetothisrssfeed lang-c yourprivacy byclicking acceptallcookies customizesettings>



請為這篇文章評分?