Shaders - LearnOpenGL

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

GLSL. Shaders are written in the C-like language GLSL. GLSL is tailored for use with graphics and contains useful features specifically targeted at vector and ... Ifyou'rerunningAdBlock,pleaseconsiderwhitelistingthissiteifyou'dliketosupportLearnOpenGL;andnoworries,Iwon'tbemadifyoudon't:) IntroductionGettingstartedOpenGLCreatingawindowHelloWindowHelloTriangleShadersTexturesTransformationsCoordinateSystemsCameraReviewLightingColorsBasicLightingMaterialsLightingmapsLightcastersMultiplelightsReviewModelLoadingAssimpMeshModelAdvancedOpenGLDepthtestingStenciltestingBlendingFacecullingFramebuffersCubemapsAdvancedDataAdvancedGLSLGeometryShaderInstancingAntiAliasingAdvancedLightingAdvancedLightingGammaCorrectionShadowsShadowMappingPointShadowsNormalMappingParallaxMappingHDRBloomDeferredShadingSSAOPBRTheoryLightingIBLDiffuseirradianceSpecularIBLInPracticeDebuggingTextRendering2DGameBreakoutSettingupRenderingSpritesLevelsCollisionsBallCollisiondetectionCollisionresolutionParticlesPostprocessingPowerupsAudioRendertextFinalthoughtsGuestArticlesHowtopublish2020OITIntroductionWeightedBlendedSkeletalAnimation2021CSMSceneSceneGraphFrustumCullingTessellationHeightmapTessellationDSACoderepositoryTranslationsAbout BTC 1CLGKgmBSuYJ1nnvDGAepVTKNNDpUjfpRa ETH/ERC20 0x1de59bd9e52521a46309474f8372531533bd7c43 Shaders Getting-started/Shaders AsmentionedintheHelloTrianglechapter,shadersarelittleprogramsthatrestontheGPU.Theseprogramsarerunforeachspecificsectionofthegraphicspipeline.Inabasicsense,shadersarenothingmorethanprogramstransforminginputstooutputs.Shadersarealsoveryisolatedprogramsinthatthey'renotallowedtocommunicatewitheachother;theonlycommunicationtheyhaveisviatheirinputsandoutputs. Inthepreviouschapterwebrieflytouchedthesurfaceofshadersandhowtoproperlyusethem.Wewillnowexplainshaders,andspecificallytheOpenGLShadingLanguage,inamoregeneralfashion. GLSL ShadersarewrittenintheC-likelanguageGLSL.GLSListailoredforusewithgraphicsandcontainsusefulfeaturesspecificallytargetedatvectorandmatrixmanipulation. Shadersalwaysbeginwithaversiondeclaration,followedbyalistofinputandoutputvariables,uniformsanditsmainfunction.Eachshader'sentrypointisatitsmainfunctionwhereweprocessanyinputvariablesandoutputtheresultsinitsoutputvariables.Don'tworryifyoudon'tknowwhatuniformsare,we'llgettothoseshortly. Ashadertypicallyhasthefollowingstructure: #versionversion_number intypein_variable_name; intypein_variable_name; outtypeout_variable_name; uniformtypeuniform_name; voidmain() { //processinput(s)anddosomeweirdgraphicsstuff ... //outputprocessedstufftooutputvariable out_variable_name=weird_stuff_we_processed; } Whenwe'retalkingspecificallyaboutthevertexshadereachinputvariableisalsoknownasavertexattribute.Thereisamaximumnumberofvertexattributeswe'reallowedtodeclarelimitedbythehardware.OpenGLguaranteestherearealwaysatleast164-componentvertexattributesavailable,butsomehardwaremayallowformorewhichyoucanretrievebyqueryingGL_MAX_VERTEX_ATTRIBS: intnrAttributes; glGetIntegerv(GL_MAX_VERTEX_ATTRIBS,&nrAttributes); std::cout<//includegladtogetalltherequiredOpenGLheaders #include #include #include #include classShader { public: //theprogramID unsignedintID; //constructorreadsandbuildstheshader Shader(constchar*vertexPath,constchar*fragmentPath); //use/activatetheshader voiduse(); //utilityuniformfunctions voidsetBool(conststd::string&name,boolvalue)const; voidsetInt(conststd::string&name,intvalue)const; voidsetFloat(conststd::string&name,floatvalue)const; }; #endif Weusedseveralpreprocessordirectivesatthetopoftheheaderfile.Usingtheselittlelinesofcodeinformsyourcompilertoonlyincludeandcompilethisheaderfileifithasn'tbeenincludedyet,evenifmultiplefilesincludetheshaderheader.Thispreventslinkingconflicts. TheshaderclassholdstheIDoftheshaderprogram.Itsconstructorrequiresthefilepathsofthesourcecodeofthevertexandfragmentshaderrespectivelythatwecanstoreondiskassimpletextfiles.Toaddalittleextrawealsoaddseveralutilityfunctionstoeaseourlivesalittle:useactivatestheshaderprogram,andallset...functionsqueryauniformlocationandsetitsvalue. Readingfromfile We'reusingC++filestreamstoreadthecontentfromthefileintoseveralstringobjects: Shader(constchar*vertexPath,constchar*fragmentPath) { //1.retrievethevertex/fragmentsourcecodefromfilePath std::stringvertexCode; std::stringfragmentCode; std::ifstreamvShaderFile; std::ifstreamfShaderFile; //ensureifstreamobjectscanthrowexceptions: vShaderFile.exceptions(std::ifstream::failbit|std::ifstream::badbit); fShaderFile.exceptions(std::ifstream::failbit|std::ifstream::badbit); try { //openfiles vShaderFile.open(vertexPath); fShaderFile.open(fragmentPath); std::stringstreamvShaderStream,fShaderStream; //readfile'sbuffercontentsintostreams vShaderStream<



請為這篇文章評分?