Stencil Test - OpenGL Wiki

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

The Stencil Test is a per-sample operation performed after the Fragment Shader. The fragment's stencil value is tested against the value in ... StencilTest FromOpenGLWiki Jumptonavigation Jumptosearch OpenGLRenderingPipeline RenderingPipelineOverview VertexSpecification VertexRendering Primitive VertexProcessing VertexShader Tessellation GeometryShader VertexPost-Processing TransformFeedback PrimitiveAssembly FaceCulling Rasterization FragmentShader Per-SampleProcessing ScissorTest StencilTest DepthTest Blending LogicalOperation WriteMask TheStencilTestisaper-sampleoperationperformedaftertheFragmentShader.Thefragment'sstencilvalueistestedagainstthevalueinthecurrentstencilbuffer;ifthetestfails,thefragmentisculled. Contents 1Stencilbuffer 2Fragmentstencilvalue 3Stenciltest 4Stenciloperations 5Seealso Stencilbuffer Inordertousethestenciltest,thecurrentFramebuffermusthaveastencilbuffer.Thestencilbufferisanimagethatusesastencilimageformat.TheDefaultFramebuffermayhaveastencilbuffer,anduser-definedframebufferscanattachstencilformattedimages(eitherdepth/stencilorstencil-only)totheGL_STENCIL_ATTACHMENTattachmentpoint. Ifthecurrentframebufferhasnostencilbuffer,thenthestenciltestwillalwaysbehaveasifitisdisabled. Ifthereisastencilbuffer,thatbufferhasacertainbitdepth.Thisspecifiesthenumberofstencilbitsavailable. Fragmentstencilvalue EachFragmenthasastencilvalue,whichisanunsignedinteger.Thestenciltestoperationwilltestthisstencilvalueagainstthevaluefromthecurrentframebufferatthefragment'sposition. Thisvalueisusuallydefinedbythesamefunctionthatsetsthestenciltest,below.Thismeansthateveryfragmentfromeveryprimitiveforeveryobjectinadrawcallgetsthesamefragmentstencilvalue(withthefront/backfacepointnotedbelow). Stenciltest Toenablestenciltesting,callglEnablewithGL_STENCIL_TEST.Whenthestenciltestisenabled,thetestinglogicofthereferencestencilvalueagainsttheframebufferstencilvalueisskipped.Thestenciloperationsarestillprocessedwhenthetestisdisabled;theoperationproceedsasifthestenciltesthadpassed. Whenrenderingtoaframebufferthathasnoastencilbuffer,stencilingwillalwaysbehaveasifitisdisabled. Stencilingoperationstakeintoaccountthefactthattriangleshavetwosides.Therefore,allstenciltestsandoperationfunctionshavetwosetsofdata:oneforthefrontsideoftrianglesandonefortheback.Whichsideisuseddependsonwhetherthefragmentsgeneratedfromtheprimitivecamefromthefrontorbackface. Thesefunctionsalltakeaface​parameter,whichspecifieswhichfacingstateissetbythatfunction.Theface​canbeGL_FRONTorGL_BACK,butitcanalsosetbothsidesatoncewithGL_FRONT_AND_BACK. ForPrimitivesthathavenofacing,thefrontsidestencilstatearealwaysused. Thestenciltestitselfissetbythisfunction: voidglStencilFuncSeparate(GLenumface​,GLenumfunc​,GLintref​,GLuintmask​); Theref​definesthefragment'sstencilvalueforallfragmentsgeneratedforthegivenfacing.Thefragmentstencilvaluewillbeclampedtotherangedefinedbythestencilbuffer'sbitdepth. Thefirststepofthestenciltestistogetthedestinationstencilvaluefromthestencilbuffer(calledDs).Thisisanunsignedintegervalue.Thefragment'sstencilvaluewillbecalledFs. ThenextstepistoperformabitwiseANDwithbothFsandDs,againstthemask​parameterforboth.Thisallowstheusertomaskoffcertainstencilbits,reservingthemfordifferentconditionaltests.Thisresultsintwomaskedunsignedintegers,FmandDm. ThenthestenciltestitselfisperformedbetweenFmandDm,basedonthefunc​parameter.Thetestisoftheform(FmFUNCDm);themaskedfragmentvalueisontheleft-handside.Theavailablefunctionsare: Enum Test Enum Test GL_NEVER Alwaysfails. GL_ALWAYS Alwayspasses GL_LESS < GL_LEQUAL ≤ GL_GREATER > GL_GEQUAL ≥ GL_EQUAL = GL_NOTEQUAL ≠ glStencilFunccanbeusedtosetbothfaceoperationsinonecall,ifyoudon'tliketouseGL_FRONT_AND_BACK. Stenciloperations Thestenciloperationusestheresultofthestenciltest.Ifstenciltestingisdisabled,thenthestenciltestiscountedaspassing;thestenciloperationdetailedbelowstillhappensevenifstenciltestingisdisabled. Ifthestenciltestfails,thefragmentisdiscarded.Normally,discardingafragmentmeansthatithasnovisibleeffects.However,thestencilbuffercanbeupdatedevenfromdiscardedfragments,ifthedepthorstenciltestsdiscardthem. Note:Recallthatthestencilanddepthtestsarenormallythelastper-sampleprocessingstepsthatcandiscardfragments.However,iffragmenttestshappenearly,evenifthefragmentshaderdiscardsthefragment,thestencilbuffercanbemodified. Therearethreepossiblecasesinvolvedhere: Thestenciltestfails.Thefragmentwillbediscarded.Rememberthatthestenciltesthappensbeforethedepthtest,sotheresultofthatisnevercomputedifthestenciltestfails. Thestenciltestpasses,buttheDepthTestfails.Thefragmentwillbediscarded(duetothedepthfailure).Thiscaseonlyappliesifthedepthtestisenabled. Thestenciltestpasses,andtheDepthTestpasses.Ifthedepthtestisdisabled,thenitisalwaysassumedtopass. Todefinehowthestencilvalueintheframebufferismodifiedineachcase,usethefollowingfunction: voidglStencilOpSeparate(GLenumface​,GLenumsfail​,GLenumdpfail​,GLenumdppass​); Theparameterssfail​,dpfail​,anddppass​definethestencilupdateoperationstoperforminthethreecasesabove,respectively.Eachcasecanuseanyofthefollowingoperations("currentvalue"heremeansthevaluealreadyinthestencilbuffer): Enum Operation Enum Operation GL_KEEP Don'tmodifythecurrentvalue(default) GL_INVERT Invertthecurrentvalue GL_ZERO Setittozero GL_REPLACE Replacewiththemaskedfragmentvalue GL_INCR Incrementthecurrentvalue,saturating1ifitwouldoverflow GL_INCR_WRAP Incrementthecurrentvalue,wrappingifitwouldoverflow GL_DECR Decrementthecurrentvalue,settingtozeroifitwouldunderflow GL_DECR_WRAP Decrementthecurrentvalue,wrappingifitwouldunderflow 1:Meaningthatitstopsatthemaximumrepresentableintegeratthestencilbuffer'sbitdepth.Foran8-bitstencilbuffer,thatwouldbe255. glStencilOpcanbeusedtosetbothfaceoperationsinonecall,ifyoudon'tliketouseGL_FRONT_AND_BACK. Seealso DepthTest FragmentShader Retrievedfrom"http://www.khronos.org/opengl/wiki_opengl/index.php?title=Stencil_Test&oldid=14660" Category:SampleWriting Navigationmenu Personaltools CreateaccountLogin Namespaces PageDiscussion Variants Views ReadViewsourceViewhistory More Search Navigation MainPageGettingStartedDownloadOpenGLRegistryReferencePagesReferenceGuideNewsCommunityForumsAboutOpenGL Help ContactUsPrivacyPolicyHelpEditingRecentchanges Tools WhatlinkshereRelatedchangesSpecialpagesPrintableversionPermanentlinkPageinformationCitethispage



請為這篇文章評分?