GL/GLES/EGL/GLX/WGLLoader-Generatorbasedontheofficialspecs.
Usethewebservicetogeneratethefilesyouneed!
#include<glad/glad.h>intmain(){//--snip--GLFWwindow*window=glfwCreateWindow(WIDTH,HEIGHT,"LearnOpenGL",NULL,NULL);glfwMakeContextCurrent(window);if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)){std::cout<<"FailedtoinitializeOpenGLcontext"<<std::endl;return-1;}glViewport(0,0,WIDTH,HEIGHT);//--snip--Thefullcode:hellowindow2.cpp.
Glad2Glad2isbecomingmatureandisprettystablenow,considerusingtheglad2branchoritswebservice.
Thereisnoneedtoswitch,ifyoudon'twantto.Iwillsupportbothversions.
Glad2bringsseveralimprovementsandnewfeatures:
BetterEGL,GLX,WGLsupportVulkanSupportRustSupportMoreGeneratorFeatures(e.g.headeronly)BetterXML-SpecificationparsingBetterWeb-GeneratorBetterCmakesupportBetterExamplesBetterCLIBetterLoaderBetterAPIIfyou'reusinggladformorethanGL,Ihighlyrecommendcheckingoutglad2.
UsageIfyoudon'twanttoinstallgladyoucanusethewebservice
Otherwiseeitherinstallgladviapip:
#Windowspipinstallglad#Linuxpipinstall--userglad#Linuxglobal(root)pipinstallgladToinstallthemostrecentversionfromGithub:
pipinstall--upgradegit+https://github.com/dav1dde/glad.git#egg=gladOrlaunchgladdirectly(aftercloningtherepository):
python-mglad--helpInstallingandbuildinggladviavcpkg
Youcandownloadandinstallgladusingthevcpkgdependencymanager:
gitclonehttps://github.com/Microsoft/vcpkg.gitcdvcpkg./bootstrap-vcpkg.sh./vcpkgintegrateinstallvcpkginstallgladThegladportinvcpkgiskeptuptodatebyMicrosoftteammembersandcommunitycontributors.Iftheversionisoutofdate,pleasecreateanissueorpullrequestonthevcpkgrepository.
Whenintegratinggladintoyourbuildsystemthe--reproducibleoptionishighlyrecommended.
GeneratorsC/C++structgladGLversionStruct{intmajor;intminor;};externstructgladGLversionStructGLVersion;typedefvoid*(*GLADloadproc)(constchar*name);/**LoadOpenGLusingtheinternalloader.*Returnsthetrue/1ifloadingsucceeded.**/intgladLoadGL(void);/**LoadOpenGLusinganexternalloaderlikeSDL_GL_GetProcAddress.**SubstituteGLwiththeAPIyougenerated**/intgladLoadGLLoader(GLADloadproc);/***WGLandGLXhaveanunloadfunctiontofreethemodulehandle.*CalltheunloadfunctionafteryourlastGLXorWGLAPIcall.*/voidgladUnloadGLX(void);voidgladUnloadWGL(void);glad.hcompletelyreplacesanygl.horgl3.honlyincludeglad.h.
if(!gladLoadGL()){exit(-1);}printf("OpenGLVersion%d.%dloaded",GLVersion.major,GLVersion.minor);if(GLAD_GL_EXT_framebuffer_multisample){/*GL_EXT_framebuffer_multisampleissupported*/}if(GLAD_GL_VERSION_3_0){/*WesupportatleastOpenGLversion3*/}Onnon-Windowsplatformsgladrequireslibdl,makesuretolinkwithit(-ldl).
Note,therearetwokindsofextension/versionsymbols,e.g.GL_VERSION_3_0andGLAD_VERSION_3_0.Latterisaruntimeboolean(representedasinteger),whereasthefirst(notprefixedwithGLAD_)isacompiletime-constant,indicatingthatthisheadersupportsthisversion(theofficialheadersdefinethesesymbolsaswell).TheruntimebooleansareonlyvalidafterasuccessfulcalltogladLoadGLorgladLoadGLLoader.
C/C++DebugTheC-DebuggeneratorextendstheAPIbythesetwofunctions:
//thissymbolonlyexistsifgeneratedwiththec-debuggenerator#defineGLAD_DEBUGtypedefvoid(*GLADcallback)(constchar*name,void*funcptr,intlen_args,...);/**Setsacallbackwhichwillbecalledbeforeeveryfunctioncall*toafunctionloadedbyglad.**/GLAPIvoidglad_set_pre_callback(GLADcallbackcb);/**Setsacallbackwhichwillbecalledaftereveryfunctioncall*toafunctionloadedbyglad.**/GLAPIvoidglad_set_post_callback(GLADcallbackcb);TocallafunctionlikeglGetErrorinacallbackprefixitwithglad_,e.g.thedefaultpostcallbacklookslikethis:
void_post_call_callback_default(constchar*name,void*funcptr,intlen_args,...){GLenumerror_code;error_code=glad_glGetError();if(error_code!=GL_NO_ERROR){fprintf(stderr,"ERROR%din%s\n",error_code,name);}}Youcanalsosubmitownimplementationsforeverycallmadebyoverwritingthefunctionpointerwiththenameofthefunctionprefixedbyglad_debug_.
E.g.youcoulddisablethecallbacksforglClearwithglad_debug_glClear=glad_glClear,whereglad_glClearisthefunctionpointerloadedbyglad.
TheglClearmacroisdefinedas#defineglClearglad_debug_glClear,glad_debug_glClearisinitializedwithadefaultimplementation,whichcallsthetwocallbacksandtherealfunction,inthiscaseglad_glClear.
FAQHowdoIbuildgladorhowdoIintegrateglad?Easiestwayofusinggladisthroughthewebservice.
Alternativelygladintegrateswith:
CMakeConanVCPKGThanksforallthehelpandsupportmaintainingthose!
gladincludeswindows.h#42Since0.1.30:gladdoesnotincludewindows.hanymore.
Before0.1.30:DefiningAPIENTRYbeforeincludingglad.hsolvesthisproblem:
#ifdef_WIN32#defineAPIENTRY__stdcall#endif#include<glad/glad.h>ButmakesureyouhavethecorrectdefinitionofAPIENTRYforplatformswhichdefine_WIN32butdon'tuse__stdcall
What'sthelicenseofgladgeneratedcode?#101#253
ThegladgeneratedcodeitselfisanyofPublicDomain,WTFPLorCC0,thesourcefilesforthegeneratedcodeareundervariouslicensesfromKhronos.
EGL:Seeegl.xmlGL:ApacheVersion2.0GLX:ApacheVersion2.0WGL:ApacheVersion2.0Vulkan:ApacheVersion2.0withexceptionsforgeneratedcodeNowtheApacheLicensemayapplytothegeneratedcode(notalawyer),butseethisclarifyingcomment.
GladalsoaddsheaderfilesformKhronos,thesehaveseparatedlicensesintheirheader.
ContributeContributingiseasy!Foundabug?Messagemeormakeapullrequest!Addedanewgeneratorbackend?Makeapullrequest!
Specialthanksforallthepeoplewhocontributedandaregoingtocontribute!AlsotothesewhohelpedmesolveaproblemwhenIsimplycouldnotthinkofasolution.
评论