Stencil Test - OpenGL Wiki
文章推薦指數: 80 %
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. Thesefunctionsalltakeafaceparameter,whichspecifieswhichfacingstateissetbythatfunction.ThefacecanbeGL_FRONTorGL_BACK,butitcanalsosetbothsidesatoncewithGL_FRONT_AND_BACK. ForPrimitivesthathavenofacing,thefrontsidestencilstatearealwaysused. Thestenciltestitselfissetbythisfunction: voidglStencilFuncSeparate(GLenumface,GLenumfunc,GLintref,GLuintmask); Therefdefinesthefragment'sstencilvalueforallfragmentsgeneratedforthegivenfacing.Thefragmentstencilvaluewillbeclampedtotherangedefinedbythestencilbuffer'sbitdepth. Thefirststepofthestenciltestistogetthedestinationstencilvaluefromthestencilbuffer(calledDs).Thisisanunsignedintegervalue.Thefragment'sstencilvaluewillbecalledFs. ThenextstepistoperformabitwiseANDwithbothFsandDs,againstthemaskparameterforboth.Thisallowstheusertomaskoffcertainstencilbits,reservingthemfordifferentconditionaltests.Thisresultsintwomaskedunsignedintegers,FmandDm. ThenthestenciltestitselfisperformedbetweenFmandDm,basedonthefuncparameter.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,anddppassdefinethestencilupdateoperationstoperforminthethreecasesabove,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
延伸文章資訊
- 1Stencil testing - LearnOpenGL
A stencil buffer (usually) contains 8 bits per stencil value that amounts to a total of 256 diffe...
- 2模板测试
当片段着色器处理完片段之后,模板测试(Stencil Test) 就开始执行了,和深度测试 ... 模板测试基于另一个缓冲,这个缓冲叫做模板缓冲(Stencil Buffer),我们被允许在 ...
- 3走进Stencil Buffer 系列0 : 模板缓冲和模板测试是什么?
模板测试(Stencil Test)是现代渲染流水线的一环,其中涉及到的就是模板缓冲(Stencil Buffer),模板缓冲可以用来制作物体的遮罩、轮廓描边、阴影、 ...
- 4Unit Testing - Stencil.js
Stencil makes it easy to unit test components and app utility functions using Jest. Unit tests va...
- 5Stencil Test(模板测试)不得不说的那些事- 中文社区博客
Stencil test是per-fragment operations的一种,这就意味着它处于fragment shader (片段着色器)之后,stencil test的主要作用就是根据st...