Playing with gl_FragDepth - OpenGL - Khronos Forums

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

I continued with my last example. I have a lots of quads that they are always looking to the camera with different sizes and I would like to ... Playingwithgl_FragDepth OpenGL glsl Capagris March18,2009,4:55am #1 IthoughtitwouldbequiteinterestingtofindouthowZbufferworksinFS.So… Icontinuedwithmylastexample.IhavealotsofquadsthattheyarealwayslookingtothecamerawithdifferentsizesandIwouldliketoimaginetheyare3Dobjects.Therefore,asmallquad,whichisoverahugequad,possiblyiscoveredbythebigone. Thefirstproblemisgl_FragCoord.zisnon-linearsoisuselessbecauseIsendthesizeasuniformvariable. Ihavetriedthefollowingoptionstofakethegl_FragDepth VS varyingvec4position; position=gl_Position.z; FS varyingvec4position; uniformfloatradio;//Tosimulatethefrontfaceofacube uniformfloatdepthRange;//zFar-zNearofgluPerspective //Option1Themostreasonable gl_FragDepth=(position-radio)/depthRange; //Option2 gl_FragDepth=(position-radio)*gl_FragCoord.w; //Option3 gl_FragDepth=gl_FragCoord.z-(gl_FragCoord.w*radio); Nooneofthemwork,itlooksliketheradioishugeinanyoption.anyidea? Brolingstanz March18,2009,11:33am #2 IreckonfromtheGLSLspecthatgl_FragCoordcontainsthescreenspacex,y,zand1/w(whichIbelieveis-1/z_in_eye_space). Capagris March19,2009,3:55am #3 Thebestoptionuntilthemomentis //Option4 gl_FragDepth=((1.0/gl_FragCoord.w)-radio)/depthRange; Buttheoclussionisonlygoodamongquadsofsimilarradio.IftheradioisverydifferentorwithobjectswhichtheyarentcalculatedbyFSitfails… Whyissohardtomodifythedepthvalue? Capagris March23,2009,11:27am #4 SorrytorevivebutIamindespair.Iamonlygoingtoaskforasimplequestion. Whatistheformulathatequalizesgl_FragCoord.z? Imean…gl_FragCoord.z==formula; martinsm March23,2009,11:43am #5 Itdependsonyourprojectionmatrix. Whenyougetworldcoordinates(Wx,Wy,Wz,1)theyaremultipliedwithprojectionmatrixyieldingpost-perspectivecoordinates(Px,Py,Pz,Pw).ThenPzandPwarevaluesthatareinterpolatedbetwenverticesanddivisioninterpolated(Pz)/interpolated(Pw)ispassedintogl_FragCoord.z. Dark_Photon March23,2009,4:26pm #6 Capagris: SorrytorevivebutIamindespair.Iamonlygoingtoaskforasimplequestion. Whatistheformulathatequalizesgl_FragCoord.z? Imean…gl_FragCoord.z==formula; See: gl_FragCoord.z(Opengl.orgthread) glFrustum(manpage) Capagris March24,2009,4:51am #7 Despiteyouradvicesithasntbeenenough… Martinsm,ifIfollowyou… VS varyingposition; position=gl_Vertex*gl_ProjectionMatrix; FS varyingposition; floatdepth=position.z/position.w; if(depth==gl_FragCoord.z)discard; else… Itisnotdiscardedsoisnotequal DarkPhotonthanksbutthatthreadisunfinishedandIhaveseenthembefore.WhatisitsupposedIshouldunderstandwhenIseeglFrustum? Thefollowingequationsareverysimilartogl_FragCoord.z.Anyoneseesomethingwrong? uniformfloatzNear,zFar;//1.0to300.0 position=gl_Position*gl_ModelViewMatrix; gl_FragCoord.z~(zFar/(zFar-zNear))+((zFar*zNear/(zNear-zFar))/position.z); gl_FragCoord.z~((1.0/zNear)-(1.0/position.z))/((1.0/zNear)-(1.0/zFar)); Thelastworksverywellwhenyoumixcodewithoutshadercalculation.Myfinalaimistomodifyposition.zaddinganyvalue,ifthedifferencebetweenvaluesisnothighitworksproperly…butbothsometimesfailamongobjectscalculatedbyShaders position.z=position.z+radio;//whereradiowantstogivedephttoaplainquad martinsm March24,2009,5:39am #8 Capagris: position=gl_Position*gl_ProjectionMatrix; Youshouldmultiplyalsobygl_ModelViewMatrix,ifyourgl_Positionisinobjectspace: position=gl_ModelViewProjectionMatirx*gl_Position; TheformulafromotherthreadissameasmultiplyingbyMVPmatrixanddividinginfragmentshader.Itisjustusingcoefficientsfromperspectiveprojectionmatrixdirectlyinequationtocomputegl_FragCoord.z.Itwillbedifferentifyouwillusedifferentkindofprojectionmatrix. bertgp March24,2009,6:03am #9 Capagris: floatdepth=position.z/position.w; if(depth==gl_FragCoord.z)discard; else… martinsm: Youshouldmultiplyalsobygl_ModelViewMatrix,ifyourgl_Positionisinobjectspace Also,keepinmindthatcomparingtwofloatvaluesforequalityinthiscontexthasahighchanceofnotworkingasyouexpect.Forthistesttowork,youmustusetheexactsamecomputationstepsforthetwovaluesandsomehowguaranteethatthecompilerwon’tmodifythem.Iassumethatyouusedftransform()totransformthevertexposition;therefore,thepositionvalues(gl_FragCoord)arenotnecessarilycomputedexactlyintheexactsameorderthatyouuseforthe“position”varying,whichcouldyielddiscrepancies. Xmas March24,2009,9:27am #10 gl_Positionisthevertexshaderoutputinclipspace.Itdoesn’tmakealotofsensetomultiplyitwiththemodelvieworprojectionmatrices. Togetfromgl_Position(oravaryingthatisequaltogl_Position)tothefragmentdepthvalueyouneedtodivideZbyW(transformationfromclipspacetonormalizeddevicecoordinates),thenmaptherange[-1,1]tothe[n,f]rangespecifiedwithglDepthRange(whichiscompletelyunrelatedtothenear/farrangespecifiedwhenusingglFrustum).Usuallythisrangeis[0,1],soitboilsdownto: Zwindow=0.5+0.5*Zndc=0.5+0.5*Zclip/Wclip. Capagris: Icontinuedwithmylastexample.IhavealotsofquadsthattheyarealwayslookingtothecamerawithdifferentsizesandIwouldliketoimaginetheyare3Dobjects.Therefore,asmallquad,whichisoverahugequad,possiblyiscoveredbythebigone. Whydon’tyouspecifythedepthpervertex? Capagris March24,2009,11:05am #11 WhenIusedgl_Positionisamistake.ReallyIwantedtomentiongl_Vertex.Ihaveediteditintheformerpost. InthefollowingformulacanIsupposeZandWclipcomefromgl_FragCoord? Zwindow=0.5+0.5*Zndc=0.5+0.5*Zclip/Wclip. Ipreferthisone…butIamnotsureifZisinobject,eyeorprojectioncoordinates…Ithinkisineyecoordinates ((1.0/zNear)-(1.0/Z))/((1.0/zNear)-(1.0/zFar)); Xmas: Whydon’tyouspecifythedepthpervertex? Itisagoodpoint.ItisbecauselaterIwanttorenderspheres,andforthateachfragmenttheradioisvariablebutIamtryingwithamoresimplefigureatthemoment. Ageneralformulaitshouldworkalwayswithanyfigureis: //zNear,zFarandradioareuniformvariables floatheight=radio;//Calculatetheheightforthefigureusingtheradio,ifquadisthat vec4newPosition=position;//position=gl_Vertex; newPosition=gl_ModelViewMatrix*newPosition; newPosition.z=newPosition.z+height; gl_FragDepth=((1.0/zNear)-(1.0/newPosition.z))/((1.0/zNear)-(1.0/zFar)); Xmas March24,2009,12:15pm #12 Capagris: InthefollowingformulacanIsupposeZandWclipcomefromgl_FragCoord? No,thosewouldbeZandWofgl_Position,i.e.thevertexshaderoutput. ButtherealquestionisinwhichspacedoyouwanttochangeZ.Itseemsyouwanttosimulatespheres,soeyespacewouldwork.InthatcaseyousimplypasseyespaceZasavarying,modifyit,thenapplytheusualtransformationsgoingfromeyespacetoclipspacetoNDCtowindowspace. varyingfloateyeDepth; floateyeDepth_modified=eyeDepth+...; vec2clipZW=eyeDepth_modified*gl_ProjectionMatrix[2].zw+gl_ProjectionMatrix[3].zw; gl_FragDepth=0.5+0.5*clipZW.x/clipZW.y; Capagris March26,2009,3:41am #13 ThankyouXmasbuttheresultisthesame.Ishowyoutheresultofyourcode(IamusingquadsandusingtheradioIdiscardpixelstocreatespheres). Youcanseehowtheyellowandbrownspheredontdoagoodocclusionwiththeredsphere. Whydonttheimagesappear?Well,anyonecanfollowthelink. system closed October19,2021,7:32pm #14 Thistopicwasautomaticallyclosed183daysafterthelastreply.Newrepliesarenolongerallowed.



請為這篇文章評分?