From b805754aa17c87d413dd13bdac864a1aba5ee4f4 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 31 Jul 2021 18:10:26 +0200 Subject: [PATCH 1/7] REVIEWED: Avoid bool type collisions --- src/raylib.h | 10 +++++++--- src/rlgl.h | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/raylib.h b/src/raylib.h index f3d56a017..facad9935 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -102,9 +102,12 @@ #ifndef PI #define PI 3.14159265358979323846f #endif - -#define DEG2RAD (PI/180.0f) -#define RAD2DEG (180.0f/PI) +#ifndef DEG2RAD + #define DEG2RAD (PI/180.0f) +#endif +#ifndef RAD2DEG + #define RAD2DEG (180.0f/PI) +#endif // Allow custom memory allocators #ifndef RL_MALLOC @@ -184,6 +187,7 @@ #include #elif !defined(__cplusplus) && !defined(bool) typedef enum bool { false, true } bool; + #define RL_BOOL_TYPE #endif // Vector2, 2 components diff --git a/src/rlgl.h b/src/rlgl.h index ece72f8c0..980be611f 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -291,7 +291,7 @@ typedef struct rlRenderBatch { #if defined(__STDC__) && __STDC_VERSION__ >= 199901L #include -#elif !defined(__cplusplus) && !defined(bool) +#elif !defined(__cplusplus) && !defined(bool) && !defined(RL_BOOL_TYPE) // Boolean type typedef enum bool { false, true } bool; #endif From 73c8ddc8bf60dda51bffd5803a5a22f40737d2f4 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 31 Jul 2021 18:36:06 +0200 Subject: [PATCH 2/7] REVIEWED: raylib_opengl_interop --- ...engl_interop.c => raylib_opengl_interop.c} | 74 +++++++++++------- ..._interop.png => raylib_opengl_interop.png} | Bin 2 files changed, 44 insertions(+), 30 deletions(-) rename examples/others/{opengl_interop.c => raylib_opengl_interop.c} (68%) rename examples/others/{opengl_interop.png => raylib_opengl_interop.png} (100%) diff --git a/examples/others/opengl_interop.c b/examples/others/raylib_opengl_interop.c similarity index 68% rename from examples/others/opengl_interop.c rename to examples/others/raylib_opengl_interop.c index 5d435f2a8..2ecc637d5 100644 --- a/examples/others/opengl_interop.c +++ b/examples/others/raylib_opengl_interop.c @@ -2,7 +2,7 @@ * * raylib [shaders] example - OpenGL point particle system * -* This example has been created using raylib 2ad3eb1 (www.raylib.com) +* This example has been created using raylib 3.8 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * * Example contributed by Stephan Soller (@arkanis - http://arkanis.de/) @@ -24,8 +24,9 @@ ********************************************************************************************/ #include "raylib.h" -#include "rlgl.h" -#include "glad.h" +#include "rlgl.h" // Required for: rlDrawRenderBatchActive(), rlGetMatrixModelview(), rlGetMatrixProjection() +#include "glad.h" // Required for: OpenGL functionality +#include "raymath.h" // Required for: MatrixMultiply(), MatrixToFloat() #if defined(PLATFORM_DESKTOP) #define GLSL_VERSION 330 @@ -33,6 +34,15 @@ #define GLSL_VERSION 100 #endif +#define MAX_PARTICLES 1000 + +// Particle type +typedef struct Particle { + float x; + float y; + float period; +} Particle; + int main() { // Initialization @@ -42,32 +52,34 @@ int main() InitWindow(screenWidth, screenHeight, "raylib - point particles"); - Shader shader = LoadShader( - TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION), - TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION)); + Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/point_particle.vs", GLSL_VERSION), + TextFormat("resources/shaders/glsl%i/point_particle.fs", GLSL_VERSION)); + int currentTimeLoc = GetShaderLocation(shader, "currentTime"); int colorLoc = GetShaderLocation(shader, "color"); // Initialize the vertex buffer for the particles and assign each particle random values - struct { float x, y, period; } particles[10000]; - const size_t particleCount = sizeof(particles) / sizeof(particles[0]); - for (size_t i = 0; i < particleCount; i++) + Particle particles[MAX_PARTICLES] = { 0 }; + + for (int i = 0; i < MAX_PARTICLES; i++) { - particles[i].x = GetRandomValue(20, screenWidth - 20); - particles[i].y = GetRandomValue(50, screenHeight - 20); - // Give each particle a slightly different period. But don't spread it to much. This way the particles line up - // every so often and you get a glimps of what is going on. - particles[i].period = GetRandomValue(10, 30) / 10.0f; + particles[i].x = (float)GetRandomValue(20, screenWidth - 20); + particles[i].y = (float)GetRandomValue(50, screenHeight - 20); + + // Give each particle a slightly different period. But don't spread it to much. + // This way the particles line up every so often and you get a glimps of what is going on. + particles[i].period = (float)GetRandomValue(10, 30)/10.0f; } - // Create a plain OpenGL vertex buffer with the data and an vertex array object that feeds the data from the buffer - // into the vertexPosition shader attribute. - GLuint vao = 0, vbo = 0; + // Create a plain OpenGL vertex buffer with the data and an vertex array object + // that feeds the data from the buffer into the vertexPosition shader attribute. + GLuint vao = 0; + GLuint vbo = 0; glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(particles), particles, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, MAX_PARTICLES*sizeof(Particle), particles, GL_STATIC_DRAW); // Note: LoadShader() automatically fetches the attribute index of "vertexPosition" and saves it in shader.locs[SHADER_LOC_VERTEX_POSITION] glVertexAttribPointer(shader.locs[SHADER_LOC_VERTEX_POSITION], 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0); @@ -89,32 +101,33 @@ int main() ClearBackground(WHITE); DrawRectangle(10, 10, 210, 30, MAROON); - DrawText(TextFormat("%zu particles in one vertex buffer", particleCount), 20, 20, 10, RAYWHITE); + DrawText(TextFormat("%zu particles in one vertex buffer", MAX_PARTICLES), 20, 20, 10, RAYWHITE); + + rlDrawRenderBatchActive(); // Draw iternal buffers data (previous draw calls) // Switch to plain OpenGL //------------------------------------------------------------------------------ - // rlglDraw() in raylib 3.5 - rlDrawRenderBatchActive(); glUseProgram(shader.id); + glUniform1f(currentTimeLoc, GetTime()); Vector4 color = ColorNormalize((Color){ 255, 0, 0, 128 }); - glUniform4fv(colorLoc, 1, (float*)&color); + glUniform4fv(colorLoc, 1, (float *)&color); - // The the current model view projection matrix so the particle system is displayed and transformed - // (e.g. by cameras) just like everything else. - // GetMatrixModelview() and GetMatrixProjection() in raylib 3.5 + // Get the current modelview and projection matrix so the particle system is displayed and transformed Matrix modelViewProjection = MatrixMultiply(rlGetMatrixModelview(), rlGetMatrixProjection()); + glUniformMatrix4fv(shader.locs[SHADER_LOC_MATRIX_MVP], 1, false, MatrixToFloat(modelViewProjection)); glBindVertexArray(vao); - glDrawArrays(GL_POINTS, 0, particleCount); + glDrawArrays(GL_POINTS, 0, MAX_PARTICLES); glBindVertexArray(0); + glUseProgram(0); - - // And back to raylib again //------------------------------------------------------------------------------ + DrawFPS(screenWidth - 100, 10); + EndDrawing(); //---------------------------------------------------------------------------------- } @@ -124,8 +137,9 @@ int main() glDeleteBuffers(1, &vbo); glDeleteVertexArrays(1, &vao); - UnloadShader(shader); - CloseWindow(); // Close window and OpenGL context + UnloadShader(shader); // Unload shader + + CloseWindow(); // Close window and OpenGL context //-------------------------------------------------------------------------------------- return 0; diff --git a/examples/others/opengl_interop.png b/examples/others/raylib_opengl_interop.png similarity index 100% rename from examples/others/opengl_interop.png rename to examples/others/raylib_opengl_interop.png From 559f1919c6105d58dbc32ac0b9f4ba143ed15195 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 31 Jul 2021 18:41:44 +0200 Subject: [PATCH 3/7] REVIEWED: Improved font loading info --- src/text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/text.c b/src/text.c index e16547f38..0371a0cec 100644 --- a/src/text.c +++ b/src/text.c @@ -266,7 +266,7 @@ extern void LoadFontDefault(void) defaultFont.baseSize = (int)defaultFont.recs[0].height; - TRACELOG(LOG_INFO, "FONT: Default font loaded successfully"); + TRACELOG(LOG_INFO, "FONT: [ID %i] Default font texture loaded successfully", defaultFont.texture.id); } // Unload raylib default font From 7013e609260d4abe820e884a2465ce91078f0fd4 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 31 Jul 2021 18:44:07 +0200 Subject: [PATCH 4/7] Update models_rlgl_solar_system.c --- examples/models/models_rlgl_solar_system.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/examples/models/models_rlgl_solar_system.c b/examples/models/models_rlgl_solar_system.c index 97d692453..24394bec2 100644 --- a/examples/models/models_rlgl_solar_system.c +++ b/examples/models/models_rlgl_solar_system.c @@ -14,6 +14,8 @@ #include "raylib.h" #include "rlgl.h" +#include // Required for: cosf(), sinf() + //------------------------------------------------------------------------------------ // Module Functions Declaration //------------------------------------------------------------------------------------ From 3e75a2109dcc48154fc2e47243a2e365c727ffc6 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 31 Jul 2021 19:08:13 +0200 Subject: [PATCH 5/7] REVIEWED: rlgl module header info --- src/rlgl.h | 72 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 53 insertions(+), 19 deletions(-) diff --git a/src/rlgl.h b/src/rlgl.h index 980be611f..b6da19fa2 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -2,13 +2,18 @@ * * rlgl v4.0 - OpenGL abstraction layer * -* rlgl is a wrapper for multiple OpenGL versions (1.1, 2.1, 3.3 Core, ES 2.0) to -* pseudo-OpenGL 1.1 style functions (rlVertex, rlTranslate, rlRotate...). +* rlgl is an abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, ES 2.0) +* to pseudo-OpenGL 1.1 immediate-mode style functions (rlVertex, rlTranslate, rlRotate...) * * When chosing an OpenGL version greater than OpenGL 1.1, rlgl stores vertex data on internal -* VBO buffers (and VAOs if available). It requires calling 3 functions: -* rlglInit() - Initialize internal buffers and auxiliary resources -* rlglClose() - De-initialize internal buffers data and other auxiliar resources +* buffers loaded at initialization. It requires calling 2 functions: +* - rlglInit(): Initialize internal buffers and auxiliary resources +* - rlglClose(): De-initialize internal buffers data and other auxiliar resources +* +* The following resources are loaded when calling rlglInit(): +* - Default batch (RLGL.defaultBatch): RenderBatch system to accumulate vertex data +* - Default texture (RLGL.defaultTextureId): 1x1 white pixel R8G8B8A8 +* - Default shader (RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs) * * CONFIGURATION: * @@ -32,8 +37,42 @@ * #define SUPPORT_GL_DETAILS_INFO * Show OpenGL extensions and capabilities detailed logs on init * +* rlgl capabilities could be customized just defining some internal +* values before library inclusion (default values listed): +* +* #define DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch limits +* #define DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) +* #define DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) +* #define MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack +* #define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh +* #define MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported +* #define MAX_MATERIAL_MAPS 12 // Maximum number of shader maps supported +* #define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance +* #define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance +* +* When loading a shader, the following vertex attribute and uniform +* location names are tried to be set automatically: +* +* #define RL_DEFAULT_SHADER_ATTRIB_NAME_POSITION "vertexPosition" // Binded by default to shader location: 0 +* #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD "vertexTexCoord" // Binded by default to shader location: 1 +* #define RL_DEFAULT_SHADER_ATTRIB_NAME_NORMAL "vertexNormal" // Binded by default to shader location: 2 +* #define RL_DEFAULT_SHADER_ATTRIB_NAME_COLOR "vertexColor" // Binded by default to shader location: 3 +* #define RL_DEFAULT_SHADER_ATTRIB_NAME_TANGENT "vertexTangent" // Binded by default to shader location: 4 +* #define RL_DEFAULT_SHADER_ATTRIB_NAME_TEXCOORD2 "vertexTexCoord2" // Binded by default to shader location: 5 +* #define RL_DEFAULT_SHADER_UNIFORM_NAME_MVP "mvp" // model-view-projection matrix +* #define RL_DEFAULT_SHADER_UNIFORM_NAME_VIEW "matView" // view matrix +* #define RL_DEFAULT_SHADER_UNIFORM_NAME_PROJECTION "matProjection" // projection matrix +* #define RL_DEFAULT_SHADER_UNIFORM_NAME_MODEL "matModel" // model matrix +* #define RL_DEFAULT_SHADER_UNIFORM_NAME_NORMAL "matNormal" // normal matrix (transpose(inverse(matModelView)) +* #define RL_DEFAULT_SHADER_UNIFORM_NAME_COLOR "colDiffuse" // color diffuse (base tint color, multiplied by texture color) +* #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE0 "texture0" // texture0 (texture slot active 0) +* #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE1 "texture1" // texture1 (texture slot active 1) +* #define RL_DEFAULT_SHADER_SAMPLER2D_NAME_TEXTURE2 "texture2" // texture2 (texture slot active 2) +* * DEPENDENCIES: -* GLAD - OpenGL extensions loading (OpenGL 3.3 Core only) +* +* - OpenGL libraries (depending on platform and OpenGL version selected) +* - GLAD OpenGL extensions loading library (only for OpenGL 3.3 Core) * * * LICENSE: zlib/libpng @@ -617,15 +656,6 @@ RLAPI void rlLoadDrawQuad(void); // Load and draw a quad #if defined(RLGL_IMPLEMENTATION) -// Check if config flags have been externally provided on compilation line -#if !defined(EXTERNAL_CONFIG_FLAGS) - #include "config.h" // Defines module configuration flags -#endif - -#include // Required for: malloc(), free() -#include // Required for: strcmp(), strlen() [Used in rlglInit(), on extensions loading] -#include // Required for: sqrtf(), sinf(), cosf(), floor(), log() - #if defined(GRAPHICS_API_OPENGL_11) #if defined(__APPLE__) #include // OpenGL 1.1 library for OSX @@ -676,6 +706,10 @@ RLAPI void rlLoadDrawQuad(void); // Load and draw a quad #endif #endif +#include // Required for: malloc(), free() +#include // Required for: strcmp(), strlen() [Used in rlglInit(), on extensions loading] +#include // Required for: sqrtf(), sinf(), cosf(), floor(), log() + //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- @@ -886,8 +920,8 @@ static PFNGLVERTEXATTRIBDIVISOREXTPROC glVertexAttribDivisor = NULL; // Module specific Functions Declaration //---------------------------------------------------------------------------------- #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) -static void rlLoadShaderDefault(void); // Load default shader (RLGL.State.defaultShader) -static void rlUnloadShaderDefault(void); // Unload default shader (RLGL.State.defaultShader) +static void rlLoadShaderDefault(void); // Load default shader +static void rlUnloadShaderDefault(void); // Unload default shader #if defined(SUPPORT_GL_DETAILS_INFO) static char *rlGetCompressedFormatName(int format); // Get compressed format official GL identifier name #endif // SUPPORT_GL_DETAILS_INFO @@ -3883,7 +3917,7 @@ const char *rlGetPixelFormatName(unsigned int format) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Load default shader (just vertex positioning and texture coloring) // NOTE: This shader program is used for internal buffers -// NOTE: It uses global variable: RLGL.State.defaultShader +// NOTE: Loaded: RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs static void rlLoadShaderDefault(void) { RLGL.State.defaultShaderLocs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int)); @@ -3988,7 +4022,7 @@ static void rlLoadShaderDefault(void) } // Unload default shader -// NOTE: It uses global variable: RLGL.State.defaultShader +// NOTE: Unloads: RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs static void rlUnloadShaderDefault(void) { glUseProgram(0); From 5b6d83b53331190db632d68de7d5beb6539ebf3a Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 31 Jul 2021 19:46:44 +0200 Subject: [PATCH 6/7] REVIEWED: rlgl defines for consistency --- examples/others/rlgl_standalone.c | 11 +++- src/config.h | 25 ++++----- src/core.c | 4 +- src/models.c | 7 ++- src/raylib.h | 2 +- src/rlgl.h | 93 ++++++++++++++----------------- 6 files changed, 74 insertions(+), 68 deletions(-) diff --git a/examples/others/rlgl_standalone.c b/examples/others/rlgl_standalone.c index 6df5b8412..c8c0b0deb 100644 --- a/examples/others/rlgl_standalone.c +++ b/examples/others/rlgl_standalone.c @@ -48,6 +48,16 @@ * ********************************************************************************************/ +// NOTE: rlgl can be configured just re-defining the following values: +//#define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch elements limits +//#define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) +//#define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) +//#define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture()) +//#define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack +//#define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported +//#define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance +//#define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance + #define RLGL_IMPLEMENTATION #include "rlgl.h" // OpenGL abstraction layer to OpenGL 1.1, 3.3+ or ES2 @@ -57,7 +67,6 @@ #if defined(__EMSCRIPTEN__) #define GLFW_INCLUDE_ES2 #endif - #include "GLFW/glfw3.h" // Windows/Context and inputs management #include // Required for: printf() diff --git a/src/config.h b/src/config.h index 084c235db..b7358d7e7 100644 --- a/src/config.h +++ b/src/config.h @@ -87,22 +87,17 @@ // Show OpenGL extensions and capabilities detailed logs on init //#define SUPPORT_GL_DETAILS_INFO 1 -#if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) - #define DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch limits -#elif defined(GRAPHICS_API_OPENGL_ES2) - #define DEFAULT_BATCH_BUFFER_ELEMENTS 2048 // Default internal render batch limits -#endif +//#define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 4096 // Default internal render batch elements limits +#define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) +#define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) +#define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture()) -#define DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) -#define DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) +#define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack -#define MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack -#define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh -#define MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported -#define MAX_MATERIAL_MAPS 12 // Maximum number of shader maps supported +#define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported -#define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance -#define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance +#define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance +#define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance // Default shader vertex attribute names to set location points // NOTE: When a new shader is loaded, the following locations are tried to be set for convenience @@ -191,6 +186,10 @@ // NOTE: Some generated meshes DO NOT include generated texture coordinates #define SUPPORT_MESH_GENERATION 1 +// models: Configuration values +//------------------------------------------------------------------------------------ +#define MAX_MATERIAL_MAPS 12 // Maximum number of shader maps supported +#define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh //------------------------------------------------------------------------------------ // Module: audio - Configuration Flags diff --git a/src/core.c b/src/core.c index 264921229..b03216cc2 100644 --- a/src/core.c +++ b/src/core.c @@ -2296,10 +2296,10 @@ Shader LoadShader(const char *vsFileName, const char *fsFileName) RLAPI Shader LoadShaderFromMemory(const char *vsCode, const char *fsCode) { Shader shader = { 0 }; - shader.locs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int)); + shader.locs = (int *)RL_CALLOC(RL_MAX_SHADER_LOCATIONS, sizeof(int)); // NOTE: All locations must be reseted to -1 (no location) - for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1; + for (int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++) shader.locs[i] = -1; shader.id = rlLoadShaderCode(vsCode, fsCode); diff --git a/src/models.c b/src/models.c index 870c5e8cb..bce9c8d82 100644 --- a/src/models.c +++ b/src/models.c @@ -92,7 +92,12 @@ //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- -// ... +#ifndef MAX_MATERIAL_MAPS + #define MAX_MATERIAL_MAPS 12 // Maximum number of maps supported +#endif +#ifndef MAX_MESH_VERTEX_BUFFERS + #define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh +#endif //---------------------------------------------------------------------------------- // Types and Structures Definition diff --git a/src/raylib.h b/src/raylib.h index facad9935..781c14202 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -348,7 +348,7 @@ typedef struct Mesh { // Shader typedef struct Shader { unsigned int id; // Shader program id - int *locs; // Shader locations array (MAX_SHADER_LOCATIONS) + int *locs; // Shader locations array (RL_MAX_SHADER_LOCATIONS) } Shader; // MaterialMap diff --git a/src/rlgl.h b/src/rlgl.h index b6da19fa2..2784af703 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -40,15 +40,15 @@ * rlgl capabilities could be customized just defining some internal * values before library inclusion (default values listed): * -* #define DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch limits -* #define DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) -* #define DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) -* #define MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack -* #define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh -* #define MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported -* #define MAX_MATERIAL_MAPS 12 // Maximum number of shader maps supported -* #define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance -* #define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance +* #define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192 // Default internal render batch elements limits +* #define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) +* #define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) +* #define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture()) +* +* #define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of internal Matrix stack +* #define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported +* #define RL_CULL_DISTANCE_NEAR 0.01 // Default projection matrix near cull distance +* #define RL_CULL_DISTANCE_FAR 1000.0 // Default projection matrix far cull distance * * When loading a shader, the following vertex attribute and uniform * location names are tried to be set automatically: @@ -163,54 +163,47 @@ //---------------------------------------------------------------------------------- // Defines and Macros //---------------------------------------------------------------------------------- -// Default internal render batch limits -#ifndef DEFAULT_BATCH_BUFFER_ELEMENTS + +// Default internal render batch elements limits +#ifndef RL_DEFAULT_BATCH_BUFFER_ELEMENTS #if defined(GRAPHICS_API_OPENGL_11) || defined(GRAPHICS_API_OPENGL_33) // This is the maximum amount of elements (quads) per batch // NOTE: Be careful with text, every letter maps to a quad - #define DEFAULT_BATCH_BUFFER_ELEMENTS 8192 + #define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 8192 #endif #if defined(GRAPHICS_API_OPENGL_ES2) // We reduce memory sizes for embedded systems (RPI and HTML5) // NOTE: On HTML5 (emscripten) this is allocated on heap, // by default it's only 16MB!...just take care... - #define DEFAULT_BATCH_BUFFER_ELEMENTS 2048 + #define RL_DEFAULT_BATCH_BUFFER_ELEMENTS 2048 #endif #endif -#ifndef DEFAULT_BATCH_BUFFERS - #define DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) +#ifndef RL_DEFAULT_BATCH_BUFFERS + #define RL_DEFAULT_BATCH_BUFFERS 1 // Default number of batch buffers (multi-buffering) #endif -#ifndef DEFAULT_BATCH_DRAWCALLS - #define DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) +#ifndef RL_DEFAULT_BATCH_DRAWCALLS + #define RL_DEFAULT_BATCH_DRAWCALLS 256 // Default number of batch draw calls (by state changes: mode, texture) #endif -#ifndef MAX_BATCH_ACTIVE_TEXTURES - #define MAX_BATCH_ACTIVE_TEXTURES 4 // Maximum number of additional textures that can be activated on batch drawing (SetShaderValueTexture()) +#ifndef RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS + #define RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS 4 // Maximum number of textures units that can be activated on batch drawing (SetShaderValueTexture()) #endif // Internal Matrix stack -#ifndef MAX_MATRIX_STACK_SIZE - #define MAX_MATRIX_STACK_SIZE 32 // Maximum size of Matrix stack +#ifndef RL_MAX_MATRIX_STACK_SIZE + #define RL_MAX_MATRIX_STACK_SIZE 32 // Maximum size of Matrix stack #endif -// Vertex buffers id limit -#ifndef MAX_MESH_VERTEX_BUFFERS - #define MAX_MESH_VERTEX_BUFFERS 7 // Maximum vertex buffers (VBO) per mesh -#endif - -// Shader and material limits -#ifndef MAX_SHADER_LOCATIONS - #define MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported -#endif -#ifndef MAX_MATERIAL_MAPS - #define MAX_MATERIAL_MAPS 12 // Maximum number of shader maps supported +// Shader limits +#ifndef RL_MAX_SHADER_LOCATIONS + #define RL_MAX_SHADER_LOCATIONS 32 // Maximum number of shader locations supported #endif // Projection matrix culling #ifndef RL_CULL_DISTANCE_NEAR - #define RL_CULL_DISTANCE_NEAR 0.01 // Default near cull distance + #define RL_CULL_DISTANCE_NEAR 0.01 // Default near cull distance #endif #ifndef RL_CULL_DISTANCE_FAR - #define RL_CULL_DISTANCE_FAR 1000.0 // Default far cull distance + #define RL_CULL_DISTANCE_FAR 1000.0 // Default far cull distance #endif // Texture parameters (equivalent to OpenGL defines) @@ -848,11 +841,11 @@ typedef struct rlglData { Matrix projection; // Default projection matrix Matrix transform; // Transform matrix to be used with rlTranslate, rlRotate, rlScale bool transformRequired; // Require transform matrix application to current draw-call vertex (if required) - Matrix stack[MAX_MATRIX_STACK_SIZE];// Matrix stack for push/pop + Matrix stack[RL_MAX_MATRIX_STACK_SIZE];// Matrix stack for push/pop int stackCounter; // Matrix stack counter unsigned int defaultTextureId; // Default texture used on shapes/poly drawing (required by shader) - unsigned int activeTextureId[MAX_BATCH_ACTIVE_TEXTURES]; // Active texture ids to be enabled on batch drawing (0 active by default) + unsigned int activeTextureId[RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS]; // Active texture ids to be enabled on batch drawing (0 active by default) unsigned int defaultVShaderId; // Default vertex shader id (used by default shader program) unsigned int defaultFShaderId; // Default fragment shader id (used by default shader program) unsigned int defaultShaderId; // Default shader program id, supports vertex color and diffuse texture @@ -985,7 +978,7 @@ void rlMatrixMode(int mode) // Push the current matrix into RLGL.State.stack void rlPushMatrix(void) { - if (RLGL.State.stackCounter >= MAX_MATRIX_STACK_SIZE) TRACELOG(RL_LOG_ERROR, "RLGL: Matrix stack overflow (MAX_MATRIX_STACK_SIZE)"); + if (RLGL.State.stackCounter >= RL_MAX_MATRIX_STACK_SIZE) TRACELOG(RL_LOG_ERROR, "RLGL: Matrix stack overflow (RL_MAX_MATRIX_STACK_SIZE)"); if (RLGL.State.currentMatrixMode == RL_MODELVIEW) { @@ -1231,7 +1224,7 @@ void rlBegin(int mode) } } - if (RLGL.currentBatch->drawsCounter >= DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch); + if (RLGL.currentBatch->drawsCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch); RLGL.currentBatch->draws[RLGL.currentBatch->drawsCounter - 1].mode = mode; RLGL.currentBatch->draws[RLGL.currentBatch->drawsCounter - 1].vertexCount = 0; @@ -1422,7 +1415,7 @@ void rlSetTexture(unsigned int id) } } - if (RLGL.currentBatch->drawsCounter >= DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch); + if (RLGL.currentBatch->drawsCounter >= RL_DEFAULT_BATCH_DRAWCALLS) rlDrawRenderBatch(RLGL.currentBatch); RLGL.currentBatch->draws[RLGL.currentBatch->drawsCounter - 1].textureId = id; RLGL.currentBatch->draws[RLGL.currentBatch->drawsCounter - 1].vertexCount = 0; @@ -1746,11 +1739,11 @@ void rlglInit(int width, int height) RLGL.State.currentShaderLocs = RLGL.State.defaultShaderLocs; // Init default vertex arrays buffers - RLGL.defaultBatch = rlLoadRenderBatch(DEFAULT_BATCH_BUFFERS, DEFAULT_BATCH_BUFFER_ELEMENTS); + RLGL.defaultBatch = rlLoadRenderBatch(RL_DEFAULT_BATCH_BUFFERS, RL_DEFAULT_BATCH_BUFFER_ELEMENTS); RLGL.currentBatch = &RLGL.defaultBatch; // Init stack matrices (emulating OpenGL 1.1) - for (int i = 0; i < MAX_MATRIX_STACK_SIZE; i++) RLGL.State.stack[i] = rlMatrixIdentity(); + for (int i = 0; i < RL_MAX_MATRIX_STACK_SIZE; i++) RLGL.State.stack[i] = rlMatrixIdentity(); // Init internal matrices RLGL.State.transform = rlMatrixIdentity(); @@ -2217,9 +2210,9 @@ rlRenderBatch rlLoadRenderBatch(int numBuffers, int bufferElements) // Init draw calls tracking system //-------------------------------------------------------------------------------------------- - batch.draws = (rlDrawCall *)RL_MALLOC(DEFAULT_BATCH_DRAWCALLS*sizeof(rlDrawCall)); + batch.draws = (rlDrawCall *)RL_MALLOC(RL_DEFAULT_BATCH_DRAWCALLS*sizeof(rlDrawCall)); - for (int i = 0; i < DEFAULT_BATCH_DRAWCALLS; i++) + for (int i = 0; i < RL_DEFAULT_BATCH_DRAWCALLS; i++) { batch.draws[i].mode = RL_QUADS; batch.draws[i].vertexCount = 0; @@ -2391,7 +2384,7 @@ void rlDrawRenderBatch(rlRenderBatch *batch) // Activate additional sampler textures // Those additional textures will be common for all draw calls of the batch - for (int i = 0; i < MAX_BATCH_ACTIVE_TEXTURES; i++) + for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) { if (RLGL.State.activeTextureId[i] > 0) { @@ -2456,7 +2449,7 @@ void rlDrawRenderBatch(rlRenderBatch *batch) RLGL.State.modelview = matModelView; // Reset RLGL.currentBatch->draws array - for (int i = 0; i < DEFAULT_BATCH_DRAWCALLS; i++) + for (int i = 0; i < RL_DEFAULT_BATCH_DRAWCALLS; i++) { batch->draws[i].mode = RL_QUADS; batch->draws[i].vertexCount = 0; @@ -2464,7 +2457,7 @@ void rlDrawRenderBatch(rlRenderBatch *batch) } // Reset active texture units for next batch - for (int i = 0; i < MAX_BATCH_ACTIVE_TEXTURES; i++) RLGL.State.activeTextureId[i] = 0; + for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) RLGL.State.activeTextureId[i] = 0; // Reset draws counter to one draw for the batch batch->drawsCounter = 1; @@ -3605,11 +3598,11 @@ void rlSetUniformSampler(int locIndex, unsigned int textureId) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Check if texture is already active - for (int i = 0; i < MAX_BATCH_ACTIVE_TEXTURES; i++) if (RLGL.State.activeTextureId[i] == textureId) return; + for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) if (RLGL.State.activeTextureId[i] == textureId) return; // Register a new active texture for the internal batch system // NOTE: Default texture is always activated as GL_TEXTURE0 - for (int i = 0; i < MAX_BATCH_ACTIVE_TEXTURES; i++) + for (int i = 0; i < RL_DEFAULT_BATCH_MAX_TEXTURE_UNITS; i++) { if (RLGL.State.activeTextureId[i] == 0) { @@ -3920,10 +3913,10 @@ const char *rlGetPixelFormatName(unsigned int format) // NOTE: Loaded: RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs static void rlLoadShaderDefault(void) { - RLGL.State.defaultShaderLocs = (int *)RL_CALLOC(MAX_SHADER_LOCATIONS, sizeof(int)); + RLGL.State.defaultShaderLocs = (int *)RL_CALLOC(RL_MAX_SHADER_LOCATIONS, sizeof(int)); // NOTE: All locations must be reseted to -1 (no location) - for (int i = 0; i < MAX_SHADER_LOCATIONS; i++) RLGL.State.defaultShaderLocs[i] = -1; + for (int i = 0; i < RL_MAX_SHADER_LOCATIONS; i++) RLGL.State.defaultShaderLocs[i] = -1; // Vertex shader directly defined, no external file required const char *defaultVShaderCode = From aa67903f96846d473e3de6f34c166bc4e78b9fdb Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 31 Jul 2021 19:58:30 +0200 Subject: [PATCH 7/7] Improve rlgl description --- src/rlgl.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/rlgl.h b/src/rlgl.h index 2784af703..76b5114af 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1,20 +1,24 @@ /********************************************************************************************** * -* rlgl v4.0 - OpenGL abstraction layer +* rlgl v4.0 * -* rlgl is an abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, ES 2.0) -* to pseudo-OpenGL 1.1 immediate-mode style functions (rlVertex, rlTranslate, rlRotate...) +* An abstraction layer for multiple OpenGL versions (1.1, 2.1, 3.3 Core, ES 2.0) +* that provides a pseudo-OpenGL 1.1 immediate-mode style API (rlVertex, rlTranslate, rlRotate...) * -* When chosing an OpenGL version greater than OpenGL 1.1, rlgl stores vertex data on internal -* buffers loaded at initialization. It requires calling 2 functions: -* - rlglInit(): Initialize internal buffers and auxiliary resources -* - rlglClose(): De-initialize internal buffers data and other auxiliar resources +* When chosing an OpenGL backend different than OpenGL 1.1, some internal buffer are +* initialized on rlglInit() to accumulate vertex data. * -* The following resources are loaded when calling rlglInit(): +* When an internal state change is required all the stored vertex data is renderer in batch, +* additioanlly, rlDrawRenderBatchActive() could be called to force flushing of the batch. +* +* Some additional resources are also loaded for convenience, here the complete list: * - Default batch (RLGL.defaultBatch): RenderBatch system to accumulate vertex data * - Default texture (RLGL.defaultTextureId): 1x1 white pixel R8G8B8A8 * - Default shader (RLGL.State.defaultShaderId, RLGL.State.defaultShaderLocs) * +* Internal buffer (and additional resources) must be manually unloaded calling rlglClose(). +* +* * CONFIGURATION: * * #define GRAPHICS_API_OPENGL_11