Face culling - LearnOpenGL
文章推薦指數: 80 %
face culling does. OpenGL checks all the faces that are · front facing towards the viewer and renders those while discarding all the faces that are · back facing ... Ifyou'rerunningAdBlock,pleaseconsiderwhitelistingthissiteifyou'dliketosupportLearnOpenGL;andnoworries,Iwon'tbemadifyoudon't:) IntroductionGettingstartedOpenGLCreatingawindowHelloWindowHelloTriangleShadersTexturesTransformationsCoordinateSystemsCameraReviewLightingColorsBasicLightingMaterialsLightingmapsLightcastersMultiplelightsReviewModelLoadingAssimpMeshModelAdvancedOpenGLDepthtestingStenciltestingBlendingFacecullingFramebuffersCubemapsAdvancedDataAdvancedGLSLGeometryShaderInstancingAntiAliasingAdvancedLightingAdvancedLightingGammaCorrectionShadowsShadowMappingPointShadowsNormalMappingParallaxMappingHDRBloomDeferredShadingSSAOPBRTheoryLightingIBLDiffuseirradianceSpecularIBLInPracticeDebuggingTextRendering2DGameBreakoutSettingupRenderingSpritesLevelsCollisionsBallCollisiondetectionCollisionresolutionParticlesPostprocessingPowerupsAudioRendertextFinalthoughtsGuestArticlesHowtopublish2020OITIntroductionWeightedBlendedSkeletalAnimation2021CSMSceneSceneGraphFrustumCullingTessellationHeightmapTessellationDSACoderepositoryTranslationsAbout BTC 1CLGKgmBSuYJ1nnvDGAepVTKNNDpUjfpRa ETH/ERC20 0x1de59bd9e52521a46309474f8372531533bd7c43 Faceculling Advanced-OpenGL/Face-culling Trymentallyvisualizinga3Dcubeandcountthemaximumnumberoffacesyou'llbeabletoseefromanydirection.Ifyourimaginationisnottoocreativeyouprobablyendedupwithamaximumnumberof3.Youcanviewacubefromanypositionand/ordirection,butyouwouldneverbeabletoseemorethan3faces.Sowhywouldwewastetheeffortofdrawingthoseother3facesthatwecan'tevensee.Ifwecoulddiscardthoseinsomewaywewouldsavemorethan50%ofthiscube'stotalfragmentshaderruns! Wesaymorethan50%insteadof50%,becausefromcertainanglesonly2oreven1facecouldbevisible.Inthatcasewe'dsavemorethan50%. Thisisareallygreatidea,butthere'soneproblemweneedtosolve:howdoweknowifafaceofanobjectisnotvisiblefromtheviewer'spointofview?Ifweimagineanyclosedshape,eachofitsfaceshastwosides.Eachsidewouldeitherfacetheuserorshowitsbacktotheuser.Whatifwecouldonlyrenderthefacesthatarefacingtheviewer? Thisisexactlywhatfacecullingdoes.OpenGLchecksallthefacesthatarefrontfacingtowardstheviewerandrendersthosewhilediscardingallthefacesthatarebackfacing,savingusalotoffragmentshadercalls.WedoneedtotellOpenGLwhichofthefacesweuseareactuallythefrontfacesandwhichfacesarethebackfaces.OpenGLusesaclevertrickforthisbyanalyzingthewindingorderofthevertexdata. Windingorder Whenwedefineasetoftriangleverticeswe'redefiningtheminacertainwindingorderthatiseitherclockwiseorcounter-clockwise.Eachtriangleconsistsof3verticesandwespecifythose3verticesinawindingorderasseenfromthecenterofthetriangle. Asyoucanseeintheimagewefirstdefinevertex1andfromtherewecanchoosewhetherthenextvertexis2or3.Thischoicedefinesthewindingorderofthistriangle.Thefollowingcodeillustratesthis: floatvertices[]={ //clockwise vertices[0],//vertex1 vertices[1],//vertex2 vertices[2],//vertex3 //counter-clockwise vertices[0],//vertex1 vertices[2],//vertex3 vertices[1]//vertex2 }; Eachsetof3verticesthatformatriangleprimitivethuscontainawindingorder.OpenGLusesthisinformationwhenrenderingyourprimitivestodetermineifatriangleisafront-facingoraback-facingtriangle.Bydefault,trianglesdefinedwithcounter-clockwiseverticesareprocessedasfront-facingtriangles. Whendefiningyourvertexorderyouvisualizethecorrespondingtriangleasifitwasfacingyou,soeachtrianglethatyou'respecifyingshouldbecounter-clockwiseasifyou'redirectlyfacingthattriangle.Thecoolthingaboutspecifyingallyourverticeslikethisisthattheactualwindingorderiscalculatedattherasterizationstage,sowhenthevertexshaderhasalreadyrun.Theverticesarethenseenasfromtheviewer'spointofview. Allthetriangleverticesthatthevieweristhenfacingareindeedinthecorrectwindingorderaswespecifiedthem,buttheverticesofthetrianglesattheothersideofthecubearenowrenderedinsuchawaythattheirwindingorderbecomesreversed.Theresultisthatthetriangleswe'refacingareseenasfront-facingtrianglesandthetrianglesatthebackareseenasback-facingtriangles.Thefollowingimageshowsthiseffect: Inthevertexdatawedefinedbothtrianglesincounter-clockwiseorder(thefrontandbacktriangleas1,2,3).However,fromtheviewer'sdirectionthebacktriangleisrenderedclockwiseifwedrawitintheorderof1,2and3fromtheviewer'scurrentpointofview.Eventhoughwespecifiedthebacktriangleincounter-clockwiseorder,itisnowrenderedinaclockwiseorder.Thisisexactlywhatwewanttocull(discard)non-visiblefaces! Faceculling AtthestartofthechapterwesaidthatOpenGLisabletodiscardtriangleprimitivesifthey'rerenderedasback-facingtriangles.NowthatweknowhowtosetthewindingorderoftheverticeswecanstartusingOpenGL'sfacecullingoptionwhichisdisabledbydefault. Thecubevertexdataweusedinthepreviouschapterswasn'tdefinedwiththecounter-clockwisewindingorderinmind,soIupdatedthevertexdatatoreflectacounter-clockwisewindingorderwhichyoucancopyfromhere.It'sagoodpracticetotryandvisualizethattheseverticesareindeedalldefinedinacounter-clockwiseorderforeachtriangle. ToenablefacecullingweonlyhavetoenableOpenGL'sGL_CULL_FACEoption: glEnable(GL_CULL_FACE); Fromthispointon,allthefacesthatarenotfront-facesarediscarded(tryflyinginsidethecubetoseethatallinnerfacesareindeeddiscarded).Currentlywesaveover50%ofperformanceonrenderingfragmentsifOpenGLdecidestorenderthebackfacesfirst(otherwisedepthtestingwould'vediscardedthemalready).Donotethatthisonlyreallyworkswithclosedshapeslikeacube.Wedohavetodisablefacecullingagainwhenwedrawthegrassleavesfromthepreviouschapter,sincetheirfrontandbackfaceshouldbevisible. OpenGLallowsustochangethetypeoffacewewanttocullaswell.Whatifwewanttocullfrontfacesandnotthebackfaces?WecandefinethisbehaviorwithglCullFace: glCullFace(GL_FRONT); TheglCullFacefunctionhasthreepossibleoptions: GL_BACK:Cullsonlythebackfaces. GL_FRONT:Cullsonlythefrontfaces. GL_FRONT_AND_BACK:Cullsboththefrontandbackfaces. TheinitialvalueofglCullFaceisGL_BACK.WecanalsotellOpenGLwe'dratherpreferclockwisefacesasthefront-facesinsteadofcounter-clockwisefacesviaglFrontFace: glFrontFace(GL_CCW); ThedefaultvalueisGL_CCWthatstandsforcounter-clockwiseorderingwiththeotheroptionbeingGL_CWwhich(obviously)standsforclockwiseordering. AsasimpletestwecouldreversethewindingorderbytellingOpenGLthatthefront-facesarenowdeterminedbyaclockwiseorderinginsteadofacounter-clockwiseordering: glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); Theresultisthatonlythebackfacesarerendered: Notethatyoucancreatethesameeffectbycullingfrontfaceswiththedefaultcounter-clockwisewindingorder: glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); Asyoucansee,facecullingisagreattoolforincreasingperformanceofyourOpenGLapplicationswithminimaleffort;especiallyasall3Dapplicationsexportmodelswithconsistentwindingorders(CCWbydefault).Youdohavetokeeptrackoftheobjectsthatwillactuallybenefitfromfacecullingandwhichobjectsshouldn'tbeculledatall. Exercises Canyoure-definethevertexdatabyspecifyingeachtriangleinclockwiseorderandthenrenderthescenewithclockwisetrianglessetasthefrontfaces:solution HI
延伸文章資訊
- 1learnopengl学习笔记——面剔除(face culling) - CSDN博客
这正是面剔除(Face Culling)所做的。OpenGL能够检查所有面向(Front Facing)观察者的面,并渲染它们,而丢弃那些背向(Back Facing)的面,节省我们很多 ...
- 2面剔除
这正是面剔除(Face culling)所要做的。OpenGL允许检查所有正面朝向(Front facing)观察者的面,并渲染它们,而丢弃所有背面朝向(Back facing)的面,这样就节约...
- 3Face Culling - OpenGL Wiki
- 4A Compact Method for Backface Culling - Gamasutra
Backface culling is an important part of how a 3D engine performs visibility checks. Its purpose ...
- 5OpenGL學習腳印:背面剔除(Face Culling) - 程式人生
OpenGL學習腳印:背面剔除(Face Culling). 阿新• • 發佈:2019-02-02. 寫在前面在繪製封閉型別的幾何物件時,開啟背面剔除功能能夠提高渲染效能。本節簡要介紹下背面 ...