Getting the real fragment depth in GLSL - Game Development ...

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

Alternatively, you can get it recover it from gl_FragCoord... In a fragment shader, gl_FragCoord contains (x, y, z, 1/w) in window coordinates. 2022DeveloperSurveyisopen!Takesurvey. GameDevelopmentStackExchangeisaquestionandanswersiteforprofessionalandindependentgamedevelopers.Itonlytakesaminutetosignup. Signuptojointhiscommunity Anybodycanaskaquestion Anybodycananswer Thebestanswersarevotedupandrisetothetop Home Public Questions Tags Users Companies Unanswered Teams StackOverflowforTeams –Startcollaboratingandsharingorganizationalknowledge. CreateafreeTeam WhyTeams? Teams CreatefreeTeam Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore GettingtherealfragmentdepthinGLSL AskQuestion Asked 7years,3monthsago Modified 7years,3monthsago Viewed 10ktimes 1 \$\begingroup\$ IamtryingtowriteasimpleGLSLshaderthatjustrendersthereal(notnormalized)depthofafragmentasafloatingpointvalue. Sofar,I'vefiguredouthowtogetthedepthofavertex,andjustinterpolateacrossthefragments,butI'mstartingtorealizethatthisisnotgoingtogivemethecorrectdepthofeachfragment,isit? Here'swhatIhavesofar: Vertexshader: uniformmat4world; uniformmat4view; uniformmat4projection; attributevec3position; attributevec3color; varyingfloatdistToCamera; voidmain() { vec4cs_position=view*world*vec4(position,1.0); distToCamera=-cs_position.z; gl_Position=projection*cs_position; } Fragmentshader: varyingfloatdistToCamera; voidmain() { gl_FragColor=vec4(distToCamera,distToCamera,distToCamera,1.0); } openglgraphicsglsldepth-buffer Share Improvethisquestion Follow askedJan28,2015at22:23 mklingenmklingen 3,56711goldbadge1616silverbadges2525bronzebadges \$\endgroup\$ Addacomment  |  1Answer 1 Sortedby: Resettodefault Highestscore(default) Datemodified(newestfirst) Datecreated(oldestfirst) 4 \$\begingroup\$ Yes,yourZvalue,distToCamera,willbecorrectateachtriangle'svertices,butwon'tbeanywhereelse(exceptbetweenverticesofmatchingZ-value),becauseoflinearinterpolation. Edit:IncurrentGLSL,thedefaultinterpolationis"inperspective",wheregl_Position/gl_FragCoordistakenintoaccountforinterpolating.Thiscanbeoverridden(youwon'twanttoforyourcase)asatypequalifier,like: noperspectiveoutfloatdistToCamera;//vertexshader noperspectiveinfloatdistToCamera;//fragmentshader Alternatively,youcangetitrecoveritfromgl_FragCoord... Inafragmentshader,gl_FragCoordcontains(x,y,z,1/w)inwindowcoordinates.x,y,andzhavealreadybeendividedbyw. So,youcanrecoveryourvertexshaderoutput,gl_Position.z,interpolatedcorrectlyin3d,as floatoriginalZ=gl_FragCoord.z/gl_FragCoord.w; (Dependinghowyou'vedoneyourvertexshaderprojectionmatrix,thismightnotbeyourworldZ,ofcourse.) Share Improvethisanswer Follow editedJan28,2015at23:42 answeredJan28,2015at23:14 davidvanbrinkdavidvanbrink 2,5521111silverbadges1616bronzebadges \$\endgroup\$ 4 1 \$\begingroup\$ perfect.Ididn'tknowIcouldsimplydividebywtogetitbackfromnormalizedcoordinates! \$\endgroup\$ – mklingen Jan28,2015at23:18 1 \$\begingroup\$ ...dividingby(1/w)...:-) \$\endgroup\$ – davidvanbrink Jan28,2015at23:19 \$\begingroup\$ Imayneedtoretractoramendthisanswer--Ithinkyouroriginalapproachmayworkfine,distToCamera(orsameasfragcoord.z/fragcoord.w).ThedefaultinterpolationincurrentGLSLis,indeed,perspected.Onmyowntestshader,interpolatingacrossviewPosVarying.xyzgivesnicestraightdepthlines.(Iusegray=z-floor(z)tovisualizethelines.) \$\endgroup\$ – davidvanbrink Jan28,2015at23:30 \$\begingroup\$ Thisdoesnotappeartowork,eveninthecausethisabsolutelyshould(orthogonalprojectionmatrix).Settinganoutputvariabletomyfragmentshaderoutvec4Position;toPosition=mvp_matrix*vec4(vertex,1.0);andcheckingagainstdepthofanotherpointwhichanothermatrixwiththesameprojectionworks(comparingbothzvaluestogetvertexesdrawninbehindseveraltransparentobjectstonotdraw),whenItrytoderivePositionfromgl_FragCoord.z/gl_FragCoord.w;itisn'tthesamevalue.Notsurewhatthedealis. \$\endgroup\$ – Krupip Sep12,2019at22:36 Addacomment  |  Youmustlogintoanswerthisquestion. Nottheansweryou'relookingfor?Browseotherquestionstaggedopenglgraphicsglsldepth-buffer. TheOverflowBlog Makeyouropen-sourceprojectpublicbeforeyou’reready(Ep.444) Thescienceofinterviewingdevelopers FeaturedonMeta AnnouncingthearrivalofValuedAssociate#1214:Dalmarus Improvementstositestatusandincidentcommunication Related 0 FormulaforreplicatingglTexGeninOpenGLES2.0GLSL 7 GLSLdefaultshaders 3 HowcanIpassanarrayoffloatstothefragmentshaderusingtextures? 4 GLSLAtmosphericScatteringIssue 0 (WebGL)Pointlightper-fragmentissue 0 GLSL-Stretchedverticesaftertranslating 2 WhatareLayoutQualifierinGLSLusedfor? HotNetworkQuestions CovidTestRequirementforDubaiTransitFromIrelandtoIndonesia Howtomodelasimpleshoe Whyishavingbloatedinterfacesanantipattern? MostcommoncommandtocompileaLaTeXdocument? Didmyprofessoractunethicallybypublishingapaperwithafigurefrommythesiswithoutanyacknowledgement? Isitillegaltorideadrunkhorse? HowManyDaysAfterSkimCoatCanIPaint? Twoparallelrelaysfordoublecurrent Waystomakeanalienatmospherebreathable,butuncomfortable? Whatdoesthisyumpackageversionmean? WhentochangeoilfornewgenerationMazda? HowcanItellwhich3rdpartyflashesarecompatiblewithCanon'sSL3missingpinhotshoemount? Isthissinkfaucetremovable? Whatmakesaluminumaerospacegrade? Inbetweenfractions GameinJamesBondfilm"FromRussiawithlove" WhatwasthereasonforextendingNATOinthe90s? QGISreadsblankcellsas"nan"insteadof"NULL" SelectonlyonepointinpolygonusingQGIS ShouldIrevealmyslidesbit-by-bitwhengivingapresentation/lecture? StarWars:R2D2connectedtomainframe Ihaveanarmadaofteleportingskyfortresses.DoIstillneedanavy? AquestionregardingisomorphismincohomologyformodulispaceofstablebundlesoveracompactRiemannsurface UnabletomountaLinuxRAIDarray morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. default Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?