| 1 | /********************************************************************************************** |
| 2 | * |
| 3 | * rlgl v6.0 - A multi-OpenGL abstraction layer with an immediate-mode style API |
| 4 | * |
| 5 | * DESCRIPTION: |
| 6 | * An abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, 4.3 Core, ES 2.0, ES 3.0) |
| 7 | * that provides a pseudo-OpenGL 1.1 immediate-mode style API (rlVertex, rlTranslate, rlRotate...) |
| 8 | * |
| 9 | * ADDITIONAL NOTES: |
| 10 | * When choosing an OpenGL backend different than OpenGL 1.1, some internal buffers are |
| 11 | * initialized on rlglInit() to accumulate vertex data |
| 12 | * |
| 13 | * When an internal state change is required all the stored vertex data is rendered in a batch, |
| 14 | * additionally, rlDrawRenderBatchActive() could be called to force flushing of the batch |
| 15 | * |
| 16 | * Some resources are also loaded for convenience, here the complete list: |
| 17 | * - Default batch (RLGL.defaultBatch): RenderBatch system to accumulate vertex data |
| 18 | * - Default texture (RLGL.defaultTextureId): 1x1 white pixel R8G8B8A8 |
| 19 | * - Default shader (RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs) |
| 20 | * |
| 21 | * Internal buffer (and resources) must be manually unloaded calling rlglClose() |
| 22 | * |
| 23 | * CONFIGURATION: |
| 24 | * #define GRAPHICS_API_OPENGL_SOFTWARE |
| 25 | * #define GRAPHICS_API_OPENGL_11 |
| 26 | * #define GRAPHICS_API_OPENGL_21 |
| 27 | * #define GRAPHICS_API_OPENGL_33 |
| 28 | * #define GRAPHICS_API_OPENGL_43 |
| 29 | * #define GRAPHICS_API_OPENGL_ES2 |
| 30 | * #define GRAPHICS_API_OPENGL_ES3 |
| 31 | * Use selected OpenGL graphics backend, should be supported by platform |
| 32 | * Those preprocessor defines are only used on the rlgl module, if OpenGL version is |
| 33 | * required by any other module, use rlGetVersion() to check it |
| 34 | * |
| 35 | * #define RLGL_IMPLEMENTATION |
| 36 | * Generates the implementation of the library into the included file |
| 37 | * If not defined, the library is in header only mode and can be included in other headers |
| 38 | * or source files without problems. But only ONE file should hold the implementation |
| 39 | * |
| 40 | * #if RLGL_SHOW_GL_DETAILS_INFO |
| 41 | * Show OpenGL extensions and capabilities detailed logs on init |
| 42 | * |
| 43 | * #if RLGL_ENABLE_OPENGL_DEBUG_CONTEXT |
| 44 | * Enable debug context (only available on OpenGL 4.3) |
| 45 | * |
| 46 | * rlgl capabilities could be customized defining some internal |
| 47 | * values before library inclusion (default values listed): |
| 48 | * |
| 49 | * #define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch elements limits |
| 50 | * #define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) |
| 51 | * #define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) |
| 52 | * #define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of texture units that can be activated on batch drawing (SetShaderValueTexture()) |
| 53 | * |
| 54 | * #define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack |
| 55 | * #define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported |
| 56 | * #define RL_CULL_DISTANCE_NEAR 0.05f // Default projection matrix near cull distance |
| 57 | * #define RL_CULL_DISTANCE_FAR 4000.0f // Default projection matrix far cull distance |
| 58 | * |
| 59 | * When loading a shader, the following vertex attributes and uniform location names are tried to be set automatically: |
| 60 | * WARNING: Pre-defined names can not be changed, they are used by default shaders and all raylib examples shaders, they are just listed here for reference |
| 61 | * |
| 62 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION |
| 63 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD |
| 64 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL |
| 65 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR |
| 66 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT |
| 67 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2 |
| 68 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_BONEINDICES "vertexBoneIndices" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEINDICES |
| 69 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS "vertexBoneWeights" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS |
| 70 | * #define RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCETRANSFORM "instanceTransform" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCETRANSFORM |
| 71 | * |
| 72 | * #define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix |
| 73 | * #define RL_DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix |
| 74 | * #define RL_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix |
| 75 | * #define RL_DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix |
| 76 | * #define RL_DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView))) |
| 77 | * #define RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color) |
| 78 | * #define RL_DEFAULT_SHADER_UNIFORM_NAME_BONEMATRICES "boneMatrices" // bone matrices |
| 79 | * |
| 80 | * #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0) |
| 81 | * #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1) |
| 82 | * #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2) |
| 83 | * |
| 84 | * DEPENDENCIES: |
| 85 | * - OpenGL libraries (depending on platform and OpenGL version selected) |
| 86 | * - GLAD OpenGL extensions loading library (only for OpenGL 3.3 Core, 4.3 Core) |
| 87 | * |
| 88 | * |
| 89 | * LICENSE: zlib/libpng |
| 90 | * |
| 91 | * Copyright (c) 2014-2026 Ramon Santamaria (@raysan5) |
| 92 | * |
| 93 | * This software is provided "as-is", without any express or implied warranty. In no event |
| 94 | * will the authors be held liable for any damages arising from the use of this software. |
| 95 | * |
| 96 | * Permission is granted to anyone to use this software for any purpose, including commercial |
| 97 | * applications, and to alter it and redistribute it freely, subject to the following restrictions: |
| 98 | * |
| 99 | * 1. The origin of this software must not be misrepresented; you must not claim that you |
| 100 | * wrote the original software. If you use this software in a product, an acknowledgment |
| 101 | * in the product documentation would be appreciated but is not required. |
| 102 | * |
| 103 | * 2. Altered source versions must be plainly marked as such, and must not be misrepresented |
| 104 | * as being the original software. |
| 105 | * |
| 106 | * 3. This notice may not be removed or altered from any source distribution. |
| 107 | * |
| 108 | **********************************************************************************************/ |
| 109 | |
| 110 | #ifndef RLGL_H |
| 111 | #define RLGL_H |
| 112 | |
| 113 | #define RLGL_VERSION "6.0" |
| 114 | |
| 115 | // Function specifiers in case library is build/used as a shared library |
| 116 | // NOTE: Microsoft specifiers to tell compiler that symbols are imported/exported from a .dll |
| 117 | // NOTE: visibility(default) attribute makes symbols "visible" when compiled with -fvisibility=hidden |
| 118 | #if defined(_WIN32) && defined(BUILD_LIBTYPE_SHARED) |
| 119 | #define RLAPI __declspec(dllexport) // Building the library as a Win32 shared library (.dll) |
| 120 | #elif defined(BUILD_LIBTYPE_SHARED) |
| 121 | #define RLAPI __attribute__((visibility("default"))) // Building the library as a Unix shared library (.so/.dylib) |
| 122 | #elif defined(_WIN32) && defined(USE_LIBTYPE_SHARED) |
| 123 | #define RLAPI __declspec(dllimport) // Using the library as a Win32 shared library (.dll) |
| 124 | #endif |
| 125 | |
| 126 | // Function specifiers definition |
| 127 | #ifndef RLAPI |
| 128 | #define RLAPI // Functions defined as 'extern' by default (implicit specifiers) |
| 129 | #endif |
| 130 | |
| 131 | // Support TRACELOG macros |
| 132 | #ifndef TRACELOG |
| 133 | #define TRACELOG(level, ...) (void)0 |
| 134 | #endif |
| 135 | |
| 136 | // Allow custom memory allocators |
| 137 | #ifndef RL_MALLOC |
| 138 | #define RL_MALLOC(sz) malloc(sz) |
| 139 | #endif |
| 140 | #ifndef RL_CALLOC |
| 141 | #define RL_CALLOC(n,sz) calloc(n,sz) |
| 142 | #endif |
| 143 | #ifndef RL_REALLOC |
| 144 | #define RL_REALLOC(n,sz) realloc(n,sz) |
| 145 | #endif |
| 146 | #ifndef RL_FREE |
| 147 | #define RL_FREE(p) free(p) |
| 148 | #endif |
| 149 | |
| 150 | // Security check in case no GRAPHICS_API_OPENGL_* defined |
| 151 | #if !defined(GRAPHICS_API_OPENGL_SOFTWARE) && \ |
| 152 | !defined(GRAPHICS_API_OPENGL_11) && \ |
| 153 | !defined(GRAPHICS_API_OPENGL_21) && \ |
| 154 | !defined(GRAPHICS_API_OPENGL_33) && \ |
| 155 | !defined(GRAPHICS_API_OPENGL_43) && \ |
| 156 | !defined(GRAPHICS_API_OPENGL_ES2) && \ |
| 157 | !defined(GRAPHICS_API_OPENGL_ES3) |
| 158 | #define GRAPHICS_API_OPENGL_33 |
| 159 | #endif |
| 160 | |
| 161 | // Security check in case multiple GRAPHICS_API_OPENGL_* defined |
| 162 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 163 | #if defined(GRAPHICS_API_OPENGL_21) |
| 164 | #undef GRAPHICS_API_OPENGL_21 |
| 165 | #endif |
| 166 | #if defined(GRAPHICS_API_OPENGL_33) |
| 167 | #undef GRAPHICS_API_OPENGL_33 |
| 168 | #endif |
| 169 | #if defined(GRAPHICS_API_OPENGL_43) |
| 170 | #undef GRAPHICS_API_OPENGL_43 |
| 171 | #endif |
| 172 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 173 | #undef GRAPHICS_API_OPENGL_ES2 |
| 174 | #endif |
| 175 | #endif |
| 176 | |
| 177 | // Software implementation uses OpenGL 1.1 functionality |
| 178 | #if defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 179 | #define GRAPHICS_API_OPENGL_11 |
| 180 | #endif |
| 181 | |
| 182 | // OpenGL 2.1 uses most of OpenGL 3.3 Core functionality |
| 183 | // WARNING: Specific parts are checked with #if defines |
| 184 | #if defined(GRAPHICS_API_OPENGL_21) |
| 185 | #define GRAPHICS_API_OPENGL_33 |
| 186 | #endif |
| 187 | |
| 188 | // OpenGL 4.3 uses OpenGL 3.3 Core functionality |
| 189 | #if defined(GRAPHICS_API_OPENGL_43) |
| 190 | #define GRAPHICS_API_OPENGL_33 |
| 191 | #endif |
| 192 | |
| 193 | // OpenGL ES 3.0 uses OpenGL ES 2.0 functionality (and more) |
| 194 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 195 | #define GRAPHICS_API_OPENGL_ES2 |
| 196 | #endif |
| 197 | |
| 198 | //---------------------------------------------------------------------------------- |
| 199 | // Defines and Macros |
| 200 | //---------------------------------------------------------------------------------- |
| 201 | |
| 202 | // Default internal render batch elements limits |
| 203 | #ifndef RL_DEFAULT_BATCH_BUFFER_ELEMENTS |
| 204 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) |
| 205 | // This is the maximum amount of elements (quads) per batch |
| 206 | // NOTE: Be careful with text, every letter maps to a quad |
| 207 | #define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192 |
| 208 | #endif |
| 209 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 210 | // Reducing memory sizes for embedded systems (RPI and HTML5) |
| 211 | // NOTE: On HTML5 (emscripten) this is allocated on heap, |
| 212 | // by default heap is only 16MB!...just take care... |
| 213 | #define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 2048 |
| 214 | #endif |
| 215 | #endif |
| 216 | #ifndef RL_DEFAULT_BATCH_BUFFERS |
| 217 | #define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) |
| 218 | #endif |
| 219 | #ifndef RL_DEFAULT_BATCH_DRAWCALLS |
| 220 | #define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) |
| 221 | #endif |
| 222 | #ifndef RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS |
| 223 | #define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture()) |
| 224 | #endif |
| 225 | |
| 226 | // Internal Matrix stack |
| 227 | #ifndef RL_MAX_MATRIX_STACK_SIZE |
| 228 | #define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of Matrix stack |
| 229 | #endif |
| 230 | |
| 231 | // Shader limits |
| 232 | #ifndef RL_MAX_SHADER_LOCATIONS |
| 233 | #define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported |
| 234 | #endif |
| 235 | |
| 236 | // Projection matrix culling |
| 237 | #ifndef RL_CULL_DISTANCE_NEAR |
| 238 | #define RL_CULL_DISTANCE_NEAR 0.05f // Default near cull distance |
| 239 | #endif |
| 240 | #ifndef RL_CULL_DISTANCE_FAR |
| 241 | #define RL_CULL_DISTANCE_FAR 4000.0f // Default far cull distance |
| 242 | #endif |
| 243 | |
| 244 | // Texture parameters (equivalent to OpenGL defines) |
| 245 | #define RL_TEXTURE_WRAP_S 0x2802 // GL_TEXTURE_WRAP_S |
| 246 | #define RL_TEXTURE_WRAP_T 0x2803 // GL_TEXTURE_WRAP_T |
| 247 | #define RL_TEXTURE_MAG_FILTER 0x2800 // GL_TEXTURE_MAG_FILTER |
| 248 | #define RL_TEXTURE_MIN_FILTER 0x2801 // GL_TEXTURE_MIN_FILTER |
| 249 | |
| 250 | #define RL_TEXTURE_FILTER_NEAREST 0x2600 // GL_NEAREST |
| 251 | #define RL_TEXTURE_FILTER_LINEAR 0x2601 // GL_LINEAR |
| 252 | #define RL_TEXTURE_FILTER_MIP_NEAREST 0x2700 // GL_NEAREST_MIPMAP_NEAREST |
| 253 | #define RL_TEXTURE_FILTER_NEAREST_MIP_LINEAR 0x2702 // GL_NEAREST_MIPMAP_LINEAR |
| 254 | #define RL_TEXTURE_FILTER_LINEAR_MIP_NEAREST 0x2701 // GL_LINEAR_MIPMAP_NEAREST |
| 255 | #define RL_TEXTURE_FILTER_MIP_LINEAR 0x2703 // GL_LINEAR_MIPMAP_LINEAR |
| 256 | #define RL_TEXTURE_FILTER_ANISOTROPIC 0x3000 // Anisotropic filter (custom identifier) |
| 257 | #define RL_TEXTURE_MIPMAP_BIAS_RATIO 0x4000 // Texture mipmap bias, percentage ratio (custom identifier) |
| 258 | |
| 259 | #define RL_TEXTURE_WRAP_REPEAT 0x2901 // GL_REPEAT |
| 260 | #define RL_TEXTURE_WRAP_CLAMP 0x812F // GL_CLAMP_TO_EDGE |
| 261 | #define RL_TEXTURE_WRAP_MIRROR_REPEAT 0x8370 // GL_MIRRORED_REPEAT |
| 262 | #define RL_TEXTURE_WRAP_MIRROR_CLAMP 0x8742 // GL_MIRROR_CLAMP_EXT |
| 263 | |
| 264 | // Matrix modes (equivalent to OpenGL) |
| 265 | #define RL_MODELVIEW 0x1700 // GL_MODELVIEW |
| 266 | #define RL_PROJECTION 0x1701 // GL_PROJECTION |
| 267 | #define RL_TEXTURE 0x1702 // GL_TEXTURE |
| 268 | |
| 269 | // Primitive assembly draw modes |
| 270 | #define RL_LINES 0x0001 // GL_LINES |
| 271 | #define RL_TRIANGLES 0x0004 // GL_TRIANGLES |
| 272 | #define RL_QUADS 0x0007 // GL_QUADS |
| 273 | |
| 274 | // GL equivalent data types |
| 275 | #define RL_UNSIGNED_BYTE 0x1401 // GL_UNSIGNED_BYTE |
| 276 | #define RL_FLOAT 0x1406 // GL_FLOAT |
| 277 | |
| 278 | // GL buffer usage hint |
| 279 | #define RL_STREAM_DRAW 0x88E0 // GL_STREAM_DRAW |
| 280 | #define RL_STREAM_READ 0x88E1 // GL_STREAM_READ |
| 281 | #define RL_STREAM_COPY 0x88E2 // GL_STREAM_COPY |
| 282 | #define RL_STATIC_DRAW 0x88E4 // GL_STATIC_DRAW |
| 283 | #define RL_STATIC_READ 0x88E5 // GL_STATIC_READ |
| 284 | #define RL_STATIC_COPY 0x88E6 // GL_STATIC_COPY |
| 285 | #define RL_DYNAMIC_DRAW 0x88E8 // GL_DYNAMIC_DRAW |
| 286 | #define RL_DYNAMIC_READ 0x88E9 // GL_DYNAMIC_READ |
| 287 | #define RL_DYNAMIC_COPY 0x88EA // GL_DYNAMIC_COPY |
| 288 | |
| 289 | // GL Shader type |
| 290 | #define RL_FRAGMENT_SHADER 0x8B30 // GL_FRAGMENT_SHADER |
| 291 | #define RL_VERTEX_SHADER 0x8B31 // GL_VERTEX_SHADER |
| 292 | #define RL_COMPUTE_SHADER 0x91B9 // GL_COMPUTE_SHADER |
| 293 | |
| 294 | // GL blending factors |
| 295 | #define RL_ZERO 0 // GL_ZERO |
| 296 | #define RL_ONE 1 // GL_ONE |
| 297 | #define RL_SRC_COLOR 0x0300 // GL_SRC_COLOR |
| 298 | #define RL_ONE_MINUS_SRC_COLOR 0x0301 // GL_ONE_MINUS_SRC_COLOR |
| 299 | #define RL_SRC_ALPHA 0x0302 // GL_SRC_ALPHA |
| 300 | #define RL_ONE_MINUS_SRC_ALPHA 0x0303 // GL_ONE_MINUS_SRC_ALPHA |
| 301 | #define RL_DST_ALPHA 0x0304 // GL_DST_ALPHA |
| 302 | #define RL_ONE_MINUS_DST_ALPHA 0x0305 // GL_ONE_MINUS_DST_ALPHA |
| 303 | #define RL_DST_COLOR 0x0306 // GL_DST_COLOR |
| 304 | #define RL_ONE_MINUS_DST_COLOR 0x0307 // GL_ONE_MINUS_DST_COLOR |
| 305 | #define RL_SRC_ALPHA_SATURATE 0x0308 // GL_SRC_ALPHA_SATURATE |
| 306 | #define RL_CONSTANT_COLOR 0x8001 // GL_CONSTANT_COLOR |
| 307 | #define RL_ONE_MINUS_CONSTANT_COLOR 0x8002 // GL_ONE_MINUS_CONSTANT_COLOR |
| 308 | #define RL_CONSTANT_ALPHA 0x8003 // GL_CONSTANT_ALPHA |
| 309 | #define RL_ONE_MINUS_CONSTANT_ALPHA 0x8004 // GL_ONE_MINUS_CONSTANT_ALPHA |
| 310 | |
| 311 | // GL blending functions/equations |
| 312 | #define RL_FUNC_ADD 0x8006 // GL_FUNC_ADD |
| 313 | #define RL_MIN 0x8007 // GL_MIN |
| 314 | #define RL_MAX 0x8008 // GL_MAX |
| 315 | #define RL_FUNC_SUBTRACT 0x800A // GL_FUNC_SUBTRACT |
| 316 | #define RL_FUNC_REVERSE_SUBTRACT 0x800B // GL_FUNC_REVERSE_SUBTRACT |
| 317 | #define RL_BLEND_EQUATION 0x8009 // GL_BLEND_EQUATION |
| 318 | #define RL_BLEND_EQUATION_RGB 0x8009 // GL_BLEND_EQUATION_RGB // (Same as BLEND_EQUATION) |
| 319 | #define RL_BLEND_EQUATION_ALPHA 0x883D // GL_BLEND_EQUATION_ALPHA |
| 320 | #define RL_BLEND_DST_RGB 0x80C8 // GL_BLEND_DST_RGB |
| 321 | #define RL_BLEND_SRC_RGB 0x80C9 // GL_BLEND_SRC_RGB |
| 322 | #define RL_BLEND_DST_ALPHA 0x80CA // GL_BLEND_DST_ALPHA |
| 323 | #define RL_BLEND_SRC_ALPHA 0x80CB // GL_BLEND_SRC_ALPHA |
| 324 | #define RL_BLEND_COLOR 0x8005 // GL_BLEND_COLOR |
| 325 | |
| 326 | #define RL_READ_FRAMEBUFFER 0x8CA8 // GL_READ_FRAMEBUFFER |
| 327 | #define RL_DRAW_FRAMEBUFFER 0x8CA9 // GL_DRAW_FRAMEBUFFER |
| 328 | |
| 329 | // Default shader vertex attribute locations |
| 330 | // NOTE: Locations can be redefined by user if required |
| 331 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION |
| 332 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION 0 |
| 333 | #endif |
| 334 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD |
| 335 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD 1 |
| 336 | #endif |
| 337 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL |
| 338 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL 2 |
| 339 | #endif |
| 340 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR |
| 341 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR 3 |
| 342 | #endif |
| 343 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT |
| 344 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT 4 |
| 345 | #endif |
| 346 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2 |
| 347 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2 5 |
| 348 | #endif |
| 349 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_INDICES |
| 350 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_INDICES 6 |
| 351 | #endif |
| 352 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEINDICES |
| 353 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEINDICES 7 |
| 354 | #endif |
| 355 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS |
| 356 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS 8 |
| 357 | #endif |
| 358 | #ifndef RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCETRANSFORM |
| 359 | #define RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCETRANSFORM 9 |
| 360 | #endif |
| 361 | |
| 362 | //---------------------------------------------------------------------------------- |
| 363 | // Types and Structures Definition |
| 364 | //---------------------------------------------------------------------------------- |
| 365 | #if !defined(__cplusplus) |
| 366 | #if (defined(__STDC__) && __STDC_VERSION__ >= 199901L) || (defined(_MSC_VER) && _MSC_VER >= 1800) |
| 367 | #include <stdbool.h> |
| 368 | #elif !defined(bool) |
| 369 | typedef enum bool { false = 0, true = !false } bool; |
| 370 | #define RL_BOOL_TYPE |
| 371 | #endif |
| 372 | #endif |
| 373 | |
| 374 | #if !defined(RL_MATRIX_TYPE) |
| 375 | // Matrix, 4x4 components, column major, OpenGL style, right handed |
| 376 | typedef struct Matrix { |
| 377 | float m0, m4, m8, m12; // Matrix first row (4 components) |
| 378 | float m1, m5, m9, m13; // Matrix second row (4 components) |
| 379 | float m2, m6, m10, m14; // Matrix third row (4 components) |
| 380 | float m3, m7, m11, m15; // Matrix fourth row (4 components) |
| 381 | } Matrix; |
| 382 | #define RL_MATRIX_TYPE |
| 383 | #endif |
| 384 | |
| 385 | // Dynamic vertex buffers (position + texcoords + colors + indices arrays) |
| 386 | typedef struct rlVertexBuffer { |
| 387 | int elementCount; // Number of elements in the buffer (QUADS) |
| 388 | |
| 389 | float *vertices; // Vertex position (XYZ - 3 components per vertex) (shader-location = 0) |
| 390 | float *texcoords; // Vertex texture coordinates (UV - 2 components per vertex) (shader-location = 1) |
| 391 | float *normals; // Vertex normal (XYZ - 3 components per vertex) (shader-location = 2) |
| 392 | unsigned char *colors; // Vertex colors (RGBA - 4 components per vertex) (shader-location = 3) |
| 393 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) |
| 394 | unsigned int *indices; // Vertex indices (in case vertex data comes indexed) (6 indices per quad) |
| 395 | #endif |
| 396 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 397 | unsigned short *indices; // Vertex indices (in case vertex data comes indexed) (6 indices per quad) |
| 398 | #endif |
| 399 | unsigned int vaoId; // OpenGL Vertex Array Object id |
| 400 | unsigned int vboId[5]; // OpenGL Vertex Buffer Objects id (5 types of vertex data) |
| 401 | } rlVertexBuffer; |
| 402 | |
| 403 | // Draw call type |
| 404 | // NOTE: Only texture changes register a new draw, other state-change-related elements are not |
| 405 | // used at this moment (vaoId, shaderId, matrices), raylib forces a batch draw call if any |
| 406 | // of those state-change happens (this is done in core module) |
| 407 | typedef struct rlDrawCall { |
| 408 | int mode; // Drawing mode: LINES, TRIANGLES, QUADS |
| 409 | int vertexCount; // Number of vertex of the draw |
| 410 | int vertexAlignment; // Number of vertex required for index alignment (LINES, TRIANGLES) |
| 411 | //unsigned int vaoId; // Vertex array id to be used on the draw -> Using RLGL.currentBatch->vertexBuffer.vaoId |
| 412 | //unsigned int shaderId; // Shader id to be used on the draw -> Using RLGL.currentShaderId |
| 413 | unsigned int textureId; // Texture id to be used on the draw -> Use to create new draw call if changes |
| 414 | |
| 415 | //Matrix projection; // Projection matrix for this draw -> Using RLGL.projection by default |
| 416 | //Matrix modelview; // Modelview matrix for this draw -> Using RLGL.modelview by default |
| 417 | } rlDrawCall; |
| 418 | |
| 419 | // rlRenderBatch type |
| 420 | typedef struct rlRenderBatch { |
| 421 | int bufferCount; // Number of vertex buffers (multi-buffering support) |
| 422 | int currentBuffer; // Current buffer tracking in case of multi-buffering |
| 423 | rlVertexBuffer *vertexBuffer; // Dynamic buffer(s) for vertex data |
| 424 | |
| 425 | rlDrawCall *draws; // Draw calls array, depends on textureId |
| 426 | int drawCounter; // Draw calls counter |
| 427 | float currentDepth; // Current depth value for next draw |
| 428 | } rlRenderBatch; |
| 429 | |
| 430 | // OpenGL version |
| 431 | typedef enum { |
| 432 | RL_OPENGL_SOFTWARE = 0, // Software rendering |
| 433 | RL_OPENGL_11, // OpenGL 1.1 |
| 434 | RL_OPENGL_21, // OpenGL 2.1 (GLSL 120) |
| 435 | RL_OPENGL_33, // OpenGL 3.3 (GLSL 330) |
| 436 | RL_OPENGL_43, // OpenGL 4.3 (using GLSL 330) |
| 437 | RL_OPENGL_ES_20, // OpenGL ES 2.0 (GLSL 100) |
| 438 | RL_OPENGL_ES_30 // OpenGL ES 3.0 (GLSL 300 es) |
| 439 | } rlGlVersion; |
| 440 | |
| 441 | // Trace log level |
| 442 | // NOTE: Organized by priority level |
| 443 | typedef enum { |
| 444 | RL_LOG_ALL = 0, // Display all logs |
| 445 | RL_LOG_TRACE, // Trace logging, intended for internal use only |
| 446 | RL_LOG_DEBUG, // Debug logging, used for internal debugging, it should be disabled on release builds |
| 447 | RL_LOG_INFO, // Info logging, used for program execution info |
| 448 | RL_LOG_WARNING, // Warning logging, used on recoverable failures |
| 449 | RL_LOG_ERROR, // Error logging, used on unrecoverable failures |
| 450 | RL_LOG_FATAL, // Fatal logging, used to abort program: exit(EXIT_FAILURE) |
| 451 | RL_LOG_NONE // Disable logging |
| 452 | } rlTraceLogLevel; |
| 453 | |
| 454 | // Texture pixel formats |
| 455 | // NOTE: Support depends on OpenGL version |
| 456 | typedef enum { |
| 457 | RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE = 1, // 8 bit per pixel (no alpha) |
| 458 | RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA, // 8*2 bpp (2 channels) |
| 459 | RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5, // 16 bpp |
| 460 | RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8, // 24 bpp |
| 461 | RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1, // 16 bpp (1 bit alpha) |
| 462 | RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4, // 16 bpp (4 bit alpha) |
| 463 | RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, // 32 bpp |
| 464 | RL_PIXELFORMAT_UNCOMPRESSED_R32, // 32 bpp (1 channel - float) |
| 465 | RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32, // 32*3 bpp (3 channels - float) |
| 466 | RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32, // 32*4 bpp (4 channels - float) |
| 467 | RL_PIXELFORMAT_UNCOMPRESSED_R16, // 16 bpp (1 channel - half float) |
| 468 | RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, // 16*3 bpp (3 channels - half float) |
| 469 | RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16, // 16*4 bpp (4 channels - half float) |
| 470 | RL_PIXELFORMAT_COMPRESSED_DXT1_RGB, // 4 bpp (no alpha) |
| 471 | RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA, // 4 bpp (1 bit alpha) |
| 472 | RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA, // 8 bpp |
| 473 | RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA, // 8 bpp |
| 474 | RL_PIXELFORMAT_COMPRESSED_ETC1_RGB, // 4 bpp |
| 475 | RL_PIXELFORMAT_COMPRESSED_ETC2_RGB, // 4 bpp |
| 476 | RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA, // 8 bpp |
| 477 | RL_PIXELFORMAT_COMPRESSED_PVRT_RGB, // 4 bpp |
| 478 | RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA, // 4 bpp |
| 479 | RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA, // 8 bpp |
| 480 | RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA // 2 bpp |
| 481 | } rlPixelFormat; |
| 482 | |
| 483 | // Texture parameters: filter mode |
| 484 | // NOTE 1: Filtering considers mipmaps if available in the texture |
| 485 | // NOTE 2: Filter is accordingly set for minification and magnification |
| 486 | typedef enum { |
| 487 | RL_TEXTURE_FILTER_POINT = 0, // No filter, pixel approximation |
| 488 | RL_TEXTURE_FILTER_BILINEAR, // Linear filtering |
| 489 | RL_TEXTURE_FILTER_TRILINEAR, // Trilinear filtering (linear with mipmaps) |
| 490 | RL_TEXTURE_FILTER_ANISOTROPIC_4X, // Anisotropic filtering 4x |
| 491 | RL_TEXTURE_FILTER_ANISOTROPIC_8X, // Anisotropic filtering 8x |
| 492 | RL_TEXTURE_FILTER_ANISOTROPIC_16X, // Anisotropic filtering 16x |
| 493 | } rlTextureFilter; |
| 494 | |
| 495 | // Color blending modes (pre-defined) |
| 496 | typedef enum { |
| 497 | RL_BLEND_ALPHA = 0, // Blend textures considering alpha (default) |
| 498 | RL_BLEND_ADDITIVE, // Blend textures adding colors |
| 499 | RL_BLEND_MULTIPLIED, // Blend textures multiplying colors |
| 500 | RL_BLEND_ADD_COLORS, // Blend textures adding colors (alternative) |
| 501 | RL_BLEND_SUBTRACT_COLORS, // Blend textures subtracting colors (alternative) |
| 502 | RL_BLEND_ALPHA_PREMULTIPLY, // Blend premultiplied textures considering alpha |
| 503 | RL_BLEND_CUSTOM, // Blend textures using custom src/dst factors (use rlSetBlendFactors()) |
| 504 | RL_BLEND_CUSTOM_SEPARATE // Blend textures using custom src/dst factors (use rlSetBlendFactorsSeparate()) |
| 505 | } rlBlendMode; |
| 506 | |
| 507 | // Shader location point type |
| 508 | typedef enum { |
| 509 | RL_SHADER_LOC_VERTEX_POSITION = 0, // Shader location: vertex attribute: position |
| 510 | RL_SHADER_LOC_VERTEX_TEXCOORD01, // Shader location: vertex attribute: texcoord01 |
| 511 | RL_SHADER_LOC_VERTEX_TEXCOORD02, // Shader location: vertex attribute: texcoord02 |
| 512 | RL_SHADER_LOC_VERTEX_NORMAL, // Shader location: vertex attribute: normal |
| 513 | RL_SHADER_LOC_VERTEX_TANGENT, // Shader location: vertex attribute: tangent |
| 514 | RL_SHADER_LOC_VERTEX_COLOR, // Shader location: vertex attribute: color |
| 515 | RL_SHADER_LOC_MATRIX_MVP, // Shader location: matrix uniform: model-view-projection |
| 516 | RL_SHADER_LOC_MATRIX_VIEW, // Shader location: matrix uniform: view (camera transform) |
| 517 | RL_SHADER_LOC_MATRIX_PROJECTION, // Shader location: matrix uniform: projection |
| 518 | RL_SHADER_LOC_MATRIX_MODEL, // Shader location: matrix uniform: model (transform) |
| 519 | RL_SHADER_LOC_MATRIX_NORMAL, // Shader location: matrix uniform: normal |
| 520 | RL_SHADER_LOC_VECTOR_VIEW, // Shader location: vector uniform: view |
| 521 | RL_SHADER_LOC_COLOR_DIFFUSE, // Shader location: vector uniform: diffuse color |
| 522 | RL_SHADER_LOC_COLOR_SPECULAR, // Shader location: vector uniform: specular color |
| 523 | RL_SHADER_LOC_COLOR_AMBIENT, // Shader location: vector uniform: ambient color |
| 524 | RL_SHADER_LOC_MAP_ALBEDO, // Shader location: sampler2d texture: albedo (same as: RL_SHADER_LOC_MAP_DIFFUSE) |
| 525 | RL_SHADER_LOC_MAP_METALNESS, // Shader location: sampler2d texture: metalness (same as: RL_SHADER_LOC_MAP_SPECULAR) |
| 526 | RL_SHADER_LOC_MAP_NORMAL, // Shader location: sampler2d texture: normal |
| 527 | RL_SHADER_LOC_MAP_ROUGHNESS, // Shader location: sampler2d texture: roughness |
| 528 | RL_SHADER_LOC_MAP_OCCLUSION, // Shader location: sampler2d texture: occlusion |
| 529 | RL_SHADER_LOC_MAP_EMISSION, // Shader location: sampler2d texture: emission |
| 530 | RL_SHADER_LOC_MAP_HEIGHT, // Shader location: sampler2d texture: height |
| 531 | RL_SHADER_LOC_MAP_CUBEMAP, // Shader location: samplerCube texture: cubemap |
| 532 | RL_SHADER_LOC_MAP_IRRADIANCE, // Shader location: samplerCube texture: irradiance |
| 533 | RL_SHADER_LOC_MAP_PREFILTER, // Shader location: samplerCube texture: prefilter |
| 534 | RL_SHADER_LOC_MAP_BRDF // Shader location: sampler2d texture: brdf |
| 535 | } rlShaderLocationIndex; |
| 536 | |
| 537 | #define RL_SHADER_LOC_MAP_DIFFUSE RL_SHADER_LOC_MAP_ALBEDO |
| 538 | #define RL_SHADER_LOC_MAP_SPECULAR RL_SHADER_LOC_MAP_METALNESS |
| 539 | |
| 540 | // Shader uniform data type |
| 541 | typedef enum { |
| 542 | RL_SHADER_UNIFORM_FLOAT = 0, // Shader uniform type: float |
| 543 | RL_SHADER_UNIFORM_VEC2, // Shader uniform type: vec2 (2 float) |
| 544 | RL_SHADER_UNIFORM_VEC3, // Shader uniform type: vec3 (3 float) |
| 545 | RL_SHADER_UNIFORM_VEC4, // Shader uniform type: vec4 (4 float) |
| 546 | RL_SHADER_UNIFORM_INT, // Shader uniform type: int |
| 547 | RL_SHADER_UNIFORM_IVEC2, // Shader uniform type: ivec2 (2 int) |
| 548 | RL_SHADER_UNIFORM_IVEC3, // Shader uniform type: ivec3 (3 int) |
| 549 | RL_SHADER_UNIFORM_IVEC4, // Shader uniform type: ivec4 (4 int) |
| 550 | RL_SHADER_UNIFORM_UINT, // Shader uniform type: unsigned int |
| 551 | RL_SHADER_UNIFORM_UIVEC2, // Shader uniform type: uivec2 (2 unsigned int) |
| 552 | RL_SHADER_UNIFORM_UIVEC3, // Shader uniform type: uivec3 (3 unsigned int) |
| 553 | RL_SHADER_UNIFORM_UIVEC4, // Shader uniform type: uivec4 (4 unsigned int) |
| 554 | RL_SHADER_UNIFORM_SAMPLER2D // Shader uniform type: sampler2d |
| 555 | } rlShaderUniformDataType; |
| 556 | |
| 557 | // Shader attribute data types |
| 558 | typedef enum { |
| 559 | RL_SHADER_ATTRIB_FLOAT = 0, // Shader attribute type: float |
| 560 | RL_SHADER_ATTRIB_VEC2, // Shader attribute type: vec2 (2 float) |
| 561 | RL_SHADER_ATTRIB_VEC3, // Shader attribute type: vec3 (3 float) |
| 562 | RL_SHADER_ATTRIB_VEC4 // Shader attribute type: vec4 (4 float) |
| 563 | } rlShaderAttributeDataType; |
| 564 | |
| 565 | // Framebuffer attachment type |
| 566 | // NOTE: By default up to 8 color channels defined, but it can be more |
| 567 | typedef enum { |
| 568 | RL_ATTACHMENT_COLOR_CHANNEL0 = 0, // Framebuffer attachment type: color 0 |
| 569 | RL_ATTACHMENT_COLOR_CHANNEL1 = 1, // Framebuffer attachment type: color 1 |
| 570 | RL_ATTACHMENT_COLOR_CHANNEL2 = 2, // Framebuffer attachment type: color 2 |
| 571 | RL_ATTACHMENT_COLOR_CHANNEL3 = 3, // Framebuffer attachment type: color 3 |
| 572 | RL_ATTACHMENT_COLOR_CHANNEL4 = 4, // Framebuffer attachment type: color 4 |
| 573 | RL_ATTACHMENT_COLOR_CHANNEL5 = 5, // Framebuffer attachment type: color 5 |
| 574 | RL_ATTACHMENT_COLOR_CHANNEL6 = 6, // Framebuffer attachment type: color 6 |
| 575 | RL_ATTACHMENT_COLOR_CHANNEL7 = 7, // Framebuffer attachment type: color 7 |
| 576 | RL_ATTACHMENT_DEPTH = 100, // Framebuffer attachment type: depth |
| 577 | RL_ATTACHMENT_STENCIL = 200, // Framebuffer attachment type: stencil |
| 578 | } rlFramebufferAttachType; |
| 579 | |
| 580 | // Framebuffer texture attachment type |
| 581 | typedef enum { |
| 582 | RL_ATTACHMENT_CUBEMAP_POSITIVE_X = 0, // Framebuffer texture attachment type: cubemap, +X side |
| 583 | RL_ATTACHMENT_CUBEMAP_NEGATIVE_X = 1, // Framebuffer texture attachment type: cubemap, -X side |
| 584 | RL_ATTACHMENT_CUBEMAP_POSITIVE_Y = 2, // Framebuffer texture attachment type: cubemap, +Y side |
| 585 | RL_ATTACHMENT_CUBEMAP_NEGATIVE_Y = 3, // Framebuffer texture attachment type: cubemap, -Y side |
| 586 | RL_ATTACHMENT_CUBEMAP_POSITIVE_Z = 4, // Framebuffer texture attachment type: cubemap, +Z side |
| 587 | RL_ATTACHMENT_CUBEMAP_NEGATIVE_Z = 5, // Framebuffer texture attachment type: cubemap, -Z side |
| 588 | RL_ATTACHMENT_TEXTURE2D = 100, // Framebuffer texture attachment type: texture2d |
| 589 | RL_ATTACHMENT_RENDERBUFFER = 200, // Framebuffer texture attachment type: renderbuffer |
| 590 | } rlFramebufferAttachTextureType; |
| 591 | |
| 592 | // Face culling mode |
| 593 | typedef enum { |
| 594 | RL_CULL_FACE_FRONT = 0, |
| 595 | RL_CULL_FACE_BACK |
| 596 | } rlCullMode; |
| 597 | |
| 598 | //------------------------------------------------------------------------------------ |
| 599 | // Functions Declaration - Matrix operations |
| 600 | //------------------------------------------------------------------------------------ |
| 601 | |
| 602 | #if defined(__cplusplus) |
| 603 | extern "C" { // Prevents name mangling of functions |
| 604 | #endif |
| 605 | |
| 606 | RLAPI void rlMatrixMode(int mode); // Choose the current matrix to be transformed |
| 607 | RLAPI void rlPushMatrix(void); // Push the current matrix to stack |
| 608 | RLAPI void rlPopMatrix(void); // Pop latest inserted matrix from stack |
| 609 | RLAPI void rlLoadIdentity(void); // Reset current matrix to identity matrix |
| 610 | RLAPI void rlTranslatef(float x, float y, float z); // Multiply the current matrix by a translation matrix |
| 611 | RLAPI void rlRotatef(float angle, float x, float y, float z); // Multiply the current matrix by a rotation matrix |
| 612 | RLAPI void rlScalef(float x, float y, float z); // Multiply the current matrix by a scaling matrix |
| 613 | RLAPI void rlMultMatrixf(const float *matf); // Multiply the current matrix by another matrix |
| 614 | RLAPI void rlFrustum(float left, float right, float bottom, float top, float znear, float zfar); |
| 615 | RLAPI void rlOrtho(float left, float right, float bottom, float top, float znear, float zfar); |
| 616 | RLAPI void rlViewport(int x, int y, int width, int height); // Set the viewport area |
| 617 | RLAPI void rlSetClipPlanes(float nearPlane, float farPlane); // Set clip planes distances |
| 618 | RLAPI float rlGetCullDistanceNear(void); // Get cull plane distance near |
| 619 | RLAPI float rlGetCullDistanceFar(void); // Get cull plane distance far |
| 620 | |
| 621 | //------------------------------------------------------------------------------------ |
| 622 | // Functions Declaration - Vertex level operations |
| 623 | //------------------------------------------------------------------------------------ |
| 624 | RLAPI void rlBegin(int mode); // Initialize drawing mode (how to organize vertex) |
| 625 | RLAPI void rlEnd(void); // Finish vertex providing |
| 626 | RLAPI void rlVertex2i(int x, int y); // Define one vertex (position) - 2 int |
| 627 | RLAPI void rlVertex2f(float x, float y); // Define one vertex (position) - 2 float |
| 628 | RLAPI void rlVertex3f(float x, float y, float z); // Define one vertex (position) - 3 float |
| 629 | RLAPI void rlTexCoord2f(float x, float y); // Define one vertex (texture coordinate) - 2 float |
| 630 | RLAPI void rlNormal3f(float x, float y, float z); // Define one vertex (normal) - 3 float |
| 631 | RLAPI void rlColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a); // Define one vertex (color) - 4 byte |
| 632 | RLAPI void rlColor3f(float x, float y, float z); // Define one vertex (color) - 3 float |
| 633 | RLAPI void rlColor4f(float x, float y, float z, float w); // Define one vertex (color) - 4 float |
| 634 | |
| 635 | //------------------------------------------------------------------------------------ |
| 636 | // Functions Declaration - OpenGL style functions (common to 1.1, 3.3+, ES2) |
| 637 | // NOTE: This functions are used to completely abstract raylib code from OpenGL layer, |
| 638 | // some of them are direct wrappers over OpenGL calls, some others are custom |
| 639 | //------------------------------------------------------------------------------------ |
| 640 | |
| 641 | // Vertex buffers state |
| 642 | RLAPI bool rlEnableVertexArray(unsigned int vaoId); // Enable vertex array (VAO, if supported) |
| 643 | RLAPI void rlDisableVertexArray(void); // Disable vertex array (VAO, if supported) |
| 644 | RLAPI void rlEnableVertexBuffer(unsigned int id); // Enable vertex buffer (VBO) |
| 645 | RLAPI void rlDisableVertexBuffer(void); // Disable vertex buffer (VBO) |
| 646 | RLAPI void rlEnableVertexBufferElement(unsigned int id); // Enable vertex buffer element (VBO element) |
| 647 | RLAPI void rlDisableVertexBufferElement(void); // Disable vertex buffer element (VBO element) |
| 648 | RLAPI void rlEnableVertexAttribute(unsigned int index); // Enable vertex attribute index |
| 649 | RLAPI void rlDisableVertexAttribute(unsigned int index); // Disable vertex attribute index |
| 650 | RLAPI void rlEnableStatePointer(int vertexAttribType, void *buffer); // Enable attribute state pointer |
| 651 | RLAPI void rlDisableStatePointer(int vertexAttribType); // Disable attribute state pointer |
| 652 | |
| 653 | // Textures state |
| 654 | RLAPI void rlActiveTextureSlot(int slot); // Select and active a texture slot |
| 655 | RLAPI void rlEnableTexture(unsigned int id); // Enable texture |
| 656 | RLAPI void rlDisableTexture(void); // Disable texture |
| 657 | RLAPI void rlEnableTextureCubemap(unsigned int id); // Enable texture cubemap |
| 658 | RLAPI void rlDisableTextureCubemap(void); // Disable texture cubemap |
| 659 | RLAPI void rlTextureParameters(unsigned int id, int param, int value); // Set texture parameters (filter, wrap) |
| 660 | RLAPI void rlCubemapParameters(unsigned int id, int param, int value); // Set cubemap parameters (filter, wrap) |
| 661 | |
| 662 | // Shader state |
| 663 | RLAPI void rlEnableShader(unsigned int id); // Enable shader program |
| 664 | RLAPI void rlDisableShader(void); // Disable shader program |
| 665 | |
| 666 | // Framebuffer state |
| 667 | RLAPI void rlEnableFramebuffer(unsigned int id); // Enable render texture (fbo) |
| 668 | RLAPI void rlDisableFramebuffer(void); // Disable render texture (fbo), return to default framebuffer |
| 669 | RLAPI unsigned int rlGetActiveFramebuffer(void); // Get the currently active render texture (fbo), 0 for default framebuffer |
| 670 | RLAPI void rlActiveDrawBuffers(int count); // Activate multiple draw color buffers |
| 671 | RLAPI void rlBlitFramebuffer(int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight, int bufferMask); // Blit active framebuffer to main framebuffer |
| 672 | RLAPI void rlBindFramebuffer(unsigned int target, unsigned int framebuffer); // Bind framebuffer (FBO) |
| 673 | |
| 674 | // General render state |
| 675 | RLAPI void rlEnableColorBlend(void); // Enable color blending |
| 676 | RLAPI void rlDisableColorBlend(void); // Disable color blending |
| 677 | RLAPI void rlEnableDepthTest(void); // Enable depth test |
| 678 | RLAPI void rlDisableDepthTest(void); // Disable depth test |
| 679 | RLAPI void rlEnableDepthMask(void); // Enable depth write |
| 680 | RLAPI void rlDisableDepthMask(void); // Disable depth write |
| 681 | RLAPI void rlEnableBackfaceCulling(void); // Enable backface culling |
| 682 | RLAPI void rlDisableBackfaceCulling(void); // Disable backface culling |
| 683 | RLAPI void rlColorMask(bool r, bool g, bool b, bool a); // Color mask control |
| 684 | RLAPI void rlSetCullFace(int mode); // Set face culling mode |
| 685 | RLAPI void rlEnableScissorTest(void); // Enable scissor test |
| 686 | RLAPI void rlDisableScissorTest(void); // Disable scissor test |
| 687 | RLAPI void rlScissor(int x, int y, int width, int height); // Scissor test |
| 688 | RLAPI void rlEnablePointMode(void); // Enable point mode |
| 689 | RLAPI void rlDisablePointMode(void); // Disable point mode |
| 690 | RLAPI void rlSetPointSize(float size); // Set the point drawing size |
| 691 | RLAPI float rlGetPointSize(void); // Get the point drawing size |
| 692 | RLAPI void rlEnableWireMode(void); // Enable wire mode |
| 693 | RLAPI void rlDisableWireMode(void); // Disable wire mode |
| 694 | RLAPI void rlSetLineWidth(float width); // Set the line drawing width |
| 695 | RLAPI float rlGetLineWidth(void); // Get the line drawing width |
| 696 | RLAPI void rlEnableSmoothLines(void); // Enable line aliasing |
| 697 | RLAPI void rlDisableSmoothLines(void); // Disable line aliasing |
| 698 | RLAPI void rlEnableStereoRender(void); // Enable stereo rendering |
| 699 | RLAPI void rlDisableStereoRender(void); // Disable stereo rendering |
| 700 | RLAPI bool rlIsStereoRenderEnabled(void); // Check if stereo render is enabled |
| 701 | |
| 702 | RLAPI void rlClearColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a); // Clear color buffer with color |
| 703 | RLAPI void rlClearScreenBuffers(void); // Clear used screen buffers (color and depth) |
| 704 | RLAPI void rlCheckErrors(void); // Check and log OpenGL error codes |
| 705 | RLAPI void rlSetBlendMode(int mode); // Set blending mode |
| 706 | RLAPI void rlSetBlendFactors(int glSrcFactor, int glDstFactor, int glEquation); // Set blending mode factor and equation (using OpenGL factors) |
| 707 | RLAPI void rlSetBlendFactorsSeparate(int glSrcRGB, int glDstRGB, int glSrcAlpha, int glDstAlpha, int glEqRGB, int glEqAlpha); // Set blending mode factors and equations separately (using OpenGL factors) |
| 708 | |
| 709 | //------------------------------------------------------------------------------------ |
| 710 | // Functions Declaration - rlgl functionality |
| 711 | //------------------------------------------------------------------------------------ |
| 712 | // rlgl initialization functions |
| 713 | RLAPI void rlglInit(int width, int height); // Initialize rlgl (buffers, shaders, textures, states) |
| 714 | RLAPI void rlglClose(void); // De-initialize rlgl (buffers, shaders, textures) |
| 715 | RLAPI void rlLoadExtensions(void *loader); // Load OpenGL extensions (loader function required) |
| 716 | RLAPI void *rlGetProcAddress(const char *procName); // Get OpenGL procedure address |
| 717 | RLAPI int rlGetVersion(void); // Get current OpenGL version |
| 718 | RLAPI void rlSetFramebufferWidth(int width); // Set current framebuffer width |
| 719 | RLAPI int rlGetFramebufferWidth(void); // Get default framebuffer width |
| 720 | RLAPI void rlSetFramebufferHeight(int height); // Set current framebuffer height |
| 721 | RLAPI int rlGetFramebufferHeight(void); // Get default framebuffer height |
| 722 | |
| 723 | RLAPI unsigned int rlGetTextureIdDefault(void); // Get default texture id |
| 724 | RLAPI unsigned int rlGetShaderIdDefault(void); // Get default shader id |
| 725 | RLAPI int *rlGetShaderLocsDefault(void); // Get default shader locations |
| 726 | |
| 727 | // Render batch management |
| 728 | // NOTE: rlgl provides a default render batch to behave like OpenGL 1.1 immediate mode |
| 729 | // but this render batch API is exposed in case of custom batches are required |
| 730 | RLAPI rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements); // Load a render batch system |
| 731 | RLAPI void rlUnloadRenderBatch(rlRenderBatch batch); // Unload render batch system |
| 732 | RLAPI void rlDrawRenderBatch(rlRenderBatch *batch); // Draw render batch data (Update->Draw->Reset) |
| 733 | RLAPI void rlSetRenderBatchActive(rlRenderBatch *batch); // Set the active render batch for rlgl (NULL for default internal) |
| 734 | RLAPI void rlDrawRenderBatchActive(void); // Update and draw internal render batch |
| 735 | RLAPI bool rlCheckRenderBatchLimit(int vCount); // Check internal buffer overflow for a given number of vertex |
| 736 | |
| 737 | RLAPI void rlSetTexture(unsigned int id); // Set current texture for render batch and check buffers limits |
| 738 | |
| 739 | //------------------------------------------------------------------------------------------------------------------------ |
| 740 | |
| 741 | // Vertex buffers management |
| 742 | RLAPI unsigned int rlLoadVertexArray(void); // Load vertex array (vao) if supported |
| 743 | RLAPI unsigned int rlLoadVertexBuffer(const void *buffer, int size, bool dynamic); // Load a vertex buffer object |
| 744 | RLAPI unsigned int rlLoadVertexBufferElement(const void *buffer, int size, bool dynamic); // Load vertex buffer elements object |
| 745 | RLAPI void rlUpdateVertexBuffer(unsigned int bufferId, const void *data, int dataSize, int offset); // Update vertex buffer object data on GPU buffer |
| 746 | RLAPI void rlUpdateVertexBufferElements(unsigned int id, const void *data, int dataSize, int offset); // Update vertex buffer elements data on GPU buffer |
| 747 | RLAPI void rlUnloadVertexArray(unsigned int vaoId); // Unload vertex array (vao) |
| 748 | RLAPI void rlUnloadVertexBuffer(unsigned int vboId); // Unload vertex buffer object |
| 749 | RLAPI void rlSetVertexAttribute(unsigned int index, int compSize, int type, bool normalized, int stride, int offset); // Set vertex attribute data configuration |
| 750 | RLAPI void rlSetVertexAttributeDivisor(unsigned int index, int divisor); // Set vertex attribute data divisor |
| 751 | RLAPI void rlSetVertexAttributeDefault(int locIndex, const void *value, int attribType, int count); // Set vertex attribute default value, when attribute to provided |
| 752 | RLAPI void rlDrawVertexArray(int offset, int count); // Draw vertex array (currently active vao) |
| 753 | RLAPI void rlDrawVertexArrayElements(int offset, int count, const void *buffer); // Draw vertex array elements |
| 754 | RLAPI void rlDrawVertexArrayInstanced(int offset, int count, int instances); // Draw vertex array (currently active vao) with instancing |
| 755 | RLAPI void rlDrawVertexArrayElementsInstanced(int offset, int count, const void *buffer, int instances); // Draw vertex array elements with instancing |
| 756 | |
| 757 | // Textures management |
| 758 | RLAPI unsigned int rlLoadTexture(const void *data, int width, int height, int format, int mipmapCount); // Load texture data |
| 759 | RLAPI unsigned int rlLoadTextureDepth(int width, int height, bool useRenderBuffer); // Load depth texture/renderbuffer (to be attached to fbo) |
| 760 | RLAPI unsigned int rlLoadTextureCubemap(const void *data, int size, int format, int mipmapCount); // Load texture cubemap data |
| 761 | RLAPI void rlUpdateTexture(unsigned int id, int offsetX, int offsetY, int width, int height, int format, const void *data); // Update texture with new data on GPU |
| 762 | RLAPI void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned int *glFormat, unsigned int *glType); // Get OpenGL internal formats |
| 763 | RLAPI const char *rlGetPixelFormatName(unsigned int format); // Get name string for pixel format |
| 764 | RLAPI void rlUnloadTexture(unsigned int id); // Unload texture from GPU memory |
| 765 | RLAPI void rlGenTextureMipmaps(unsigned int id, int width, int height, int format, int *mipmaps); // Generate mipmap data for selected texture |
| 766 | RLAPI void *rlReadTexturePixels(unsigned int id, int width, int height, int format); // Read texture pixel data |
| 767 | RLAPI unsigned char *rlReadScreenPixels(int width, int height); // Read screen pixel data (color buffer) |
| 768 | |
| 769 | // Framebuffer management (fbo) |
| 770 | RLAPI unsigned int rlLoadFramebuffer(void); // Load an empty framebuffer |
| 771 | RLAPI void rlFramebufferAttach(unsigned int id, unsigned int texId, int attachType, int texType, int mipLevel); // Attach texture/renderbuffer to a framebuffer |
| 772 | RLAPI bool rlFramebufferComplete(unsigned int id); // Verify framebuffer is complete |
| 773 | RLAPI void rlUnloadFramebuffer(unsigned int id); // Delete framebuffer from GPU |
| 774 | // WARNING: Copy and resize framebuffer functionality only defined for software backend |
| 775 | RLAPI void rlCopyFramebuffer(int x, int y, int width, int height, int format, void *pixels); // Copy framebuffer pixel data to internal buffer |
| 776 | RLAPI void rlResizeFramebuffer(int width, int height); // Resize internal framebuffer |
| 777 | |
| 778 | // Shaders management |
| 779 | RLAPI unsigned int rlLoadShader(const char *code, int type); // Load (compile) shader and return shader id (type: RL_VERTEX_SHADER, RL_FRAGMENT_SHADER, RL_COMPUTE_SHADER) |
| 780 | RLAPI unsigned int rlLoadShaderProgram(const char *vsCode, const char *fsCode); // Load shader from code strings |
| 781 | RLAPI unsigned int rlLoadShaderProgramEx(unsigned int vsId, unsigned int fsId); // Load shader program, using already loaded shader ids |
| 782 | RLAPI unsigned int rlLoadShaderProgramCompute(unsigned int csId); // Load compute shader program |
| 783 | RLAPI void rlUnloadShader(unsigned int id); // Unload shader, loaded with rlLoadShader() |
| 784 | RLAPI void rlUnloadShaderProgram(unsigned int id); // Unload shader program |
| 785 | RLAPI int rlGetLocationUniform(unsigned int id, const char *uniformName); // Get shader location uniform, requires shader program id |
| 786 | RLAPI int rlGetLocationAttrib(unsigned int id, const char *attribName); // Get shader location attribute, requires shader program id |
| 787 | RLAPI void rlSetUniform(int locIndex, const void *value, int uniformType, int count); // Set shader value uniform |
| 788 | RLAPI void rlSetUniformMatrix(int locIndex, Matrix mat); // Set shader value matrix |
| 789 | RLAPI void rlSetUniformMatrices(int locIndex, const Matrix *mat, int count); // Set shader value matrices |
| 790 | RLAPI void rlSetUniformSampler(int locIndex, unsigned int textureId); // Set shader value sampler |
| 791 | RLAPI void rlSetShader(unsigned int id, int *locs); // Set shader currently active (id and locations) |
| 792 | |
| 793 | // Compute shader management |
| 794 | RLAPI void rlComputeShaderDispatch(unsigned int groupX, unsigned int groupY, unsigned int groupZ); // Dispatch compute shader (equivalent to *draw* for graphics pipeline) |
| 795 | |
| 796 | // Shader buffer storage object management (ssbo) |
| 797 | RLAPI unsigned int rlLoadShaderBuffer(unsigned int size, const void *data, int usageHint); // Load shader storage buffer object (SSBO) |
| 798 | RLAPI void rlUnloadShaderBuffer(unsigned int ssboId); // Unload shader storage buffer object (SSBO) |
| 799 | RLAPI void rlUpdateShaderBuffer(unsigned int id, const void *data, unsigned int dataSize, unsigned int offset); // Update SSBO buffer data |
| 800 | RLAPI void rlBindShaderBuffer(unsigned int id, unsigned int index); // Bind SSBO buffer |
| 801 | RLAPI void rlReadShaderBuffer(unsigned int id, void *dest, unsigned int count, unsigned int offset); // Read SSBO buffer data (GPU->CPU) |
| 802 | RLAPI void rlCopyShaderBuffer(unsigned int destId, unsigned int srcId, unsigned int destOffset, unsigned int srcOffset, unsigned int count); // Copy SSBO data between buffers |
| 803 | RLAPI unsigned int rlGetShaderBufferSize(unsigned int id); // Get SSBO buffer size |
| 804 | |
| 805 | // Buffer management |
| 806 | RLAPI void rlBindImageTexture(unsigned int id, unsigned int index, int format, bool readonly); // Bind image texture |
| 807 | |
| 808 | // Matrix state management |
| 809 | RLAPI Matrix rlGetMatrixModelview(void); // Get internal modelview matrix |
| 810 | RLAPI Matrix rlGetMatrixProjection(void); // Get internal projection matrix |
| 811 | RLAPI Matrix rlGetMatrixTransform(void); // Get internal accumulated transform matrix |
| 812 | RLAPI Matrix rlGetMatrixProjectionStereo(int eye); // Get internal projection matrix for stereo render (selected eye) |
| 813 | RLAPI Matrix rlGetMatrixViewOffsetStereo(int eye); // Get internal view offset matrix for stereo render (selected eye) |
| 814 | RLAPI void rlSetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) |
| 815 | RLAPI void rlSetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) |
| 816 | RLAPI void rlSetMatrixProjectionStereo(Matrix right, Matrix left); // Set eyes projection matrices for stereo rendering |
| 817 | RLAPI void rlSetMatrixViewOffsetStereo(Matrix right, Matrix left); // Set eyes view offsets matrices for stereo rendering |
| 818 | |
| 819 | // Quick and dirty cube/quad buffers load->draw->unload |
| 820 | RLAPI void rlLoadDrawCube(void); // Load and draw a cube |
| 821 | RLAPI void rlLoadDrawQuad(void); // Load and draw a quad |
| 822 | |
| 823 | #if defined(__cplusplus) |
| 824 | } |
| 825 | #endif |
| 826 | |
| 827 | #endif // RLGL_H |
| 828 | |
| 829 | /*********************************************************************************** |
| 830 | * |
| 831 | * RLGL IMPLEMENTATION |
| 832 | * |
| 833 | ************************************************************************************/ |
| 834 | |
| 835 | #if defined(RLGL_IMPLEMENTATION) |
| 836 | |
| 837 | // Expose OpenGL functions from glad in raylib |
| 838 | #if defined(BUILD_LIBTYPE_SHARED) |
| 839 | #define GLAD_API_CALL_EXPORT |
| 840 | #define GLAD_API_CALL_EXPORT_BUILD |
| 841 | #endif |
| 842 | |
| 843 | #if defined(GRAPHICS_API_OPENGL_11) |
| 844 | #if defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 845 | #define RLSW_IMPLEMENTATION |
| 846 | #define SW_MALLOC(sz) RL_MALLOC(sz) |
| 847 | #define SW_CALLOC(n,sz) RL_CALLOC(n, sz) |
| 848 | #define SW_REALLOC(ptr, newSz) RL_REALLOC(ptr, newSz) |
| 849 | #define SW_FREE(ptr) RL_FREE(ptr) |
| 850 | #include "external/rlsw.h" // OpenGL 1.1 software implementation |
| 851 | #else |
| 852 | #if defined(__APPLE__) |
| 853 | #include <OpenGL/gl.h> // OpenGL 1.1 library for OSX |
| 854 | #include <OpenGL/glext.h> // OpenGL extensions library |
| 855 | #else |
| 856 | // APIENTRY for OpenGL function pointer declarations is required |
| 857 | #if !defined(APIENTRY) |
| 858 | #if defined(_WIN32) |
| 859 | #define APIENTRY __stdcall |
| 860 | #else |
| 861 | #define APIENTRY |
| 862 | #endif |
| 863 | #endif |
| 864 | // WINGDIAPI definition. Some Windows OpenGL headers need it |
| 865 | #if !defined(WINGDIAPI) && defined(_WIN32) |
| 866 | #define WINGDIAPI __declspec(dllimport) |
| 867 | #endif |
| 868 | |
| 869 | #include <GL/gl.h> // OpenGL 1.1 library |
| 870 | #endif |
| 871 | #endif |
| 872 | #endif |
| 873 | |
| 874 | #if defined(GRAPHICS_API_OPENGL_33) |
| 875 | #define GLAD_MALLOC RL_MALLOC |
| 876 | #define GLAD_FREE RL_FREE |
| 877 | |
| 878 | #define GLAD_GL_IMPLEMENTATION |
| 879 | #include "external/glad.h" // GLAD extensions loading library, includes OpenGL headers |
| 880 | #endif |
| 881 | |
| 882 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 883 | #include <GLES3/gl3.h> // OpenGL ES 3.0 library |
| 884 | #define GL_GLEXT_PROTOTYPES |
| 885 | #include <GLES2/gl2ext.h> // OpenGL ES 2.0 extensions library |
| 886 | #elif defined(GRAPHICS_API_OPENGL_ES2) |
| 887 | // NOTE: OpenGL ES 2.0 can be enabled on Desktop platforms, |
| 888 | // in that case, functions are loaded from a custom glad for OpenGL ES 2.0 |
| 889 | // TODO: rlgl OpenGL ES 2.0 selection shouldn't depend on raylib platform macros, a specific GLAD shouldn't be neither required |
| 890 | #if defined(PLATFORM_DESKTOP_GLFW) || defined(PLATFORM_DESKTOP_SDL) |
| 891 | #define GLAD_GLES2_IMPLEMENTATION |
| 892 | #include "external/glad_gles2.h" |
| 893 | #else |
| 894 | #define GL_GLEXT_PROTOTYPES |
| 895 | //#include <EGL/egl.h> // EGL library -> not required, platform layer |
| 896 | #include <GLES2/gl2.h> // OpenGL ES 2.0 library |
| 897 | #include <GLES2/gl2ext.h> // OpenGL ES 2.0 extensions library |
| 898 | #endif |
| 899 | |
| 900 | // It seems OpenGL ES 2.0 instancing entry points are not defined on Raspberry Pi |
| 901 | // provided headers (despite being defined in official Khronos GLES2 headers) |
| 902 | // TODO: Avoid raylib platform-dependent code on rlgl, it should be a completely portable library |
| 903 | #if defined(PLATFORM_DRM) // Use CONFIG_DRM? |
| 904 | typedef void (GL_APIENTRYP PFNGLDRAWARRAYSINSTANCEDEXTPROC) (GLenum mode, GLint start, GLsizei count, GLsizei primcount); |
| 905 | typedef void (GL_APIENTRYP PFNGLDRAWELEMENTSINSTANCEDEXTPROC) (GLenum mode, GLsizei count, GLenum type, const void *indices, GLsizei primcount); |
| 906 | typedef void (GL_APIENTRYP PFNGLVERTEXATTRIBDIVISOREXTPROC) (GLuint index, GLuint divisor); |
| 907 | #endif |
| 908 | #endif |
| 909 | |
| 910 | #include <stdlib.h> // Required for: calloc(), free() |
| 911 | #include <string.h> // Required for: strcmp(), strlen() [Used in rlglInit(), on extensions loading] |
| 912 | #include <math.h> // Required for: sqrtf(), sinf(), cosf(), floor(), log() |
| 913 | #include <limits.h> // Required for: INT_MAX |
| 914 | |
| 915 | //---------------------------------------------------------------------------------- |
| 916 | // Defines and Macros |
| 917 | //---------------------------------------------------------------------------------- |
| 918 | #ifndef PI |
| 919 | #define PI 3.14159265358979323846f |
| 920 | #endif |
| 921 | #ifndef DEG2RAD |
| 922 | #define DEG2RAD (PI/180.0f) |
| 923 | #endif |
| 924 | #ifndef RAD2DEG |
| 925 | #define RAD2DEG (180.0f/PI) |
| 926 | #endif |
| 927 | |
| 928 | #ifndef GL_SHADING_LANGUAGE_VERSION |
| 929 | #define GL_SHADING_LANGUAGE_VERSION 0x8B8C |
| 930 | #endif |
| 931 | |
| 932 | #ifndef GL_COMPRESSED_RGB_S3TC_DXT1_EXT |
| 933 | #define GL_COMPRESSED_RGB_S3TC_DXT1_EXT 0x83F0 |
| 934 | #endif |
| 935 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT1_EXT |
| 936 | #define GL_COMPRESSED_RGBA_S3TC_DXT1_EXT 0x83F1 |
| 937 | #endif |
| 938 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT3_EXT |
| 939 | #define GL_COMPRESSED_RGBA_S3TC_DXT3_EXT 0x83F2 |
| 940 | #endif |
| 941 | #ifndef GL_COMPRESSED_RGBA_S3TC_DXT5_EXT |
| 942 | #define GL_COMPRESSED_RGBA_S3TC_DXT5_EXT 0x83F3 |
| 943 | #endif |
| 944 | #ifndef GL_ETC1_RGB8_OES |
| 945 | #define GL_ETC1_RGB8_OES 0x8D64 |
| 946 | #endif |
| 947 | #ifndef GL_COMPRESSED_RGB8_ETC2 |
| 948 | #define GL_COMPRESSED_RGB8_ETC2 0x9274 |
| 949 | #endif |
| 950 | #ifndef GL_COMPRESSED_RGBA8_ETC2_EAC |
| 951 | #define GL_COMPRESSED_RGBA8_ETC2_EAC 0x9278 |
| 952 | #endif |
| 953 | #ifndef GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG |
| 954 | #define GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG 0x8C00 |
| 955 | #endif |
| 956 | #ifndef GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG |
| 957 | #define GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG 0x8C02 |
| 958 | #endif |
| 959 | #ifndef GL_COMPRESSED_RGBA_ASTC_4x4_KHR |
| 960 | #define GL_COMPRESSED_RGBA_ASTC_4x4_KHR 0x93b0 |
| 961 | #endif |
| 962 | #ifndef GL_COMPRESSED_RGBA_ASTC_8x8_KHR |
| 963 | #define GL_COMPRESSED_RGBA_ASTC_8x8_KHR 0x93b7 |
| 964 | #endif |
| 965 | |
| 966 | #ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT |
| 967 | #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF |
| 968 | #endif |
| 969 | #ifndef GL_TEXTURE_MAX_ANISOTROPY_EXT |
| 970 | #define GL_TEXTURE_MAX_ANISOTROPY_EXT 0x84FE |
| 971 | #endif |
| 972 | |
| 973 | #ifndef GL_PROGRAM_POINT_SIZE |
| 974 | #define GL_PROGRAM_POINT_SIZE 0x8642 |
| 975 | #endif |
| 976 | |
| 977 | #ifndef GL_LINE_WIDTH |
| 978 | #define GL_LINE_WIDTH 0x0B21 |
| 979 | #endif |
| 980 | |
| 981 | #if defined(GRAPHICS_API_OPENGL_11) |
| 982 | #define GL_UNSIGNED_SHORT_5_6_5 0x8363 |
| 983 | #define GL_UNSIGNED_SHORT_5_5_5_1 0x8034 |
| 984 | #define GL_UNSIGNED_SHORT_4_4_4_4 0x8033 |
| 985 | #endif |
| 986 | |
| 987 | #if defined(GRAPHICS_API_OPENGL_21) |
| 988 | #define GL_LUMINANCE 0x1909 |
| 989 | #define GL_LUMINANCE_ALPHA 0x190A |
| 990 | #endif |
| 991 | |
| 992 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 993 | #define glClearDepth glClearDepthf |
| 994 | #if !defined(GRAPHICS_API_OPENGL_ES3) |
| 995 | #define GL_READ_FRAMEBUFFER GL_FRAMEBUFFER |
| 996 | #define GL_DRAW_FRAMEBUFFER GL_FRAMEBUFFER |
| 997 | #define GL_DRAW_FRAMEBUFFER_BINDING GL_FRAMEBUFFER_BINDING |
| 998 | #endif |
| 999 | #endif |
| 1000 | |
| 1001 | // Default shader vertex attribute names to set location points |
| 1002 | // WARNING: Pre-defined names can not be changed, they are used by default shaders and all raylib examples shaders, they are just listed here for reference |
| 1003 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION |
| 1004 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD |
| 1005 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL |
| 1006 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR |
| 1007 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT |
| 1008 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2 |
| 1009 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_BONEINDICES "vertexBoneIndices" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEINDICES |
| 1010 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS "vertexBoneWeights" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS |
| 1011 | #define RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCETRANSFORM "instanceTransform" // Bound by default to shader location: RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCETRANSFORM |
| 1012 | |
| 1013 | #define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix |
| 1014 | #define RL_DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix |
| 1015 | #define RL_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix |
| 1016 | #define RL_DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix |
| 1017 | #define RL_DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView)) |
| 1018 | #define RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color) |
| 1019 | #define RL_DEFAULT_SHADER_UNIFORM_NAME_BONEMATRICES "boneMatrices" // bone matrices (required for GPU skinning) |
| 1020 | |
| 1021 | #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0) |
| 1022 | #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1) |
| 1023 | #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2) |
| 1024 | |
| 1025 | //---------------------------------------------------------------------------------- |
| 1026 | // Module Types and Structures Definition |
| 1027 | //---------------------------------------------------------------------------------- |
| 1028 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1029 | |
| 1030 | typedef void *(*rlglLoadProc)(const char *name); // OpenGL extension functions loader signature (same as GLADloadproc) |
| 1031 | |
| 1032 | typedef struct rlglData { |
| 1033 | rlRenderBatch *currentBatch; // Current render batch |
| 1034 | rlRenderBatch defaultBatch; // Default internal render batch |
| 1035 | |
| 1036 | rlglLoadProc loader; // OpenGL function loader |
| 1037 | |
| 1038 | struct { |
| 1039 | int vertexCounter; // Current active render batch vertex counter (generic, used for all batches) |
| 1040 | float texcoordx, texcoordy; // Current active texture coordinate (added on glVertex*()) |
| 1041 | float normalx, normaly, normalz; // Current active normal (added on glVertex*()) |
| 1042 | unsigned char colorr, colorg, colorb, colora; // Current active color (added on glVertex*()) |
| 1043 | |
| 1044 | int currentMatrixMode; // Current matrix mode |
| 1045 | Matrix *currentMatrix; // Current matrix pointer |
| 1046 | Matrix modelview; // Default modelview matrix |
| 1047 | Matrix projection; // Default projection matrix |
| 1048 | Matrix transform; // Transform matrix to be used with rlTranslate, rlRotate, rlScale |
| 1049 | bool transformRequired; // Require transform matrix application to current draw-call vertex (if required) |
| 1050 | Matrix stack[RL_MAX_MATRIX_STACK_SIZE];// Matrix stack for push/pop |
| 1051 | int stackCounter; // Matrix stack counter |
| 1052 | |
| 1053 | unsigned int currentTextureId; // Current texture id to be used on glBegin |
| 1054 | unsigned int defaultTextureId; // Default texture used on shapes/poly drawing (required by shader) |
| 1055 | unsigned int activeTextureId[RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS]; // Active texture ids to be enabled on batch drawing (0 active by default) |
| 1056 | unsigned int defaultVShaderId; // Default vertex shader id (used by default shader program) |
| 1057 | unsigned int defaultFShaderId; // Default fragment shader id (used by default shader program) |
| 1058 | unsigned int defaultShaderId; // Default shader program id, supports vertex color and diffuse texture |
| 1059 | int *defaultShaderLocs; // Default shader locations pointer to be used on rendering |
| 1060 | unsigned int currentShaderId; // Current shader id to be used on rendering (by default, defaultShaderId) |
| 1061 | int *currentShaderLocs; // Current shader locations pointer to be used on rendering (by default, defaultShaderLocs) |
| 1062 | |
| 1063 | bool stereoRender; // Stereo rendering flag |
| 1064 | Matrix projectionStereo[2]; // VR stereo rendering eyes projection matrices |
| 1065 | Matrix viewOffsetStereo[2]; // VR stereo rendering eyes view offset matrices |
| 1066 | |
| 1067 | // Blending variables |
| 1068 | int currentBlendMode; // Blending mode active |
| 1069 | int glBlendSrcFactor; // Blending source factor |
| 1070 | int glBlendDstFactor; // Blending destination factor |
| 1071 | int glBlendEquation; // Blending equation |
| 1072 | int glBlendSrcFactorRGB; // Blending source RGB factor |
| 1073 | int glBlendDestFactorRGB; // Blending destination RGB factor |
| 1074 | int glBlendSrcFactorAlpha; // Blending source alpha factor |
| 1075 | int glBlendDestFactorAlpha; // Blending destination alpha factor |
| 1076 | int glBlendEquationRGB; // Blending equation for RGB |
| 1077 | int glBlendEquationAlpha; // Blending equation for alpha |
| 1078 | bool glCustomBlendModeModified; // Custom blending factor and equation modification status |
| 1079 | |
| 1080 | int framebufferWidth; // Current framebuffer width |
| 1081 | int framebufferHeight; // Current framebuffer height |
| 1082 | |
| 1083 | } State; // Renderer state |
| 1084 | struct { |
| 1085 | bool vao; // VAO support (OpenGL ES2 could not support VAO extension) (GL_ARB_vertex_array_object) |
| 1086 | bool instancing; // Instancing supported (GL_ANGLE_instanced_arrays, GL_EXT_draw_instanced + GL_EXT_instanced_arrays) |
| 1087 | bool texNPOT; // NPOT textures full support (GL_ARB_texture_non_power_of_two, GL_OES_texture_npot) |
| 1088 | bool texDepth; // Depth textures supported (GL_ARB_depth_texture, GL_OES_depth_texture) |
| 1089 | bool texDepthWebGL; // Depth textures supported WebGL specific (GL_WEBGL_depth_texture) |
| 1090 | bool texFloat32; // float textures support (32 bit per channel) (GL_OES_texture_float) |
| 1091 | bool texFloat16; // half float textures support (16 bit per channel) (GL_OES_texture_half_float) |
| 1092 | bool texCompDXT; // DDS texture compression support (GL_EXT_texture_compression_s3tc, GL_WEBGL_compressed_texture_s3tc, GL_WEBKIT_WEBGL_compressed_texture_s3tc) |
| 1093 | bool texCompETC1; // ETC1 texture compression support (GL_OES_compressed_ETC1_RGB8_texture, GL_WEBGL_compressed_texture_etc1) |
| 1094 | bool texCompETC2; // ETC2/EAC texture compression support (GL_ARB_ES3_compatibility) |
| 1095 | bool texCompPVRT; // PVR texture compression support (GL_IMG_texture_compression_pvrtc) |
| 1096 | bool texCompASTC; // ASTC texture compression support (GL_KHR_texture_compression_astc_hdr, GL_KHR_texture_compression_astc_ldr) |
| 1097 | bool texMirrorClamp; // Clamp mirror wrap mode supported (GL_EXT_texture_mirror_clamp) |
| 1098 | bool texAnisoFilter; // Anisotropic texture filtering support (GL_EXT_texture_filter_anisotropic) |
| 1099 | bool computeShader; // Compute shaders support (GL_ARB_compute_shader) |
| 1100 | bool ssbo; // Shader storage buffer object support (GL_ARB_shader_storage_buffer_object) |
| 1101 | |
| 1102 | float maxAnisotropyLevel; // Maximum anisotropy level supported (minimum is 2.0f) |
| 1103 | int maxDepthBits; // Maximum bits for depth component |
| 1104 | |
| 1105 | } ExtSupported; // Extensions supported flags |
| 1106 | } rlglData; |
| 1107 | |
| 1108 | #endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2 |
| 1109 | |
| 1110 | //---------------------------------------------------------------------------------- |
| 1111 | // Global Variables Definition |
| 1112 | //---------------------------------------------------------------------------------- |
| 1113 | static bool isGpuReady = false; |
| 1114 | static float rlCullDistanceNear = RL_CULL_DISTANCE_NEAR; |
| 1115 | static float rlCullDistanceFar = RL_CULL_DISTANCE_FAR; |
| 1116 | |
| 1117 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1118 | static rlglData RLGL = { 0 }; |
| 1119 | #endif |
| 1120 | |
| 1121 | #if defined(GRAPHICS_API_OPENGL_ES2) && !defined(GRAPHICS_API_OPENGL_ES3) |
| 1122 | // VAO functions entry points |
| 1123 | // NOTE: VAO functionality is exposed through extensions (OES) |
| 1124 | static PFNGLGENVERTEXARRAYSOESPROC glGenVertexArrays = NULL; |
| 1125 | static PFNGLBINDVERTEXARRAYOESPROC glBindVertexArray = NULL; |
| 1126 | static PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays = NULL; |
| 1127 | |
| 1128 | // Instancing functionality entry points |
| 1129 | // NOTE: Instancing functionality could be available through extensions |
| 1130 | static PFNGLDRAWARRAYSINSTANCEDEXTPROC glDrawArraysInstanced = NULL; |
| 1131 | static PFNGLDRAWELEMENTSINSTANCEDEXTPROC glDrawElementsInstanced = NULL; |
| 1132 | static PFNGLVERTEXATTRIBDIVISOREXTPROC glVertexAttribDivisor = NULL; |
| 1133 | #endif |
| 1134 | |
| 1135 | //---------------------------------------------------------------------------------- |
| 1136 | // Module Functions Declaration |
| 1137 | //---------------------------------------------------------------------------------- |
| 1138 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1139 | static void rlLoadShaderDefault(void); // Load default shader |
| 1140 | static void rlUnloadShaderDefault(void); // Unload default shader |
| 1141 | #if RLGL_SHOW_GL_DETAILS_INFO |
| 1142 | static const char *rlGetCompressedFormatName(int format); // Get compressed format official GL identifier name |
| 1143 | #endif |
| 1144 | #endif |
| 1145 | |
| 1146 | static int rlGetPixelDataSize(int width, int height, int format); // Get pixel data size in bytes (image or texture) |
| 1147 | |
| 1148 | static Matrix rlMatrixIdentity(void); // Get identity matrix |
| 1149 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1150 | // Auxiliar matrix math functions |
| 1151 | typedef struct rl_float16 { float v[16]; } rl_float16; |
| 1152 | static rl_float16 rlMatrixToFloatV(Matrix mat); // Get float array of matrix data |
| 1153 | #define rlMatrixToFloat(mat) (rlMatrixToFloatV(mat).v) // Get float vector for Matrix |
| 1154 | static Matrix rlMatrixMultiply(Matrix left, Matrix right); // Multiply two matrices |
| 1155 | static Matrix rlMatrixTranspose(Matrix mat); // Transposes provided matrix |
| 1156 | static Matrix rlMatrixInvert(Matrix mat); // Invert provided matrix |
| 1157 | #endif |
| 1158 | |
| 1159 | //---------------------------------------------------------------------------------- |
| 1160 | // Module Functions Definition - Matrix operations |
| 1161 | //---------------------------------------------------------------------------------- |
| 1162 | |
| 1163 | #if defined(GRAPHICS_API_OPENGL_11) |
| 1164 | // Fallback to OpenGL 1.1 function calls |
| 1165 | //--------------------------------------- |
| 1166 | void rlMatrixMode(int mode) |
| 1167 | { |
| 1168 | switch (mode) |
| 1169 | { |
| 1170 | case RL_PROJECTION: glMatrixMode(GL_PROJECTION); break; |
| 1171 | case RL_MODELVIEW: glMatrixMode(GL_MODELVIEW); break; |
| 1172 | case RL_TEXTURE: glMatrixMode(GL_TEXTURE); break; |
| 1173 | default: break; |
| 1174 | } |
| 1175 | } |
| 1176 | |
| 1177 | void rlFrustum(float left, float right, float bottom, float top, float znear, float zfar) |
| 1178 | { |
| 1179 | glFrustum(left, right, bottom, top, znear, zfar); |
| 1180 | } |
| 1181 | |
| 1182 | void rlOrtho(float left, float right, float bottom, float top, float znear, float zfar) |
| 1183 | { |
| 1184 | glOrtho(left, right, bottom, top, znear, zfar); |
| 1185 | } |
| 1186 | |
| 1187 | void rlPushMatrix(void) { glPushMatrix(); } |
| 1188 | void rlPopMatrix(void) { glPopMatrix(); } |
| 1189 | void rlLoadIdentity(void) { glLoadIdentity(); } |
| 1190 | void rlTranslatef(float x, float y, float z) { glTranslatef(x, y, z); } |
| 1191 | void rlRotatef(float angle, float x, float y, float z) { glRotatef(angle, x, y, z); } |
| 1192 | void rlScalef(float x, float y, float z) { glScalef(x, y, z); } |
| 1193 | void rlMultMatrixf(const float *matf) { glMultMatrixf(matf); } |
| 1194 | #endif |
| 1195 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1196 | // Choose the current matrix to be transformed |
| 1197 | void rlMatrixMode(int mode) |
| 1198 | { |
| 1199 | if (mode == RL_PROJECTION) RLGL.State.currentMatrix = &RLGL.State.projection; |
| 1200 | else if (mode == RL_MODELVIEW) RLGL.State.currentMatrix = &RLGL.State.modelview; |
| 1201 | //else if (mode == RL_TEXTURE) // Not supported |
| 1202 | |
| 1203 | RLGL.State.currentMatrixMode = mode; |
| 1204 | } |
| 1205 | |
| 1206 | // Push the current matrix into RLGL.State.stack |
| 1207 | void rlPushMatrix(void) |
| 1208 | { |
| 1209 | if (RLGL.State.stackCounter >= RL_MAX_MATRIX_STACK_SIZE) |
| 1210 | { |
| 1211 | TRACELOG(RL_LOG_ERROR, "RLGL: Matrix stack overflow (RL_MAX_MATRIX_STACK_SIZE)"); |
| 1212 | return; |
| 1213 | } |
| 1214 | |
| 1215 | if (RLGL.State.currentMatrixMode == RL_MODELVIEW) |
| 1216 | { |
| 1217 | RLGL.State.transformRequired = true; |
| 1218 | RLGL.State.currentMatrix = &RLGL.State.transform; |
| 1219 | } |
| 1220 | |
| 1221 | RLGL.State.stack[RLGL.State.stackCounter] = *RLGL.State.currentMatrix; |
| 1222 | RLGL.State.stackCounter++; |
| 1223 | } |
| 1224 | |
| 1225 | // Pop latest inserted matrix from RLGL.State.stack |
| 1226 | void rlPopMatrix(void) |
| 1227 | { |
| 1228 | if (RLGL.State.stackCounter > 0) |
| 1229 | { |
| 1230 | Matrix mat = RLGL.State.stack[RLGL.State.stackCounter - 1]; |
| 1231 | *RLGL.State.currentMatrix = mat; |
| 1232 | RLGL.State.stackCounter--; |
| 1233 | } |
| 1234 | |
| 1235 | if ((RLGL.State.stackCounter == 0) && (RLGL.State.currentMatrixMode == RL_MODELVIEW)) |
| 1236 | { |
| 1237 | RLGL.State.currentMatrix = &RLGL.State.modelview; |
| 1238 | RLGL.State.transformRequired = false; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | // Reset current matrix to identity matrix |
| 1243 | void rlLoadIdentity(void) |
| 1244 | { |
| 1245 | *RLGL.State.currentMatrix = rlMatrixIdentity(); |
| 1246 | } |
| 1247 | |
| 1248 | // Multiply the current matrix by a translation matrix |
| 1249 | void rlTranslatef(float x, float y, float z) |
| 1250 | { |
| 1251 | Matrix matTranslation = rlMatrixIdentity(); |
| 1252 | |
| 1253 | // Set translation component of matrix |
| 1254 | matTranslation.m12 = x; |
| 1255 | matTranslation.m13 = y; |
| 1256 | matTranslation.m14 = z; |
| 1257 | |
| 1258 | // NOTE: Transposing matrix by multiplication order |
| 1259 | *RLGL.State.currentMatrix = rlMatrixMultiply(matTranslation, *RLGL.State.currentMatrix); |
| 1260 | } |
| 1261 | |
| 1262 | // Multiply the current matrix by a rotation matrix |
| 1263 | // NOTE: The provided angle must be in degrees |
| 1264 | void rlRotatef(float angle, float x, float y, float z) |
| 1265 | { |
| 1266 | Matrix matRotation = rlMatrixIdentity(); |
| 1267 | |
| 1268 | // Axis vector (x, y, z) normalization |
| 1269 | float lengthSquared = x*x + y*y + z*z; |
| 1270 | if ((lengthSquared != 1.0f) && (lengthSquared != 0.0f)) |
| 1271 | { |
| 1272 | float inverseLength = 1.0f/sqrtf(lengthSquared); |
| 1273 | x *= inverseLength; |
| 1274 | y *= inverseLength; |
| 1275 | z *= inverseLength; |
| 1276 | } |
| 1277 | |
| 1278 | // Rotation matrix generation |
| 1279 | float sinres = sinf(DEG2RAD*angle); |
| 1280 | float cosres = cosf(DEG2RAD*angle); |
| 1281 | float t = 1.0f - cosres; |
| 1282 | |
| 1283 | matRotation.m0 = x*x*t + cosres; |
| 1284 | matRotation.m1 = y*x*t + z*sinres; |
| 1285 | matRotation.m2 = z*x*t - y*sinres; |
| 1286 | matRotation.m3 = 0.0f; |
| 1287 | |
| 1288 | matRotation.m4 = x*y*t - z*sinres; |
| 1289 | matRotation.m5 = y*y*t + cosres; |
| 1290 | matRotation.m6 = z*y*t + x*sinres; |
| 1291 | matRotation.m7 = 0.0f; |
| 1292 | |
| 1293 | matRotation.m8 = x*z*t + y*sinres; |
| 1294 | matRotation.m9 = y*z*t - x*sinres; |
| 1295 | matRotation.m10 = z*z*t + cosres; |
| 1296 | matRotation.m11 = 0.0f; |
| 1297 | |
| 1298 | matRotation.m12 = 0.0f; |
| 1299 | matRotation.m13 = 0.0f; |
| 1300 | matRotation.m14 = 0.0f; |
| 1301 | matRotation.m15 = 1.0f; |
| 1302 | |
| 1303 | // NOTE: Transposing matrix by multiplication order |
| 1304 | *RLGL.State.currentMatrix = rlMatrixMultiply(matRotation, *RLGL.State.currentMatrix); |
| 1305 | } |
| 1306 | |
| 1307 | // Multiply the current matrix by a scaling matrix |
| 1308 | void rlScalef(float x, float y, float z) |
| 1309 | { |
| 1310 | Matrix matScale = rlMatrixIdentity(); |
| 1311 | |
| 1312 | // Set scale component of matrix |
| 1313 | matScale.m0 = x; |
| 1314 | matScale.m5 = y; |
| 1315 | matScale.m10 = z; |
| 1316 | |
| 1317 | // NOTE: Transposing matrix by multiplication order |
| 1318 | *RLGL.State.currentMatrix = rlMatrixMultiply(matScale, *RLGL.State.currentMatrix); |
| 1319 | } |
| 1320 | |
| 1321 | // Multiply the current matrix by another matrix |
| 1322 | void rlMultMatrixf(const float *matf) |
| 1323 | { |
| 1324 | // Matrix creation from array |
| 1325 | // Conversion from column-major to row-major memory order |
| 1326 | Matrix mat = { matf[0], matf[4], matf[8], matf[12], |
| 1327 | matf[1], matf[5], matf[9], matf[13], |
| 1328 | matf[2], matf[6], matf[10], matf[14], |
| 1329 | matf[3], matf[7], matf[11], matf[15] }; |
| 1330 | |
| 1331 | *RLGL.State.currentMatrix = rlMatrixMultiply(mat, *RLGL.State.currentMatrix); |
| 1332 | } |
| 1333 | |
| 1334 | // Multiply the current matrix by a perspective matrix generated by parameters |
| 1335 | void rlFrustum(float left, float right, float bottom, float top, float znear, float zfar) |
| 1336 | { |
| 1337 | Matrix matFrustum = { 0 }; |
| 1338 | |
| 1339 | float rl = right - left; |
| 1340 | float tb = top - bottom; |
| 1341 | float fn = zfar - znear; |
| 1342 | |
| 1343 | matFrustum.m0 = (znear*2.0f)/rl; |
| 1344 | matFrustum.m1 = 0.0f; |
| 1345 | matFrustum.m2 = 0.0f; |
| 1346 | matFrustum.m3 = 0.0f; |
| 1347 | |
| 1348 | matFrustum.m4 = 0.0f; |
| 1349 | matFrustum.m5 = (znear*2.0f)/tb; |
| 1350 | matFrustum.m6 = 0.0f; |
| 1351 | matFrustum.m7 = 0.0f; |
| 1352 | |
| 1353 | matFrustum.m8 = (right + left)/rl; |
| 1354 | matFrustum.m9 = (top + bottom)/tb; |
| 1355 | matFrustum.m10 = -(zfar + znear)/fn; |
| 1356 | matFrustum.m11 = -1.0f; |
| 1357 | |
| 1358 | matFrustum.m12 = 0.0f; |
| 1359 | matFrustum.m13 = 0.0f; |
| 1360 | matFrustum.m14 = -(zfar*znear*2.0f)/fn; |
| 1361 | matFrustum.m15 = 0.0f; |
| 1362 | |
| 1363 | *RLGL.State.currentMatrix = rlMatrixMultiply(*RLGL.State.currentMatrix, matFrustum); |
| 1364 | } |
| 1365 | |
| 1366 | // Multiply the current matrix by an orthographic matrix generated by parameters |
| 1367 | void rlOrtho(float left, float right, float bottom, float top, float znear, float zfar) |
| 1368 | { |
| 1369 | // NOTE: If left-right and top-botton values are equal it could create a division by zero, |
| 1370 | // response to it is platform/compiler dependent |
| 1371 | Matrix matOrtho = { 0 }; |
| 1372 | |
| 1373 | float rl = right - left; |
| 1374 | float tb = top - bottom; |
| 1375 | float fn = zfar - znear; |
| 1376 | |
| 1377 | matOrtho.m0 = 2.0f/rl; |
| 1378 | matOrtho.m1 = 0.0f; |
| 1379 | matOrtho.m2 = 0.0f; |
| 1380 | matOrtho.m3 = 0.0f; |
| 1381 | matOrtho.m4 = 0.0f; |
| 1382 | matOrtho.m5 = 2.0f/tb; |
| 1383 | matOrtho.m6 = 0.0f; |
| 1384 | matOrtho.m7 = 0.0f; |
| 1385 | matOrtho.m8 = 0.0f; |
| 1386 | matOrtho.m9 = 0.0f; |
| 1387 | matOrtho.m10 = -2.0f/fn; |
| 1388 | matOrtho.m11 = 0.0f; |
| 1389 | matOrtho.m12 = -(left + right)/rl; |
| 1390 | matOrtho.m13 = -(top + bottom)/tb; |
| 1391 | matOrtho.m14 = -(zfar + znear)/fn; |
| 1392 | matOrtho.m15 = 1.0f; |
| 1393 | |
| 1394 | *RLGL.State.currentMatrix = rlMatrixMultiply(*RLGL.State.currentMatrix, matOrtho); |
| 1395 | } |
| 1396 | #endif |
| 1397 | |
| 1398 | // Set the viewport area (transformation from normalized device coordinates to window coordinates) |
| 1399 | void rlViewport(int x, int y, int width, int height) |
| 1400 | { |
| 1401 | glViewport(x, y, width, height); |
| 1402 | } |
| 1403 | |
| 1404 | // Set clip planes distances |
| 1405 | void rlSetClipPlanes(float nearPlane, float farPlane) |
| 1406 | { |
| 1407 | rlCullDistanceNear = nearPlane; |
| 1408 | rlCullDistanceFar = farPlane; |
| 1409 | } |
| 1410 | |
| 1411 | // Get cull plane distance near |
| 1412 | float rlGetCullDistanceNear(void) |
| 1413 | { |
| 1414 | return rlCullDistanceNear; |
| 1415 | } |
| 1416 | |
| 1417 | // Get cull plane distance far |
| 1418 | float rlGetCullDistanceFar(void) |
| 1419 | { |
| 1420 | return rlCullDistanceFar; |
| 1421 | } |
| 1422 | |
| 1423 | //---------------------------------------------------------------------------------- |
| 1424 | // Module Functions Definition - Vertex level operations |
| 1425 | //---------------------------------------------------------------------------------- |
| 1426 | #if defined(GRAPHICS_API_OPENGL_11) |
| 1427 | // Fallback to OpenGL 1.1 function calls |
| 1428 | //--------------------------------------- |
| 1429 | void rlBegin(int mode) |
| 1430 | { |
| 1431 | switch (mode) |
| 1432 | { |
| 1433 | case RL_LINES: glBegin(GL_LINES); break; |
| 1434 | case RL_TRIANGLES: glBegin(GL_TRIANGLES); break; |
| 1435 | case RL_QUADS: glBegin(GL_QUADS); break; |
| 1436 | default: break; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | void rlEnd(void) { glEnd(); } |
| 1441 | void rlVertex2i(int x, int y) { glVertex2i(x, y); } |
| 1442 | void rlVertex2f(float x, float y) { glVertex2f(x, y); } |
| 1443 | void rlVertex3f(float x, float y, float z) { glVertex3f(x, y, z); } |
| 1444 | void rlTexCoord2f(float x, float y) { glTexCoord2f(x, y); } |
| 1445 | void rlNormal3f(float x, float y, float z) { glNormal3f(x, y, z); } |
| 1446 | void rlColor4ub(unsigned char r, unsigned char g, unsigned char b, unsigned char a) { glColor4ub(r, g, b, a); } |
| 1447 | void rlColor3f(float x, float y, float z) { glColor3f(x, y, z); } |
| 1448 | void rlColor4f(float x, float y, float z, float w) { glColor4f(x, y, z, w); } |
| 1449 | #endif |
| 1450 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1451 | // Initialize drawing mode (how to organize vertex) |
| 1452 | void rlBegin(int mode) |
| 1453 | { |
| 1454 | // Draw mode can be RL_LINES, RL_TRIANGLES and RL_QUADS |
| 1455 | // NOTE: In all three cases, vertex are accumulated over default internal vertex buffer |
| 1456 | if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode != mode) |
| 1457 | { |
| 1458 | if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0) |
| 1459 | { |
| 1460 | // Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4, |
| 1461 | // that way, following QUADS drawing will keep aligned with index processing |
| 1462 | // It implies adding some extra alignment vertex at the end of the draw, |
| 1463 | // those vertex are not processed but they are considered as an additional offset |
| 1464 | // for the next set of vertex to be drawn |
| 1465 | if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_LINES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount : RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4); |
| 1466 | else if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_TRIANGLES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? 1 : (4 - (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4))); |
| 1467 | else RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = 0; |
| 1468 | |
| 1469 | if (!rlCheckRenderBatchLimit(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment)) |
| 1470 | { |
| 1471 | RLGL.State.vertexCounter += RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment; |
| 1472 | RLGL.currentBatch->drawCounter++; |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | if (RLGL.currentBatch->drawCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch); |
| 1477 | |
| 1478 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = mode; |
| 1479 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = RLGL.State.currentTextureId; |
| 1480 | RLGL.State.currentTextureId = RLGL.State.defaultTextureId; |
| 1481 | } |
| 1482 | } |
| 1483 | |
| 1484 | // Finish vertex providing |
| 1485 | void rlEnd(void) |
| 1486 | { |
| 1487 | // NOTE: Depth increment is dependent on rlOrtho(): z-near and z-far values, |
| 1488 | // as well as depth buffer bit-depth (16bit or 24bit or 32bit) |
| 1489 | // Correct increment formula would be: depthInc = (zfar - znear)/pow(2, bits) |
| 1490 | RLGL.currentBatch->currentDepth += (1.0f/20000.0f); |
| 1491 | } |
| 1492 | |
| 1493 | // Define one vertex (position) |
| 1494 | // NOTE: Vertex position data is the basic information required for drawing |
| 1495 | void rlVertex3f(float x, float y, float z) |
| 1496 | { |
| 1497 | float tx = x; |
| 1498 | float ty = y; |
| 1499 | float tz = z; |
| 1500 | |
| 1501 | // Transform provided vector if required |
| 1502 | if (RLGL.State.transformRequired) |
| 1503 | { |
| 1504 | tx = RLGL.State.transform.m0*x + RLGL.State.transform.m4*y + RLGL.State.transform.m8*z + RLGL.State.transform.m12; |
| 1505 | ty = RLGL.State.transform.m1*x + RLGL.State.transform.m5*y + RLGL.State.transform.m9*z + RLGL.State.transform.m13; |
| 1506 | tz = RLGL.State.transform.m2*x + RLGL.State.transform.m6*y + RLGL.State.transform.m10*z + RLGL.State.transform.m14; |
| 1507 | } |
| 1508 | |
| 1509 | // WARNING: Be careful with primitives breaking when launching a new batch! |
| 1510 | // RL_LINES comes in pairs, RL_TRIANGLES come in groups of 3 vertices and RL_QUADS come in groups of 4 vertices |
| 1511 | // Checking current draw.mode when a new vertex is required and finish the batch only if the draw.mode draw.vertexCount is %2, %3 or %4 |
| 1512 | if (RLGL.State.vertexCounter > (RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].elementCount*4 - 4)) |
| 1513 | { |
| 1514 | if ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_LINES) && |
| 1515 | (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%2 == 0)) |
| 1516 | { |
| 1517 | // Reached the maximum number of vertices for RL_LINES drawing |
| 1518 | // Launch a draw call but keep current state for next vertices comming |
| 1519 | // NOTE: Adding +1 vertex to the check for some safety |
| 1520 | rlCheckRenderBatchLimit(2 + 1); |
| 1521 | } |
| 1522 | else if ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_TRIANGLES) && |
| 1523 | (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%3 == 0)) |
| 1524 | { |
| 1525 | rlCheckRenderBatchLimit(3 + 1); |
| 1526 | } |
| 1527 | else if ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_QUADS) && |
| 1528 | (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4 == 0)) |
| 1529 | { |
| 1530 | rlCheckRenderBatchLimit(4 + 1); |
| 1531 | } |
| 1532 | } |
| 1533 | |
| 1534 | // Add vertices |
| 1535 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].vertices[3*RLGL.State.vertexCounter] = tx; |
| 1536 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].vertices[3*RLGL.State.vertexCounter + 1] = ty; |
| 1537 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].vertices[3*RLGL.State.vertexCounter + 2] = tz; |
| 1538 | |
| 1539 | // Add current texcoord |
| 1540 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].texcoords[2*RLGL.State.vertexCounter] = RLGL.State.texcoordx; |
| 1541 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].texcoords[2*RLGL.State.vertexCounter + 1] = RLGL.State.texcoordy; |
| 1542 | |
| 1543 | // Add current normal |
| 1544 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].normals[3*RLGL.State.vertexCounter] = RLGL.State.normalx; |
| 1545 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].normals[3*RLGL.State.vertexCounter + 1] = RLGL.State.normaly; |
| 1546 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].normals[3*RLGL.State.vertexCounter + 2] = RLGL.State.normalz; |
| 1547 | |
| 1548 | // Add current color |
| 1549 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter] = RLGL.State.colorr; |
| 1550 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter + 1] = RLGL.State.colorg; |
| 1551 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter + 2] = RLGL.State.colorb; |
| 1552 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].colors[4*RLGL.State.vertexCounter + 3] = RLGL.State.colora; |
| 1553 | |
| 1554 | RLGL.State.vertexCounter++; |
| 1555 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount++; |
| 1556 | } |
| 1557 | |
| 1558 | // Define one vertex (position) |
| 1559 | void rlVertex2f(float x, float y) |
| 1560 | { |
| 1561 | rlVertex3f(x, y, RLGL.currentBatch->currentDepth); |
| 1562 | } |
| 1563 | |
| 1564 | // Define one vertex (position) |
| 1565 | void rlVertex2i(int x, int y) |
| 1566 | { |
| 1567 | rlVertex3f((float)x, (float)y, RLGL.currentBatch->currentDepth); |
| 1568 | } |
| 1569 | |
| 1570 | // Define one vertex (texture coordinate) |
| 1571 | // NOTE: Texture coordinates are limited to QUADS only |
| 1572 | void rlTexCoord2f(float x, float y) |
| 1573 | { |
| 1574 | RLGL.State.texcoordx = x; |
| 1575 | RLGL.State.texcoordy = y; |
| 1576 | } |
| 1577 | |
| 1578 | // Define one vertex (normal) |
| 1579 | // NOTE: Normals limited to TRIANGLES only? |
| 1580 | void rlNormal3f(float x, float y, float z) |
| 1581 | { |
| 1582 | float normalx = x; |
| 1583 | float normaly = y; |
| 1584 | float normalz = z; |
| 1585 | if (RLGL.State.transformRequired) |
| 1586 | { |
| 1587 | normalx = RLGL.State.transform.m0*x + RLGL.State.transform.m4*y + RLGL.State.transform.m8*z; |
| 1588 | normaly = RLGL.State.transform.m1*x + RLGL.State.transform.m5*y + RLGL.State.transform.m9*z; |
| 1589 | normalz = RLGL.State.transform.m2*x + RLGL.State.transform.m6*y + RLGL.State.transform.m10*z; |
| 1590 | } |
| 1591 | |
| 1592 | // NOTE: Default behavior assumes the normal vector is in the correct space for what the shader expects, |
| 1593 | // it could be not normalized to 0.0f..1.0f, magnitud can be useed for some effects |
| 1594 | /* |
| 1595 | // WARNING: Vector normalization if required |
| 1596 | float length = sqrtf(normalx*normalx + normaly*normaly + normalz*normalz); |
| 1597 | if (length != 0.0f) |
| 1598 | { |
| 1599 | float ilength = 1.0f/length; |
| 1600 | normalx *= ilength; |
| 1601 | normaly *= ilength; |
| 1602 | normalz *= ilength; |
| 1603 | } |
| 1604 | */ |
| 1605 | RLGL.State.normalx = normalx; |
| 1606 | RLGL.State.normaly = normaly; |
| 1607 | RLGL.State.normalz = normalz; |
| 1608 | } |
| 1609 | |
| 1610 | // Define one vertex (color) |
| 1611 | void rlColor4ub(unsigned char x, unsigned char y, unsigned char z, unsigned char w) |
| 1612 | { |
| 1613 | RLGL.State.colorr = x; |
| 1614 | RLGL.State.colorg = y; |
| 1615 | RLGL.State.colorb = z; |
| 1616 | RLGL.State.colora = w; |
| 1617 | } |
| 1618 | |
| 1619 | // Define one vertex (color) |
| 1620 | void rlColor4f(float r, float g, float b, float a) |
| 1621 | { |
| 1622 | rlColor4ub((unsigned char)(r*255), (unsigned char)(g*255), (unsigned char)(b*255), (unsigned char)(a*255)); |
| 1623 | } |
| 1624 | |
| 1625 | // Define one vertex (color) |
| 1626 | void rlColor3f(float x, float y, float z) |
| 1627 | { |
| 1628 | rlColor4ub((unsigned char)(x*255), (unsigned char)(y*255), (unsigned char)(z*255), 255); |
| 1629 | } |
| 1630 | |
| 1631 | #endif |
| 1632 | |
| 1633 | //-------------------------------------------------------------------------------------- |
| 1634 | // Module Functions Definition - OpenGL style functions (common to 1.1, 3.3+, ES2) |
| 1635 | //-------------------------------------------------------------------------------------- |
| 1636 | |
| 1637 | // Set current texture to use |
| 1638 | void rlSetTexture(unsigned int id) |
| 1639 | { |
| 1640 | if (id == 0) |
| 1641 | { |
| 1642 | #if defined(GRAPHICS_API_OPENGL_11) |
| 1643 | rlDisableTexture(); |
| 1644 | #else |
| 1645 | // NOTE: If quads batch limit is reached, force a draw call and next batch starts |
| 1646 | if (RLGL.State.vertexCounter >= |
| 1647 | RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].elementCount*4) |
| 1648 | { |
| 1649 | rlDrawRenderBatch(RLGL.currentBatch); |
| 1650 | } |
| 1651 | RLGL.State.currentTextureId = RLGL.State.defaultTextureId; |
| 1652 | #endif |
| 1653 | } |
| 1654 | else |
| 1655 | { |
| 1656 | #if defined(GRAPHICS_API_OPENGL_11) |
| 1657 | rlEnableTexture(id); |
| 1658 | #else |
| 1659 | RLGL.State.currentTextureId = id; |
| 1660 | if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId != id) |
| 1661 | { |
| 1662 | if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount > 0) |
| 1663 | { |
| 1664 | // Make sure current RLGL.currentBatch->draws[i].vertexCount is aligned a multiple of 4, |
| 1665 | // that way, following QUADS drawing will keep aligned with index processing |
| 1666 | // It implies adding some extra alignment vertex at the end of the draw, |
| 1667 | // those vertex are not processed but they are considered as an additional offset |
| 1668 | // for the next set of vertex to be drawn |
| 1669 | if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_LINES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount : RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4); |
| 1670 | else if (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode == RL_TRIANGLES) RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = ((RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount < 4)? 1 : (4 - (RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount%4))); |
| 1671 | else RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment = 0; |
| 1672 | |
| 1673 | if (!rlCheckRenderBatchLimit(RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment)) |
| 1674 | { |
| 1675 | RLGL.State.vertexCounter += RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexAlignment; |
| 1676 | |
| 1677 | RLGL.currentBatch->drawCounter++; |
| 1678 | |
| 1679 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 2].mode; |
| 1680 | |
| 1681 | } |
| 1682 | } |
| 1683 | |
| 1684 | if (RLGL.currentBatch->drawCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch); |
| 1685 | |
| 1686 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = id; |
| 1687 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].vertexCount = 0; |
| 1688 | } |
| 1689 | #endif |
| 1690 | } |
| 1691 | } |
| 1692 | |
| 1693 | // Select and active a texture slot |
| 1694 | void rlActiveTextureSlot(int slot) |
| 1695 | { |
| 1696 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1697 | glActiveTexture(GL_TEXTURE0 + slot); |
| 1698 | #endif |
| 1699 | } |
| 1700 | |
| 1701 | // Enable texture |
| 1702 | void rlEnableTexture(unsigned int id) |
| 1703 | { |
| 1704 | #if defined(GRAPHICS_API_OPENGL_11) |
| 1705 | glEnable(GL_TEXTURE_2D); |
| 1706 | #endif |
| 1707 | glBindTexture(GL_TEXTURE_2D, id); |
| 1708 | } |
| 1709 | |
| 1710 | // Disable texture |
| 1711 | void rlDisableTexture(void) |
| 1712 | { |
| 1713 | #if defined(GRAPHICS_API_OPENGL_11) |
| 1714 | glDisable(GL_TEXTURE_2D); |
| 1715 | #endif |
| 1716 | glBindTexture(GL_TEXTURE_2D, 0); |
| 1717 | } |
| 1718 | |
| 1719 | // Enable texture cubemap |
| 1720 | void rlEnableTextureCubemap(unsigned int id) |
| 1721 | { |
| 1722 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1723 | glBindTexture(GL_TEXTURE_CUBE_MAP, id); |
| 1724 | #endif |
| 1725 | } |
| 1726 | |
| 1727 | // Disable texture cubemap |
| 1728 | void rlDisableTextureCubemap(void) |
| 1729 | { |
| 1730 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 1731 | glBindTexture(GL_TEXTURE_CUBE_MAP, 0); |
| 1732 | #endif |
| 1733 | } |
| 1734 | |
| 1735 | // Set texture parameters (wrap mode/filter mode) |
| 1736 | void rlTextureParameters(unsigned int id, int param, int value) |
| 1737 | { |
| 1738 | glBindTexture(GL_TEXTURE_2D, id); |
| 1739 | |
| 1740 | switch (param) |
| 1741 | { |
| 1742 | case RL_TEXTURE_WRAP_S: |
| 1743 | case RL_TEXTURE_WRAP_T: |
| 1744 | { |
| 1745 | if (value == RL_TEXTURE_WRAP_MIRROR_CLAMP) |
| 1746 | { |
| 1747 | #if !defined(GRAPHICS_API_OPENGL_11) |
| 1748 | if (RLGL.ExtSupported.texMirrorClamp) glTexParameteri(GL_TEXTURE_2D, param, value); |
| 1749 | else TRACELOG(RL_LOG_WARNING, "GL: Clamp mirror wrap mode not supported (GL_MIRROR_CLAMP_EXT)"); |
| 1750 | #endif |
| 1751 | } |
| 1752 | else glTexParameteri(GL_TEXTURE_2D, param, value); |
| 1753 | } break; |
| 1754 | case RL_TEXTURE_MAG_FILTER: |
| 1755 | case RL_TEXTURE_MIN_FILTER: glTexParameteri(GL_TEXTURE_2D, param, value); break; |
| 1756 | case RL_TEXTURE_FILTER_ANISOTROPIC: |
| 1757 | { |
| 1758 | #if !defined(GRAPHICS_API_OPENGL_11) |
| 1759 | // Reset anisotropy filter, in case it was set |
| 1760 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); |
| 1761 | |
| 1762 | if (value <= RLGL.ExtSupported.maxAnisotropyLevel) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value); |
| 1763 | else if (RLGL.ExtSupported.maxAnisotropyLevel > 0.0f) |
| 1764 | { |
| 1765 | TRACELOG(RL_LOG_WARNING, "GL: Maximum anisotropic filter level supported is %iX", id, (int)RLGL.ExtSupported.maxAnisotropyLevel); |
| 1766 | glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value); |
| 1767 | } |
| 1768 | else TRACELOG(RL_LOG_WARNING, "GL: Anisotropic filtering not supported"); |
| 1769 | #endif |
| 1770 | } break; |
| 1771 | #if defined(GRAPHICS_API_OPENGL_33) |
| 1772 | case RL_TEXTURE_MIPMAP_BIAS_RATIO: glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_LOD_BIAS, value/100.0f); |
| 1773 | #endif |
| 1774 | default: break; |
| 1775 | } |
| 1776 | |
| 1777 | glBindTexture(GL_TEXTURE_2D, 0); |
| 1778 | } |
| 1779 | |
| 1780 | // Set cubemap parameters (wrap mode/filter mode) |
| 1781 | void rlCubemapParameters(unsigned int id, int param, int value) |
| 1782 | { |
| 1783 | #if !defined(GRAPHICS_API_OPENGL_11) |
| 1784 | glBindTexture(GL_TEXTURE_CUBE_MAP, id); |
| 1785 | |
| 1786 | // Reset anisotropy filter, in case it was set |
| 1787 | glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); |
| 1788 | |
| 1789 | switch (param) |
| 1790 | { |
| 1791 | case RL_TEXTURE_WRAP_S: |
| 1792 | case RL_TEXTURE_WRAP_T: |
| 1793 | { |
| 1794 | if (value == RL_TEXTURE_WRAP_MIRROR_CLAMP) |
| 1795 | { |
| 1796 | if (RLGL.ExtSupported.texMirrorClamp) glTexParameteri(GL_TEXTURE_CUBE_MAP, param, value); |
| 1797 | else TRACELOG(RL_LOG_WARNING, "GL: Clamp mirror wrap mode not supported (GL_MIRROR_CLAMP_EXT)"); |
| 1798 | } |
| 1799 | else glTexParameteri(GL_TEXTURE_CUBE_MAP, param, value); |
| 1800 | } break; |
| 1801 | case RL_TEXTURE_MAG_FILTER: |
| 1802 | case RL_TEXTURE_MIN_FILTER: glTexParameteri(GL_TEXTURE_CUBE_MAP, param, value); break; |
| 1803 | case RL_TEXTURE_FILTER_ANISOTROPIC: |
| 1804 | { |
| 1805 | if (value <= RLGL.ExtSupported.maxAnisotropyLevel) glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value); |
| 1806 | else if (RLGL.ExtSupported.maxAnisotropyLevel > 0.0f) |
| 1807 | { |
| 1808 | TRACELOG(RL_LOG_WARNING, "GL: Maximum anisotropic filter level supported is %iX", id, (int)RLGL.ExtSupported.maxAnisotropyLevel); |
| 1809 | glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, (float)value); |
| 1810 | } |
| 1811 | else TRACELOG(RL_LOG_WARNING, "GL: Anisotropic filtering not supported"); |
| 1812 | } break; |
| 1813 | #if defined(GRAPHICS_API_OPENGL_33) |
| 1814 | case RL_TEXTURE_MIPMAP_BIAS_RATIO: glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_LOD_BIAS, value/100.0f); |
| 1815 | #endif |
| 1816 | default: break; |
| 1817 | } |
| 1818 | |
| 1819 | glBindTexture(GL_TEXTURE_CUBE_MAP, 0); |
| 1820 | #endif |
| 1821 | } |
| 1822 | |
| 1823 | // Enable shader program |
| 1824 | void rlEnableShader(unsigned int id) |
| 1825 | { |
| 1826 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) |
| 1827 | glUseProgram(id); |
| 1828 | #endif |
| 1829 | } |
| 1830 | |
| 1831 | // Disable shader program |
| 1832 | void rlDisableShader(void) |
| 1833 | { |
| 1834 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) |
| 1835 | glUseProgram(0); |
| 1836 | #endif |
| 1837 | } |
| 1838 | |
| 1839 | // Enable rendering to texture (fbo) |
| 1840 | void rlEnableFramebuffer(unsigned int id) |
| 1841 | { |
| 1842 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 1843 | glBindFramebuffer(GL_FRAMEBUFFER, id); |
| 1844 | #endif |
| 1845 | } |
| 1846 | |
| 1847 | // return the active render texture (fbo) |
| 1848 | unsigned int rlGetActiveFramebuffer(void) |
| 1849 | { |
| 1850 | GLint fboId = 0; |
| 1851 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 1852 | glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &fboId); |
| 1853 | #endif |
| 1854 | return fboId; |
| 1855 | } |
| 1856 | |
| 1857 | // Disable rendering to texture |
| 1858 | void rlDisableFramebuffer(void) |
| 1859 | { |
| 1860 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 1861 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 1862 | #endif |
| 1863 | } |
| 1864 | |
| 1865 | // Blit active framebuffer to main framebuffer |
| 1866 | void rlBlitFramebuffer(int srcX, int srcY, int srcWidth, int srcHeight, int dstX, int dstY, int dstWidth, int dstHeight, int bufferMask) |
| 1867 | { |
| 1868 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES3)) |
| 1869 | glBlitFramebuffer(srcX, srcY, srcWidth, srcHeight, dstX, dstY, dstWidth, dstHeight, bufferMask, GL_NEAREST); |
| 1870 | #endif |
| 1871 | } |
| 1872 | |
| 1873 | // Bind framebuffer object (fbo) |
| 1874 | void rlBindFramebuffer(unsigned int target, unsigned int framebuffer) |
| 1875 | { |
| 1876 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 1877 | glBindFramebuffer(target, framebuffer); |
| 1878 | #endif |
| 1879 | } |
| 1880 | |
| 1881 | // Activate multiple draw color buffers |
| 1882 | // NOTE: One color buffer is always active by default |
| 1883 | void rlActiveDrawBuffers(int count) |
| 1884 | { |
| 1885 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES3)) |
| 1886 | // NOTE: Maximum number of draw buffers supported is implementation dependent, |
| 1887 | // it can be queried with glGet*() but it must be at least 8 |
| 1888 | //GLint maxDrawBuffers = 0; |
| 1889 | //glGetIntegerv(GL_MAX_DRAW_BUFFERS, &maxDrawBuffers); |
| 1890 | |
| 1891 | if (count > 0) |
| 1892 | { |
| 1893 | if (count > 8) TRACELOG(RL_LOG_WARNING, "GL: Max color buffers limited to 8"); |
| 1894 | else |
| 1895 | { |
| 1896 | unsigned int buffers[8] = { |
| 1897 | GL_COLOR_ATTACHMENT0, |
| 1898 | GL_COLOR_ATTACHMENT1, |
| 1899 | GL_COLOR_ATTACHMENT2, |
| 1900 | GL_COLOR_ATTACHMENT3, |
| 1901 | GL_COLOR_ATTACHMENT4, |
| 1902 | GL_COLOR_ATTACHMENT5, |
| 1903 | GL_COLOR_ATTACHMENT6, |
| 1904 | GL_COLOR_ATTACHMENT7, |
| 1905 | }; |
| 1906 | |
| 1907 | glDrawBuffers(count, buffers); |
| 1908 | } |
| 1909 | } |
| 1910 | else TRACELOG(RL_LOG_WARNING, "GL: One color buffer active by default"); |
| 1911 | #endif |
| 1912 | } |
| 1913 | |
| 1914 | //---------------------------------------------------------------------------------- |
| 1915 | // General render state configuration |
| 1916 | //---------------------------------------------------------------------------------- |
| 1917 | |
| 1918 | // Enable color blending |
| 1919 | void rlEnableColorBlend(void) { glEnable(GL_BLEND); } |
| 1920 | |
| 1921 | // Disable color blending |
| 1922 | void rlDisableColorBlend(void) { glDisable(GL_BLEND); } |
| 1923 | |
| 1924 | // Enable depth test |
| 1925 | void rlEnableDepthTest(void) { glEnable(GL_DEPTH_TEST); } |
| 1926 | |
| 1927 | // Disable depth test |
| 1928 | void rlDisableDepthTest(void) { glDisable(GL_DEPTH_TEST); } |
| 1929 | |
| 1930 | // Enable depth write |
| 1931 | void rlEnableDepthMask(void) { glDepthMask(GL_TRUE); } |
| 1932 | |
| 1933 | // Disable depth write |
| 1934 | void rlDisableDepthMask(void) { glDepthMask(GL_FALSE); } |
| 1935 | |
| 1936 | // Enable backface culling |
| 1937 | void rlEnableBackfaceCulling(void) { glEnable(GL_CULL_FACE); } |
| 1938 | |
| 1939 | // Disable backface culling |
| 1940 | void rlDisableBackfaceCulling(void) { glDisable(GL_CULL_FACE); } |
| 1941 | |
| 1942 | // Set color mask active for screen read/draw |
| 1943 | void rlColorMask(bool r, bool g, bool b, bool a) { glColorMask(r, g, b, a); } |
| 1944 | |
| 1945 | // Set face culling mode |
| 1946 | void rlSetCullFace(int mode) |
| 1947 | { |
| 1948 | switch (mode) |
| 1949 | { |
| 1950 | case RL_CULL_FACE_BACK: glCullFace(GL_BACK); break; |
| 1951 | case RL_CULL_FACE_FRONT: glCullFace(GL_FRONT); break; |
| 1952 | default: break; |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | // Enable scissor test |
| 1957 | void rlEnableScissorTest(void) { glEnable(GL_SCISSOR_TEST); } |
| 1958 | |
| 1959 | // Disable scissor test |
| 1960 | void rlDisableScissorTest(void) { glDisable(GL_SCISSOR_TEST); } |
| 1961 | |
| 1962 | // Scissor test |
| 1963 | void rlScissor(int x, int y, int width, int height) { glScissor(x, y, width, height); } |
| 1964 | |
| 1965 | // Enable wire mode |
| 1966 | void rlEnableWireMode(void) |
| 1967 | { |
| 1968 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) |
| 1969 | // NOTE: glPolygonMode() not available on OpenGL ES |
| 1970 | glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); |
| 1971 | #endif |
| 1972 | } |
| 1973 | |
| 1974 | // Disable wire mode |
| 1975 | void rlDisableWireMode(void) |
| 1976 | { |
| 1977 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) |
| 1978 | // NOTE: glPolygonMode() not available on OpenGL ES |
| 1979 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 1980 | #endif |
| 1981 | } |
| 1982 | |
| 1983 | // Enable point mode |
| 1984 | void rlEnablePointMode(void) |
| 1985 | { |
| 1986 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) |
| 1987 | // NOTE: glPolygonMode() not available on OpenGL ES |
| 1988 | glPolygonMode(GL_FRONT_AND_BACK, GL_POINT); |
| 1989 | glEnable(GL_PROGRAM_POINT_SIZE); |
| 1990 | #endif |
| 1991 | } |
| 1992 | |
| 1993 | // Disable point mode |
| 1994 | void rlDisablePointMode(void) |
| 1995 | { |
| 1996 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) |
| 1997 | // NOTE: glPolygonMode() not available on OpenGL ES |
| 1998 | glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); |
| 1999 | #endif |
| 2000 | } |
| 2001 | |
| 2002 | // Set the line drawing width |
| 2003 | void rlSetLineWidth(float width) { glLineWidth(width); } |
| 2004 | |
| 2005 | // Get the line drawing width |
| 2006 | float rlGetLineWidth(void) |
| 2007 | { |
| 2008 | float width = 0; |
| 2009 | glGetFloatv(GL_LINE_WIDTH, &width); |
| 2010 | return width; |
| 2011 | } |
| 2012 | |
| 2013 | // Set the point drawing size |
| 2014 | void rlSetPointSize(float size) |
| 2015 | { |
| 2016 | #if defined(GRAPHICS_API_OPENGL_11) |
| 2017 | glPointSize(size); |
| 2018 | #endif |
| 2019 | } |
| 2020 | |
| 2021 | // Get the point drawing size |
| 2022 | float rlGetPointSize(void) |
| 2023 | { |
| 2024 | float size = 1; |
| 2025 | #if defined(GRAPHICS_API_OPENGL_11) |
| 2026 | glGetFloatv(GL_POINT_SIZE, &size); |
| 2027 | #endif |
| 2028 | return size; |
| 2029 | |
| 2030 | } |
| 2031 | |
| 2032 | // Enable line aliasing |
| 2033 | void rlEnableSmoothLines(void) |
| 2034 | { |
| 2035 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_11) |
| 2036 | glEnable(GL_LINE_SMOOTH); |
| 2037 | #endif |
| 2038 | } |
| 2039 | |
| 2040 | // Disable line aliasing |
| 2041 | void rlDisableSmoothLines(void) |
| 2042 | { |
| 2043 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_11) |
| 2044 | glDisable(GL_LINE_SMOOTH); |
| 2045 | #endif |
| 2046 | } |
| 2047 | |
| 2048 | // Enable stereo rendering |
| 2049 | void rlEnableStereoRender(void) |
| 2050 | { |
| 2051 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) |
| 2052 | RLGL.State.stereoRender = true; |
| 2053 | #endif |
| 2054 | } |
| 2055 | |
| 2056 | // Disable stereo rendering |
| 2057 | void rlDisableStereoRender(void) |
| 2058 | { |
| 2059 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) |
| 2060 | RLGL.State.stereoRender = false; |
| 2061 | #endif |
| 2062 | } |
| 2063 | |
| 2064 | // Check if stereo render is enabled |
| 2065 | bool rlIsStereoRenderEnabled(void) |
| 2066 | { |
| 2067 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2)) |
| 2068 | return RLGL.State.stereoRender; |
| 2069 | #else |
| 2070 | return false; |
| 2071 | #endif |
| 2072 | } |
| 2073 | |
| 2074 | // Clear color buffer with color |
| 2075 | void rlClearColor(unsigned char r, unsigned char g, unsigned char b, unsigned char a) |
| 2076 | { |
| 2077 | // Color values clamp to 0.0f(0) and 1.0f(255) |
| 2078 | float cr = (float)r/255; |
| 2079 | float cg = (float)g/255; |
| 2080 | float cb = (float)b/255; |
| 2081 | float ca = (float)a/255; |
| 2082 | |
| 2083 | glClearColor(cr, cg, cb, ca); |
| 2084 | } |
| 2085 | |
| 2086 | // Clear used screen buffers (color and depth) |
| 2087 | void rlClearScreenBuffers(void) |
| 2088 | { |
| 2089 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear used buffers: Color and Depth (Depth is used for 3D) |
| 2090 | //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Stencil buffer not used... |
| 2091 | } |
| 2092 | |
| 2093 | // Check and log OpenGL error codes |
| 2094 | void rlCheckErrors(void) |
| 2095 | { |
| 2096 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2097 | int check = 1; |
| 2098 | while (check) |
| 2099 | { |
| 2100 | const GLenum err = glGetError(); |
| 2101 | switch (err) |
| 2102 | { |
| 2103 | case GL_NO_ERROR: check = 0; break; |
| 2104 | case 0x0500: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_ENUM"); break; |
| 2105 | case 0x0501: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_VALUE"); break; |
| 2106 | case 0x0502: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_OPERATION"); break; |
| 2107 | case 0x0503: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_STACK_OVERFLOW"); break; |
| 2108 | case 0x0504: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_STACK_UNDERFLOW"); break; |
| 2109 | case 0x0505: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_OUT_OF_MEMORY"); break; |
| 2110 | case 0x0506: TRACELOG(RL_LOG_WARNING, "GL: Error detected: GL_INVALID_FRAMEBUFFER_OPERATION"); break; |
| 2111 | default: TRACELOG(RL_LOG_WARNING, "GL: Error detected: Unknown error code: %x", err); break; |
| 2112 | } |
| 2113 | } |
| 2114 | #endif |
| 2115 | } |
| 2116 | |
| 2117 | // Set blend mode |
| 2118 | void rlSetBlendMode(int mode) |
| 2119 | { |
| 2120 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2121 | if ((RLGL.State.currentBlendMode != mode) || ((mode == RL_BLEND_CUSTOM || mode == RL_BLEND_CUSTOM_SEPARATE) && RLGL.State.glCustomBlendModeModified)) |
| 2122 | { |
| 2123 | rlDrawRenderBatch(RLGL.currentBatch); |
| 2124 | |
| 2125 | switch (mode) |
| 2126 | { |
| 2127 | case RL_BLEND_ALPHA: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); break; |
| 2128 | case RL_BLEND_ADDITIVE: glBlendFunc(GL_SRC_ALPHA, GL_ONE); glBlendEquation(GL_FUNC_ADD); break; |
| 2129 | case RL_BLEND_MULTIPLIED: glBlendFunc(GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); break; |
| 2130 | case RL_BLEND_ADD_COLORS: glBlendFunc(GL_ONE, GL_ONE); glBlendEquation(GL_FUNC_ADD); break; |
| 2131 | case RL_BLEND_SUBTRACT_COLORS: glBlendFunc(GL_ONE, GL_ONE); glBlendEquation(GL_FUNC_SUBTRACT); break; |
| 2132 | case RL_BLEND_ALPHA_PREMULTIPLY: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glBlendEquation(GL_FUNC_ADD); break; |
| 2133 | case RL_BLEND_CUSTOM: |
| 2134 | { |
| 2135 | // NOTE: Using GL blend src/dst factors and GL equation configured with rlSetBlendFactors() |
| 2136 | glBlendFunc(RLGL.State.glBlendSrcFactor, RLGL.State.glBlendDstFactor); glBlendEquation(RLGL.State.glBlendEquation); |
| 2137 | } break; |
| 2138 | case RL_BLEND_CUSTOM_SEPARATE: |
| 2139 | { |
| 2140 | // NOTE: Using GL blend src/dst factors and GL equation configured with rlSetBlendFactorsSeparate() |
| 2141 | glBlendFuncSeparate(RLGL.State.glBlendSrcFactorRGB, RLGL.State.glBlendDestFactorRGB, RLGL.State.glBlendSrcFactorAlpha, RLGL.State.glBlendDestFactorAlpha); |
| 2142 | glBlendEquationSeparate(RLGL.State.glBlendEquationRGB, RLGL.State.glBlendEquationAlpha); |
| 2143 | } break; |
| 2144 | default: break; |
| 2145 | } |
| 2146 | |
| 2147 | RLGL.State.currentBlendMode = mode; |
| 2148 | RLGL.State.glCustomBlendModeModified = false; |
| 2149 | } |
| 2150 | #endif |
| 2151 | } |
| 2152 | |
| 2153 | // Set blending mode factor and equation |
| 2154 | void rlSetBlendFactors(int glSrcFactor, int glDstFactor, int glEquation) |
| 2155 | { |
| 2156 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2157 | if ((RLGL.State.glBlendSrcFactor != glSrcFactor) || |
| 2158 | (RLGL.State.glBlendDstFactor != glDstFactor) || |
| 2159 | (RLGL.State.glBlendEquation != glEquation)) |
| 2160 | { |
| 2161 | RLGL.State.glBlendSrcFactor = glSrcFactor; |
| 2162 | RLGL.State.glBlendDstFactor = glDstFactor; |
| 2163 | RLGL.State.glBlendEquation = glEquation; |
| 2164 | |
| 2165 | RLGL.State.glCustomBlendModeModified = true; |
| 2166 | } |
| 2167 | #endif |
| 2168 | } |
| 2169 | |
| 2170 | // Set blending mode factor and equation separately for RGB and alpha |
| 2171 | void rlSetBlendFactorsSeparate(int glSrcRGB, int glDstRGB, int glSrcAlpha, int glDstAlpha, int glEqRGB, int glEqAlpha) |
| 2172 | { |
| 2173 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2174 | if ((RLGL.State.glBlendSrcFactorRGB != glSrcRGB) || |
| 2175 | (RLGL.State.glBlendDestFactorRGB != glDstRGB) || |
| 2176 | (RLGL.State.glBlendSrcFactorAlpha != glSrcAlpha) || |
| 2177 | (RLGL.State.glBlendDestFactorAlpha != glDstAlpha) || |
| 2178 | (RLGL.State.glBlendEquationRGB != glEqRGB) || |
| 2179 | (RLGL.State.glBlendEquationAlpha != glEqAlpha)) |
| 2180 | { |
| 2181 | RLGL.State.glBlendSrcFactorRGB = glSrcRGB; |
| 2182 | RLGL.State.glBlendDestFactorRGB = glDstRGB; |
| 2183 | RLGL.State.glBlendSrcFactorAlpha = glSrcAlpha; |
| 2184 | RLGL.State.glBlendDestFactorAlpha = glDstAlpha; |
| 2185 | RLGL.State.glBlendEquationRGB = glEqRGB; |
| 2186 | RLGL.State.glBlendEquationAlpha = glEqAlpha; |
| 2187 | |
| 2188 | RLGL.State.glCustomBlendModeModified = true; |
| 2189 | } |
| 2190 | #endif |
| 2191 | } |
| 2192 | |
| 2193 | //---------------------------------------------------------------------------------- |
| 2194 | // Module Functions Definition - OpenGL Debug |
| 2195 | //---------------------------------------------------------------------------------- |
| 2196 | #if defined(GRAPHICS_API_OPENGL_43) && RLGL_ENABLE_OPENGL_DEBUG_CONTEXT |
| 2197 | static void GLAPIENTRY rlDebugMessageCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar *message, const void *userParam) |
| 2198 | { |
| 2199 | // Ignore non-significant error/warning codes (NVidia drivers) |
| 2200 | // NOTE: Here there are the details with a sample output: |
| 2201 | // - #131169 - Framebuffer detailed info: The driver allocated storage for renderbuffer 2. (severity: low) |
| 2202 | // - #131185 - Buffer detailed info: Buffer object 1 (bound to GL_ELEMENT_ARRAY_BUFFER_ARB, usage hint is GL_ENUM_88e4) |
| 2203 | // will use VIDEO memory as the source for buffer object operations. (severity: low) |
| 2204 | // - #131218 - Program/shader state performance warning: Vertex shader in program 7 is being recompiled based on GL state. (severity: medium) |
| 2205 | // - #131204 - Texture state usage warning: The texture object (0) bound to texture image unit 0 does not have |
| 2206 | // a defined base level and cannot be used for texture mapping. (severity: low) |
| 2207 | if ((id == 131169) || (id == 131185) || (id == 131218) || (id == 131204)) return; |
| 2208 | |
| 2209 | const char *msgSource = NULL; |
| 2210 | switch (source) |
| 2211 | { |
| 2212 | case GL_DEBUG_SOURCE_API: msgSource = "API"; break; |
| 2213 | case GL_DEBUG_SOURCE_WINDOW_SYSTEM: msgSource = "WINDOW_SYSTEM"; break; |
| 2214 | case GL_DEBUG_SOURCE_SHADER_COMPILER: msgSource = "SHADER_COMPILER"; break; |
| 2215 | case GL_DEBUG_SOURCE_THIRD_PARTY: msgSource = "THIRD_PARTY"; break; |
| 2216 | case GL_DEBUG_SOURCE_APPLICATION: msgSource = "APPLICATION"; break; |
| 2217 | case GL_DEBUG_SOURCE_OTHER: msgSource = "OTHER"; break; |
| 2218 | default: break; |
| 2219 | } |
| 2220 | |
| 2221 | const char *msgType = NULL; |
| 2222 | switch (type) |
| 2223 | { |
| 2224 | case GL_DEBUG_TYPE_ERROR: msgType = "ERROR"; break; |
| 2225 | case GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR: msgType = "DEPRECATED_BEHAVIOR"; break; |
| 2226 | case GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR: msgType = "UNDEFINED_BEHAVIOR"; break; |
| 2227 | case GL_DEBUG_TYPE_PORTABILITY: msgType = "PORTABILITY"; break; |
| 2228 | case GL_DEBUG_TYPE_PERFORMANCE: msgType = "PERFORMANCE"; break; |
| 2229 | case GL_DEBUG_TYPE_MARKER: msgType = "MARKER"; break; |
| 2230 | case GL_DEBUG_TYPE_PUSH_GROUP: msgType = "PUSH_GROUP"; break; |
| 2231 | case GL_DEBUG_TYPE_POP_GROUP: msgType = "POP_GROUP"; break; |
| 2232 | case GL_DEBUG_TYPE_OTHER: msgType = "OTHER"; break; |
| 2233 | default: break; |
| 2234 | } |
| 2235 | |
| 2236 | const char *msgSeverity = "DEFAULT"; |
| 2237 | switch (severity) |
| 2238 | { |
| 2239 | case GL_DEBUG_SEVERITY_LOW: msgSeverity = "LOW"; break; |
| 2240 | case GL_DEBUG_SEVERITY_MEDIUM: msgSeverity = "MEDIUM"; break; |
| 2241 | case GL_DEBUG_SEVERITY_HIGH: msgSeverity = "HIGH"; break; |
| 2242 | case GL_DEBUG_SEVERITY_NOTIFICATION: msgSeverity = "NOTIFICATION"; break; |
| 2243 | default: break; |
| 2244 | } |
| 2245 | |
| 2246 | TRACELOG(RL_LOG_WARNING, "GL: OpenGL debug message: %s", message); |
| 2247 | TRACELOG(RL_LOG_WARNING, " > Type: %s", msgType); |
| 2248 | TRACELOG(RL_LOG_WARNING, " > Source = %s", msgSource); |
| 2249 | TRACELOG(RL_LOG_WARNING, " > Severity = %s", msgSeverity); |
| 2250 | } |
| 2251 | #endif |
| 2252 | |
| 2253 | //---------------------------------------------------------------------------------- |
| 2254 | // Module Functions Definition - rlgl functionality |
| 2255 | //---------------------------------------------------------------------------------- |
| 2256 | |
| 2257 | // Initialize rlgl: OpenGL extensions, default buffers/shaders/textures, OpenGL states |
| 2258 | void rlglInit(int width, int height) |
| 2259 | { |
| 2260 | isGpuReady = true; |
| 2261 | |
| 2262 | // Enable OpenGL debug context if requested (and supported) |
| 2263 | #if defined(GRAPHICS_API_OPENGL_43) && RLGL_ENABLE_OPENGL_DEBUG_CONTEXT |
| 2264 | if ((glDebugMessageCallback != NULL) && (glDebugMessageControl != NULL)) |
| 2265 | { |
| 2266 | glDebugMessageCallback(rlDebugMessageCallback, 0); |
| 2267 | // glDebugMessageControl(GL_DEBUG_SOURCE_API, GL_DEBUG_TYPE_ERROR, GL_DEBUG_SEVERITY_HIGH, 0, 0, GL_TRUE); |
| 2268 | |
| 2269 | // Debug context options: |
| 2270 | // - GL_DEBUG_OUTPUT - Faster version but not useful for breakpoints |
| 2271 | // - GL_DEBUG_OUTPUT_SYNCHRONUS - Callback is in sync with errors, so a breakpoint can be placed on the callback in order to get a stacktrace for the GL error |
| 2272 | glEnable(GL_DEBUG_OUTPUT); |
| 2273 | glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); |
| 2274 | } |
| 2275 | #endif |
| 2276 | |
| 2277 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2278 | // Init default white texture |
| 2279 | unsigned char pixels[4] = { 255, 255, 255, 255 }; // 1 pixel RGBA (4 bytes) |
| 2280 | RLGL.State.defaultTextureId = rlLoadTexture(pixels, 1, 1, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1); |
| 2281 | RLGL.State.currentTextureId = RLGL.State.defaultTextureId; |
| 2282 | |
| 2283 | if (RLGL.State.defaultTextureId != 0) TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Default texture loaded successfully", RLGL.State.defaultTextureId); |
| 2284 | else TRACELOG(RL_LOG_WARNING, "TEXTURE: Failed to load default texture"); |
| 2285 | |
| 2286 | // Init default Shader (customized for GL 3.3 and ES2) |
| 2287 | // Loaded: RLGL.State.defaultShaderId + RLGL.State.defaultShaderLocs |
| 2288 | rlLoadShaderDefault(); |
| 2289 | RLGL.State.currentShaderId = RLGL.State.defaultShaderId; |
| 2290 | RLGL.State.currentShaderLocs = RLGL.State.defaultShaderLocs; |
| 2291 | |
| 2292 | // Init default vertex arrays buffers |
| 2293 | // Simulate that the default shader has the location RL_SHADER_LOC_VERTEX_NORMAL to bind the normal buffer for the default render batch |
| 2294 | RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL] = RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL; |
| 2295 | RLGL.defaultBatch = rlLoadRenderBatch(RL_DEFAULT_BATCH_BUFFERS, RL_DEFAULT_BATCH_BUFFER_ELEMENTS); |
| 2296 | RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL] = -1; |
| 2297 | RLGL.currentBatch = &RLGL.defaultBatch; |
| 2298 | |
| 2299 | // Init stack matrices (emulating OpenGL 1.1) |
| 2300 | for (int i = 0; i < RL_MAX_MATRIX_STACK_SIZE; i++) RLGL.State.stack[i] = rlMatrixIdentity(); |
| 2301 | |
| 2302 | // Init internal matrices |
| 2303 | RLGL.State.transform = rlMatrixIdentity(); |
| 2304 | RLGL.State.projection = rlMatrixIdentity(); |
| 2305 | RLGL.State.modelview = rlMatrixIdentity(); |
| 2306 | RLGL.State.currentMatrix = &RLGL.State.modelview; |
| 2307 | #endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2 |
| 2308 | |
| 2309 | #if defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 2310 | // Initialize software renderer backend |
| 2311 | int result = swInit(width, height); |
| 2312 | if (result == 0) |
| 2313 | { |
| 2314 | TRACELOG(RL_LOG_ERROR, "RLSW: Software renderer initialization failed!"); |
| 2315 | exit(-1); |
| 2316 | } |
| 2317 | #endif |
| 2318 | |
| 2319 | // Initialize OpenGL default states |
| 2320 | //---------------------------------------------------------- |
| 2321 | // Init state: Depth test |
| 2322 | glDepthFunc(GL_LEQUAL); // Type of depth testing to apply |
| 2323 | glDisable(GL_DEPTH_TEST); // Disable depth testing for 2D (only used for 3D) |
| 2324 | |
| 2325 | // Init state: Blending mode |
| 2326 | glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Color blending function (how colors are mixed) |
| 2327 | glEnable(GL_BLEND); // Enable color blending (required to work with transparencies) |
| 2328 | |
| 2329 | // Init state: Culling |
| 2330 | // NOTE: All shapes/models triangles are drawn CCW |
| 2331 | glCullFace(GL_BACK); // Cull the back face (default) |
| 2332 | glFrontFace(GL_CCW); // Front face are defined counter clockwise (default) |
| 2333 | glEnable(GL_CULL_FACE); // Enable backface culling |
| 2334 | |
| 2335 | #if defined(GRAPHICS_API_OPENGL_11) |
| 2336 | // Init state: Color hints (deprecated in OpenGL 3.0+) |
| 2337 | glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Improve quality of color and texture coordinate interpolation |
| 2338 | glShadeModel(GL_SMOOTH); // Smooth shading between vertex (vertex colors interpolation) |
| 2339 | #endif |
| 2340 | #if defined(GRAPHICS_API_OPENGL_33) |
| 2341 | // Init state: Cubemap seamless |
| 2342 | glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); // Seamless cubemaps (not supported on OpenGL ES 2.0) |
| 2343 | #endif |
| 2344 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2345 | // Store screen size into global variables |
| 2346 | RLGL.State.framebufferWidth = width; |
| 2347 | RLGL.State.framebufferHeight = height; |
| 2348 | #endif |
| 2349 | |
| 2350 | // Init state: Color/Depth buffers clear |
| 2351 | glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set clear color (black) |
| 2352 | glClearDepth(1.0f); // Set clear depth value (default) |
| 2353 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers (depth buffer required for 3D) |
| 2354 | |
| 2355 | TRACELOG(RL_LOG_INFO, "RLGL: Default OpenGL state initialized successfully"); |
| 2356 | //---------------------------------------------------------- |
| 2357 | } |
| 2358 | |
| 2359 | // Vertex Buffer Object deinitialization (memory free) |
| 2360 | void rlglClose(void) |
| 2361 | { |
| 2362 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2363 | rlUnloadRenderBatch(RLGL.defaultBatch); |
| 2364 | |
| 2365 | rlUnloadShaderDefault(); // Unload default shader |
| 2366 | |
| 2367 | glDeleteTextures(1, &RLGL.State.defaultTextureId); // Unload default texture |
| 2368 | TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Default texture unloaded successfully", RLGL.State.defaultTextureId); |
| 2369 | #endif |
| 2370 | |
| 2371 | #if defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 2372 | swClose(); // Unload sofware renderer resources |
| 2373 | #endif |
| 2374 | isGpuReady = false; |
| 2375 | } |
| 2376 | |
| 2377 | // Load OpenGL extensions |
| 2378 | // NOTE: External loader function must be provided |
| 2379 | void rlLoadExtensions(void *loader) |
| 2380 | { |
| 2381 | #if defined(GRAPHICS_API_OPENGL_33) // Also defined for GRAPHICS_API_OPENGL_21 |
| 2382 | // NOTE: glad is generated and contains only required OpenGL 3.3 Core extensions (and lower versions) |
| 2383 | if (gladLoadGL((GLADloadfunc)loader) == 0) TRACELOG(RL_LOG_WARNING, "GLAD: Cannot load OpenGL extensions"); |
| 2384 | else TRACELOG(RL_LOG_INFO, "GLAD: OpenGL extensions loaded successfully"); |
| 2385 | |
| 2386 | // Get number of supported extensions |
| 2387 | GLint numExt = 0; |
| 2388 | glGetIntegerv(GL_NUM_EXTENSIONS, &numExt); |
| 2389 | TRACELOG(RL_LOG_INFO, "GL: Supported extensions count: %i", numExt); |
| 2390 | |
| 2391 | #if RLGL_SHOW_GL_DETAILS_INFO |
| 2392 | // Get supported extensions list |
| 2393 | // WARNING: glGetStringi() not available on OpenGL 2.1 |
| 2394 | TRACELOG(RL_LOG_INFO, "GL: OpenGL extensions:"); |
| 2395 | for (int i = 0; i < numExt; i++) TRACELOG(RL_LOG_INFO, " %s", glGetStringi(GL_EXTENSIONS, i)); |
| 2396 | #endif |
| 2397 | |
| 2398 | #if defined(GRAPHICS_API_OPENGL_21) |
| 2399 | // Register supported extensions flags |
| 2400 | // Optional OpenGL 2.1 extensions |
| 2401 | RLGL.ExtSupported.vao = GLAD_GL_ARB_vertex_array_object; |
| 2402 | RLGL.ExtSupported.instancing = (GLAD_GL_EXT_draw_instanced && GLAD_GL_ARB_instanced_arrays); |
| 2403 | RLGL.ExtSupported.texNPOT = GLAD_GL_ARB_texture_non_power_of_two; |
| 2404 | RLGL.ExtSupported.texFloat32 = GLAD_GL_ARB_texture_float; |
| 2405 | RLGL.ExtSupported.texFloat16 = GLAD_GL_ARB_texture_float; |
| 2406 | RLGL.ExtSupported.texDepth = GLAD_GL_ARB_depth_texture; |
| 2407 | RLGL.ExtSupported.maxDepthBits = 32; |
| 2408 | RLGL.ExtSupported.texAnisoFilter = GLAD_GL_EXT_texture_filter_anisotropic; |
| 2409 | RLGL.ExtSupported.texMirrorClamp = GLAD_GL_EXT_texture_mirror_clamp; |
| 2410 | #else |
| 2411 | // Register supported extensions flags |
| 2412 | // OpenGL 3.3 extensions supported by default (core) |
| 2413 | RLGL.ExtSupported.vao = true; |
| 2414 | RLGL.ExtSupported.instancing = true; |
| 2415 | RLGL.ExtSupported.texNPOT = true; |
| 2416 | RLGL.ExtSupported.texFloat32 = true; |
| 2417 | RLGL.ExtSupported.texFloat16 = true; |
| 2418 | RLGL.ExtSupported.texDepth = true; |
| 2419 | RLGL.ExtSupported.maxDepthBits = 32; |
| 2420 | RLGL.ExtSupported.texAnisoFilter = true; |
| 2421 | RLGL.ExtSupported.texMirrorClamp = true; |
| 2422 | #endif |
| 2423 | |
| 2424 | // Optional OpenGL 3.3 extensions |
| 2425 | RLGL.ExtSupported.texCompASTC = GLAD_GL_KHR_texture_compression_astc_hdr && GLAD_GL_KHR_texture_compression_astc_ldr; |
| 2426 | RLGL.ExtSupported.texCompDXT = GLAD_GL_EXT_texture_compression_s3tc; // Texture compression: DXT |
| 2427 | RLGL.ExtSupported.texCompETC2 = GLAD_GL_ARB_ES3_compatibility; // Texture compression: ETC2/EAC |
| 2428 | #if defined(GRAPHICS_API_OPENGL_43) |
| 2429 | RLGL.ExtSupported.computeShader = GLAD_GL_ARB_compute_shader; |
| 2430 | RLGL.ExtSupported.ssbo = GLAD_GL_ARB_shader_storage_buffer_object; |
| 2431 | #endif |
| 2432 | |
| 2433 | #endif // GRAPHICS_API_OPENGL_33 |
| 2434 | |
| 2435 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 2436 | // Register supported extensions flags |
| 2437 | // OpenGL ES 3.0 extensions supported by default (or it should be) |
| 2438 | RLGL.ExtSupported.vao = true; |
| 2439 | RLGL.ExtSupported.instancing = true; |
| 2440 | RLGL.ExtSupported.texNPOT = true; |
| 2441 | RLGL.ExtSupported.texFloat32 = true; |
| 2442 | RLGL.ExtSupported.texFloat16 = true; |
| 2443 | RLGL.ExtSupported.texDepth = true; |
| 2444 | RLGL.ExtSupported.texDepthWebGL = true; |
| 2445 | RLGL.ExtSupported.maxDepthBits = 24; |
| 2446 | RLGL.ExtSupported.texAnisoFilter = true; |
| 2447 | RLGL.ExtSupported.texMirrorClamp = true; |
| 2448 | // TODO: Check for additional OpenGL ES 3.0 supported extensions: |
| 2449 | //RLGL.ExtSupported.texCompDXT = true; |
| 2450 | //RLGL.ExtSupported.texCompETC1 = true; |
| 2451 | //RLGL.ExtSupported.texCompETC2 = true; |
| 2452 | //RLGL.ExtSupported.texCompPVRT = true; |
| 2453 | //RLGL.ExtSupported.texCompASTC = true; |
| 2454 | //RLGL.ExtSupported.maxAnisotropyLevel = true; |
| 2455 | //RLGL.ExtSupported.computeShader = true; |
| 2456 | //RLGL.ExtSupported.ssbo = true; |
| 2457 | |
| 2458 | #elif defined(GRAPHICS_API_OPENGL_ES2) |
| 2459 | |
| 2460 | #if defined(PLATFORM_DESKTOP_GLFW) || defined(PLATFORM_DESKTOP_SDL) |
| 2461 | // TODO: Support GLAD loader for OpenGL ES 3.0 |
| 2462 | if (gladLoadGLES2((GLADloadfunc)loader) == 0) TRACELOG(RL_LOG_WARNING, "GLAD: Cannot load OpenGL ES2.0 functions"); |
| 2463 | else TRACELOG(RL_LOG_INFO, "GLAD: OpenGL ES 2.0 loaded successfully"); |
| 2464 | #endif |
| 2465 | |
| 2466 | // Get supported extensions list |
| 2467 | GLint numExt = 0; |
| 2468 | const char **extList = (const char **)RL_CALLOC(512, sizeof(const char *)); // Allocate 512 strings pointers (2 KB) |
| 2469 | const char *extensions = (const char *)glGetString(GL_EXTENSIONS); // One big const string |
| 2470 | |
| 2471 | // NOTE: String duplication rquired because glGetString() returns a const string |
| 2472 | int extensionsLength = (int)strlen(extensions); // Get extensions string size in bytes |
| 2473 | char *extensionsDup = (char *)RL_CALLOC(extensionsLength + 1, sizeof(char)); // Allocate space for copy with additional EOL byte |
| 2474 | snprintf(extensionsDup, extensionsLength, "%s", extensions); |
| 2475 | extList[numExt] = extensionsDup; |
| 2476 | |
| 2477 | for (int i = 0; i < extensionsLength; i++) |
| 2478 | { |
| 2479 | if (extensionsDup[i] == ' ') |
| 2480 | { |
| 2481 | extensionsDup[i] = '\0'; |
| 2482 | numExt++; |
| 2483 | extList[numExt] = &extensionsDup[i + 1]; |
| 2484 | } |
| 2485 | } |
| 2486 | |
| 2487 | TRACELOG(RL_LOG_INFO, "GL: Supported extensions count: %i", numExt); |
| 2488 | |
| 2489 | #if RLGL_SHOW_GL_DETAILS_INFO |
| 2490 | TRACELOG(RL_LOG_INFO, "GL: OpenGL extensions:"); |
| 2491 | for (int i = 0; i < numExt; i++) TRACELOG(RL_LOG_INFO, " %s", extList[i]); |
| 2492 | #endif |
| 2493 | |
| 2494 | // Check required extensions |
| 2495 | for (int i = 0; i < numExt; i++) |
| 2496 | { |
| 2497 | // Check VAO support |
| 2498 | // NOTE: Only check on OpenGL ES, OpenGL 3.3 has VAO support as core feature |
| 2499 | if (strcmp(extList[i], (const char *)"GL_OES_vertex_array_object") == 0) |
| 2500 | { |
| 2501 | // The extension is supported by our hardware and driver, try to get related functions pointers |
| 2502 | // NOTE: emscripten does not support VAOs natively, it uses emulation and it reduces overall performance... |
| 2503 | glGenVertexArrays = (PFNGLGENVERTEXARRAYSOESPROC)((rlglLoadProc)loader)("glGenVertexArraysOES"); |
| 2504 | glBindVertexArray = (PFNGLBINDVERTEXARRAYOESPROC)((rlglLoadProc)loader)("glBindVertexArrayOES"); |
| 2505 | glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSOESPROC)((rlglLoadProc)loader)("glDeleteVertexArraysOES"); |
| 2506 | //glIsVertexArray = (PFNGLISVERTEXARRAYOESPROC)loader("glIsVertexArrayOES"); // NOTE: Fails in WebGL, omitted |
| 2507 | |
| 2508 | if ((glGenVertexArrays != NULL) && (glBindVertexArray != NULL) && (glDeleteVertexArrays != NULL)) RLGL.ExtSupported.vao = true; |
| 2509 | } |
| 2510 | |
| 2511 | // Check instanced rendering support |
| 2512 | if (strstr(extList[i], (const char *)"instanced_arrays") != NULL) // Broad check for instanced_arrays |
| 2513 | { |
| 2514 | // Specific check |
| 2515 | if (strcmp(extList[i], (const char *)"GL_ANGLE_instanced_arrays") == 0) // ANGLE |
| 2516 | { |
| 2517 | glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawArraysInstancedANGLE"); |
| 2518 | glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawElementsInstancedANGLE"); |
| 2519 | glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISOREXTPROC)((rlglLoadProc)loader)("glVertexAttribDivisorANGLE"); |
| 2520 | } |
| 2521 | else if (strcmp(extList[i], (const char *)"GL_EXT_instanced_arrays") == 0) // EXT |
| 2522 | { |
| 2523 | glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawArraysInstancedEXT"); |
| 2524 | glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawElementsInstancedEXT"); |
| 2525 | glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISOREXTPROC)((rlglLoadProc)loader)("glVertexAttribDivisorEXT"); |
| 2526 | } |
| 2527 | else if (strcmp(extList[i], (const char *)"GL_NV_instanced_arrays") == 0) // NVIDIA GLES |
| 2528 | { |
| 2529 | glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawArraysInstancedNV"); |
| 2530 | glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawElementsInstancedNV"); |
| 2531 | glVertexAttribDivisor = (PFNGLVERTEXATTRIBDIVISOREXTPROC)((rlglLoadProc)loader)("glVertexAttribDivisorNV"); |
| 2532 | } |
| 2533 | |
| 2534 | // The feature will only be marked as supported if the elements from GL_XXX_instanced_arrays are present |
| 2535 | if ((glDrawArraysInstanced != NULL) && (glDrawElementsInstanced != NULL) && (glVertexAttribDivisor != NULL)) RLGL.ExtSupported.instancing = true; |
| 2536 | } |
| 2537 | else if (strstr(extList[i], (const char *)"draw_instanced") != NULL) |
| 2538 | { |
| 2539 | // GL_ANGLE_draw_instanced doesn't exist |
| 2540 | if (strcmp(extList[i], (const char *)"GL_EXT_draw_instanced") == 0) |
| 2541 | { |
| 2542 | glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawArraysInstancedEXT"); |
| 2543 | glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawElementsInstancedEXT"); |
| 2544 | } |
| 2545 | else if (strcmp(extList[i], (const char *)"GL_NV_draw_instanced") == 0) |
| 2546 | { |
| 2547 | glDrawArraysInstanced = (PFNGLDRAWARRAYSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawArraysInstancedNV"); |
| 2548 | glDrawElementsInstanced = (PFNGLDRAWELEMENTSINSTANCEDEXTPROC)((rlglLoadProc)loader)("glDrawElementsInstancedNV"); |
| 2549 | } |
| 2550 | |
| 2551 | // But the functions will at least be loaded if only GL_XX_EXT_draw_instanced exist |
| 2552 | if ((glDrawArraysInstanced != NULL) && (glDrawElementsInstanced != NULL) && (glVertexAttribDivisor != NULL)) RLGL.ExtSupported.instancing = true; |
| 2553 | } |
| 2554 | |
| 2555 | // Check NPOT textures support |
| 2556 | // NOTE: Only check on OpenGL ES, OpenGL 3.3 has NPOT textures full support as core feature |
| 2557 | if (strcmp(extList[i], (const char *)"GL_OES_texture_npot") == 0) RLGL.ExtSupported.texNPOT = true; |
| 2558 | |
| 2559 | // Check texture float support |
| 2560 | if (strcmp(extList[i], (const char *)"GL_OES_texture_float") == 0) RLGL.ExtSupported.texFloat32 = true; |
| 2561 | if (strcmp(extList[i], (const char *)"GL_OES_texture_half_float") == 0) RLGL.ExtSupported.texFloat16 = true; |
| 2562 | |
| 2563 | // Check depth texture support |
| 2564 | if (strcmp(extList[i], (const char *)"GL_OES_depth_texture") == 0) RLGL.ExtSupported.texDepth = true; |
| 2565 | if (strcmp(extList[i], (const char *)"GL_WEBGL_depth_texture") == 0) RLGL.ExtSupported.texDepthWebGL = true; // WebGL requires unsized internal format |
| 2566 | if (RLGL.ExtSupported.texDepthWebGL) RLGL.ExtSupported.texDepth = true; |
| 2567 | |
| 2568 | if (strcmp(extList[i], (const char *)"GL_OES_depth24") == 0) RLGL.ExtSupported.maxDepthBits = 24; // Not available on WebGL |
| 2569 | if (strcmp(extList[i], (const char *)"GL_OES_depth32") == 0) RLGL.ExtSupported.maxDepthBits = 32; // Not available on WebGL |
| 2570 | |
| 2571 | // Check texture compression support: DXT |
| 2572 | if ((strcmp(extList[i], (const char *)"GL_EXT_texture_compression_s3tc") == 0) || |
| 2573 | (strcmp(extList[i], (const char *)"GL_WEBGL_compressed_texture_s3tc") == 0) || |
| 2574 | (strcmp(extList[i], (const char *)"GL_WEBKIT_WEBGL_compressed_texture_s3tc") == 0)) RLGL.ExtSupported.texCompDXT = true; |
| 2575 | |
| 2576 | // Check texture compression support: ETC1 |
| 2577 | if ((strcmp(extList[i], (const char *)"GL_OES_compressed_ETC1_RGB8_texture") == 0) || |
| 2578 | (strcmp(extList[i], (const char *)"GL_WEBGL_compressed_texture_etc1") == 0)) RLGL.ExtSupported.texCompETC1 = true; |
| 2579 | |
| 2580 | // Check texture compression support: ETC2/EAC |
| 2581 | if (strcmp(extList[i], (const char *)"GL_ARB_ES3_compatibility") == 0) RLGL.ExtSupported.texCompETC2 = true; |
| 2582 | |
| 2583 | // Check texture compression support: PVR |
| 2584 | if (strcmp(extList[i], (const char *)"GL_IMG_texture_compression_pvrtc") == 0) RLGL.ExtSupported.texCompPVRT = true; |
| 2585 | |
| 2586 | // Check texture compression support: ASTC |
| 2587 | if (strcmp(extList[i], (const char *)"GL_KHR_texture_compression_astc_hdr") == 0) RLGL.ExtSupported.texCompASTC = true; |
| 2588 | |
| 2589 | // Check anisotropic texture filter support |
| 2590 | if (strcmp(extList[i], (const char *)"GL_EXT_texture_filter_anisotropic") == 0) RLGL.ExtSupported.texAnisoFilter = true; |
| 2591 | |
| 2592 | // Check clamp mirror wrap mode support |
| 2593 | if (strcmp(extList[i], (const char *)"GL_EXT_texture_mirror_clamp") == 0) RLGL.ExtSupported.texMirrorClamp = true; |
| 2594 | } |
| 2595 | |
| 2596 | // Free extensions pointers |
| 2597 | RL_FREE(extList); |
| 2598 | RL_FREE(extensionsDup); // Duplicated string must be deallocated |
| 2599 | #endif // GRAPHICS_API_OPENGL_ES2 |
| 2600 | |
| 2601 | // Check OpenGL information and capabilities |
| 2602 | //------------------------------------------------------------------------------ |
| 2603 | // Show current OpenGL and GLSL version |
| 2604 | TRACELOG(RL_LOG_INFO, "GL: OpenGL device information:"); |
| 2605 | TRACELOG(RL_LOG_INFO, " > Vendor: %s", glGetString(GL_VENDOR)); |
| 2606 | TRACELOG(RL_LOG_INFO, " > Renderer: %s", glGetString(GL_RENDERER)); |
| 2607 | TRACELOG(RL_LOG_INFO, " > Version: %s", glGetString(GL_VERSION)); |
| 2608 | TRACELOG(RL_LOG_INFO, " > GLSL: %s", glGetString(GL_SHADING_LANGUAGE_VERSION)); |
| 2609 | |
| 2610 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2611 | RLGL.loader = (rlglLoadProc)loader; |
| 2612 | |
| 2613 | // NOTE: Anisotropy levels capability is an extension |
| 2614 | #ifndef GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT |
| 2615 | #define GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT 0x84FF |
| 2616 | #endif |
| 2617 | glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &RLGL.ExtSupported.maxAnisotropyLevel); |
| 2618 | |
| 2619 | #if RLGL_SHOW_GL_DETAILS_INFO |
| 2620 | // Show some OpenGL GPU capabilities |
| 2621 | TRACELOG(RL_LOG_INFO, "GL: OpenGL capabilities:"); |
| 2622 | GLint capability = 0; |
| 2623 | glGetIntegerv(GL_MAX_TEXTURE_SIZE, &capability); |
| 2624 | TRACELOG(RL_LOG_INFO, " GL_MAX_TEXTURE_SIZE: %i", capability); |
| 2625 | glGetIntegerv(GL_MAX_CUBE_MAP_TEXTURE_SIZE, &capability); |
| 2626 | TRACELOG(RL_LOG_INFO, " GL_MAX_CUBE_MAP_TEXTURE_SIZE: %i", capability); |
| 2627 | glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS, &capability); |
| 2628 | TRACELOG(RL_LOG_INFO, " GL_MAX_TEXTURE_IMAGE_UNITS: %i", capability); |
| 2629 | glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &capability); |
| 2630 | TRACELOG(RL_LOG_INFO, " GL_MAX_VERTEX_ATTRIBS: %i", capability); |
| 2631 | #if !defined(GRAPHICS_API_OPENGL_ES2) |
| 2632 | glGetIntegerv(GL_MAX_UNIFORM_BLOCK_SIZE, &capability); |
| 2633 | TRACELOG(RL_LOG_INFO, " GL_MAX_UNIFORM_BLOCK_SIZE: %i", capability); |
| 2634 | glGetIntegerv(GL_MAX_DRAW_BUFFERS, &capability); |
| 2635 | TRACELOG(RL_LOG_INFO, " GL_MAX_DRAW_BUFFERS: %i", capability); |
| 2636 | if (RLGL.ExtSupported.texAnisoFilter) TRACELOG(RL_LOG_INFO, " GL_MAX_TEXTURE_MAX_ANISOTROPY: %.0f", RLGL.ExtSupported.maxAnisotropyLevel); |
| 2637 | #endif |
| 2638 | glGetIntegerv(GL_NUM_COMPRESSED_TEXTURE_FORMATS, &capability); |
| 2639 | TRACELOG(RL_LOG_INFO, " GL_NUM_COMPRESSED_TEXTURE_FORMATS: %i", capability); |
| 2640 | GLint *compFormats = (GLint *)RL_CALLOC(capability, sizeof(GLint)); |
| 2641 | glGetIntegerv(GL_COMPRESSED_TEXTURE_FORMATS, compFormats); |
| 2642 | for (int i = 0; i < capability; i++) TRACELOG(RL_LOG_INFO, " %s", rlGetCompressedFormatName(compFormats[i])); |
| 2643 | RL_FREE(compFormats); |
| 2644 | |
| 2645 | #if defined(GRAPHICS_API_OPENGL_43) |
| 2646 | glGetIntegerv(GL_MAX_VERTEX_ATTRIB_BINDINGS, &capability); |
| 2647 | TRACELOG(RL_LOG_INFO, " GL_MAX_VERTEX_ATTRIB_BINDINGS: %i", capability); |
| 2648 | glGetIntegerv(GL_MAX_UNIFORM_LOCATIONS, &capability); |
| 2649 | TRACELOG(RL_LOG_INFO, " GL_MAX_UNIFORM_LOCATIONS: %i", capability); |
| 2650 | #endif |
| 2651 | |
| 2652 | #else // !RLGL_SHOW_GL_DETAILS_INFO |
| 2653 | |
| 2654 | // Show some basic info about GL supported features |
| 2655 | if (RLGL.ExtSupported.vao) TRACELOG(RL_LOG_INFO, "GL: VAO extension detected, VAO functions loaded successfully"); |
| 2656 | else TRACELOG(RL_LOG_WARNING, "GL: VAO extension not found, VAO not supported"); |
| 2657 | if (RLGL.ExtSupported.texNPOT) TRACELOG(RL_LOG_INFO, "GL: NPOT textures extension detected, full NPOT textures supported"); |
| 2658 | else TRACELOG(RL_LOG_WARNING, "GL: NPOT textures extension not found, limited NPOT support (no-mipmaps, no-repeat)"); |
| 2659 | if (RLGL.ExtSupported.texCompDXT) TRACELOG(RL_LOG_INFO, "GL: DXT compressed textures supported"); |
| 2660 | if (RLGL.ExtSupported.texCompETC1) TRACELOG(RL_LOG_INFO, "GL: ETC1 compressed textures supported"); |
| 2661 | if (RLGL.ExtSupported.texCompETC2) TRACELOG(RL_LOG_INFO, "GL: ETC2/EAC compressed textures supported"); |
| 2662 | if (RLGL.ExtSupported.texCompPVRT) TRACELOG(RL_LOG_INFO, "GL: PVRT compressed textures supported"); |
| 2663 | if (RLGL.ExtSupported.texCompASTC) TRACELOG(RL_LOG_INFO, "GL: ASTC compressed textures supported"); |
| 2664 | if (RLGL.ExtSupported.computeShader) TRACELOG(RL_LOG_INFO, "GL: Compute shaders supported"); |
| 2665 | if (RLGL.ExtSupported.ssbo) TRACELOG(RL_LOG_INFO, "GL: Shader storage buffer objects supported"); |
| 2666 | #endif |
| 2667 | |
| 2668 | #endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2 |
| 2669 | } |
| 2670 | |
| 2671 | // Get OpenGL procedure address |
| 2672 | void *rlGetProcAddress(const char *procName) |
| 2673 | { |
| 2674 | void *func = NULL; |
| 2675 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2676 | func = RLGL.loader(procName); |
| 2677 | #endif |
| 2678 | return func; |
| 2679 | } |
| 2680 | |
| 2681 | // Get current OpenGL version |
| 2682 | int rlGetVersion(void) |
| 2683 | { |
| 2684 | int glVersion = 0; |
| 2685 | |
| 2686 | #if defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 2687 | glVersion = RL_OPENGL_SOFTWARE; |
| 2688 | #elif defined(GRAPHICS_API_OPENGL_11) |
| 2689 | glVersion = RL_OPENGL_11; |
| 2690 | #endif |
| 2691 | #if defined(GRAPHICS_API_OPENGL_21) |
| 2692 | glVersion = RL_OPENGL_21; |
| 2693 | #elif defined(GRAPHICS_API_OPENGL_43) |
| 2694 | glVersion = RL_OPENGL_43; |
| 2695 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 2696 | glVersion = RL_OPENGL_33; |
| 2697 | #endif |
| 2698 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 2699 | glVersion = RL_OPENGL_ES_30; |
| 2700 | #elif defined(GRAPHICS_API_OPENGL_ES2) |
| 2701 | glVersion = RL_OPENGL_ES_20; |
| 2702 | #endif |
| 2703 | |
| 2704 | return glVersion; |
| 2705 | } |
| 2706 | |
| 2707 | // Set current framebuffer width |
| 2708 | void rlSetFramebufferWidth(int width) |
| 2709 | { |
| 2710 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2711 | RLGL.State.framebufferWidth = width; |
| 2712 | #endif |
| 2713 | } |
| 2714 | |
| 2715 | // Set current framebuffer height |
| 2716 | void rlSetFramebufferHeight(int height) |
| 2717 | { |
| 2718 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2719 | RLGL.State.framebufferHeight = height; |
| 2720 | #endif |
| 2721 | } |
| 2722 | |
| 2723 | // Get default framebuffer width |
| 2724 | int rlGetFramebufferWidth(void) |
| 2725 | { |
| 2726 | int width = 0; |
| 2727 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2728 | width = RLGL.State.framebufferWidth; |
| 2729 | #endif |
| 2730 | return width; |
| 2731 | } |
| 2732 | |
| 2733 | // Get default framebuffer height |
| 2734 | int rlGetFramebufferHeight(void) |
| 2735 | { |
| 2736 | int height = 0; |
| 2737 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2738 | height = RLGL.State.framebufferHeight; |
| 2739 | #endif |
| 2740 | return height; |
| 2741 | } |
| 2742 | |
| 2743 | // Get default internal texture (white texture) |
| 2744 | // NOTE: Default texture is a 1x1 pixel UNCOMPRESSED_R8G8B8A8 |
| 2745 | unsigned int rlGetTextureIdDefault(void) |
| 2746 | { |
| 2747 | unsigned int id = 0; |
| 2748 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2749 | id = RLGL.State.defaultTextureId; |
| 2750 | #endif |
| 2751 | return id; |
| 2752 | } |
| 2753 | |
| 2754 | // Get default shader id |
| 2755 | unsigned int rlGetShaderIdDefault(void) |
| 2756 | { |
| 2757 | unsigned int id = 0; |
| 2758 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2759 | id = RLGL.State.defaultShaderId; |
| 2760 | #endif |
| 2761 | return id; |
| 2762 | } |
| 2763 | |
| 2764 | // Get default shader locs |
| 2765 | int *rlGetShaderLocsDefault(void) |
| 2766 | { |
| 2767 | int *locs = NULL; |
| 2768 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2769 | locs = RLGL.State.defaultShaderLocs; |
| 2770 | #endif |
| 2771 | return locs; |
| 2772 | } |
| 2773 | |
| 2774 | // Render batch management |
| 2775 | //------------------------------------------------------------------------------------------------ |
| 2776 | // Load render batch |
| 2777 | rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements) |
| 2778 | { |
| 2779 | rlRenderBatch batch = { 0 }; |
| 2780 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return batch; } |
| 2781 | |
| 2782 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2783 | // Initialize CPU (RAM) vertex buffers (position, texcoord, color data and indexes) |
| 2784 | //-------------------------------------------------------------------------------------------- |
| 2785 | batch.vertexBuffer = (rlVertexBuffer *)RL_CALLOC(numBuffers, sizeof(rlVertexBuffer)); |
| 2786 | |
| 2787 | for (int i = 0; i < numBuffers; i++) |
| 2788 | { |
| 2789 | batch.vertexBuffer[i].elementCount = bufferElements; |
| 2790 | |
| 2791 | batch.vertexBuffer[i].vertices = (float *)RL_CALLOC(bufferElements*3*4, sizeof(float)); // 3 float by vertex, 4 vertex by quad |
| 2792 | batch.vertexBuffer[i].texcoords = (float *)RL_CALLOC(bufferElements*2*4, sizeof(float)); // 2 float by texcoord, 4 texcoord by quad |
| 2793 | batch.vertexBuffer[i].normals = (float *)RL_CALLOC(bufferElements*3*4, sizeof(float)); // 3 float by vertex, 4 vertex by quad |
| 2794 | batch.vertexBuffer[i].colors = (unsigned char *)RL_CALLOC(bufferElements*4*4, sizeof(unsigned char)); // 4 float by color, 4 colors by quad |
| 2795 | #if defined(GRAPHICS_API_OPENGL_33) |
| 2796 | batch.vertexBuffer[i].indices = (unsigned int *)RL_CALLOC(bufferElements*6, sizeof(unsigned int)); // 6 int by quad (indices) |
| 2797 | #endif |
| 2798 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 2799 | batch.vertexBuffer[i].indices = (unsigned short *)RL_CALLOC(bufferElements*6, sizeof(unsigned short)); // 6 int by quad (indices) |
| 2800 | #endif |
| 2801 | |
| 2802 | for (int j = 0; j < (3*4*bufferElements); j++) batch.vertexBuffer[i].vertices[j] = 0.0f; |
| 2803 | for (int j = 0; j < (2*4*bufferElements); j++) batch.vertexBuffer[i].texcoords[j] = 0.0f; |
| 2804 | for (int j = 0; j < (3*4*bufferElements); j++) batch.vertexBuffer[i].normals[j] = 0.0f; |
| 2805 | for (int j = 0; j < (4*4*bufferElements); j++) batch.vertexBuffer[i].colors[j] = 0; |
| 2806 | |
| 2807 | int k = 0; |
| 2808 | |
| 2809 | // Indices can be initialized right now |
| 2810 | for (int j = 0; j < (6*bufferElements); j += 6) |
| 2811 | { |
| 2812 | batch.vertexBuffer[i].indices[j] = 4*k; |
| 2813 | batch.vertexBuffer[i].indices[j + 1] = 4*k + 1; |
| 2814 | batch.vertexBuffer[i].indices[j + 2] = 4*k + 2; |
| 2815 | batch.vertexBuffer[i].indices[j + 3] = 4*k; |
| 2816 | batch.vertexBuffer[i].indices[j + 4] = 4*k + 2; |
| 2817 | batch.vertexBuffer[i].indices[j + 5] = 4*k + 3; |
| 2818 | |
| 2819 | k++; |
| 2820 | } |
| 2821 | |
| 2822 | RLGL.State.vertexCounter = 0; |
| 2823 | } |
| 2824 | |
| 2825 | TRACELOG(RL_LOG_INFO, "RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)"); |
| 2826 | //-------------------------------------------------------------------------------------------- |
| 2827 | |
| 2828 | // Upload to GPU (VRAM) vertex data and initialize VAOs/VBOs |
| 2829 | //-------------------------------------------------------------------------------------------- |
| 2830 | for (int i = 0; i < numBuffers; i++) |
| 2831 | { |
| 2832 | if (RLGL.ExtSupported.vao) |
| 2833 | { |
| 2834 | // Initialize Quads VAO |
| 2835 | glGenVertexArrays(1, &batch.vertexBuffer[i].vaoId); |
| 2836 | glBindVertexArray(batch.vertexBuffer[i].vaoId); |
| 2837 | } |
| 2838 | |
| 2839 | // Quads - Vertex buffers binding and attributes enable |
| 2840 | // Vertex position buffer (shader-location = 0) |
| 2841 | glGenBuffers(1, &batch.vertexBuffer[i].vboId[0]); |
| 2842 | glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[0]); |
| 2843 | glBufferData(GL_ARRAY_BUFFER, bufferElements*3*4*sizeof(float), batch.vertexBuffer[i].vertices, GL_DYNAMIC_DRAW); |
| 2844 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION]); |
| 2845 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, 0, 0, 0); |
| 2846 | |
| 2847 | // Vertex texcoord buffer (shader-location = 1) |
| 2848 | glGenBuffers(1, &batch.vertexBuffer[i].vboId[1]); |
| 2849 | glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[1]); |
| 2850 | glBufferData(GL_ARRAY_BUFFER, bufferElements*2*4*sizeof(float), batch.vertexBuffer[i].texcoords, GL_DYNAMIC_DRAW); |
| 2851 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01]); |
| 2852 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01], 2, GL_FLOAT, 0, 0, 0); |
| 2853 | |
| 2854 | // Vertex normal buffer (shader-location = 2) |
| 2855 | glGenBuffers(1, &batch.vertexBuffer[i].vboId[2]); |
| 2856 | glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[2]); |
| 2857 | glBufferData(GL_ARRAY_BUFFER, bufferElements*3*4*sizeof(float), batch.vertexBuffer[i].normals, GL_DYNAMIC_DRAW); |
| 2858 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL]); |
| 2859 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL], 3, GL_FLOAT, 0, 0, 0); |
| 2860 | |
| 2861 | // Vertex color buffer (shader-location = 3) |
| 2862 | glGenBuffers(1, &batch.vertexBuffer[i].vboId[3]); |
| 2863 | glBindBuffer(GL_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[3]); |
| 2864 | glBufferData(GL_ARRAY_BUFFER, bufferElements*4*4*sizeof(unsigned char), batch.vertexBuffer[i].colors, GL_DYNAMIC_DRAW); |
| 2865 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR]); |
| 2866 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR], 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); |
| 2867 | |
| 2868 | // Fill index buffer |
| 2869 | glGenBuffers(1, &batch.vertexBuffer[i].vboId[4]); |
| 2870 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch.vertexBuffer[i].vboId[4]); |
| 2871 | #if defined(GRAPHICS_API_OPENGL_33) |
| 2872 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferElements*6*sizeof(int), batch.vertexBuffer[i].indices, GL_STATIC_DRAW); |
| 2873 | #endif |
| 2874 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 2875 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufferElements*6*sizeof(short), batch.vertexBuffer[i].indices, GL_STATIC_DRAW); |
| 2876 | #endif |
| 2877 | } |
| 2878 | |
| 2879 | TRACELOG(RL_LOG_INFO, "RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)"); |
| 2880 | |
| 2881 | // Unbind the current VAO |
| 2882 | if (RLGL.ExtSupported.vao) glBindVertexArray(0); |
| 2883 | //-------------------------------------------------------------------------------------------- |
| 2884 | |
| 2885 | // Init draw calls tracking system |
| 2886 | //-------------------------------------------------------------------------------------------- |
| 2887 | batch.draws = (rlDrawCall *)RL_CALLOC(RL_DEFAULT_BATCH_DRAWCALLS, sizeof(rlDrawCall)); |
| 2888 | |
| 2889 | for (int i = 0; i < RL_DEFAULT_BATCH_DRAWCALLS; i++) |
| 2890 | { |
| 2891 | batch.draws[i].mode = RL_QUADS; |
| 2892 | batch.draws[i].vertexCount = 0; |
| 2893 | batch.draws[i].vertexAlignment = 0; |
| 2894 | //batch.draws[i].vaoId = 0; |
| 2895 | //batch.draws[i].shaderId = 0; |
| 2896 | batch.draws[i].textureId = RLGL.State.defaultTextureId; |
| 2897 | //batch.draws[i].RLGL.State.projection = rlMatrixIdentity(); |
| 2898 | //batch.draws[i].RLGL.State.modelview = rlMatrixIdentity(); |
| 2899 | } |
| 2900 | |
| 2901 | batch.bufferCount = numBuffers; // Record buffer count |
| 2902 | batch.drawCounter = 1; // Reset draws counter |
| 2903 | batch.currentDepth = -1.0f; // Reset depth value |
| 2904 | //-------------------------------------------------------------------------------------------- |
| 2905 | #endif |
| 2906 | |
| 2907 | return batch; |
| 2908 | } |
| 2909 | |
| 2910 | // Unload default internal buffers vertex data from CPU and GPU |
| 2911 | void rlUnloadRenderBatch(rlRenderBatch batch) |
| 2912 | { |
| 2913 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2914 | // Unbind everything |
| 2915 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 2916 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 2917 | |
| 2918 | // Unload all vertex buffers data |
| 2919 | for (int i = 0; i < batch.bufferCount; i++) |
| 2920 | { |
| 2921 | // Unbind VAO attribs data |
| 2922 | if (RLGL.ExtSupported.vao) |
| 2923 | { |
| 2924 | glBindVertexArray(batch.vertexBuffer[i].vaoId); |
| 2925 | glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION); |
| 2926 | glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD); |
| 2927 | glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL); |
| 2928 | glDisableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR); |
| 2929 | glBindVertexArray(0); |
| 2930 | } |
| 2931 | |
| 2932 | // Delete VBOs from GPU (VRAM) |
| 2933 | glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[0]); |
| 2934 | glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[1]); |
| 2935 | glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[2]); |
| 2936 | glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[3]); |
| 2937 | glDeleteBuffers(1, &batch.vertexBuffer[i].vboId[4]); |
| 2938 | |
| 2939 | // Delete VAOs from GPU (VRAM) |
| 2940 | if (RLGL.ExtSupported.vao) glDeleteVertexArrays(1, &batch.vertexBuffer[i].vaoId); |
| 2941 | |
| 2942 | // Free vertex arrays memory from CPU (RAM) |
| 2943 | RL_FREE(batch.vertexBuffer[i].vertices); |
| 2944 | RL_FREE(batch.vertexBuffer[i].texcoords); |
| 2945 | RL_FREE(batch.vertexBuffer[i].normals); |
| 2946 | RL_FREE(batch.vertexBuffer[i].colors); |
| 2947 | RL_FREE(batch.vertexBuffer[i].indices); |
| 2948 | } |
| 2949 | |
| 2950 | // Unload arrays |
| 2951 | RL_FREE(batch.vertexBuffer); |
| 2952 | RL_FREE(batch.draws); |
| 2953 | #endif |
| 2954 | } |
| 2955 | |
| 2956 | // Draw render batch |
| 2957 | // NOTE: Batch is reseted and current buffer is updated (for multi-buffer config) |
| 2958 | void rlDrawRenderBatch(rlRenderBatch *batch) |
| 2959 | { |
| 2960 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 2961 | // Update batch vertex buffers |
| 2962 | //------------------------------------------------------------------------------------------------------------ |
| 2963 | // NOTE: If there is not vertex data, buffers doesn't need to be updated (vertexCount > 0) |
| 2964 | if (RLGL.State.vertexCounter > 0) |
| 2965 | { |
| 2966 | // Activate elements VAO |
| 2967 | if (RLGL.ExtSupported.vao) glBindVertexArray(batch->vertexBuffer[batch->currentBuffer].vaoId); |
| 2968 | |
| 2969 | // NOTE: If no data changed on the CPU arrays there is no need to re-upload data to GPU, |
| 2970 | // a flag can be used to detect changes but it would imply keeping a copy buffer and memcmp() both |
| 2971 | // It was tested and the gain seemed to be minimal or nothing at all, probably driver already takes care of that |
| 2972 | |
| 2973 | // Vertex positions buffer |
| 2974 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[0]); |
| 2975 | glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*3*sizeof(float), batch->vertexBuffer[batch->currentBuffer].vertices); |
| 2976 | //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].vertices, GL_DYNAMIC_DRAW); // Update all buffer |
| 2977 | |
| 2978 | // Texture coordinates buffer |
| 2979 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[1]); |
| 2980 | glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*2*sizeof(float), batch->vertexBuffer[batch->currentBuffer].texcoords); |
| 2981 | //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].texcoords, GL_DYNAMIC_DRAW); // Update all buffer |
| 2982 | |
| 2983 | // Normals buffer |
| 2984 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[2]); |
| 2985 | glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*3*sizeof(float), batch->vertexBuffer[batch->currentBuffer].normals); |
| 2986 | //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].normals, GL_DYNAMIC_DRAW); // Update all buffer |
| 2987 | |
| 2988 | // Colors buffer |
| 2989 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[3]); |
| 2990 | glBufferSubData(GL_ARRAY_BUFFER, 0, RLGL.State.vertexCounter*4*sizeof(unsigned char), batch->vertexBuffer[batch->currentBuffer].colors); |
| 2991 | //glBufferData(GL_ARRAY_BUFFER, sizeof(float)*4*4*batch->vertexBuffer[batch->currentBuffer].elementCount, batch->vertexBuffer[batch->currentBuffer].colors, GL_DYNAMIC_DRAW); // Update all buffer |
| 2992 | |
| 2993 | // NOTE: glMapBuffer() causes sync issue |
| 2994 | // If GPU is working with this buffer, glMapBuffer() will wait(stall) until GPU to finish its job |
| 2995 | // To avoid waiting (idle), glBufferData() can be called first with NULL pointer before glMapBuffer() |
| 2996 | // Doing that, the previous data in PBO will be discarded and glMapBuffer() returns a new |
| 2997 | // allocated pointer immediately even if GPU is still working with the previous data |
| 2998 | |
| 2999 | // Another option: map the buffer object into client's memory |
| 3000 | //batch->vertexBuffer[batch->currentBuffer].vertices = (float *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); |
| 3001 | //if (batch->vertexBuffer[batch->currentBuffer].vertices) |
| 3002 | //{ |
| 3003 | // Update vertex data |
| 3004 | //} |
| 3005 | //glUnmapBuffer(GL_ARRAY_BUFFER); |
| 3006 | |
| 3007 | // Unbind the current VAO |
| 3008 | if (RLGL.ExtSupported.vao) glBindVertexArray(0); |
| 3009 | } |
| 3010 | //------------------------------------------------------------------------------------------------------------ |
| 3011 | |
| 3012 | // Draw batch vertex buffers (considering VR stereo if required) |
| 3013 | //------------------------------------------------------------------------------------------------------------ |
| 3014 | Matrix matProjection = RLGL.State.projection; |
| 3015 | Matrix matModelView = RLGL.State.modelview; |
| 3016 | |
| 3017 | int eyeCount = 1; |
| 3018 | if (RLGL.State.stereoRender) eyeCount = 2; |
| 3019 | |
| 3020 | for (int eye = 0; eye < eyeCount; eye++) |
| 3021 | { |
| 3022 | if (eyeCount == 2) |
| 3023 | { |
| 3024 | // Setup current eye viewport (half screen width) |
| 3025 | rlViewport(eye*RLGL.State.framebufferWidth/2, 0, RLGL.State.framebufferWidth/2, RLGL.State.framebufferHeight); |
| 3026 | |
| 3027 | // Set current eye view offset to modelview matrix |
| 3028 | rlSetMatrixModelview(rlMatrixMultiply(matModelView, RLGL.State.viewOffsetStereo[eye])); |
| 3029 | // Set current eye projection matrix |
| 3030 | rlSetMatrixProjection(RLGL.State.projectionStereo[eye]); |
| 3031 | } |
| 3032 | |
| 3033 | // Draw buffers |
| 3034 | if (RLGL.State.vertexCounter > 0) |
| 3035 | { |
| 3036 | // Set current shader and upload current MVP matrix |
| 3037 | glUseProgram(RLGL.State.currentShaderId); |
| 3038 | |
| 3039 | // Create modelview-projection matrix and upload to shader |
| 3040 | Matrix matMVP = rlMatrixMultiply(RLGL.State.modelview, RLGL.State.projection); |
| 3041 | glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_MVP], 1, false, rlMatrixToFloat(matMVP)); |
| 3042 | |
| 3043 | if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_PROJECTION] != -1) |
| 3044 | { |
| 3045 | glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_PROJECTION], 1, false, rlMatrixToFloat(RLGL.State.projection)); |
| 3046 | } |
| 3047 | |
| 3048 | // WARNING: For the following setup of the view, model, and normal matrices, it is expected that |
| 3049 | // transformations and rendering occur between rlPushMatrix() and rlPopMatrix() |
| 3050 | |
| 3051 | if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_VIEW] != -1) |
| 3052 | { |
| 3053 | glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_VIEW], 1, false, rlMatrixToFloat(RLGL.State.modelview)); |
| 3054 | } |
| 3055 | |
| 3056 | if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_MODEL] != -1) |
| 3057 | { |
| 3058 | glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_MODEL], 1, false, rlMatrixToFloat(RLGL.State.transform)); |
| 3059 | } |
| 3060 | |
| 3061 | if (RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_NORMAL] != -1) |
| 3062 | { |
| 3063 | glUniformMatrix4fv(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MATRIX_NORMAL], 1, false, rlMatrixToFloat(rlMatrixTranspose(rlMatrixInvert(RLGL.State.transform)))); |
| 3064 | } |
| 3065 | |
| 3066 | if (RLGL.ExtSupported.vao) glBindVertexArray(batch->vertexBuffer[batch->currentBuffer].vaoId); |
| 3067 | else |
| 3068 | { |
| 3069 | // Bind vertex attrib: position (shader-location = 0) |
| 3070 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[0]); |
| 3071 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, 0, 0, 0); |
| 3072 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_POSITION]); |
| 3073 | |
| 3074 | // Bind vertex attrib: texcoord (shader-location = 1) |
| 3075 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[1]); |
| 3076 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01], 2, GL_FLOAT, 0, 0, 0); |
| 3077 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01]); |
| 3078 | |
| 3079 | // Bind vertex attrib: normal (shader-location = 2) |
| 3080 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[2]); |
| 3081 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL], 3, GL_FLOAT, 0, 0, 0); |
| 3082 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_NORMAL]); |
| 3083 | |
| 3084 | // Bind vertex attrib: color (shader-location = 3) |
| 3085 | glBindBuffer(GL_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[3]); |
| 3086 | glVertexAttribPointer(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR], 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0); |
| 3087 | glEnableVertexAttribArray(RLGL.State.currentShaderLocs[RL_SHADER_LOC_VERTEX_COLOR]); |
| 3088 | |
| 3089 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, batch->vertexBuffer[batch->currentBuffer].vboId[4]); |
| 3090 | } |
| 3091 | |
| 3092 | // Setup some default shader values |
| 3093 | glUniform4f(RLGL.State.currentShaderLocs[RL_SHADER_LOC_COLOR_DIFFUSE], 1.0f, 1.0f, 1.0f, 1.0f); |
| 3094 | glUniform1i(RLGL.State.currentShaderLocs[RL_SHADER_LOC_MAP_DIFFUSE], 0); // Active default sampler2D: texture0 |
| 3095 | |
| 3096 | // Activate additional sampler textures |
| 3097 | // Those additional textures will be common for all draw calls of the batch |
| 3098 | for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) |
| 3099 | { |
| 3100 | if (RLGL.State.activeTextureId[i] > 0) |
| 3101 | { |
| 3102 | glActiveTexture(GL_TEXTURE0 + 1 + i); |
| 3103 | glBindTexture(GL_TEXTURE_2D, RLGL.State.activeTextureId[i]); |
| 3104 | } |
| 3105 | } |
| 3106 | |
| 3107 | // Activate default sampler2D texture0 (one texture is always active for default batch shader) |
| 3108 | // NOTE: Batch system accumulates calls by texture0 changes, additional textures are enabled for all the draw calls |
| 3109 | glActiveTexture(GL_TEXTURE0); |
| 3110 | |
| 3111 | for (int i = 0, vertexOffset = 0; i < batch->drawCounter; i++) |
| 3112 | { |
| 3113 | // Bind current draw call texture, activated as GL_TEXTURE0 and bound to sampler2D texture0 by default |
| 3114 | glBindTexture(GL_TEXTURE_2D, batch->draws[i].textureId); |
| 3115 | |
| 3116 | if ((batch->draws[i].mode == RL_LINES) || (batch->draws[i].mode == RL_TRIANGLES)) glDrawArrays(batch->draws[i].mode, vertexOffset, batch->draws[i].vertexCount); |
| 3117 | else |
| 3118 | { |
| 3119 | #if defined(GRAPHICS_API_OPENGL_33) |
| 3120 | // The number of indices to be processed needs to be defined: elementCount*6 |
| 3121 | // NOTE: The final parameter tells the GPU the offset in bytes from the |
| 3122 | // start of the index buffer to the location of the first index to process |
| 3123 | glDrawElements(GL_TRIANGLES, batch->draws[i].vertexCount/4*6, GL_UNSIGNED_INT, (GLvoid *)(vertexOffset/4*6*sizeof(GLuint))); |
| 3124 | #endif |
| 3125 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 3126 | glDrawElements(GL_TRIANGLES, batch->draws[i].vertexCount/4*6, GL_UNSIGNED_SHORT, (GLvoid *)(vertexOffset/4*6*sizeof(GLushort))); |
| 3127 | #endif |
| 3128 | } |
| 3129 | |
| 3130 | vertexOffset += (batch->draws[i].vertexCount + batch->draws[i].vertexAlignment); |
| 3131 | } |
| 3132 | |
| 3133 | if (!RLGL.ExtSupported.vao) |
| 3134 | { |
| 3135 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 3136 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 3137 | } |
| 3138 | |
| 3139 | glBindTexture(GL_TEXTURE_2D, 0); // Unbind textures |
| 3140 | } |
| 3141 | |
| 3142 | if (RLGL.ExtSupported.vao) glBindVertexArray(0); // Unbind VAO |
| 3143 | |
| 3144 | glUseProgram(0); // Unbind shader program |
| 3145 | } |
| 3146 | |
| 3147 | // Restore viewport to default measures |
| 3148 | if (eyeCount == 2) rlViewport(0, 0, RLGL.State.framebufferWidth, RLGL.State.framebufferHeight); |
| 3149 | //------------------------------------------------------------------------------------------------------------ |
| 3150 | |
| 3151 | // Reset batch buffers |
| 3152 | //------------------------------------------------------------------------------------------------------------ |
| 3153 | // Reset vertex counter for next frame |
| 3154 | RLGL.State.vertexCounter = 0; |
| 3155 | |
| 3156 | // Reset depth for next draw |
| 3157 | batch->currentDepth = -1.0f; |
| 3158 | |
| 3159 | // Restore projection/modelview matrices |
| 3160 | RLGL.State.projection = matProjection; |
| 3161 | RLGL.State.modelview = matModelView; |
| 3162 | |
| 3163 | // Reset RLGL.currentBatch->draws array |
| 3164 | for (int i = 0; i < RL_DEFAULT_BATCH_DRAWCALLS; i++) |
| 3165 | { |
| 3166 | batch->draws[i].mode = RL_QUADS; |
| 3167 | batch->draws[i].vertexCount = 0; |
| 3168 | batch->draws[i].textureId = RLGL.State.defaultTextureId; |
| 3169 | } |
| 3170 | |
| 3171 | // Reset active texture units for next batch |
| 3172 | for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) RLGL.State.activeTextureId[i] = 0; |
| 3173 | |
| 3174 | // Reset draws counter to one draw for the batch |
| 3175 | batch->drawCounter = 1; |
| 3176 | //------------------------------------------------------------------------------------------------------------ |
| 3177 | |
| 3178 | // Change to next buffer in the list (in case of multi-buffering) |
| 3179 | batch->currentBuffer++; |
| 3180 | if (batch->currentBuffer >= batch->bufferCount) batch->currentBuffer = 0; |
| 3181 | #endif |
| 3182 | } |
| 3183 | |
| 3184 | // Set the active render batch for rlgl |
| 3185 | void rlSetRenderBatchActive(rlRenderBatch *batch) |
| 3186 | { |
| 3187 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3188 | rlDrawRenderBatch(RLGL.currentBatch); |
| 3189 | |
| 3190 | if (batch != NULL) RLGL.currentBatch = batch; |
| 3191 | else RLGL.currentBatch = &RLGL.defaultBatch; |
| 3192 | #endif |
| 3193 | } |
| 3194 | |
| 3195 | // Update and draw internal render batch |
| 3196 | void rlDrawRenderBatchActive(void) |
| 3197 | { |
| 3198 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3199 | rlDrawRenderBatch(RLGL.currentBatch); // NOTE: Stereo rendering is checked inside |
| 3200 | #endif |
| 3201 | } |
| 3202 | |
| 3203 | // Check internal buffer overflow for a given number of vertex |
| 3204 | // and force a rlRenderBatch draw call if required |
| 3205 | bool rlCheckRenderBatchLimit(int vCount) |
| 3206 | { |
| 3207 | bool overflow = false; |
| 3208 | |
| 3209 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3210 | if ((RLGL.State.vertexCounter + vCount) >= |
| 3211 | (RLGL.currentBatch->vertexBuffer[RLGL.currentBatch->currentBuffer].elementCount*4)) |
| 3212 | { |
| 3213 | overflow = true; |
| 3214 | |
| 3215 | // Store current primitive drawing mode and texture id |
| 3216 | int currentMode = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode; |
| 3217 | int currentTexture = RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId; |
| 3218 | |
| 3219 | rlDrawRenderBatch(RLGL.currentBatch); // NOTE: Stereo rendering is checked inside |
| 3220 | |
| 3221 | // Restore state of last batch so new vertices can be added |
| 3222 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].mode = currentMode; |
| 3223 | RLGL.currentBatch->draws[RLGL.currentBatch->drawCounter - 1].textureId = currentTexture; |
| 3224 | } |
| 3225 | #endif |
| 3226 | |
| 3227 | return overflow; |
| 3228 | } |
| 3229 | |
| 3230 | // Textures data management |
| 3231 | //----------------------------------------------------------------------------------------- |
| 3232 | // Convert image data to OpenGL texture (returns OpenGL valid Id) |
| 3233 | unsigned int rlLoadTexture(const void *data, int width, int height, int format, int mipmapCount) |
| 3234 | { |
| 3235 | unsigned int id = 0; |
| 3236 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return id; } |
| 3237 | |
| 3238 | glBindTexture(GL_TEXTURE_2D, 0); // Free any old binding |
| 3239 | |
| 3240 | // Check texture format support by OpenGL 1.1 (compressed textures not supported) |
| 3241 | #if defined(GRAPHICS_API_OPENGL_11) |
| 3242 | if (format >= RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) |
| 3243 | { |
| 3244 | TRACELOG(RL_LOG_WARNING, "GL: OpenGL 1.1 does not support GPU compressed texture formats"); |
| 3245 | return id; |
| 3246 | } |
| 3247 | #else |
| 3248 | if ((!RLGL.ExtSupported.texCompDXT) && ((format == RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) || (format == RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA) || |
| 3249 | (format == RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA) || (format == RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA))) |
| 3250 | { |
| 3251 | TRACELOG(RL_LOG_WARNING, "GL: DXT compressed texture format not supported"); |
| 3252 | return id; |
| 3253 | } |
| 3254 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3255 | if ((!RLGL.ExtSupported.texCompETC1) && (format == RL_PIXELFORMAT_COMPRESSED_ETC1_RGB)) |
| 3256 | { |
| 3257 | TRACELOG(RL_LOG_WARNING, "GL: ETC1 compressed texture format not supported"); |
| 3258 | return id; |
| 3259 | } |
| 3260 | |
| 3261 | if ((!RLGL.ExtSupported.texCompETC2) && ((format == RL_PIXELFORMAT_COMPRESSED_ETC2_RGB) || (format == RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA))) |
| 3262 | { |
| 3263 | TRACELOG(RL_LOG_WARNING, "GL: ETC2 compressed texture format not supported"); |
| 3264 | return id; |
| 3265 | } |
| 3266 | |
| 3267 | if ((!RLGL.ExtSupported.texCompPVRT) && ((format == RL_PIXELFORMAT_COMPRESSED_PVRT_RGB) || (format == RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA))) |
| 3268 | { |
| 3269 | TRACELOG(RL_LOG_WARNING, "GL: PVRT compressed texture format not supported"); |
| 3270 | return id; |
| 3271 | } |
| 3272 | |
| 3273 | if ((!RLGL.ExtSupported.texCompASTC) && ((format == RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA) || (format == RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA))) |
| 3274 | { |
| 3275 | TRACELOG(RL_LOG_WARNING, "GL: ASTC compressed texture format not supported"); |
| 3276 | return id; |
| 3277 | } |
| 3278 | #endif |
| 3279 | #endif // GRAPHICS_API_OPENGL_11 |
| 3280 | |
| 3281 | glPixelStorei(GL_UNPACK_ALIGNMENT, 1); |
| 3282 | |
| 3283 | glGenTextures(1, &id); // Generate texture id |
| 3284 | |
| 3285 | glBindTexture(GL_TEXTURE_2D, id); |
| 3286 | |
| 3287 | int mipWidth = width; |
| 3288 | int mipHeight = height; |
| 3289 | int mipOffset = 0; // Mipmap data offset, only used for tracelog |
| 3290 | (void)mipOffset; // Used to avoid gcc warnings about unused variable |
| 3291 | |
| 3292 | // NOTE: Added pointer math separately from function to avoid UBSAN complaining |
| 3293 | unsigned char *dataPtr = NULL; |
| 3294 | if (data != NULL) dataPtr = (unsigned char *)data; |
| 3295 | |
| 3296 | // Load the different mipmap levels |
| 3297 | for (int i = 0; i < mipmapCount; i++) |
| 3298 | { |
| 3299 | unsigned int mipSize = rlGetPixelDataSize(mipWidth, mipHeight, format); |
| 3300 | |
| 3301 | unsigned int glInternalFormat, glFormat, glType; |
| 3302 | rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); |
| 3303 | |
| 3304 | TRACELOG(RL_LOG_DEBUG, "TEXTURE: Load mipmap level %i (%i x %i), size: %i, offset: %i", i, mipWidth, mipHeight, mipSize, mipOffset); |
| 3305 | |
| 3306 | if (glInternalFormat != 0) |
| 3307 | { |
| 3308 | if (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) glTexImage2D(GL_TEXTURE_2D, i, glInternalFormat, mipWidth, mipHeight, 0, glFormat, glType, dataPtr); |
| 3309 | #if !defined(GRAPHICS_API_OPENGL_11) |
| 3310 | else glCompressedTexImage2D(GL_TEXTURE_2D, i, glInternalFormat, mipWidth, mipHeight, 0, mipSize, dataPtr); |
| 3311 | #endif |
| 3312 | |
| 3313 | #if defined(GRAPHICS_API_OPENGL_33) |
| 3314 | if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE) |
| 3315 | { |
| 3316 | GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ONE }; |
| 3317 | glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); |
| 3318 | } |
| 3319 | else if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA) |
| 3320 | { |
| 3321 | #if defined(GRAPHICS_API_OPENGL_21) |
| 3322 | GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ALPHA }; |
| 3323 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 3324 | GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN }; |
| 3325 | #endif |
| 3326 | glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); |
| 3327 | } |
| 3328 | #endif |
| 3329 | } |
| 3330 | |
| 3331 | mipWidth /= 2; |
| 3332 | mipHeight /= 2; |
| 3333 | mipOffset += mipSize; // Increment offset position to next mipmap |
| 3334 | if (data != NULL) dataPtr += mipSize; // Increment data pointer to next mipmap |
| 3335 | |
| 3336 | // Security check for NPOT textures |
| 3337 | if (mipWidth < 1) mipWidth = 1; |
| 3338 | if (mipHeight < 1) mipHeight = 1; |
| 3339 | } |
| 3340 | |
| 3341 | // Texture parameters configuration |
| 3342 | // NOTE: glTexParameteri does NOT affect texture uploading |
| 3343 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 3344 | |
| 3345 | // Check if texture is power-of-two (POT) |
| 3346 | bool texIsPOT = false; |
| 3347 | |
| 3348 | if (((width > 0) && ((width & (width - 1)) == 0)) && |
| 3349 | ((height > 0) && ((height & (height - 1)) == 0))) texIsPOT = true; |
| 3350 | |
| 3351 | // NOTE: OpenGL ES 2.0 with no GL_OES_texture_npot support (i.e. WebGL) has limited NPOT support, so CLAMP_TO_EDGE must be used |
| 3352 | if ((texIsPOT) || (RLGL.ExtSupported.texNPOT)) |
| 3353 | { |
| 3354 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture to repeat on x-axis |
| 3355 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set texture to repeat on y-axis |
| 3356 | } |
| 3357 | else |
| 3358 | { |
| 3359 | // NOTE: If using negative texture coordinates (LoadOBJ()), it does not work! |
| 3360 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); // Set texture to clamp on x-axis |
| 3361 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Set texture to clamp on y-axis |
| 3362 | } |
| 3363 | #else |
| 3364 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture to repeat on x-axis |
| 3365 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set texture to repeat on y-axis |
| 3366 | #endif |
| 3367 | |
| 3368 | // Magnification and minification filters |
| 3369 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); // Alternative: GL_LINEAR |
| 3370 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Alternative: GL_LINEAR |
| 3371 | |
| 3372 | #if defined(GRAPHICS_API_OPENGL_33) |
| 3373 | if (mipmapCount > 1) |
| 3374 | { |
| 3375 | // Activate trilinear filtering if mipmaps are available |
| 3376 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 3377 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
| 3378 | |
| 3379 | // Define the maximum number of mipmap levels to be used, 0 is base texture size |
| 3380 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0); |
| 3381 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, mipmapCount - 1); |
| 3382 | |
| 3383 | // Check if the loaded texture with mipmaps is complete, |
| 3384 | // uncomplete textures will draw in black if mipmap filtering is required |
| 3385 | //GLint complete = 0; |
| 3386 | //glGetTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_IMMUTABLE_FORMAT, &complete); |
| 3387 | } |
| 3388 | #endif |
| 3389 | |
| 3390 | // At this point texture is loaded in GPU and texture parameters configured |
| 3391 | |
| 3392 | // NOTE: If mipmaps were not in data, they are not generated automatically |
| 3393 | |
| 3394 | // Unbind current texture |
| 3395 | glBindTexture(GL_TEXTURE_2D, 0); |
| 3396 | |
| 3397 | if (id > 0) TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Texture loaded successfully (%ix%i | %s | %i mipmaps)", id, width, height, rlGetPixelFormatName(format), mipmapCount); |
| 3398 | else TRACELOG(RL_LOG_WARNING, "TEXTURE: Failed to load texture"); |
| 3399 | |
| 3400 | return id; |
| 3401 | } |
| 3402 | |
| 3403 | // Load depth texture/renderbuffer (to be attached to fbo) |
| 3404 | // WARNING: OpenGL ES 2.0 requires GL_OES_depth_texture and WebGL requires WEBGL_depth_texture extensions |
| 3405 | unsigned int rlLoadTextureDepth(int width, int height, bool useRenderBuffer) |
| 3406 | { |
| 3407 | unsigned int id = 0; |
| 3408 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return id; } |
| 3409 | |
| 3410 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3411 | // In case depth textures were not supported, force renderbuffer usage |
| 3412 | if (!RLGL.ExtSupported.texDepth) useRenderBuffer = true; |
| 3413 | |
| 3414 | // NOTE: Letting the implementation to choose the best bit-depth |
| 3415 | // Possible formats: GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32 and GL_DEPTH_COMPONENT32F |
| 3416 | unsigned int glInternalFormat = GL_DEPTH_COMPONENT; |
| 3417 | |
| 3418 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 3419 | // WARNING: WebGL platform requires unsized internal format definition (GL_DEPTH_COMPONENT) |
| 3420 | // while other platforms using OpenGL ES 2.0 require/support sized internal formats depending on the GPU capabilities |
| 3421 | if (!RLGL.ExtSupported.texDepthWebGL || useRenderBuffer) |
| 3422 | { |
| 3423 | if (RLGL.ExtSupported.maxDepthBits == 32) glInternalFormat = GL_DEPTH_COMPONENT32_OES; |
| 3424 | else if (RLGL.ExtSupported.maxDepthBits == 24) glInternalFormat = GL_DEPTH_COMPONENT24_OES; |
| 3425 | else glInternalFormat = GL_DEPTH_COMPONENT16; |
| 3426 | } |
| 3427 | #endif |
| 3428 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 3429 | // NOTE: This sized internal format should also work for WebGL 2.0 |
| 3430 | // WARNING: Specification only allows GL_DEPTH_COMPONENT32F for GL_FLOAT type |
| 3431 | // REF: https://registry.khronos.org/OpenGL-Refpages/es3.0/html/glTexImage2D.xhtml |
| 3432 | if (RLGL.ExtSupported.maxDepthBits == 24) glInternalFormat = GL_DEPTH_COMPONENT24; |
| 3433 | else glInternalFormat = GL_DEPTH_COMPONENT16; |
| 3434 | #endif |
| 3435 | |
| 3436 | if (!useRenderBuffer && RLGL.ExtSupported.texDepth) |
| 3437 | { |
| 3438 | glGenTextures(1, &id); |
| 3439 | glBindTexture(GL_TEXTURE_2D, id); |
| 3440 | glTexImage2D(GL_TEXTURE_2D, 0, glInternalFormat, width, height, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); |
| 3441 | |
| 3442 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
| 3443 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
| 3444 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 3445 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 3446 | |
| 3447 | glBindTexture(GL_TEXTURE_2D, 0); |
| 3448 | |
| 3449 | TRACELOG(RL_LOG_INFO, "TEXTURE: Depth texture loaded successfully"); |
| 3450 | } |
| 3451 | else |
| 3452 | { |
| 3453 | // Create the renderbuffer that will serve as the depth attachment for the framebuffer |
| 3454 | // NOTE: A renderbuffer is simpler than a texture and could offer better performance on embedded devices |
| 3455 | glGenRenderbuffers(1, &id); |
| 3456 | glBindRenderbuffer(GL_RENDERBUFFER, id); |
| 3457 | glRenderbufferStorage(GL_RENDERBUFFER, glInternalFormat, width, height); |
| 3458 | |
| 3459 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 3460 | |
| 3461 | TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Depth renderbuffer loaded successfully (%i bits)", id, (RLGL.ExtSupported.maxDepthBits >= 24)? RLGL.ExtSupported.maxDepthBits : 16); |
| 3462 | } |
| 3463 | #elif defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 3464 | // NOTE: Renderbuffers are the same type of object as textures in rlsw |
| 3465 | // WARNING: Ensure that the depth format is the one specified at rlsw compilation |
| 3466 | glGenRenderbuffers(1, &id); |
| 3467 | glBindRenderbuffer(GL_RENDERBUFFER, id); |
| 3468 | glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, width, height); |
| 3469 | glBindRenderbuffer(GL_RENDERBUFFER, 0); |
| 3470 | #endif |
| 3471 | |
| 3472 | return id; |
| 3473 | } |
| 3474 | |
| 3475 | // Load texture cubemap |
| 3476 | // NOTE: Cubemap data is expected to be 6 images in a single data array (one after the other), |
| 3477 | // expected the following convention: +X, -X, +Y, -Y, +Z, -Z |
| 3478 | unsigned int rlLoadTextureCubemap(const void *data, int size, int format, int mipmapCount) |
| 3479 | { |
| 3480 | unsigned int id = 0; |
| 3481 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return id; } |
| 3482 | |
| 3483 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3484 | int mipSize = size; |
| 3485 | |
| 3486 | // NOTE: Added pointer math separately from function to avoid UBSAN complaining |
| 3487 | unsigned char *dataPtr = NULL; |
| 3488 | if (data != NULL) dataPtr = (unsigned char *)data; |
| 3489 | |
| 3490 | unsigned int dataSize = rlGetPixelDataSize(size, size, format); |
| 3491 | |
| 3492 | glGenTextures(1, &id); |
| 3493 | glBindTexture(GL_TEXTURE_CUBE_MAP, id); |
| 3494 | |
| 3495 | unsigned int glInternalFormat, glFormat, glType; |
| 3496 | rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); |
| 3497 | |
| 3498 | if (glInternalFormat != 0) |
| 3499 | { |
| 3500 | // Load cubemap faces/mipmaps |
| 3501 | for (int i = 0; i < 6*mipmapCount; i++) |
| 3502 | { |
| 3503 | int mipmapLevel = i/6; |
| 3504 | int face = i%6; |
| 3505 | |
| 3506 | if (data == NULL) |
| 3507 | { |
| 3508 | if (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) |
| 3509 | { |
| 3510 | if ((format == RL_PIXELFORMAT_UNCOMPRESSED_R32) || |
| 3511 | (format == RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32) || |
| 3512 | (format == RL_PIXELFORMAT_UNCOMPRESSED_R16) || |
| 3513 | (format == RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16)) TRACELOG(RL_LOG_WARNING, "TEXTURES: Cubemap requested format not supported"); |
| 3514 | else glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mipmapLevel, glInternalFormat, mipSize, mipSize, 0, glFormat, glType, NULL); |
| 3515 | } |
| 3516 | else TRACELOG(RL_LOG_WARNING, "TEXTURES: Empty cubemap creation does not support compressed format"); |
| 3517 | } |
| 3518 | else |
| 3519 | { |
| 3520 | if (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mipmapLevel, glInternalFormat, mipSize, mipSize, 0, glFormat, glType, dataPtr + face*dataSize); |
| 3521 | else glCompressedTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, mipmapLevel, glInternalFormat, mipSize, mipSize, 0, dataSize, dataPtr + face*dataSize); |
| 3522 | } |
| 3523 | |
| 3524 | #if defined(GRAPHICS_API_OPENGL_33) |
| 3525 | if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE) |
| 3526 | { |
| 3527 | GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ONE }; |
| 3528 | glTexParameteriv(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); |
| 3529 | } |
| 3530 | else if (format == RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA) |
| 3531 | { |
| 3532 | #if defined(GRAPHICS_API_OPENGL_21) |
| 3533 | GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_ALPHA }; |
| 3534 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 3535 | GLint swizzleMask[] = { GL_RED, GL_RED, GL_RED, GL_GREEN }; |
| 3536 | #endif |
| 3537 | glTexParameteriv(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_SWIZZLE_RGBA, swizzleMask); |
| 3538 | } |
| 3539 | #endif |
| 3540 | if (face == 5) |
| 3541 | { |
| 3542 | mipSize /= 2; |
| 3543 | if (data != NULL) dataPtr += dataSize*6; // Increment data pointer to next mipmap |
| 3544 | |
| 3545 | // Security check for NPOT textures |
| 3546 | if (mipSize < 1) mipSize = 1; |
| 3547 | |
| 3548 | dataSize = rlGetPixelDataSize(mipSize, mipSize, format); |
| 3549 | } |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | // Set cubemap texture sampling parameters |
| 3554 | if (mipmapCount > 1) glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); |
| 3555 | else glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 3556 | |
| 3557 | glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 3558 | glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 3559 | glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 3560 | #if defined(GRAPHICS_API_OPENGL_33) |
| 3561 | glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); // Flag not supported on OpenGL ES 2.0 |
| 3562 | #endif |
| 3563 | |
| 3564 | glBindTexture(GL_TEXTURE_CUBE_MAP, 0); |
| 3565 | #endif |
| 3566 | |
| 3567 | if (id > 0) TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Cubemap texture loaded successfully (%ix%i)", id, size, size); |
| 3568 | else TRACELOG(RL_LOG_WARNING, "TEXTURE: Failed to load cubemap texture"); |
| 3569 | |
| 3570 | return id; |
| 3571 | } |
| 3572 | |
| 3573 | // Update already loaded texture in GPU with new data |
| 3574 | // WARNING: Not possible to know safely if internal texture format is the expected one... |
| 3575 | void rlUpdateTexture(unsigned int id, int offsetX, int offsetY, int width, int height, int format, const void *data) |
| 3576 | { |
| 3577 | glBindTexture(GL_TEXTURE_2D, id); |
| 3578 | |
| 3579 | unsigned int glInternalFormat, glFormat, glType; |
| 3580 | rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); |
| 3581 | |
| 3582 | if ((glInternalFormat != 0) && (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB)) |
| 3583 | { |
| 3584 | glTexSubImage2D(GL_TEXTURE_2D, 0, offsetX, offsetY, width, height, glFormat, glType, data); |
| 3585 | } |
| 3586 | else TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] Failed to update for current texture format (%i)", id, format); |
| 3587 | } |
| 3588 | |
| 3589 | // Get OpenGL internal formats and data type from raylib PixelFormat |
| 3590 | void rlGetGlTextureFormats(int format, unsigned int *glInternalFormat, unsigned int *glFormat, unsigned int *glType) |
| 3591 | { |
| 3592 | *glInternalFormat = 0; |
| 3593 | *glFormat = 0; |
| 3594 | *glType = 0; |
| 3595 | |
| 3596 | switch (format) |
| 3597 | { |
| 3598 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_21) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3599 | // NOTE: on OpenGL ES 2.0 (WebGL), internalFormat must match format and options allowed are: GL_LUMINANCE, GL_RGB, GL_RGBA |
| 3600 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_UNSIGNED_BYTE; break; |
| 3601 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: *glInternalFormat = GL_LUMINANCE_ALPHA; *glFormat = GL_LUMINANCE_ALPHA; *glType = GL_UNSIGNED_BYTE; break; |
| 3602 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5: *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_UNSIGNED_SHORT_5_6_5; break; |
| 3603 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_UNSIGNED_BYTE; break; |
| 3604 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_5_5_5_1; break; |
| 3605 | case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_4_4_4_4; break; |
| 3606 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_BYTE; break; |
| 3607 | #if !defined(GRAPHICS_API_OPENGL_11) |
| 3608 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 3609 | case RL_PIXELFORMAT_UNCOMPRESSED_R32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_R32F_EXT; *glFormat = GL_RED_EXT; *glType = GL_FLOAT; break; |
| 3610 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB32F_EXT; *glFormat = GL_RGB; *glType = GL_FLOAT; break; |
| 3611 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA32F_EXT; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; |
| 3612 | case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_R16F_EXT; *glFormat = GL_RED_EXT; *glType = GL_HALF_FLOAT; break; |
| 3613 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB16F_EXT; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT; break; |
| 3614 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA16F_EXT; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT; break; |
| 3615 | #else |
| 3616 | case RL_PIXELFORMAT_UNCOMPRESSED_R32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float |
| 3617 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float |
| 3618 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; // NOTE: Requires extension OES_texture_float |
| 3619 | #if defined(GRAPHICS_API_OPENGL_21) |
| 3620 | case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_HALF_FLOAT_ARB; break; |
| 3621 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT_ARB; break; |
| 3622 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT_ARB; break; |
| 3623 | #else // GRAPHICS_API_OPENGL_ES2 |
| 3624 | case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_LUMINANCE; *glFormat = GL_LUMINANCE; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float |
| 3625 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float |
| 3626 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT_OES; break; // NOTE: Requires extension OES_texture_half_float |
| 3627 | #endif |
| 3628 | #endif |
| 3629 | #endif |
| 3630 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 3631 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: *glInternalFormat = GL_R8; *glFormat = GL_RED; *glType = GL_UNSIGNED_BYTE; break; |
| 3632 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: *glInternalFormat = GL_RG8; *glFormat = GL_RG; *glType = GL_UNSIGNED_BYTE; break; |
| 3633 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5: *glInternalFormat = GL_RGB565; *glFormat = GL_RGB; *glType = GL_UNSIGNED_SHORT_5_6_5; break; |
| 3634 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: *glInternalFormat = GL_RGB8; *glFormat = GL_RGB; *glType = GL_UNSIGNED_BYTE; break; |
| 3635 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: *glInternalFormat = GL_RGB5_A1; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_5_5_5_1; break; |
| 3636 | case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: *glInternalFormat = GL_RGBA4; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_SHORT_4_4_4_4; break; |
| 3637 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: *glInternalFormat = GL_RGBA8; *glFormat = GL_RGBA; *glType = GL_UNSIGNED_BYTE; break; |
| 3638 | case RL_PIXELFORMAT_UNCOMPRESSED_R32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_R32F; *glFormat = GL_RED; *glType = GL_FLOAT; break; |
| 3639 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGB32F; *glFormat = GL_RGB; *glType = GL_FLOAT; break; |
| 3640 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: if (RLGL.ExtSupported.texFloat32) *glInternalFormat = GL_RGBA32F; *glFormat = GL_RGBA; *glType = GL_FLOAT; break; |
| 3641 | case RL_PIXELFORMAT_UNCOMPRESSED_R16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_R16F; *glFormat = GL_RED; *glType = GL_HALF_FLOAT; break; |
| 3642 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGB16F; *glFormat = GL_RGB; *glType = GL_HALF_FLOAT; break; |
| 3643 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: if (RLGL.ExtSupported.texFloat16) *glInternalFormat = GL_RGBA16F; *glFormat = GL_RGBA; *glType = GL_HALF_FLOAT; break; |
| 3644 | #endif |
| 3645 | #if !defined(GRAPHICS_API_OPENGL_11) |
| 3646 | case RL_PIXELFORMAT_COMPRESSED_DXT1_RGB: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGB_S3TC_DXT1_EXT; break; |
| 3647 | case RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT1_EXT; break; |
| 3648 | case RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT3_EXT; break; |
| 3649 | case RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA: if (RLGL.ExtSupported.texCompDXT) *glInternalFormat = GL_COMPRESSED_RGBA_S3TC_DXT5_EXT; break; |
| 3650 | case RL_PIXELFORMAT_COMPRESSED_ETC1_RGB: if (RLGL.ExtSupported.texCompETC1) *glInternalFormat = GL_ETC1_RGB8_OES; break; // NOTE: Requires OpenGL ES 2.0 or OpenGL 4.3 |
| 3651 | case RL_PIXELFORMAT_COMPRESSED_ETC2_RGB: if (RLGL.ExtSupported.texCompETC2) *glInternalFormat = GL_COMPRESSED_RGB8_ETC2; break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3 |
| 3652 | case RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA: if (RLGL.ExtSupported.texCompETC2) *glInternalFormat = GL_COMPRESSED_RGBA8_ETC2_EAC; break; // NOTE: Requires OpenGL ES 3.0 or OpenGL 4.3 |
| 3653 | case RL_PIXELFORMAT_COMPRESSED_PVRT_RGB: if (RLGL.ExtSupported.texCompPVRT) *glInternalFormat = GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG; break; // NOTE: Requires PowerVR GPU |
| 3654 | case RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA: if (RLGL.ExtSupported.texCompPVRT) *glInternalFormat = GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; break; // NOTE: Requires PowerVR GPU |
| 3655 | case RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA: if (RLGL.ExtSupported.texCompASTC) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_4x4_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 |
| 3656 | case RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA: if (RLGL.ExtSupported.texCompASTC) *glInternalFormat = GL_COMPRESSED_RGBA_ASTC_8x8_KHR; break; // NOTE: Requires OpenGL ES 3.1 or OpenGL 4.3 |
| 3657 | #endif |
| 3658 | default: TRACELOG(RL_LOG_WARNING, "TEXTURE: Current format not supported (%i)", format); break; |
| 3659 | } |
| 3660 | } |
| 3661 | |
| 3662 | // Unload texture from GPU memory |
| 3663 | void rlUnloadTexture(unsigned int id) |
| 3664 | { |
| 3665 | glDeleteTextures(1, &id); |
| 3666 | } |
| 3667 | |
| 3668 | // Generate mipmap data for selected texture |
| 3669 | // NOTE: Only supports GPU mipmap generation |
| 3670 | void rlGenTextureMipmaps(unsigned int id, int width, int height, int format, int *mipmaps) |
| 3671 | { |
| 3672 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return; } |
| 3673 | |
| 3674 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3675 | glBindTexture(GL_TEXTURE_2D, id); |
| 3676 | |
| 3677 | // Check if texture is power-of-two (POT) |
| 3678 | bool texIsPOT = false; |
| 3679 | |
| 3680 | if (((width > 0) && ((width & (width - 1)) == 0)) && |
| 3681 | ((height > 0) && ((height & (height - 1)) == 0))) texIsPOT = true; |
| 3682 | |
| 3683 | if ((texIsPOT) || (RLGL.ExtSupported.texNPOT)) |
| 3684 | { |
| 3685 | //glHint(GL_GENERATE_MIPMAP_HINT, GL_DONT_CARE); // Hint for mipmaps generation algorithm: GL_FASTEST, GL_NICEST, GL_DONT_CARE |
| 3686 | glGenerateMipmap(GL_TEXTURE_2D); // Generate mipmaps automatically |
| 3687 | |
| 3688 | #define MIN(a,b) (((a)<(b))? (a):(b)) |
| 3689 | #define MAX(a,b) (((a)>(b))? (a):(b)) |
| 3690 | |
| 3691 | *mipmaps = 1 + (int)floor(log(MAX(width, height))/log(2)); |
| 3692 | TRACELOG(RL_LOG_INFO, "TEXTURE: [ID %i] Mipmaps generated automatically, total: %i", id, *mipmaps); |
| 3693 | } |
| 3694 | else TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] Failed to generate mipmaps", id); |
| 3695 | |
| 3696 | glBindTexture(GL_TEXTURE_2D, 0); |
| 3697 | #else |
| 3698 | TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] GPU mipmap generation not supported", id); |
| 3699 | #endif |
| 3700 | } |
| 3701 | |
| 3702 | // Read texture pixel data |
| 3703 | void *rlReadTexturePixels(unsigned int id, int width, int height, int format) |
| 3704 | { |
| 3705 | void *pixels = NULL; |
| 3706 | |
| 3707 | #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) |
| 3708 | glBindTexture(GL_TEXTURE_2D, id); |
| 3709 | |
| 3710 | // NOTE: Using texture id, some texture info can be retrieved (but not on OpenGL ES 2.0) |
| 3711 | // Possible texture info: GL_TEXTURE_RED_SIZE, GL_TEXTURE_GREEN_SIZE, GL_TEXTURE_BLUE_SIZE, GL_TEXTURE_ALPHA_SIZE |
| 3712 | //int width, height, format; |
| 3713 | //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); |
| 3714 | //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height); |
| 3715 | //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT, &format); |
| 3716 | |
| 3717 | // NOTE: Each row written to or read from by OpenGL pixel operations like glGetTexImage are aligned to a 4 byte boundary by default, which may add some padding |
| 3718 | // Use glPixelStorei to modify padding with the GL_[UN]PACK_ALIGNMENT setting |
| 3719 | // GL_PACK_ALIGNMENT affects operations that read from OpenGL memory (glReadPixels, glGetTexImage, etc.) |
| 3720 | // GL_UNPACK_ALIGNMENT affects operations that write to OpenGL memory (glTexImage, etc.) |
| 3721 | glPixelStorei(GL_PACK_ALIGNMENT, 1); |
| 3722 | |
| 3723 | unsigned int glInternalFormat, glFormat, glType; |
| 3724 | rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); |
| 3725 | unsigned int size = rlGetPixelDataSize(width, height, format); |
| 3726 | |
| 3727 | if ((glInternalFormat != 0) && (format < RL_PIXELFORMAT_COMPRESSED_DXT1_RGB)) |
| 3728 | { |
| 3729 | pixels = RL_CALLOC(size, 1); |
| 3730 | glGetTexImage(GL_TEXTURE_2D, 0, glFormat, glType, pixels); |
| 3731 | } |
| 3732 | else TRACELOG(RL_LOG_WARNING, "TEXTURE: [ID %i] Data retrieval not suported for pixel format (%i)", id, format); |
| 3733 | |
| 3734 | glBindTexture(GL_TEXTURE_2D, 0); |
| 3735 | #endif |
| 3736 | |
| 3737 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 3738 | // glGetTexImage() is not available on OpenGL ES 2.0 |
| 3739 | // Texture width and height are required on OpenGL ES 2.0, there is no way to get it from texture id |
| 3740 | // Two possible Options: |
| 3741 | // 1 - Bind texture to color fbo attachment and glReadPixels() |
| 3742 | // 2 - Create an fbo, activate it, render quad with texture, glReadPixels() |
| 3743 | // Using Option 1, care for texture format on retrieval |
| 3744 | // NOTE: This behaviour could be conditioned by graphic driver... |
| 3745 | unsigned int fboId = rlLoadFramebuffer(); |
| 3746 | |
| 3747 | glBindFramebuffer(GL_FRAMEBUFFER, fboId); |
| 3748 | glBindTexture(GL_TEXTURE_2D, 0); |
| 3749 | |
| 3750 | // Attach our texture to FBO |
| 3751 | glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, id, 0); |
| 3752 | |
| 3753 | #if defined(FBO_READ_TEXTURE_AS_RGBA) |
| 3754 | // Reading data as RGBA because FBO texture is configured as RGBA, despite binding another texture format |
| 3755 | pixels = RL_CALLOC(rlGetPixelDataSize(width, height, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8), 1); |
| 3756 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 3757 | #else |
| 3758 | // Reading data as original texture format, in some platforms (RPI, Wasm) it works |
| 3759 | pixels = (unsigned char *)RL_CALLOC(rlGetPixelDataSize(width, height, format), 1); |
| 3760 | unsigned int glInternalFormat = 0, glFormat = 0, glType = 0; |
| 3761 | rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); |
| 3762 | glReadPixels(0, 0, width, height, glFormat, glType, pixels); |
| 3763 | #endif |
| 3764 | |
| 3765 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 3766 | |
| 3767 | // Clean up temporal fbo |
| 3768 | rlUnloadFramebuffer(fboId); |
| 3769 | #endif |
| 3770 | |
| 3771 | return pixels; |
| 3772 | } |
| 3773 | |
| 3774 | // Copy framebuffer pixel data to internal buffer |
| 3775 | void rlCopyFramebuffer(int x, int y, int width, int height, int format, void *pixels) |
| 3776 | { |
| 3777 | #if defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 3778 | unsigned int glInternalFormat, glFormat, glType; |
| 3779 | rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); // Get OpenGL texture format |
| 3780 | swReadPixels(x, y, width, height, glFormat, glType, pixels); |
| 3781 | #endif |
| 3782 | } |
| 3783 | |
| 3784 | // Resize internal framebuffer |
| 3785 | void rlResizeFramebuffer(int width, int height) |
| 3786 | { |
| 3787 | #if defined(GRAPHICS_API_OPENGL_SOFTWARE) |
| 3788 | swResize(width, height); |
| 3789 | #endif |
| 3790 | } |
| 3791 | |
| 3792 | // Read screen pixel data (color buffer) |
| 3793 | unsigned char *rlReadScreenPixels(int width, int height) |
| 3794 | { |
| 3795 | unsigned char *imgData = (unsigned char *)RL_CALLOC(width*height*4, sizeof(unsigned char)); |
| 3796 | |
| 3797 | // NOTE: Buffer retrieved is GL_FRONT in single-buffered configurations |
| 3798 | // and GL_BACK in double-buffered configurations, make sure to call it at the end of frame |
| 3799 | //glReadBuffer(GL_BACK); |
| 3800 | |
| 3801 | // NOTE: glReadPixels() returns image flipped vertically -> (0,0) is the bottom left corner of the framebuffer |
| 3802 | // WARNING: Getting alpha channel! Be careful, it can be transparent if not cleared properly! |
| 3803 | glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, imgData); |
| 3804 | |
| 3805 | // Flip image vertically |
| 3806 | // NOTE: Alpha value has already been applied to RGB in framebuffer, not needed anymore |
| 3807 | for (int y = height - 1; y >= height/2; y--) |
| 3808 | { |
| 3809 | for (int x = 0; x < (width*4); x += 4) |
| 3810 | { |
| 3811 | unsigned int s = ((height - 1) - y)*width*4 + x; |
| 3812 | unsigned int e = y*width*4 + x; |
| 3813 | |
| 3814 | unsigned char r = imgData[s]; |
| 3815 | unsigned char g = imgData[s+1]; |
| 3816 | unsigned char b = imgData[s+2]; |
| 3817 | |
| 3818 | imgData[s] = imgData[e]; |
| 3819 | imgData[s+1] = imgData[e+1]; |
| 3820 | imgData[s+2] = imgData[e+2]; |
| 3821 | imgData[s+3] = 255; // Set alpha component value to 255 (no trasparent image retrieval) |
| 3822 | |
| 3823 | imgData[e] = r; |
| 3824 | imgData[e+1] = g; |
| 3825 | imgData[e+2] = b; |
| 3826 | imgData[e+3] = 255; // Ditto |
| 3827 | } |
| 3828 | } |
| 3829 | |
| 3830 | return imgData; // NOTE: image data should be freed |
| 3831 | } |
| 3832 | |
| 3833 | // Framebuffer management (fbo) |
| 3834 | //----------------------------------------------------------------------------------------- |
| 3835 | // Load a framebuffer to be used for rendering |
| 3836 | // NOTE: No textures attached |
| 3837 | unsigned int rlLoadFramebuffer(void) |
| 3838 | { |
| 3839 | unsigned int fboId = 0; |
| 3840 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return fboId; } |
| 3841 | |
| 3842 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 3843 | glGenFramebuffers(1, &fboId); // Create the framebuffer object |
| 3844 | glBindFramebuffer(GL_FRAMEBUFFER, 0); // Unbind any framebuffer |
| 3845 | #endif |
| 3846 | |
| 3847 | return fboId; |
| 3848 | } |
| 3849 | |
| 3850 | // Attach color buffer texture to a framebuffer object (unloads previous attachment) |
| 3851 | // NOTE: Attach type: 0-Color, 1-Depth renderbuffer, 2-Depth texture |
| 3852 | void rlFramebufferAttach(unsigned int id, unsigned int texId, int attachType, int texType, int mipLevel) |
| 3853 | { |
| 3854 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 3855 | glBindFramebuffer(GL_FRAMEBUFFER, id); |
| 3856 | |
| 3857 | switch (attachType) |
| 3858 | { |
| 3859 | case RL_ATTACHMENT_COLOR_CHANNEL0: |
| 3860 | case RL_ATTACHMENT_COLOR_CHANNEL1: |
| 3861 | case RL_ATTACHMENT_COLOR_CHANNEL2: |
| 3862 | case RL_ATTACHMENT_COLOR_CHANNEL3: |
| 3863 | case RL_ATTACHMENT_COLOR_CHANNEL4: |
| 3864 | case RL_ATTACHMENT_COLOR_CHANNEL5: |
| 3865 | case RL_ATTACHMENT_COLOR_CHANNEL6: |
| 3866 | case RL_ATTACHMENT_COLOR_CHANNEL7: |
| 3867 | { |
| 3868 | if (texType == RL_ATTACHMENT_TEXTURE2D) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachType, GL_TEXTURE_2D, texId, mipLevel); |
| 3869 | else if (texType == RL_ATTACHMENT_RENDERBUFFER) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachType, GL_RENDERBUFFER, texId); |
| 3870 | else if (texType >= RL_ATTACHMENT_CUBEMAP_POSITIVE_X) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + attachType, GL_TEXTURE_CUBE_MAP_POSITIVE_X + texType, texId, mipLevel); |
| 3871 | } break; |
| 3872 | case RL_ATTACHMENT_DEPTH: |
| 3873 | { |
| 3874 | if (texType == RL_ATTACHMENT_TEXTURE2D) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texId, mipLevel); |
| 3875 | else if (texType == RL_ATTACHMENT_RENDERBUFFER) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, texId); |
| 3876 | } break; |
| 3877 | case RL_ATTACHMENT_STENCIL: |
| 3878 | { |
| 3879 | if (texType == RL_ATTACHMENT_TEXTURE2D) glFramebufferTexture2D(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_TEXTURE_2D, texId, mipLevel); |
| 3880 | else if (texType == RL_ATTACHMENT_RENDERBUFFER) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_STENCIL_ATTACHMENT, GL_RENDERBUFFER, texId); |
| 3881 | } break; |
| 3882 | default: break; |
| 3883 | } |
| 3884 | |
| 3885 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 3886 | #endif |
| 3887 | } |
| 3888 | |
| 3889 | // Verify render texture is complete |
| 3890 | bool rlFramebufferComplete(unsigned int id) |
| 3891 | { |
| 3892 | bool result = false; |
| 3893 | |
| 3894 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 3895 | glBindFramebuffer(GL_FRAMEBUFFER, id); |
| 3896 | |
| 3897 | GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER); |
| 3898 | |
| 3899 | if (status != GL_FRAMEBUFFER_COMPLETE) |
| 3900 | { |
| 3901 | switch (status) |
| 3902 | { |
| 3903 | case GL_FRAMEBUFFER_UNSUPPORTED: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer is unsupported", id); break; |
| 3904 | case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer has incomplete attachment", id); break; |
| 3905 | #if defined(GRAPHICS_API_OPENGL_ES2) |
| 3906 | case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer has incomplete dimensions", id); break; |
| 3907 | #endif |
| 3908 | case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: TRACELOG(RL_LOG_WARNING, "FBO: [ID %i] Framebuffer has a missing attachment", id); break; |
| 3909 | default: break; |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 3914 | |
| 3915 | result = (status == GL_FRAMEBUFFER_COMPLETE); |
| 3916 | #endif |
| 3917 | |
| 3918 | return result; |
| 3919 | } |
| 3920 | |
| 3921 | // Unload framebuffer from GPU memory |
| 3922 | // NOTE: All attached textures/cubemaps/renderbuffers are also deleted |
| 3923 | void rlUnloadFramebuffer(unsigned int id) |
| 3924 | { |
| 3925 | #if (defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) || defined(GRAPHICS_API_OPENGL_SOFTWARE)) |
| 3926 | // Query depth attachment to automatically delete texture/renderbuffer |
| 3927 | int depthType = 0; |
| 3928 | glBindFramebuffer(GL_FRAMEBUFFER, id); // Bind framebuffer to query depth texture type |
| 3929 | glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE, &depthType); |
| 3930 | |
| 3931 | // WARNING: WebGL: INVALID_ENUM: getFramebufferAttachmentParameter: invalid parameter name |
| 3932 | // REF: https://registry.khronos.org/webgl/specs/latest/1.0/ |
| 3933 | int depthId = 0; |
| 3934 | glGetFramebufferAttachmentParameteriv(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME, &depthId); |
| 3935 | |
| 3936 | unsigned int depthIdU = (unsigned int)depthId; |
| 3937 | if (depthType == GL_RENDERBUFFER) glDeleteRenderbuffers(1, &depthIdU); |
| 3938 | else if (depthType == GL_TEXTURE) glDeleteTextures(1, &depthIdU); |
| 3939 | |
| 3940 | // NOTE: If a texture object is deleted while its image is attached to the *currently bound* framebuffer, |
| 3941 | // the texture image is automatically detached from the currently bound framebuffer |
| 3942 | |
| 3943 | glBindFramebuffer(GL_FRAMEBUFFER, 0); |
| 3944 | glDeleteFramebuffers(1, &id); |
| 3945 | |
| 3946 | TRACELOG(RL_LOG_INFO, "FBO: [ID %i] Unloaded framebuffer from VRAM (GPU)", id); |
| 3947 | #endif |
| 3948 | } |
| 3949 | |
| 3950 | // Vertex data management |
| 3951 | //----------------------------------------------------------------------------------------- |
| 3952 | // Load a new attributes buffer |
| 3953 | unsigned int rlLoadVertexBuffer(const void *buffer, int size, bool dynamic) |
| 3954 | { |
| 3955 | unsigned int id = 0; |
| 3956 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return id; } |
| 3957 | |
| 3958 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3959 | glGenBuffers(1, &id); |
| 3960 | glBindBuffer(GL_ARRAY_BUFFER, id); |
| 3961 | glBufferData(GL_ARRAY_BUFFER, size, buffer, dynamic? GL_DYNAMIC_DRAW : GL_STATIC_DRAW); |
| 3962 | #endif |
| 3963 | |
| 3964 | return id; |
| 3965 | } |
| 3966 | |
| 3967 | // Load a new attributes element buffer |
| 3968 | unsigned int rlLoadVertexBufferElement(const void *buffer, int size, bool dynamic) |
| 3969 | { |
| 3970 | unsigned int id = 0; |
| 3971 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return id; } |
| 3972 | |
| 3973 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3974 | glGenBuffers(1, &id); |
| 3975 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); |
| 3976 | glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, buffer, dynamic? GL_DYNAMIC_DRAW : GL_STATIC_DRAW); |
| 3977 | #endif |
| 3978 | |
| 3979 | return id; |
| 3980 | } |
| 3981 | |
| 3982 | // Enable vertex buffer (VBO) |
| 3983 | void rlEnableVertexBuffer(unsigned int id) |
| 3984 | { |
| 3985 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3986 | glBindBuffer(GL_ARRAY_BUFFER, id); |
| 3987 | #endif |
| 3988 | } |
| 3989 | |
| 3990 | // Disable vertex buffer (VBO) |
| 3991 | void rlDisableVertexBuffer(void) |
| 3992 | { |
| 3993 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 3994 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 3995 | #endif |
| 3996 | } |
| 3997 | |
| 3998 | // Enable vertex buffer element (VBO element) |
| 3999 | void rlEnableVertexBufferElement(unsigned int id) |
| 4000 | { |
| 4001 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4002 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); |
| 4003 | #endif |
| 4004 | } |
| 4005 | |
| 4006 | // Disable vertex buffer element (VBO element) |
| 4007 | void rlDisableVertexBufferElement(void) |
| 4008 | { |
| 4009 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4010 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); |
| 4011 | #endif |
| 4012 | } |
| 4013 | |
| 4014 | // Update vertex buffer with new data |
| 4015 | // NOTE: dataSize and offset must be provided in bytes |
| 4016 | void rlUpdateVertexBuffer(unsigned int id, const void *data, int dataSize, int offset) |
| 4017 | { |
| 4018 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4019 | glBindBuffer(GL_ARRAY_BUFFER, id); |
| 4020 | glBufferSubData(GL_ARRAY_BUFFER, offset, dataSize, data); |
| 4021 | #endif |
| 4022 | } |
| 4023 | |
| 4024 | // Update vertex buffer elements with new data |
| 4025 | // NOTE: dataSize and offset must be provided in bytes |
| 4026 | void rlUpdateVertexBufferElements(unsigned int id, const void *data, int dataSize, int offset) |
| 4027 | { |
| 4028 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4029 | glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); |
| 4030 | glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, dataSize, data); |
| 4031 | #endif |
| 4032 | } |
| 4033 | |
| 4034 | // Enable vertex array object (VAO) |
| 4035 | bool rlEnableVertexArray(unsigned int vaoId) |
| 4036 | { |
| 4037 | bool result = false; |
| 4038 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4039 | if (RLGL.ExtSupported.vao) |
| 4040 | { |
| 4041 | glBindVertexArray(vaoId); |
| 4042 | result = true; |
| 4043 | } |
| 4044 | #endif |
| 4045 | return result; |
| 4046 | } |
| 4047 | |
| 4048 | // Disable vertex array object (VAO) |
| 4049 | void rlDisableVertexArray(void) |
| 4050 | { |
| 4051 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4052 | if (RLGL.ExtSupported.vao) glBindVertexArray(0); |
| 4053 | #endif |
| 4054 | } |
| 4055 | |
| 4056 | // Enable vertex attribute index |
| 4057 | void rlEnableVertexAttribute(unsigned int index) |
| 4058 | { |
| 4059 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4060 | glEnableVertexAttribArray(index); |
| 4061 | #endif |
| 4062 | } |
| 4063 | |
| 4064 | // Disable vertex attribute index |
| 4065 | void rlDisableVertexAttribute(unsigned int index) |
| 4066 | { |
| 4067 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4068 | glDisableVertexAttribArray(index); |
| 4069 | #endif |
| 4070 | } |
| 4071 | |
| 4072 | // Draw vertex array |
| 4073 | void rlDrawVertexArray(int offset, int count) |
| 4074 | { |
| 4075 | glDrawArrays(GL_TRIANGLES, offset, count); |
| 4076 | } |
| 4077 | |
| 4078 | // Draw vertex array elements |
| 4079 | void rlDrawVertexArrayElements(int offset, int count, const void *buffer) |
| 4080 | { |
| 4081 | // NOTE: Added pointer math separately from function to avoid UBSAN complaining |
| 4082 | unsigned short *bufferPtr = (unsigned short *)buffer; |
| 4083 | if (offset > 0) bufferPtr += offset; |
| 4084 | |
| 4085 | glDrawElements(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const unsigned short *)bufferPtr); |
| 4086 | } |
| 4087 | |
| 4088 | // Draw vertex array instanced |
| 4089 | void rlDrawVertexArrayInstanced(int offset, int count, int instances) |
| 4090 | { |
| 4091 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4092 | glDrawArraysInstanced(GL_TRIANGLES, offset, count, instances); |
| 4093 | #endif |
| 4094 | } |
| 4095 | |
| 4096 | // Draw vertex array elements instanced |
| 4097 | void rlDrawVertexArrayElementsInstanced(int offset, int count, const void *buffer, int instances) |
| 4098 | { |
| 4099 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4100 | // NOTE: Added pointer math separately from function to avoid UBSAN complaining |
| 4101 | unsigned short *bufferPtr = (unsigned short *)buffer; |
| 4102 | if (offset > 0) bufferPtr += offset; |
| 4103 | |
| 4104 | glDrawElementsInstanced(GL_TRIANGLES, count, GL_UNSIGNED_SHORT, (const unsigned short *)bufferPtr, instances); |
| 4105 | #endif |
| 4106 | } |
| 4107 | |
| 4108 | // Enable vertex state pointer |
| 4109 | void rlEnableStatePointer(int vertexAttribType, void *buffer) |
| 4110 | { |
| 4111 | #if defined(GRAPHICS_API_OPENGL_11) |
| 4112 | if (buffer != NULL) glEnableClientState(vertexAttribType); |
| 4113 | switch (vertexAttribType) |
| 4114 | { |
| 4115 | case GL_VERTEX_ARRAY: glVertexPointer(3, GL_FLOAT, 0, buffer); break; |
| 4116 | case GL_TEXTURE_COORD_ARRAY: glTexCoordPointer(2, GL_FLOAT, 0, buffer); break; |
| 4117 | case GL_NORMAL_ARRAY: if (buffer != NULL) glNormalPointer(GL_FLOAT, 0, buffer); break; |
| 4118 | case GL_COLOR_ARRAY: if (buffer != NULL) glColorPointer(4, GL_UNSIGNED_BYTE, 0, buffer); break; |
| 4119 | //case GL_INDEX_ARRAY: if (buffer != NULL) glIndexPointer(GL_SHORT, 0, buffer); break; // Indexed colors |
| 4120 | default: break; |
| 4121 | } |
| 4122 | #endif |
| 4123 | } |
| 4124 | |
| 4125 | // Disable vertex state pointer |
| 4126 | void rlDisableStatePointer(int vertexAttribType) |
| 4127 | { |
| 4128 | #if defined(GRAPHICS_API_OPENGL_11) |
| 4129 | glDisableClientState(vertexAttribType); |
| 4130 | #endif |
| 4131 | } |
| 4132 | |
| 4133 | // Load vertex array object (VAO) |
| 4134 | unsigned int rlLoadVertexArray(void) |
| 4135 | { |
| 4136 | unsigned int vaoId = 0; |
| 4137 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return vaoId; } |
| 4138 | |
| 4139 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4140 | if (RLGL.ExtSupported.vao) glGenVertexArrays(1, &vaoId); |
| 4141 | #endif |
| 4142 | |
| 4143 | return vaoId; |
| 4144 | } |
| 4145 | |
| 4146 | // Set vertex attribute |
| 4147 | void rlSetVertexAttribute(unsigned int index, int compSize, int type, bool normalized, int stride, int offset) |
| 4148 | { |
| 4149 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4150 | // NOTE: Data type could be: GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, GL_UNSIGNED_INT |
| 4151 | // Additional types (depends on OpenGL version or extensions): |
| 4152 | // - GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE, GL_FIXED, |
| 4153 | // - GL_INT_2_10_10_10_REV, GL_UNSIGNED_INT_2_10_10_10_REV, GL_UNSIGNED_INT_10F_11F_11F_REV |
| 4154 | |
| 4155 | size_t offsetNative = offset; |
| 4156 | glVertexAttribPointer(index, compSize, type, normalized, stride, (void *)offsetNative); |
| 4157 | #endif |
| 4158 | } |
| 4159 | |
| 4160 | // Set vertex attribute divisor |
| 4161 | void rlSetVertexAttributeDivisor(unsigned int index, int divisor) |
| 4162 | { |
| 4163 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4164 | glVertexAttribDivisor(index, divisor); |
| 4165 | #endif |
| 4166 | } |
| 4167 | |
| 4168 | // Unload vertex array object (VAO) |
| 4169 | void rlUnloadVertexArray(unsigned int vaoId) |
| 4170 | { |
| 4171 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4172 | if (RLGL.ExtSupported.vao) |
| 4173 | { |
| 4174 | glBindVertexArray(0); |
| 4175 | glDeleteVertexArrays(1, &vaoId); |
| 4176 | TRACELOG(RL_LOG_INFO, "VAO: [ID %i] Unloaded vertex array data from VRAM (GPU)", vaoId); |
| 4177 | } |
| 4178 | #endif |
| 4179 | } |
| 4180 | |
| 4181 | // Unload vertex buffer (VBO) |
| 4182 | void rlUnloadVertexBuffer(unsigned int vboId) |
| 4183 | { |
| 4184 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4185 | glDeleteBuffers(1, &vboId); |
| 4186 | //TRACELOG(RL_LOG_INFO, "VBO: Unloaded vertex data from VRAM (GPU)"); |
| 4187 | #endif |
| 4188 | } |
| 4189 | |
| 4190 | // Shaders management |
| 4191 | //----------------------------------------------------------------------------------------------- |
| 4192 | // Load (compile) shader and return shader id |
| 4193 | unsigned int rlLoadShader(const char *code, int type) |
| 4194 | { |
| 4195 | unsigned int shaderId = 0; |
| 4196 | |
| 4197 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4198 | shaderId = glCreateShader(type); |
| 4199 | glShaderSource(shaderId, 1, &code, NULL); |
| 4200 | |
| 4201 | GLint success = 0; |
| 4202 | glCompileShader(shaderId); |
| 4203 | glGetShaderiv(shaderId, GL_COMPILE_STATUS, &success); |
| 4204 | |
| 4205 | if (success == GL_FALSE) |
| 4206 | { |
| 4207 | switch (type) |
| 4208 | { |
| 4209 | case GL_VERTEX_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to compile vertex shader code", shaderId); break; |
| 4210 | case GL_FRAGMENT_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to compile fragment shader code", shaderId); break; |
| 4211 | //case GL_GEOMETRY_SHADER: |
| 4212 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4213 | case GL_COMPUTE_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to compile compute shader code", shaderId); break; |
| 4214 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 4215 | case GL_COMPUTE_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: Compute shaders not enabled. Define GRAPHICS_API_OPENGL_43", shaderId); break; |
| 4216 | #endif |
| 4217 | default: break; |
| 4218 | } |
| 4219 | |
| 4220 | int maxLength = 0; |
| 4221 | glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &maxLength); |
| 4222 | |
| 4223 | if (maxLength > 0) |
| 4224 | { |
| 4225 | int length = 0; |
| 4226 | char *log = (char *)RL_CALLOC(maxLength, sizeof(char)); |
| 4227 | glGetShaderInfoLog(shaderId, maxLength, &length, log); |
| 4228 | TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Compile error: %s", shaderId, log); |
| 4229 | RL_FREE(log); |
| 4230 | } |
| 4231 | |
| 4232 | // Unload object allocated by glCreateShader(), |
| 4233 | // despite failing in the compilation process |
| 4234 | glDeleteShader(shaderId); |
| 4235 | shaderId = 0; |
| 4236 | } |
| 4237 | else |
| 4238 | { |
| 4239 | switch (type) |
| 4240 | { |
| 4241 | case GL_VERTEX_SHADER: TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Vertex shader compiled successfully", shaderId); break; |
| 4242 | case GL_FRAGMENT_SHADER: TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Fragment shader compiled successfully", shaderId); break; |
| 4243 | //case GL_GEOMETRY_SHADER: |
| 4244 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4245 | case GL_COMPUTE_SHADER: TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Compute shader compiled successfully", shaderId); break; |
| 4246 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 4247 | case GL_COMPUTE_SHADER: TRACELOG(RL_LOG_WARNING, "SHADER: Compute shaders not enabled. Define GRAPHICS_API_OPENGL_43", shaderId); break; |
| 4248 | #endif |
| 4249 | default: break; |
| 4250 | } |
| 4251 | } |
| 4252 | #endif |
| 4253 | |
| 4254 | return shaderId; |
| 4255 | } |
| 4256 | |
| 4257 | // Load shader program from code strings |
| 4258 | // NOTE: If shader string is NULL, using default vertex/fragment shaders |
| 4259 | unsigned int rlLoadShaderProgram(const char *vsCode, const char *fsCode) |
| 4260 | { |
| 4261 | unsigned int id = 0; // Shader program id |
| 4262 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return id; } |
| 4263 | |
| 4264 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4265 | unsigned int vertexShaderId = 0; |
| 4266 | unsigned int fragmentShaderId = 0; |
| 4267 | |
| 4268 | // Compile vertex shader (if provided) |
| 4269 | // NOTE: If not vertex shader is provided, use default one |
| 4270 | if (vsCode != NULL) vertexShaderId = rlLoadShader(vsCode, GL_VERTEX_SHADER); |
| 4271 | else vertexShaderId = RLGL.State.defaultVShaderId; |
| 4272 | |
| 4273 | // Compile fragment shader (if provided) |
| 4274 | // NOTE: If not vertex shader is provided, use default one |
| 4275 | if (fsCode != NULL) fragmentShaderId = rlLoadShader(fsCode, GL_FRAGMENT_SHADER); |
| 4276 | else fragmentShaderId = RLGL.State.defaultFShaderId; |
| 4277 | |
| 4278 | // In case vertex and fragment shader are the default ones, no need to recompile, assign the default shader program id |
| 4279 | if ((vertexShaderId == RLGL.State.defaultVShaderId) && (fragmentShaderId == RLGL.State.defaultFShaderId)) id = RLGL.State.defaultShaderId; |
| 4280 | else if ((vertexShaderId > 0) && (fragmentShaderId > 0)) |
| 4281 | { |
| 4282 | // One of or both shader are new, a new shader program needs to be compiled |
| 4283 | id = rlLoadShaderProgramEx(vertexShaderId, fragmentShaderId); |
| 4284 | |
| 4285 | // Detaching and deleting vertex/fragment shaders (if not default ones) |
| 4286 | // WARNING: Detach shader before deletion to make sure memory is freed |
| 4287 | if (vertexShaderId != RLGL.State.defaultVShaderId) |
| 4288 | { |
| 4289 | // WARNING: Shader program linkage could fail and returned id is 0 |
| 4290 | if (id > 0) glDetachShader(id, vertexShaderId); |
| 4291 | glDeleteShader(vertexShaderId); |
| 4292 | } |
| 4293 | if (fragmentShaderId != RLGL.State.defaultFShaderId) |
| 4294 | { |
| 4295 | // WARNING: Shader program linkage could fail and returned id is 0 |
| 4296 | if (id > 0) glDetachShader(id, fragmentShaderId); |
| 4297 | glDeleteShader(fragmentShaderId); |
| 4298 | } |
| 4299 | |
| 4300 | // In case shader program loading failed, assign default shader |
| 4301 | if (id == 0) |
| 4302 | { |
| 4303 | // In case shader loading fails, reassigning default shader |
| 4304 | TRACELOG(RL_LOG_WARNING, "SHADER: Failed to load custom shader code, using default shader"); |
| 4305 | id = RLGL.State.defaultShaderId; |
| 4306 | } |
| 4307 | /* |
| 4308 | else |
| 4309 | { |
| 4310 | // Get available shader uniforms |
| 4311 | // NOTE: This information is useful for debug... |
| 4312 | int uniformCount = -1; |
| 4313 | glGetProgramiv(id, GL_ACTIVE_UNIFORMS, &uniformCount); |
| 4314 | |
| 4315 | for (int i = 0; i < uniformCount; i++) |
| 4316 | { |
| 4317 | int namelen = -1; |
| 4318 | int num = -1; |
| 4319 | char name[256] = { 0 }; // Assume no variable names longer than 256 |
| 4320 | GLenum type = GL_ZERO; |
| 4321 | |
| 4322 | // Get the name of the uniforms |
| 4323 | glGetActiveUniform(id, i, sizeof(name) - 1, &namelen, &num, &type, name); |
| 4324 | |
| 4325 | name[namelen] = 0; |
| 4326 | TRACELOG(RL_LOG_DEBUG, "SHADER: [ID %i] Active uniform (%s) set at location: %i", id, name, glGetUniformLocation(id, name)); |
| 4327 | } |
| 4328 | } |
| 4329 | */ |
| 4330 | } |
| 4331 | #endif |
| 4332 | |
| 4333 | return id; |
| 4334 | } |
| 4335 | |
| 4336 | // Load shader program from already loaded shader ids |
| 4337 | unsigned int rlLoadShaderProgramEx(unsigned int vsId, unsigned int fsId) |
| 4338 | { |
| 4339 | unsigned int programId = 0; |
| 4340 | if (!isGpuReady) { TRACELOG(RL_LOG_WARNING, "GL: GPU is not ready to load data, trying to load before InitWindow()?"); return programId; } |
| 4341 | |
| 4342 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4343 | GLint success = 0; |
| 4344 | programId = glCreateProgram(); |
| 4345 | |
| 4346 | glAttachShader(programId, vsId); |
| 4347 | glAttachShader(programId, fsId); |
| 4348 | |
| 4349 | // Default attribute shader locations must be bound before linking |
| 4350 | // NOTE: There is no problem with binding a generic attribute index to an attribute variable name |
| 4351 | // that is never used; if some attrib name is no found on the shader, it locations becomes -1 |
| 4352 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION); |
| 4353 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD); |
| 4354 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL); |
| 4355 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_COLOR, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR); |
| 4356 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TANGENT, RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT); |
| 4357 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD2, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2); |
| 4358 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_INSTANCETRANSFORM, RL_DEFAULT_SHADER_ATTRIB_NAME_INSTANCETRANSFORM); |
| 4359 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEINDICES, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEINDICES); |
| 4360 | glBindAttribLocation(programId, RL_DEFAULT_SHADER_ATTRIB_LOCATION_BONEWEIGHTS, RL_DEFAULT_SHADER_ATTRIB_NAME_BONEWEIGHTS); |
| 4361 | |
| 4362 | glLinkProgram(programId); |
| 4363 | |
| 4364 | // NOTE: All uniform variables are intitialised to 0 when a program links |
| 4365 | |
| 4366 | glGetProgramiv(programId, GL_LINK_STATUS, &success); |
| 4367 | |
| 4368 | if (success == GL_FALSE) |
| 4369 | { |
| 4370 | TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to link shader program", programId); |
| 4371 | |
| 4372 | int maxLength = 0; |
| 4373 | glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &maxLength); |
| 4374 | |
| 4375 | if (maxLength > 0) |
| 4376 | { |
| 4377 | int length = 0; |
| 4378 | char *log = (char *)RL_CALLOC(maxLength, sizeof(char)); |
| 4379 | glGetProgramInfoLog(programId, maxLength, &length, log); |
| 4380 | TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Link error: %s", programId, log); |
| 4381 | RL_FREE(log); |
| 4382 | } |
| 4383 | |
| 4384 | glDeleteProgram(programId); |
| 4385 | |
| 4386 | programId = 0; |
| 4387 | } |
| 4388 | else |
| 4389 | { |
| 4390 | // Get the size of compiled shader program (not available on OpenGL ES 2.0) |
| 4391 | // NOTE: If GL_LINK_STATUS is GL_FALSE, program binary length is zero |
| 4392 | //GLint binarySize = 0; |
| 4393 | //glGetProgramiv(id, GL_PROGRAM_BINARY_LENGTH, &binarySize); |
| 4394 | |
| 4395 | TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Program shader loaded successfully", programId); |
| 4396 | } |
| 4397 | #endif |
| 4398 | return programId; |
| 4399 | } |
| 4400 | |
| 4401 | // Load compute shader program |
| 4402 | unsigned int rlLoadShaderProgramCompute(unsigned int csId) |
| 4403 | { |
| 4404 | unsigned int programId = 0; |
| 4405 | |
| 4406 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4407 | GLint success = 0; |
| 4408 | programId = glCreateProgram(); |
| 4409 | |
| 4410 | glAttachShader(programId, csId); |
| 4411 | |
| 4412 | glLinkProgram(programId); |
| 4413 | |
| 4414 | // NOTE: All uniform variables are intitialised to 0 when a program links |
| 4415 | |
| 4416 | glGetProgramiv(programId, GL_LINK_STATUS, &success); |
| 4417 | |
| 4418 | if (success == GL_FALSE) |
| 4419 | { |
| 4420 | TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to link compute shader program", programId); |
| 4421 | |
| 4422 | int maxLength = 0; |
| 4423 | glGetProgramiv(programId, GL_INFO_LOG_LENGTH, &maxLength); |
| 4424 | |
| 4425 | if (maxLength > 0) |
| 4426 | { |
| 4427 | int length = 0; |
| 4428 | char *log = (char *)RL_CALLOC(maxLength, sizeof(char)); |
| 4429 | glGetProgramInfoLog(programId, maxLength, &length, log); |
| 4430 | TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Link error: %s", programId, log); |
| 4431 | RL_FREE(log); |
| 4432 | } |
| 4433 | |
| 4434 | glDeleteProgram(programId); |
| 4435 | |
| 4436 | programId = 0; |
| 4437 | } |
| 4438 | else |
| 4439 | { |
| 4440 | // Get the size of compiled shader program (not available on OpenGL ES 2.0) |
| 4441 | // NOTE: If GL_LINK_STATUS is GL_FALSE, program binary length is zero |
| 4442 | //GLint binarySize = 0; |
| 4443 | //glGetProgramiv(id, GL_PROGRAM_BINARY_LENGTH, &binarySize); |
| 4444 | |
| 4445 | TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Compute shader program loaded successfully", programId); |
| 4446 | } |
| 4447 | #else |
| 4448 | TRACELOG(RL_LOG_WARNING, "SHADER: Compute shaders not supported, enable GRAPHICS_API_OPENGL_43"); |
| 4449 | #endif |
| 4450 | |
| 4451 | return programId; |
| 4452 | } |
| 4453 | |
| 4454 | // Delete shader |
| 4455 | void rlUnloadShader(unsigned int id) |
| 4456 | { |
| 4457 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4458 | glDeleteShader(id); |
| 4459 | |
| 4460 | TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Unloaded shader data from VRAM (GPU)", id); |
| 4461 | #endif |
| 4462 | } |
| 4463 | |
| 4464 | // Unload shader program |
| 4465 | void rlUnloadShaderProgram(unsigned int id) |
| 4466 | { |
| 4467 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4468 | glDeleteProgram(id); |
| 4469 | |
| 4470 | TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Unloaded shader program data from VRAM (GPU)", id); |
| 4471 | #endif |
| 4472 | } |
| 4473 | |
| 4474 | // Get shader location uniform |
| 4475 | // NOTE: First parameter refers to shader program id |
| 4476 | int rlGetLocationUniform(unsigned int id, const char *uniformName) |
| 4477 | { |
| 4478 | int location = -1; |
| 4479 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4480 | location = glGetUniformLocation(id, uniformName); |
| 4481 | |
| 4482 | //if (location == -1) TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to find shader uniform: %s", shaderId, uniformName); |
| 4483 | //else TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Shader uniform (%s) set at location: %i", shaderId, uniformName, location); |
| 4484 | #endif |
| 4485 | return location; |
| 4486 | } |
| 4487 | |
| 4488 | // Get shader location attribute |
| 4489 | // NOTE: First parameter refers to shader program id |
| 4490 | int rlGetLocationAttrib(unsigned int id, const char *attribName) |
| 4491 | { |
| 4492 | int location = -1; |
| 4493 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4494 | location = glGetAttribLocation(id, attribName); |
| 4495 | |
| 4496 | //if (location == -1) TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to find shader attribute: %s", shaderId, attribName); |
| 4497 | //else TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Shader attribute (%s) set at location: %i", shaderId, attribName, location); |
| 4498 | #endif |
| 4499 | return location; |
| 4500 | } |
| 4501 | |
| 4502 | // Set shader value uniform |
| 4503 | void rlSetUniform(int locIndex, const void *value, int uniformType, int count) |
| 4504 | { |
| 4505 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4506 | switch (uniformType) |
| 4507 | { |
| 4508 | case RL_SHADER_UNIFORM_FLOAT: glUniform1fv(locIndex, count, (float *)value); break; |
| 4509 | case RL_SHADER_UNIFORM_VEC2: glUniform2fv(locIndex, count, (float *)value); break; |
| 4510 | case RL_SHADER_UNIFORM_VEC3: glUniform3fv(locIndex, count, (float *)value); break; |
| 4511 | case RL_SHADER_UNIFORM_VEC4: glUniform4fv(locIndex, count, (float *)value); break; |
| 4512 | case RL_SHADER_UNIFORM_INT: glUniform1iv(locIndex, count, (int *)value); break; |
| 4513 | case RL_SHADER_UNIFORM_IVEC2: glUniform2iv(locIndex, count, (int *)value); break; |
| 4514 | case RL_SHADER_UNIFORM_IVEC3: glUniform3iv(locIndex, count, (int *)value); break; |
| 4515 | case RL_SHADER_UNIFORM_IVEC4: glUniform4iv(locIndex, count, (int *)value); break; |
| 4516 | #if !defined(GRAPHICS_API_OPENGL_ES2) |
| 4517 | case RL_SHADER_UNIFORM_UINT: glUniform1uiv(locIndex, count, (unsigned int *)value); break; |
| 4518 | case RL_SHADER_UNIFORM_UIVEC2: glUniform2uiv(locIndex, count, (unsigned int *)value); break; |
| 4519 | case RL_SHADER_UNIFORM_UIVEC3: glUniform3uiv(locIndex, count, (unsigned int *)value); break; |
| 4520 | case RL_SHADER_UNIFORM_UIVEC4: glUniform4uiv(locIndex, count, (unsigned int *)value); break; |
| 4521 | #endif |
| 4522 | case RL_SHADER_UNIFORM_SAMPLER2D: glUniform1iv(locIndex, count, (int *)value); break; |
| 4523 | default: TRACELOG(RL_LOG_WARNING, "SHADER: Failed to set uniform value, data type not recognized"); |
| 4524 | } |
| 4525 | #endif |
| 4526 | } |
| 4527 | |
| 4528 | // Set shader value attribute |
| 4529 | void rlSetVertexAttributeDefault(int locIndex, const void *value, int attribType, int count) |
| 4530 | { |
| 4531 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4532 | switch (attribType) |
| 4533 | { |
| 4534 | case RL_SHADER_ATTRIB_FLOAT: if (count == 1) glVertexAttrib1fv(locIndex, (float *)value); break; |
| 4535 | case RL_SHADER_ATTRIB_VEC2: if (count == 2) glVertexAttrib2fv(locIndex, (float *)value); break; |
| 4536 | case RL_SHADER_ATTRIB_VEC3: if (count == 3) glVertexAttrib3fv(locIndex, (float *)value); break; |
| 4537 | case RL_SHADER_ATTRIB_VEC4: if (count == 4) glVertexAttrib4fv(locIndex, (float *)value); break; |
| 4538 | default: TRACELOG(RL_LOG_WARNING, "SHADER: Failed to set attrib default value, data type not recognized"); |
| 4539 | } |
| 4540 | #endif |
| 4541 | } |
| 4542 | |
| 4543 | // Set shader value uniform matrix |
| 4544 | void rlSetUniformMatrix(int locIndex, Matrix mat) |
| 4545 | { |
| 4546 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4547 | glUniformMatrix4fv(locIndex, 1, false, rlMatrixToFloat(mat)); |
| 4548 | #endif |
| 4549 | } |
| 4550 | |
| 4551 | // Set shader value uniform matrix |
| 4552 | void rlSetUniformMatrices(int locIndex, const Matrix *matrices, int count) |
| 4553 | { |
| 4554 | #if defined(GRAPHICS_API_OPENGL_33) |
| 4555 | glUniformMatrix4fv(locIndex, count, true, (const float *)matrices); |
| 4556 | #elif defined(GRAPHICS_API_OPENGL_ES2) |
| 4557 | // WARNING: WebGL does not support Matrix transpose ("true" parameter) |
| 4558 | // REF: https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/uniformMatrix |
| 4559 | glUniformMatrix4fv(locIndex, count, false, (const float *)matrices); |
| 4560 | #endif |
| 4561 | } |
| 4562 | |
| 4563 | // Set shader value uniform sampler |
| 4564 | void rlSetUniformSampler(int locIndex, unsigned int textureId) |
| 4565 | { |
| 4566 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4567 | // Check if texture is already active |
| 4568 | for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) |
| 4569 | { |
| 4570 | if (RLGL.State.activeTextureId[i] == textureId) |
| 4571 | { |
| 4572 | glUniform1i(locIndex, 1 + i); |
| 4573 | return; |
| 4574 | } |
| 4575 | } |
| 4576 | |
| 4577 | // Register a new active texture for the internal batch system |
| 4578 | // NOTE: Default texture is always activated as GL_TEXTURE0 |
| 4579 | for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) |
| 4580 | { |
| 4581 | if (RLGL.State.activeTextureId[i] == 0) |
| 4582 | { |
| 4583 | glUniform1i(locIndex, 1 + i); // Activate new texture unit |
| 4584 | RLGL.State.activeTextureId[i] = textureId; // Save texture id for binding on drawing |
| 4585 | break; |
| 4586 | } |
| 4587 | } |
| 4588 | #endif |
| 4589 | } |
| 4590 | |
| 4591 | // Set shader currently active (id and locations) |
| 4592 | void rlSetShader(unsigned int id, int *locs) |
| 4593 | { |
| 4594 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4595 | if (RLGL.State.currentShaderId != id) |
| 4596 | { |
| 4597 | rlDrawRenderBatch(RLGL.currentBatch); |
| 4598 | RLGL.State.currentShaderId = id; |
| 4599 | RLGL.State.currentShaderLocs = locs; |
| 4600 | } |
| 4601 | #endif |
| 4602 | } |
| 4603 | |
| 4604 | // Dispatch compute shader (equivalent to *draw* for graphics pilepine) |
| 4605 | void rlComputeShaderDispatch(unsigned int groupX, unsigned int groupY, unsigned int groupZ) |
| 4606 | { |
| 4607 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4608 | glDispatchCompute(groupX, groupY, groupZ); |
| 4609 | #endif |
| 4610 | } |
| 4611 | |
| 4612 | // Load shader storage buffer object (SSBO) |
| 4613 | unsigned int rlLoadShaderBuffer(unsigned int size, const void *data, int usageHint) |
| 4614 | { |
| 4615 | unsigned int ssbo = 0; |
| 4616 | |
| 4617 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4618 | glGenBuffers(1, &ssbo); |
| 4619 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, ssbo); |
| 4620 | glBufferData(GL_SHADER_STORAGE_BUFFER, size, data, usageHint? usageHint : RL_STREAM_COPY); |
| 4621 | if (data == NULL) glClearBufferData(GL_SHADER_STORAGE_BUFFER, GL_R8UI, GL_RED_INTEGER, GL_UNSIGNED_BYTE, NULL); // Clear buffer data to 0 |
| 4622 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0); |
| 4623 | #else |
| 4624 | TRACELOG(RL_LOG_WARNING, "SSBO: SSBO not enabled. Define GRAPHICS_API_OPENGL_43"); |
| 4625 | #endif |
| 4626 | |
| 4627 | return ssbo; |
| 4628 | } |
| 4629 | |
| 4630 | // Unload shader storage buffer object (SSBO) |
| 4631 | void rlUnloadShaderBuffer(unsigned int ssboId) |
| 4632 | { |
| 4633 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4634 | glDeleteBuffers(1, &ssboId); |
| 4635 | #else |
| 4636 | TRACELOG(RL_LOG_WARNING, "SSBO: SSBO not enabled. Define GRAPHICS_API_OPENGL_43"); |
| 4637 | #endif |
| 4638 | } |
| 4639 | |
| 4640 | // Update SSBO buffer data |
| 4641 | void rlUpdateShaderBuffer(unsigned int id, const void *data, unsigned int dataSize, unsigned int offset) |
| 4642 | { |
| 4643 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4644 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, id); |
| 4645 | glBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, dataSize, data); |
| 4646 | #endif |
| 4647 | } |
| 4648 | |
| 4649 | // Get SSBO buffer size |
| 4650 | unsigned int rlGetShaderBufferSize(unsigned int id) |
| 4651 | { |
| 4652 | unsigned int result = 0; |
| 4653 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4654 | GLint64 size = 0; |
| 4655 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, id); |
| 4656 | glGetBufferParameteri64v(GL_SHADER_STORAGE_BUFFER, GL_BUFFER_SIZE, &size); |
| 4657 | if (size > 0) result = (unsigned int)size; |
| 4658 | #endif |
| 4659 | return result; |
| 4660 | } |
| 4661 | |
| 4662 | // Read SSBO buffer data (GPU->CPU) |
| 4663 | void rlReadShaderBuffer(unsigned int id, void *dest, unsigned int count, unsigned int offset) |
| 4664 | { |
| 4665 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4666 | glBindBuffer(GL_SHADER_STORAGE_BUFFER, id); |
| 4667 | glGetBufferSubData(GL_SHADER_STORAGE_BUFFER, offset, count, dest); |
| 4668 | #endif |
| 4669 | } |
| 4670 | |
| 4671 | // Bind SSBO buffer |
| 4672 | void rlBindShaderBuffer(unsigned int id, unsigned int index) |
| 4673 | { |
| 4674 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4675 | glBindBufferBase(GL_SHADER_STORAGE_BUFFER, index, id); |
| 4676 | #endif |
| 4677 | } |
| 4678 | |
| 4679 | // Copy SSBO buffer data |
| 4680 | void rlCopyShaderBuffer(unsigned int destId, unsigned int srcId, unsigned int destOffset, unsigned int srcOffset, unsigned int count) |
| 4681 | { |
| 4682 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4683 | glBindBuffer(GL_COPY_READ_BUFFER, srcId); |
| 4684 | glBindBuffer(GL_COPY_WRITE_BUFFER, destId); |
| 4685 | glCopyBufferSubData(GL_COPY_READ_BUFFER, GL_COPY_WRITE_BUFFER, srcOffset, destOffset, count); |
| 4686 | #endif |
| 4687 | } |
| 4688 | |
| 4689 | // Bind image texture |
| 4690 | void rlBindImageTexture(unsigned int id, unsigned int index, int format, bool readonly) |
| 4691 | { |
| 4692 | #if defined(GRAPHICS_API_OPENGL_43) |
| 4693 | unsigned int glInternalFormat = 0, glFormat = 0, glType = 0; |
| 4694 | |
| 4695 | rlGetGlTextureFormats(format, &glInternalFormat, &glFormat, &glType); |
| 4696 | glBindImageTexture(index, id, 0, 0, 0, readonly? GL_READ_ONLY : GL_READ_WRITE, glInternalFormat); |
| 4697 | #else |
| 4698 | TRACELOG(RL_LOG_WARNING, "TEXTURE: Image texture binding not enabled. Define GRAPHICS_API_OPENGL_43"); |
| 4699 | #endif |
| 4700 | } |
| 4701 | |
| 4702 | // Matrix state management |
| 4703 | //----------------------------------------------------------------------------------------- |
| 4704 | // Get internal modelview matrix |
| 4705 | Matrix rlGetMatrixModelview(void) |
| 4706 | { |
| 4707 | Matrix matrix = rlMatrixIdentity(); |
| 4708 | #if defined(GRAPHICS_API_OPENGL_11) |
| 4709 | float mat[16]; |
| 4710 | glGetFloatv(GL_MODELVIEW_MATRIX, mat); |
| 4711 | matrix.m0 = mat[0]; |
| 4712 | matrix.m1 = mat[1]; |
| 4713 | matrix.m2 = mat[2]; |
| 4714 | matrix.m3 = mat[3]; |
| 4715 | matrix.m4 = mat[4]; |
| 4716 | matrix.m5 = mat[5]; |
| 4717 | matrix.m6 = mat[6]; |
| 4718 | matrix.m7 = mat[7]; |
| 4719 | matrix.m8 = mat[8]; |
| 4720 | matrix.m9 = mat[9]; |
| 4721 | matrix.m10 = mat[10]; |
| 4722 | matrix.m11 = mat[11]; |
| 4723 | matrix.m12 = mat[12]; |
| 4724 | matrix.m13 = mat[13]; |
| 4725 | matrix.m14 = mat[14]; |
| 4726 | matrix.m15 = mat[15]; |
| 4727 | #else |
| 4728 | matrix = RLGL.State.modelview; |
| 4729 | #endif |
| 4730 | return matrix; |
| 4731 | } |
| 4732 | |
| 4733 | // Get internal projection matrix |
| 4734 | Matrix rlGetMatrixProjection(void) |
| 4735 | { |
| 4736 | #if defined(GRAPHICS_API_OPENGL_11) |
| 4737 | float mat[16]; |
| 4738 | glGetFloatv(GL_PROJECTION_MATRIX,mat); |
| 4739 | Matrix m; |
| 4740 | m.m0 = mat[0]; |
| 4741 | m.m1 = mat[1]; |
| 4742 | m.m2 = mat[2]; |
| 4743 | m.m3 = mat[3]; |
| 4744 | m.m4 = mat[4]; |
| 4745 | m.m5 = mat[5]; |
| 4746 | m.m6 = mat[6]; |
| 4747 | m.m7 = mat[7]; |
| 4748 | m.m8 = mat[8]; |
| 4749 | m.m9 = mat[9]; |
| 4750 | m.m10 = mat[10]; |
| 4751 | m.m11 = mat[11]; |
| 4752 | m.m12 = mat[12]; |
| 4753 | m.m13 = mat[13]; |
| 4754 | m.m14 = mat[14]; |
| 4755 | m.m15 = mat[15]; |
| 4756 | return m; |
| 4757 | #else |
| 4758 | return RLGL.State.projection; |
| 4759 | #endif |
| 4760 | } |
| 4761 | |
| 4762 | // Get internal accumulated transform matrix |
| 4763 | Matrix rlGetMatrixTransform(void) |
| 4764 | { |
| 4765 | Matrix mat = rlMatrixIdentity(); |
| 4766 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4767 | // TODO: Consider possible transform matrices in the RLGL.State.stack |
| 4768 | //Matrix matStackTransform = rlMatrixIdentity(); |
| 4769 | //for (int i = RLGL.State.stackCounter; i > 0; i--) matStackTransform = rlMatrixMultiply(RLGL.State.stack[i], matStackTransform); |
| 4770 | |
| 4771 | mat = RLGL.State.transform; |
| 4772 | #endif |
| 4773 | return mat; |
| 4774 | } |
| 4775 | |
| 4776 | // Get internal projection matrix for stereo render (selected eye) |
| 4777 | Matrix rlGetMatrixProjectionStereo(int eye) |
| 4778 | { |
| 4779 | Matrix mat = rlMatrixIdentity(); |
| 4780 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4781 | mat = RLGL.State.projectionStereo[eye]; |
| 4782 | #endif |
| 4783 | return mat; |
| 4784 | } |
| 4785 | |
| 4786 | // Get internal view offset matrix for stereo render (selected eye) |
| 4787 | Matrix rlGetMatrixViewOffsetStereo(int eye) |
| 4788 | { |
| 4789 | Matrix mat = rlMatrixIdentity(); |
| 4790 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4791 | mat = RLGL.State.viewOffsetStereo[eye]; |
| 4792 | #endif |
| 4793 | return mat; |
| 4794 | } |
| 4795 | |
| 4796 | // Set a custom modelview matrix (replaces internal modelview matrix) |
| 4797 | void rlSetMatrixModelview(Matrix view) |
| 4798 | { |
| 4799 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4800 | RLGL.State.modelview = view; |
| 4801 | #endif |
| 4802 | } |
| 4803 | |
| 4804 | // Set a custom projection matrix (replaces internal projection matrix) |
| 4805 | void rlSetMatrixProjection(Matrix projection) |
| 4806 | { |
| 4807 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4808 | RLGL.State.projection = projection; |
| 4809 | #endif |
| 4810 | } |
| 4811 | |
| 4812 | // Set eyes projection matrices for stereo rendering |
| 4813 | void rlSetMatrixProjectionStereo(Matrix right, Matrix left) |
| 4814 | { |
| 4815 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4816 | RLGL.State.projectionStereo[0] = right; |
| 4817 | RLGL.State.projectionStereo[1] = left; |
| 4818 | #endif |
| 4819 | } |
| 4820 | |
| 4821 | // Set eyes view offsets matrices for stereo rendering |
| 4822 | void rlSetMatrixViewOffsetStereo(Matrix right, Matrix left) |
| 4823 | { |
| 4824 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4825 | RLGL.State.viewOffsetStereo[0] = right; |
| 4826 | RLGL.State.viewOffsetStereo[1] = left; |
| 4827 | #endif |
| 4828 | } |
| 4829 | |
| 4830 | // Load and draw a quad in NDC |
| 4831 | void rlLoadDrawQuad(void) |
| 4832 | { |
| 4833 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4834 | unsigned int quadVAO = 0; |
| 4835 | unsigned int quadVBO = 0; |
| 4836 | |
| 4837 | float vertices[] = { |
| 4838 | // Positions Texcoords |
| 4839 | -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, |
| 4840 | -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, |
| 4841 | 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, |
| 4842 | 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, |
| 4843 | }; |
| 4844 | |
| 4845 | // Gen VAO to contain VBO |
| 4846 | glGenVertexArrays(1, &quadVAO); |
| 4847 | glBindVertexArray(quadVAO); |
| 4848 | |
| 4849 | // Gen and fill vertex buffer (VBO) |
| 4850 | glGenBuffers(1, &quadVBO); |
| 4851 | glBindBuffer(GL_ARRAY_BUFFER, quadVBO); |
| 4852 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); |
| 4853 | |
| 4854 | // Bind vertex attributes (position, texcoords) |
| 4855 | glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION); |
| 4856 | glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void *)0); // Positions |
| 4857 | glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD); |
| 4858 | glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void *)(3*sizeof(float))); // Texcoords |
| 4859 | |
| 4860 | // Draw quad |
| 4861 | glBindVertexArray(quadVAO); |
| 4862 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 4863 | glBindVertexArray(0); |
| 4864 | |
| 4865 | // Delete buffers (VBO and VAO) |
| 4866 | glDeleteBuffers(1, &quadVBO); |
| 4867 | glDeleteVertexArrays(1, &quadVAO); |
| 4868 | #endif |
| 4869 | } |
| 4870 | |
| 4871 | // Load and draw a cube in NDC |
| 4872 | void rlLoadDrawCube(void) |
| 4873 | { |
| 4874 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4875 | unsigned int cubeVAO = 0; |
| 4876 | unsigned int cubeVBO = 0; |
| 4877 | |
| 4878 | float vertices[] = { |
| 4879 | // Positions Normals Texcoords |
| 4880 | -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, |
| 4881 | 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, |
| 4882 | 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, |
| 4883 | 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, |
| 4884 | -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, |
| 4885 | -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, |
| 4886 | -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, |
| 4887 | 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, |
| 4888 | 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, |
| 4889 | 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, |
| 4890 | -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, |
| 4891 | -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, |
| 4892 | -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, |
| 4893 | -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 1.0f, |
| 4894 | -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 4895 | -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 4896 | -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 4897 | -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, |
| 4898 | 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, |
| 4899 | 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 4900 | 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, |
| 4901 | 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, |
| 4902 | 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, |
| 4903 | 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, |
| 4904 | -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, |
| 4905 | 1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 1.0f, |
| 4906 | 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, |
| 4907 | 1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, |
| 4908 | -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, |
| 4909 | -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, 0.0f, 1.0f, |
| 4910 | -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, |
| 4911 | 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, |
| 4912 | 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, |
| 4913 | 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, |
| 4914 | -1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, |
| 4915 | -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f |
| 4916 | }; |
| 4917 | |
| 4918 | // Gen VAO to contain VBO |
| 4919 | glGenVertexArrays(1, &cubeVAO); |
| 4920 | glBindVertexArray(cubeVAO); |
| 4921 | |
| 4922 | // Gen and fill vertex buffer (VBO) |
| 4923 | glGenBuffers(1, &cubeVBO); |
| 4924 | glBindBuffer(GL_ARRAY_BUFFER, cubeVBO); |
| 4925 | glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); |
| 4926 | |
| 4927 | // Bind vertex attributes (position, normals, texcoords) |
| 4928 | glBindVertexArray(cubeVAO); |
| 4929 | glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION); |
| 4930 | glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_POSITION, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void *)0); // Positions |
| 4931 | glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL); |
| 4932 | glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_NORMAL, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void *)(3*sizeof(float))); // Normals |
| 4933 | glEnableVertexAttribArray(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD); |
| 4934 | glVertexAttribPointer(RL_DEFAULT_SHADER_ATTRIB_LOCATION_TEXCOORD, 2, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void *)(6*sizeof(float))); // Texcoords |
| 4935 | glBindBuffer(GL_ARRAY_BUFFER, 0); |
| 4936 | glBindVertexArray(0); |
| 4937 | |
| 4938 | // Draw cube |
| 4939 | glBindVertexArray(cubeVAO); |
| 4940 | glDrawArrays(GL_TRIANGLES, 0, 36); |
| 4941 | glBindVertexArray(0); |
| 4942 | |
| 4943 | // Delete VBO and VAO |
| 4944 | glDeleteBuffers(1, &cubeVBO); |
| 4945 | glDeleteVertexArrays(1, &cubeVAO); |
| 4946 | #endif |
| 4947 | } |
| 4948 | |
| 4949 | // Get name string for pixel format |
| 4950 | const char *rlGetPixelFormatName(unsigned int format) |
| 4951 | { |
| 4952 | switch (format) |
| 4953 | { |
| 4954 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: return "GRAYSCALE"; break; // 8 bit per pixel (no alpha) |
| 4955 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: return "GRAY_ALPHA"; break; // 8*2 bpp (2 channels) |
| 4956 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5: return "R5G6B5"; break; // 16 bpp |
| 4957 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: return "R8G8B8"; break; // 24 bpp |
| 4958 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: return "R5G5B5A1"; break; // 16 bpp (1 bit alpha) |
| 4959 | case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: return "R4G4B4A4"; break; // 16 bpp (4 bit alpha) |
| 4960 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: return "R8G8B8A8"; break; // 32 bpp |
| 4961 | case RL_PIXELFORMAT_UNCOMPRESSED_R32: return "R32"; break; // 32 bpp (1 channel - float) |
| 4962 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: return "R32G32B32"; break; // 32*3 bpp (3 channels - float) |
| 4963 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: return "R32G32B32A32"; break; // 32*4 bpp (4 channels - float) |
| 4964 | case RL_PIXELFORMAT_UNCOMPRESSED_R16: return "R16"; break; // 16 bpp (1 channel - half float) |
| 4965 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: return "R16G16B16"; break; // 16*3 bpp (3 channels - half float) |
| 4966 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: return "R16G16B16A16"; break; // 16*4 bpp (4 channels - half float) |
| 4967 | case RL_PIXELFORMAT_COMPRESSED_DXT1_RGB: return "DXT1_RGB"; break; // 4 bpp (no alpha) |
| 4968 | case RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA: return "DXT1_RGBA"; break; // 4 bpp (1 bit alpha) |
| 4969 | case RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA: return "DXT3_RGBA"; break; // 8 bpp |
| 4970 | case RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA: return "DXT5_RGBA"; break; // 8 bpp |
| 4971 | case RL_PIXELFORMAT_COMPRESSED_ETC1_RGB: return "ETC1_RGB"; break; // 4 bpp |
| 4972 | case RL_PIXELFORMAT_COMPRESSED_ETC2_RGB: return "ETC2_RGB"; break; // 4 bpp |
| 4973 | case RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA: return "ETC2_RGBA"; break; // 8 bpp |
| 4974 | case RL_PIXELFORMAT_COMPRESSED_PVRT_RGB: return "PVRT_RGB"; break; // 4 bpp |
| 4975 | case RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA: return "PVRT_RGBA"; break; // 4 bpp |
| 4976 | case RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA: return "ASTC_4x4_RGBA"; break; // 8 bpp |
| 4977 | case RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA: return "ASTC_8x8_RGBA"; break; // 2 bpp |
| 4978 | default: return "UNKNOWN"; break; |
| 4979 | } |
| 4980 | } |
| 4981 | |
| 4982 | //---------------------------------------------------------------------------------- |
| 4983 | // Module Functions Definition |
| 4984 | //---------------------------------------------------------------------------------- |
| 4985 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 4986 | // Load default shader (just vertex positioning and texture coloring) |
| 4987 | // NOTE: This shader program is used for internal buffers |
| 4988 | // NOTE: Loaded: RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs |
| 4989 | static void rlLoadShaderDefault(void) |
| 4990 | { |
| 4991 | RLGL.State.defaultShaderLocs = (int *)RL_CALLOC(RL_MAX_SHADER_LOCATIONS, sizeof(int)); |
| 4992 | |
| 4993 | // NOTE: All locations must be reseted to -1 (no location) |
| 4994 | for (int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++) RLGL.State.defaultShaderLocs[i] = -1; |
| 4995 | |
| 4996 | // Vertex shader directly defined, no external file required |
| 4997 | const char *defaultVShaderCode = |
| 4998 | #if defined(GRAPHICS_API_OPENGL_21) |
| 4999 | "#version 120 \n" |
| 5000 | "attribute vec3 vertexPosition; \n" |
| 5001 | "attribute vec2 vertexTexCoord; \n" |
| 5002 | "attribute vec4 vertexColor; \n" |
| 5003 | "varying vec2 fragTexCoord; \n" |
| 5004 | "varying vec4 fragColor; \n" |
| 5005 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 5006 | "#version 330 \n" |
| 5007 | "in vec3 vertexPosition; \n" |
| 5008 | "in vec2 vertexTexCoord; \n" |
| 5009 | "in vec4 vertexColor; \n" |
| 5010 | "out vec2 fragTexCoord; \n" |
| 5011 | "out vec4 fragColor; \n" |
| 5012 | #endif |
| 5013 | |
| 5014 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 5015 | "#version 300 es \n" |
| 5016 | "precision highp float; \n" // Precision highp is mandatory in vertex shaders, mediump may run as fp16 and misplace vertices |
| 5017 | "in vec3 vertexPosition; \n" |
| 5018 | "in vec2 vertexTexCoord; \n" |
| 5019 | "in vec4 vertexColor; \n" |
| 5020 | "out vec2 fragTexCoord; \n" |
| 5021 | "out vec4 fragColor; \n" |
| 5022 | #elif defined(GRAPHICS_API_OPENGL_ES2) |
| 5023 | "#version 100 \n" |
| 5024 | "precision highp float; \n" // Precision highp is mandatory in vertex shaders, mediump may run as fp16 and misplace vertices |
| 5025 | "attribute vec3 vertexPosition; \n" |
| 5026 | "attribute vec2 vertexTexCoord; \n" |
| 5027 | "attribute vec4 vertexColor; \n" |
| 5028 | "varying vec2 fragTexCoord; \n" |
| 5029 | "varying vec4 fragColor; \n" |
| 5030 | #endif |
| 5031 | |
| 5032 | "uniform mat4 mvp; \n" |
| 5033 | "void main() \n" |
| 5034 | "{ \n" |
| 5035 | " fragTexCoord = vertexTexCoord; \n" |
| 5036 | " fragColor = vertexColor; \n" |
| 5037 | " gl_Position = mvp*vec4(vertexPosition, 1.0); \n" |
| 5038 | "} \n"; |
| 5039 | |
| 5040 | // Fragment shader directly defined, no external file required |
| 5041 | const char *defaultFShaderCode = |
| 5042 | #if defined(GRAPHICS_API_OPENGL_21) |
| 5043 | "#version 120 \n" |
| 5044 | "varying vec2 fragTexCoord; \n" |
| 5045 | "varying vec4 fragColor; \n" |
| 5046 | "uniform sampler2D texture0; \n" |
| 5047 | "uniform vec4 colDiffuse; \n" |
| 5048 | "void main() \n" |
| 5049 | "{ \n" |
| 5050 | " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" |
| 5051 | " gl_FragColor = texelColor*colDiffuse*fragColor; \n" |
| 5052 | "} \n"; |
| 5053 | #elif defined(GRAPHICS_API_OPENGL_33) |
| 5054 | "#version 330 \n" |
| 5055 | "in vec2 fragTexCoord; \n" |
| 5056 | "in vec4 fragColor; \n" |
| 5057 | "out vec4 finalColor; \n" |
| 5058 | "uniform sampler2D texture0; \n" |
| 5059 | "uniform vec4 colDiffuse; \n" |
| 5060 | "void main() \n" |
| 5061 | "{ \n" |
| 5062 | " vec4 texelColor = texture(texture0, fragTexCoord); \n" |
| 5063 | " finalColor = texelColor*colDiffuse*fragColor; \n" |
| 5064 | "} \n"; |
| 5065 | #endif |
| 5066 | |
| 5067 | #if defined(GRAPHICS_API_OPENGL_ES3) |
| 5068 | "#version 300 es \n" |
| 5069 | "#ifdef GL_FRAGMENT_PRECISION_HIGH \n" |
| 5070 | "precision highp float; \n" |
| 5071 | "#else \n" |
| 5072 | "precision mediump float; \n" |
| 5073 | "#endif \n" |
| 5074 | "in vec2 fragTexCoord; \n" |
| 5075 | "in vec4 fragColor; \n" |
| 5076 | "out vec4 finalColor; \n" |
| 5077 | "uniform sampler2D texture0; \n" |
| 5078 | "uniform vec4 colDiffuse; \n" |
| 5079 | "void main() \n" |
| 5080 | "{ \n" |
| 5081 | " vec4 texelColor = texture(texture0, fragTexCoord); \n" |
| 5082 | " finalColor = texelColor*colDiffuse*fragColor; \n" |
| 5083 | "} \n"; |
| 5084 | #elif defined(GRAPHICS_API_OPENGL_ES2) |
| 5085 | "#version 100 \n" |
| 5086 | "#ifdef GL_FRAGMENT_PRECISION_HIGH \n" |
| 5087 | "precision highp float; \n" |
| 5088 | "#else \n" |
| 5089 | "precision mediump float; \n" |
| 5090 | "#endif \n" |
| 5091 | "varying vec2 fragTexCoord; \n" |
| 5092 | "varying vec4 fragColor; \n" |
| 5093 | "uniform sampler2D texture0; \n" |
| 5094 | "uniform vec4 colDiffuse; \n" |
| 5095 | "void main() \n" |
| 5096 | "{ \n" |
| 5097 | " vec4 texelColor = texture2D(texture0, fragTexCoord); \n" |
| 5098 | " gl_FragColor = texelColor*colDiffuse*fragColor; \n" |
| 5099 | "} \n"; |
| 5100 | #endif |
| 5101 | |
| 5102 | // NOTE: Compiled vertex/fragment shaders are not deleted, |
| 5103 | // they are kept for re-use as default shaders in case some shader loading fails |
| 5104 | RLGL.State.defaultVShaderId = rlLoadShader(defaultVShaderCode, GL_VERTEX_SHADER); // Compile default vertex shader |
| 5105 | RLGL.State.defaultFShaderId = rlLoadShader(defaultFShaderCode, GL_FRAGMENT_SHADER); // Compile default fragment shader |
| 5106 | |
| 5107 | RLGL.State.defaultShaderId = rlLoadShaderProgramEx(RLGL.State.defaultVShaderId, RLGL.State.defaultFShaderId); |
| 5108 | |
| 5109 | if (RLGL.State.defaultShaderId > 0) |
| 5110 | { |
| 5111 | TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Default shader loaded successfully", RLGL.State.defaultShaderId); |
| 5112 | |
| 5113 | // Set default shader locations: attributes locations |
| 5114 | RLGL.State.defaultShaderLocs[RL_SHADER_LOC_VERTEX_POSITION] = glGetAttribLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION); |
| 5115 | RLGL.State.defaultShaderLocs[RL_SHADER_LOC_VERTEX_TEXCOORD01] = glGetAttribLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD); |
| 5116 | RLGL.State.defaultShaderLocs[RL_SHADER_LOC_VERTEX_COLOR] = glGetAttribLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR); |
| 5117 | |
| 5118 | // Set default shader locations: uniform locations |
| 5119 | RLGL.State.defaultShaderLocs[RL_SHADER_LOC_MATRIX_MVP] = glGetUniformLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_UNIFORM_NAME_MVP); |
| 5120 | RLGL.State.defaultShaderLocs[RL_SHADER_LOC_COLOR_DIFFUSE] = glGetUniformLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR); |
| 5121 | RLGL.State.defaultShaderLocs[RL_SHADER_LOC_MAP_DIFFUSE] = glGetUniformLocation(RLGL.State.defaultShaderId, RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0); |
| 5122 | } |
| 5123 | else TRACELOG(RL_LOG_WARNING, "SHADER: [ID %i] Failed to load default shader", RLGL.State.defaultShaderId); |
| 5124 | } |
| 5125 | |
| 5126 | // Unload default shader |
| 5127 | // NOTE: Unloads: RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs |
| 5128 | static void rlUnloadShaderDefault(void) |
| 5129 | { |
| 5130 | glUseProgram(0); |
| 5131 | |
| 5132 | glDetachShader(RLGL.State.defaultShaderId, RLGL.State.defaultVShaderId); |
| 5133 | glDetachShader(RLGL.State.defaultShaderId, RLGL.State.defaultFShaderId); |
| 5134 | glDeleteShader(RLGL.State.defaultVShaderId); |
| 5135 | glDeleteShader(RLGL.State.defaultFShaderId); |
| 5136 | |
| 5137 | glDeleteProgram(RLGL.State.defaultShaderId); |
| 5138 | |
| 5139 | RL_FREE(RLGL.State.defaultShaderLocs); |
| 5140 | |
| 5141 | TRACELOG(RL_LOG_INFO, "SHADER: [ID %i] Default shader unloaded successfully", RLGL.State.defaultShaderId); |
| 5142 | } |
| 5143 | |
| 5144 | #if RLGL_SHOW_GL_DETAILS_INFO |
| 5145 | // Get compressed format official GL identifier name |
| 5146 | static const char *rlGetCompressedFormatName(int format) |
| 5147 | { |
| 5148 | switch (format) |
| 5149 | { |
| 5150 | // GL_EXT_texture_compression_s3tc |
| 5151 | case 0x83F0: return "GL_COMPRESSED_RGB_S3TC_DXT1_EXT"; break; |
| 5152 | case 0x83F1: return "GL_COMPRESSED_RGBA_S3TC_DXT1_EXT"; break; |
| 5153 | case 0x83F2: return "GL_COMPRESSED_RGBA_S3TC_DXT3_EXT"; break; |
| 5154 | case 0x83F3: return "GL_COMPRESSED_RGBA_S3TC_DXT5_EXT"; break; |
| 5155 | // GL_3DFX_texture_compression_FXT1 |
| 5156 | case 0x86B0: return "GL_COMPRESSED_RGB_FXT1_3DFX"; break; |
| 5157 | case 0x86B1: return "GL_COMPRESSED_RGBA_FXT1_3DFX"; break; |
| 5158 | // GL_IMG_texture_compression_pvrtc |
| 5159 | case 0x8C00: return "GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG"; break; |
| 5160 | case 0x8C01: return "GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG"; break; |
| 5161 | case 0x8C02: return "GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG"; break; |
| 5162 | case 0x8C03: return "GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG"; break; |
| 5163 | // GL_OES_compressed_ETC1_RGB8_texture |
| 5164 | case 0x8D64: return "GL_ETC1_RGB8_OES"; break; |
| 5165 | // GL_ARB_texture_compression_rgtc |
| 5166 | case 0x8DBB: return "GL_COMPRESSED_RED_RGTC1"; break; |
| 5167 | case 0x8DBC: return "GL_COMPRESSED_SIGNED_RED_RGTC1"; break; |
| 5168 | case 0x8DBD: return "GL_COMPRESSED_RG_RGTC2"; break; |
| 5169 | case 0x8DBE: return "GL_COMPRESSED_SIGNED_RG_RGTC2"; break; |
| 5170 | // GL_ARB_texture_compression_bptc |
| 5171 | case 0x8E8C: return "GL_COMPRESSED_RGBA_BPTC_UNORM_ARB"; break; |
| 5172 | case 0x8E8D: return "GL_COMPRESSED_SRGB_ALPHA_BPTC_UNORM_ARB"; break; |
| 5173 | case 0x8E8E: return "GL_COMPRESSED_RGB_BPTC_SIGNED_FLOAT_ARB"; break; |
| 5174 | case 0x8E8F: return "GL_COMPRESSED_RGB_BPTC_UNSIGNED_FLOAT_ARB"; break; |
| 5175 | // GL_ARB_ES3_compatibility |
| 5176 | case 0x9274: return "GL_COMPRESSED_RGB8_ETC2"; break; |
| 5177 | case 0x9275: return "GL_COMPRESSED_SRGB8_ETC2"; break; |
| 5178 | case 0x9276: return "GL_COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2"; break; |
| 5179 | case 0x9277: return "GL_COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2"; break; |
| 5180 | case 0x9278: return "GL_COMPRESSED_RGBA8_ETC2_EAC"; break; |
| 5181 | case 0x9279: return "GL_COMPRESSED_SRGB8_ALPHA8_ETC2_EAC"; break; |
| 5182 | case 0x9270: return "GL_COMPRESSED_R11_EAC"; break; |
| 5183 | case 0x9271: return "GL_COMPRESSED_SIGNED_R11_EAC"; break; |
| 5184 | case 0x9272: return "GL_COMPRESSED_RG11_EAC"; break; |
| 5185 | case 0x9273: return "GL_COMPRESSED_SIGNED_RG11_EAC"; break; |
| 5186 | // GL_KHR_texture_compression_astc_hdr |
| 5187 | case 0x93B0: return "GL_COMPRESSED_RGBA_ASTC_4x4_KHR"; break; |
| 5188 | case 0x93B1: return "GL_COMPRESSED_RGBA_ASTC_5x4_KHR"; break; |
| 5189 | case 0x93B2: return "GL_COMPRESSED_RGBA_ASTC_5x5_KHR"; break; |
| 5190 | case 0x93B3: return "GL_COMPRESSED_RGBA_ASTC_6x5_KHR"; break; |
| 5191 | case 0x93B4: return "GL_COMPRESSED_RGBA_ASTC_6x6_KHR"; break; |
| 5192 | case 0x93B5: return "GL_COMPRESSED_RGBA_ASTC_8x5_KHR"; break; |
| 5193 | case 0x93B6: return "GL_COMPRESSED_RGBA_ASTC_8x6_KHR"; break; |
| 5194 | case 0x93B7: return "GL_COMPRESSED_RGBA_ASTC_8x8_KHR"; break; |
| 5195 | case 0x93B8: return "GL_COMPRESSED_RGBA_ASTC_10x5_KHR"; break; |
| 5196 | case 0x93B9: return "GL_COMPRESSED_RGBA_ASTC_10x6_KHR"; break; |
| 5197 | case 0x93BA: return "GL_COMPRESSED_RGBA_ASTC_10x8_KHR"; break; |
| 5198 | case 0x93BB: return "GL_COMPRESSED_RGBA_ASTC_10x10_KHR"; break; |
| 5199 | case 0x93BC: return "GL_COMPRESSED_RGBA_ASTC_12x10_KHR"; break; |
| 5200 | case 0x93BD: return "GL_COMPRESSED_RGBA_ASTC_12x12_KHR"; break; |
| 5201 | case 0x93D0: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_4x4_KHR"; break; |
| 5202 | case 0x93D1: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x4_KHR"; break; |
| 5203 | case 0x93D2: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_5x5_KHR"; break; |
| 5204 | case 0x93D3: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x5_KHR"; break; |
| 5205 | case 0x93D4: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_6x6_KHR"; break; |
| 5206 | case 0x93D5: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x5_KHR"; break; |
| 5207 | case 0x93D6: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x6_KHR"; break; |
| 5208 | case 0x93D7: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_8x8_KHR"; break; |
| 5209 | case 0x93D8: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x5_KHR"; break; |
| 5210 | case 0x93D9: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x6_KHR"; break; |
| 5211 | case 0x93DA: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x8_KHR"; break; |
| 5212 | case 0x93DB: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_10x10_KHR"; break; |
| 5213 | case 0x93DC: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x10_KHR"; break; |
| 5214 | case 0x93DD: return "GL_COMPRESSED_SRGB8_ALPHA8_ASTC_12x12_KHR"; break; |
| 5215 | default: return "GL_COMPRESSED_UNKNOWN"; break; |
| 5216 | } |
| 5217 | } |
| 5218 | #endif |
| 5219 | |
| 5220 | #endif // GRAPHICS_API_OPENGL_33 || GRAPHICS_API_OPENGL_ES2 |
| 5221 | |
| 5222 | // Get pixel data size in bytes (image or texture) |
| 5223 | // NOTE: Size depends on pixel format |
| 5224 | static int rlGetPixelDataSize(int width, int height, int format) |
| 5225 | { |
| 5226 | int dataSize = 0; // Size in bytes |
| 5227 | int bpp = 0; // Bits per pixel |
| 5228 | |
| 5229 | if ((width <= 0) || (height <= 0)) return dataSize; |
| 5230 | |
| 5231 | switch (format) |
| 5232 | { |
| 5233 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE: bpp = 8; break; |
| 5234 | case RL_PIXELFORMAT_UNCOMPRESSED_GRAY_ALPHA: |
| 5235 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G6B5: |
| 5236 | case RL_PIXELFORMAT_UNCOMPRESSED_R5G5B5A1: |
| 5237 | case RL_PIXELFORMAT_UNCOMPRESSED_R4G4B4A4: bpp = 16; break; |
| 5238 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8: bpp = 32; break; |
| 5239 | case RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8: bpp = 24; break; |
| 5240 | case RL_PIXELFORMAT_UNCOMPRESSED_R32: bpp = 32; break; |
| 5241 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32: bpp = 32*3; break; |
| 5242 | case RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32A32: bpp = 32*4; break; |
| 5243 | case RL_PIXELFORMAT_UNCOMPRESSED_R16: bpp = 16; break; |
| 5244 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16: bpp = 16*3; break; |
| 5245 | case RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16: bpp = 16*4; break; |
| 5246 | case RL_PIXELFORMAT_COMPRESSED_DXT1_RGB: |
| 5247 | case RL_PIXELFORMAT_COMPRESSED_DXT1_RGBA: |
| 5248 | case RL_PIXELFORMAT_COMPRESSED_ETC1_RGB: |
| 5249 | case RL_PIXELFORMAT_COMPRESSED_ETC2_RGB: |
| 5250 | case RL_PIXELFORMAT_COMPRESSED_PVRT_RGB: |
| 5251 | case RL_PIXELFORMAT_COMPRESSED_PVRT_RGBA: // 8 bytes per each 4x4 block |
| 5252 | { |
| 5253 | int blockWidth = (width + 3)/4; |
| 5254 | int blockHeight = (height + 3)/4; |
| 5255 | unsigned long long dataSizeBytes = (unsigned long long)blockWidth*blockHeight*8; |
| 5256 | if (dataSizeBytes < INT_MAX) dataSize = (int)dataSizeBytes; |
| 5257 | |
| 5258 | } break; |
| 5259 | case RL_PIXELFORMAT_COMPRESSED_DXT3_RGBA: |
| 5260 | case RL_PIXELFORMAT_COMPRESSED_DXT5_RGBA: |
| 5261 | case RL_PIXELFORMAT_COMPRESSED_ETC2_EAC_RGBA: |
| 5262 | case RL_PIXELFORMAT_COMPRESSED_ASTC_4x4_RGBA: // 16 bytes per each 4x4 block |
| 5263 | { |
| 5264 | int blockWidth = (width + 3)/4; |
| 5265 | int blockHeight = (height + 3)/4; |
| 5266 | unsigned long long dataSizeBytes = (unsigned long long)blockWidth*blockHeight*16; |
| 5267 | if (dataSizeBytes < INT_MAX) dataSize = (int)dataSizeBytes; |
| 5268 | |
| 5269 | } break; |
| 5270 | case RL_PIXELFORMAT_COMPRESSED_ASTC_8x8_RGBA: // 4 bytes per each 4x4 block |
| 5271 | { |
| 5272 | int blockWidth = (width + 3)/4; |
| 5273 | int blockHeight = (height + 3)/4; |
| 5274 | unsigned long long dataSizeBytes = (unsigned long long)blockWidth*blockHeight*4; |
| 5275 | if (dataSizeBytes < INT_MAX) dataSize = (int)dataSizeBytes; |
| 5276 | |
| 5277 | } break; |
| 5278 | default: break; |
| 5279 | } |
| 5280 | |
| 5281 | // Compute dataSize for uncompressed texture data (no blocks) |
| 5282 | if ((format >= RL_PIXELFORMAT_UNCOMPRESSED_GRAYSCALE) && |
| 5283 | (format <= RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16A16)) |
| 5284 | { |
| 5285 | unsigned long long dataSizeBytes = ((unsigned long long)width*height*bpp) >> 3; // Get size in bytes (dividing by 8) |
| 5286 | if (dataSizeBytes < INT_MAX) dataSize = (int)dataSizeBytes; |
| 5287 | else TRACELOG(LOG_WARNING, "Requested image size is larger than 2GB, it can not be allocated"); |
| 5288 | } |
| 5289 | |
| 5290 | return dataSize; |
| 5291 | } |
| 5292 | |
| 5293 | // Auxiliar math functions |
| 5294 | //------------------------------------------------------------------------------- |
| 5295 | // Get identity matrix |
| 5296 | static Matrix rlMatrixIdentity(void) |
| 5297 | { |
| 5298 | Matrix matIdentity = { 0 }; |
| 5299 | matIdentity.m0 = 1.0f; |
| 5300 | matIdentity.m5 = 1.0f; |
| 5301 | matIdentity.m10 = 1.0f; |
| 5302 | matIdentity.m15 = 1.0f; |
| 5303 | |
| 5304 | return matIdentity; |
| 5305 | } |
| 5306 | #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) |
| 5307 | // Get float array of matrix data |
| 5308 | // Explicit conversion to column-major memory layout |
| 5309 | static rl_float16 rlMatrixToFloatV(Matrix mat) |
| 5310 | { |
| 5311 | rl_float16 result = { 0 }; |
| 5312 | |
| 5313 | result.v[0] = mat.m0; |
| 5314 | result.v[1] = mat.m1; |
| 5315 | result.v[2] = mat.m2; |
| 5316 | result.v[3] = mat.m3; |
| 5317 | result.v[4] = mat.m4; |
| 5318 | result.v[5] = mat.m5; |
| 5319 | result.v[6] = mat.m6; |
| 5320 | result.v[7] = mat.m7; |
| 5321 | result.v[8] = mat.m8; |
| 5322 | result.v[9] = mat.m9; |
| 5323 | result.v[10] = mat.m10; |
| 5324 | result.v[11] = mat.m11; |
| 5325 | result.v[12] = mat.m12; |
| 5326 | result.v[13] = mat.m13; |
| 5327 | result.v[14] = mat.m14; |
| 5328 | result.v[15] = mat.m15; |
| 5329 | |
| 5330 | return result; |
| 5331 | } |
| 5332 | |
| 5333 | // Get two matrix multiplication |
| 5334 | // NOTE: When multiplying matrices... the order matters! |
| 5335 | static Matrix rlMatrixMultiply(Matrix left, Matrix right) |
| 5336 | { |
| 5337 | Matrix result = { 0 }; |
| 5338 | |
| 5339 | result.m0 = left.m0*right.m0 + left.m1*right.m4 + left.m2*right.m8 + left.m3*right.m12; |
| 5340 | result.m1 = left.m0*right.m1 + left.m1*right.m5 + left.m2*right.m9 + left.m3*right.m13; |
| 5341 | result.m2 = left.m0*right.m2 + left.m1*right.m6 + left.m2*right.m10 + left.m3*right.m14; |
| 5342 | result.m3 = left.m0*right.m3 + left.m1*right.m7 + left.m2*right.m11 + left.m3*right.m15; |
| 5343 | result.m4 = left.m4*right.m0 + left.m5*right.m4 + left.m6*right.m8 + left.m7*right.m12; |
| 5344 | result.m5 = left.m4*right.m1 + left.m5*right.m5 + left.m6*right.m9 + left.m7*right.m13; |
| 5345 | result.m6 = left.m4*right.m2 + left.m5*right.m6 + left.m6*right.m10 + left.m7*right.m14; |
| 5346 | result.m7 = left.m4*right.m3 + left.m5*right.m7 + left.m6*right.m11 + left.m7*right.m15; |
| 5347 | result.m8 = left.m8*right.m0 + left.m9*right.m4 + left.m10*right.m8 + left.m11*right.m12; |
| 5348 | result.m9 = left.m8*right.m1 + left.m9*right.m5 + left.m10*right.m9 + left.m11*right.m13; |
| 5349 | result.m10 = left.m8*right.m2 + left.m9*right.m6 + left.m10*right.m10 + left.m11*right.m14; |
| 5350 | result.m11 = left.m8*right.m3 + left.m9*right.m7 + left.m10*right.m11 + left.m11*right.m15; |
| 5351 | result.m12 = left.m12*right.m0 + left.m13*right.m4 + left.m14*right.m8 + left.m15*right.m12; |
| 5352 | result.m13 = left.m12*right.m1 + left.m13*right.m5 + left.m14*right.m9 + left.m15*right.m13; |
| 5353 | result.m14 = left.m12*right.m2 + left.m13*right.m6 + left.m14*right.m10 + left.m15*right.m14; |
| 5354 | result.m15 = left.m12*right.m3 + left.m13*right.m7 + left.m14*right.m11 + left.m15*right.m15; |
| 5355 | |
| 5356 | return result; |
| 5357 | } |
| 5358 | |
| 5359 | // Transposes provided matrix |
| 5360 | static Matrix rlMatrixTranspose(Matrix mat) |
| 5361 | { |
| 5362 | Matrix result = { 0 }; |
| 5363 | |
| 5364 | result.m0 = mat.m0; |
| 5365 | result.m1 = mat.m4; |
| 5366 | result.m2 = mat.m8; |
| 5367 | result.m3 = mat.m12; |
| 5368 | result.m4 = mat.m1; |
| 5369 | result.m5 = mat.m5; |
| 5370 | result.m6 = mat.m9; |
| 5371 | result.m7 = mat.m13; |
| 5372 | result.m8 = mat.m2; |
| 5373 | result.m9 = mat.m6; |
| 5374 | result.m10 = mat.m10; |
| 5375 | result.m11 = mat.m14; |
| 5376 | result.m12 = mat.m3; |
| 5377 | result.m13 = mat.m7; |
| 5378 | result.m14 = mat.m11; |
| 5379 | result.m15 = mat.m15; |
| 5380 | |
| 5381 | return result; |
| 5382 | } |
| 5383 | |
| 5384 | // Invert provided matrix |
| 5385 | static Matrix rlMatrixInvert(Matrix mat) |
| 5386 | { |
| 5387 | Matrix result = { 0 }; |
| 5388 | |
| 5389 | // Cache the matrix values (speed optimization) |
| 5390 | float a00 = mat.m0, a01 = mat.m1, a02 = mat.m2, a03 = mat.m3; |
| 5391 | float a10 = mat.m4, a11 = mat.m5, a12 = mat.m6, a13 = mat.m7; |
| 5392 | float a20 = mat.m8, a21 = mat.m9, a22 = mat.m10, a23 = mat.m11; |
| 5393 | float a30 = mat.m12, a31 = mat.m13, a32 = mat.m14, a33 = mat.m15; |
| 5394 | |
| 5395 | float b00 = a00*a11 - a01*a10; |
| 5396 | float b01 = a00*a12 - a02*a10; |
| 5397 | float b02 = a00*a13 - a03*a10; |
| 5398 | float b03 = a01*a12 - a02*a11; |
| 5399 | float b04 = a01*a13 - a03*a11; |
| 5400 | float b05 = a02*a13 - a03*a12; |
| 5401 | float b06 = a20*a31 - a21*a30; |
| 5402 | float b07 = a20*a32 - a22*a30; |
| 5403 | float b08 = a20*a33 - a23*a30; |
| 5404 | float b09 = a21*a32 - a22*a31; |
| 5405 | float b10 = a21*a33 - a23*a31; |
| 5406 | float b11 = a22*a33 - a23*a32; |
| 5407 | |
| 5408 | // Calculate the invert determinant (inlined to avoid double-caching) |
| 5409 | float invDet = 1.0f/(b00*b11 - b01*b10 + b02*b09 + b03*b08 - b04*b07 + b05*b06); |
| 5410 | |
| 5411 | result.m0 = (a11*b11 - a12*b10 + a13*b09)*invDet; |
| 5412 | result.m1 = (-a01*b11 + a02*b10 - a03*b09)*invDet; |
| 5413 | result.m2 = (a31*b05 - a32*b04 + a33*b03)*invDet; |
| 5414 | result.m3 = (-a21*b05 + a22*b04 - a23*b03)*invDet; |
| 5415 | result.m4 = (-a10*b11 + a12*b08 - a13*b07)*invDet; |
| 5416 | result.m5 = (a00*b11 - a02*b08 + a03*b07)*invDet; |
| 5417 | result.m6 = (-a30*b05 + a32*b02 - a33*b01)*invDet; |
| 5418 | result.m7 = (a20*b05 - a22*b02 + a23*b01)*invDet; |
| 5419 | result.m8 = (a10*b10 - a11*b08 + a13*b06)*invDet; |
| 5420 | result.m9 = (-a00*b10 + a01*b08 - a03*b06)*invDet; |
| 5421 | result.m10 = (a30*b04 - a31*b02 + a33*b00)*invDet; |
| 5422 | result.m11 = (-a20*b04 + a21*b02 - a23*b00)*invDet; |
| 5423 | result.m12 = (-a10*b09 + a11*b07 - a12*b06)*invDet; |
| 5424 | result.m13 = (a00*b09 - a01*b07 + a02*b06)*invDet; |
| 5425 | result.m14 = (-a30*b03 + a31*b01 - a32*b00)*invDet; |
| 5426 | result.m15 = (a20*b03 - a21*b01 + a22*b00)*invDet; |
| 5427 | |
| 5428 | return result; |
| 5429 | } |
| 5430 | #endif |
| 5431 | |
| 5432 | #endif // RLGL_IMPLEMENTATION |