README.md (8490B)
1 glad 2 ==== 3 4 GL/GLES/EGL/GLX/WGL Loader-Generator based on the official specs. 5 6 Checkout the [webservice](http://glad.dav1d.de) to generate the files you need! 7 8 9 ```c 10 // GLAD_DEBUG is only defined if the c-debug generator was used 11 #ifdef GLAD_DEBUG 12 // logs every gl call to the console 13 void pre_gl_call(const char *name, void *funcptr, int len_args, ...) { 14 printf("Calling: %s (%d arguments)\n", name, len_args); 15 } 16 #endif 17 18 19 int main(int argc, char **argv) 20 { 21 glutInit(&argc, argv); 22 glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE); 23 glutInitWindowSize(width, height); 24 glutCreateWindow("cookie"); 25 26 glutReshapeFunc(reshape); 27 glutDisplayFunc(display); 28 29 if(!gladLoadGL()) { 30 printf("Something went wrong!\n"); 31 exit(-1); 32 } 33 34 #ifdef GLAD_DEBUG 35 // before every opengl call call pre_gl_call 36 glad_set_pre_callback(pre_gl_call); 37 38 // post callback checks for glGetError by default 39 40 // don't use the callback for glClear 41 // (glClear could be replaced with your own function) 42 glad_debug_glClear = glad_glClear; 43 #endif 44 45 // gladLoadGLLoader(&glutGetProcAddress); 46 printf("OpenGL %d.%d\n", GLVersion.major, GLVersion.minor); 47 if (GLVersion.major < 2) { 48 printf("Your system doesn't support OpenGL >= 2!\n"); 49 return -1; 50 } 51 52 printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), 53 glGetString(GL_SHADING_LANGUAGE_VERSION)); 54 55 glutMainLoop(); 56 57 return 0; 58 } 59 ``` 60 61 Checkout the full example: [simple.c](https://github.com/Dav1dde/glad/blob/master/example/c/simple.c) 62 63 Or the C++ example using [GLFW](http://glfw.org): 64 [hellowindow2.cpp](https://github.com/Dav1dde/glad/blob/master/example/c%2B%2B/hellowindow2.cpp) 65 66 67 ## Usage ## 68 69 70 **If you don't want to install glad you can use the [webservice](http://glad.dav1d.de)** 71 72 73 Otherwise either install glad via pip: 74 75 # Windows 76 pip install glad 77 78 # Linux 79 pip install --user glad 80 # Linux global (root) 81 pip install glad 82 83 glad --help 84 85 To install the most recent version from Github: 86 87 pip install --upgrade git+https://github.com/dav1dde/glad.git#egg=glad 88 89 Or launch glad directly (after cloning the repository): 90 91 python -m glad --help 92 93 94 Possible commandline options: 95 96 usage: glad [-h] [--profile {core,compatibility}] --out-path OUT 97 [--api API] --generator {c,d,volt} 98 [--extensions EXTENSIONS] [--spec {gl,egl,glx,wgl}] 99 [--no-loader] 100 101 Uses the official Khronos-XML specs to generate a GL/GLES/EGL/GLX/WGL Loader 102 made for your needs. Glad currently supports the languages C, D and Volt. 103 104 optional arguments: 105 -h, --help show this help message and exit 106 --profile {core,compatibility} 107 OpenGL profile (defaults to compatibility) 108 --out-path OUT Output path for loader 109 --api API API type/version pairs, like "gl=3.2,gles=", no 110 version means latest 111 --generator {c,c-debug,d,volt} 112 Language to generate the binding for 113 --extensions EXTENSIONS 114 Path to extensions file or comma separated list of 115 extensions, if missing all extensions are included 116 --spec {gl,egl,glx,wgl} 117 Name of the spec 118 --no-loader 119 --omit-khrplatform Omits inclusion of the khrplatform.h file which is 120 often unnecessary. Only has an effect if used 121 together with c generators. 122 --local-files Forces every file directly into the output directory. 123 No src or include subdirectories are generated. Only 124 has an effect if used together with c generators. 125 126 127 To generate a loader for C with two extensions, it could look like this: 128 129 python main.py --generator=c --extensions=GL_EXT_framebuffer_multisample,GL_EXT_texture_filter_anisotropic --out-path=GL 130 131 `--out-path` and `--generator` are required! 132 If the `--extensions` option is missing, glad adds support for all extensions found in the OpenGL spec. 133 134 135 ## Generators ## 136 137 ### C/C++ ### 138 139 ```c 140 struct gladGLversionStruct { 141 int major; 142 int minor; 143 }; 144 145 extern struct gladGLversionStruct GLVersion; 146 147 typedef void* (* GLADloadproc)(const char *name); 148 149 /* 150 * Load OpenGL using the internal loader. 151 * Returns the true/1 if loading succeeded. 152 * 153 */ 154 int gladLoadGL(void); 155 156 /* 157 * Load OpenGL using an external loader like SDL_GL_GetProcAddress. 158 * 159 * Substitute GL with the API you generated 160 * 161 */ 162 void gladLoadGLLoader(GLADloadproc); 163 ``` 164 165 `glad.h` completely replaces any `gl.h` or `gl3.h` only include `glad.h`. 166 167 ```c 168 if(!gladLoadGL()) { exit(-1) }; 169 printf("OpenGL Version %d.%d loaded", GLVersion.major, GLVersion.minor); 170 171 if(GLAD_GL_EXT_framebuffer_multisample) { 172 /* GL_EXT_framebuffer_multisample is supported */ 173 } 174 175 if(GLAD_GL_VERSION_3_0) { 176 /* We support at least OpenGL version 3 */ 177 } 178 ``` 179 180 On non-Windows platforms glad requires `libdl`, make sure to link with it (`-ldl` for gcc)! 181 182 Note, there are two kinds of extension/version symbols, e.g. `GL_VERSION_3_0` and 183 `GLAD_VERSION_3_0`. Latter is a runtime boolean (represented as integer), whereas 184 the first (not prefixed with `GLAD_`) is a compiletime-constant, indicating that this 185 header supports this version (the official headers define these symbols as well). 186 The runtime booleans are only valid *after* a succesful call to `gladLoadGL` or `gladLoadGLLoader`. 187 188 189 ### C/C++ Debug ### 190 191 The C-Debug generator extends the API by these two functions: 192 193 ```c 194 // this symbol only exists if generated with the c-debug generator 195 #define GLAD_DEBUG 196 typedef void (* GLADcallback)(const char *name, void *funcptr, int len_args, ...); 197 198 /* 199 * Sets a callback which will be called before every function call 200 * to a function loaded by glad. 201 * 202 */ 203 GLAPI void glad_set_pre_callback(GLADcallback cb); 204 205 /* 206 * Sets a callback which will be called after every function call 207 * to a function loaded by glad. 208 * 209 */ 210 GLAPI void glad_set_post_callback(GLADcallback cb); 211 ``` 212 213 To call a function like `glGetError` in a callback prefix it with `glad_`, e.g. 214 the default post callback looks like this: 215 216 ```c 217 void _post_call_callback_default(const char *name, void *funcptr, int len_args, ...) { 218 GLenum error_code; 219 error_code = glad_glGetError(); 220 221 if (error_code != GL_NO_ERROR) { 222 fprintf(stderr, "ERROR %d in %s\n", error_code, name); 223 } 224 } 225 ``` 226 227 You can also submit own implementations for every call made by overwriting 228 the function pointer with the name of the function prefixed by `glad_debug_`. 229 230 E.g. you could disable the callbacks for glClear with `glad_debug_glClear = glad_glClear`, where 231 `glad_glClear` is the function pointer loaded by glad. 232 233 The `glClear` macro is defined as `#define glClear glad_debug_glClear`, 234 `glad_debug_glClear` is initialized with a default implementation, which calls 235 the two callbacks and the real function, in this case `glad_glClear`. 236 237 238 ### D ### 239 240 Import `glad.gl` for OpenGL functions/extensions, import `glad.loader` to import 241 the functions needed to initialize glad and load the OpenGL functions. 242 243 ```d 244 enforce(gladLoadGL()); // optionally you can pass a loader to this function 245 writefln("OpenGL Version %d.%d loaded", GLVersion.major, GLVersion.minor); 246 247 if(GL_EXT_framebuffer_multisample) { 248 /* GL_EXT_framebuffer_multisample is supported */ 249 } 250 251 if(GL_VERSION_3_0) { 252 /* We support at least OpenGL version 3 */ 253 } 254 ``` 255 256 On non-Windows platforms glad requires `libdl`, make sure to link with it (`L-ldl` for dmd)! 257 258 259 ## FAQ ## 260 261 ### glad includes windows.h which breaks my code! 262 263 Defining `APIENTRY` before including `glad.h` solves this problem: 264 265 ```c 266 #ifdef _WIN32 267 #define APIENTRY __stdcall 268 #endif 269 270 #include <glad/glad.h> 271 ``` 272 273 But make sure you have the correct definition of `APIENTRY` for platforms which define `_WIN32` but don't use `__stdcall` 274 275 Relevant issue: [#42](https://github.com/Dav1dde/glad/issues/42) 276 277 278 279 ## Contribute ## 280 281 Contributing is easy! Found a bug? Message me or make a pull request! Added a new generator backend? 282 Make a pull request! 283 284 Special thanks for all the people who contributed and are going to contribute! 285 Also to these who helped me solve a problem when I simply could not think of a solution.