From 92f68ac6be5b85b85ebab64841c8322405e08d51 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 24 Dec 2018 14:09:51 +0100 Subject: Review DrawPolyEx() Also reviewed rlCheckBufferLimit() --- src/rlgl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/rlgl.h') diff --git a/src/rlgl.h b/src/rlgl.h index e1b9a98b..8b229e9c 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -435,7 +435,7 @@ void rlglClose(void); // De-inititialize rlgl (buffers void rlglDraw(void); // Update and draw default internal buffers int rlGetVersion(void); // Returns current OpenGL version -bool rlCheckBufferLimit(int type, int vCount); // Check internal buffer overflow for a given number of vertex +bool rlCheckBufferLimit(int vCount); // Check internal buffer overflow for a given number of vertex void rlSetDebugMarker(const char *text); // Set debug marker for analysis void rlLoadExtensions(void *loader); // Load OpenGL extensions Vector3 rlUnproject(Vector3 source, Matrix proj, Matrix view); // Get world coordinates from screen coordinates @@ -1738,7 +1738,7 @@ int rlGetVersion(void) } // Check internal buffer overflow for a given number of vertex -bool rlCheckBufferLimit(int type, int vCount) +bool rlCheckBufferLimit(int vCount) { bool overflow = false; #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) -- cgit v1.2.3 From 47358fe5ce50a7dfae2db6cd3c3d86ad93eec152 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 24 Dec 2018 17:09:46 +0100 Subject: Tweaks to support OpenGL ES 2.0 desktop --- src/core.c | 31 +++++++++++++++++++------------ src/rlgl.h | 14 +++----------- 2 files changed, 22 insertions(+), 23 deletions(-) (limited to 'src/rlgl.h') diff --git a/src/core.c b/src/core.c index 79404abe..1b975214 100644 --- a/src/core.c +++ b/src/core.c @@ -2247,11 +2247,11 @@ static bool InitGraphicsDevice(int width, int height) //glfwWindowHint(GLFW_AUX_BUFFERS, 0); // Number of auxiliar buffers // Check some Window creation flags - if (configFlags & FLAG_WINDOW_RESIZABLE) glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // Resizable window - else glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Avoid window being resizable + if (configFlags & FLAG_WINDOW_RESIZABLE) glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // Resizable window + else glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // Avoid window being resizable - if (configFlags & FLAG_WINDOW_UNDECORATED) glfwWindowHint(GLFW_DECORATED, GL_FALSE); // Border and buttons on Window - else glfwWindowHint(GLFW_DECORATED, GL_TRUE); // Decorated window + if (configFlags & FLAG_WINDOW_UNDECORATED) glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); // Border and buttons on Window + else glfwWindowHint(GLFW_DECORATED, GLFW_TRUE); // Decorated window // FLAG_WINDOW_TRANSPARENT not supported on HTML5 and not included in any released GLFW version yet #if defined(GLFW_TRANSPARENT_FRAMEBUFFER) if (configFlags & FLAG_WINDOW_TRANSPARENT) glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE); // Transparent framebuffer @@ -2267,21 +2267,28 @@ static bool InitGraphicsDevice(int width, int height) // Check selection OpenGL version if (rlGetVersion() == OPENGL_21) { - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // Choose OpenGL major version (just hint) - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // Choose OpenGL minor version (just hint) + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); // Choose OpenGL major version (just hint) + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // Choose OpenGL minor version (just hint) } else if (rlGetVersion() == OPENGL_33) { - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Choose OpenGL major version (just hint) - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint) + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Choose OpenGL major version (just hint) + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); // Choose OpenGL minor version (just hint) glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // Profiles Hint: Only 3.3 and above! // Values: GLFW_OPENGL_CORE_PROFILE, GLFW_OPENGL_ANY_PROFILE, GLFW_OPENGL_COMPAT_PROFILE #if defined(__APPLE__) - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // OSX Requires fordward compatibility + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // OSX Requires fordward compatibility #else - glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_FALSE); // Fordward Compatibility Hint: Only 3.3 and above! + glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_FALSE); // Fordward Compatibility Hint: Only 3.3 and above! #endif - //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE); // Request OpenGL DEBUG context + //glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE); // Request OpenGL DEBUG context + } + else if (rlGetVersion() == OPENGL_ES_20) // Request OpenGL ES 2.0 context + { + glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); + glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0); + glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API); + glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_NATIVE_CONTEXT_API); // Alternative: GLFW_EGL_CONTEXT_API (ANGLE) } if (fullscreen) @@ -3145,7 +3152,7 @@ static void KeyCallback(GLFWwindow *window, int key, int scancode, int action, i { if (key == exitKey && action == GLFW_PRESS) { - glfwSetWindowShouldClose(window, GL_TRUE); + glfwSetWindowShouldClose(window, GLFW_TRUE); // NOTE: Before closing window, while loop must be left! } diff --git a/src/rlgl.h b/src/rlgl.h index 8b229e9c..197ecb19 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -165,14 +165,6 @@ typedef unsigned char byte; unsigned char a; } Color; - // Rectangle type - typedef struct Rectangle { - int x; - int y; - int width; - int height; - } Rectangle; - // Texture2D type // NOTE: Data stored in GPU memory typedef struct Texture2D { @@ -703,9 +695,9 @@ typedef struct DrawCall { typedef struct VrStereoConfig { RenderTexture2D stereoFbo; // VR stereo rendering framebuffer Shader distortionShader; // VR stereo rendering distortion shader - Rectangle eyesViewport[2]; // VR stereo rendering eyes viewports Matrix eyesProjection[2]; // VR stereo rendering eyes projection matrices Matrix eyesViewOffset[2]; // VR stereo rendering eyes view offset matrices + int eyesViewport[2][4]; // VR stereo rendering eyes viewports [x, y, w, h] } VrStereoConfig; #endif @@ -4268,8 +4260,8 @@ static void SetStereoConfig(VrDeviceInfo hmd) vrConfig.eyesViewOffset[1] = MatrixTranslate(hmd.interpupillaryDistance*0.5f, 0.075f, 0.045f); // Compute eyes Viewports - vrConfig.eyesViewport[0] = (Rectangle){ 0.0f, 0.0f, (float)hmd.hResolution/2, (float)hmd.vResolution }; - vrConfig.eyesViewport[1] = (Rectangle){ hmd.hResolution/2.0f, 0.0f, (float)hmd.hResolution/2, (float) hmd.vResolution }; + //vrConfig.eyesViewport[0] = { 0.0f, 0.0f, (float)hmd.hResolution/2, (float)hmd.vResolution }; + //vrConfig.eyesViewport[1] = { hmd.hResolution/2.0f, 0.0f, (float)hmd.hResolution/2, (float) hmd.vResolution }; } // Set internal projection and modelview matrix depending on eyes tracking data -- cgit v1.2.3 From 7fb2459916a36b8bc13774163005ddd9ccd3950b Mon Sep 17 00:00:00 2001 From: raysan5 Date: Mon, 24 Dec 2018 17:45:34 +0100 Subject: Added some TODO note OpenGL extensions loading could be improved... --- src/rlgl.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/rlgl.h') diff --git a/src/rlgl.h b/src/rlgl.h index 197ecb19..5c14f9be 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1468,10 +1468,10 @@ void rlglInit(int width, int height) //for (int i = 0; i < numComp; i++) TraceLog(LOG_INFO, "Supported compressed format: 0x%x", format[i]); // NOTE: We don't need that much data on screen... right now... - -#if defined(GRAPHICS_API_OPENGL_11) - //TraceLog(LOG_INFO, "OpenGL 1.1 (or driver default) profile initialized"); -#endif + + // TODO: Automatize extensions loading using rlLoadExtensions() and GLAD + // Actually, when rlglInit() is called in InitWindow() in core.c, + // OpenGL required extensions have already been loaded (PLATFORM_DESKTOP) #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Get supported extensions list @@ -1552,7 +1552,6 @@ void rlglInit(int width, int height) // Check texture float support if (strcmp(extList[i], (const char *)"GL_OES_texture_float") == 0) texFloatSupported = true; #endif - // DDS texture compression support if ((strcmp(extList[i], (const char *)"GL_EXT_texture_compression_s3tc") == 0) || (strcmp(extList[i], (const char *)"GL_WEBGL_compressed_texture_s3tc") == 0) || @@ -1608,6 +1607,8 @@ void rlglInit(int width, int height) if (debugMarkerSupported) TraceLog(LOG_INFO, "[EXTENSION] Debug Marker supported"); + + // Initialize buffers, default shaders and default textures //---------------------------------------------------------- -- cgit v1.2.3 From 7b8965eb38adcbc325533acc831e3331c3efba9c Mon Sep 17 00:00:00 2001 From: raysan5 Date: Tue, 25 Dec 2018 15:19:25 +0100 Subject: Support float texture data on OpenGL ES 2.0 --- examples/models/models_skybox.c | 2 +- examples/models/resources/shaders/cubemap.fs | 4 ++-- examples/models/resources/shaders/cubemap.vs | 4 ++-- examples/models/resources/shaders/skybox.fs | 4 ++-- examples/models/resources/shaders/skybox.vs | 4 ++-- games/transmission/transmission.c | 2 +- src/rlgl.h | 17 +++++++++++++---- 7 files changed, 23 insertions(+), 14 deletions(-) (limited to 'src/rlgl.h') diff --git a/examples/models/models_skybox.c b/examples/models/models_skybox.c index 6f6002b8..a6b233e3 100644 --- a/examples/models/models_skybox.c +++ b/examples/models/models_skybox.c @@ -21,7 +21,7 @@ int main() InitWindow(screenWidth, screenHeight, "raylib [models] example - skybox loading and drawing"); // Define the camera to look into our 3d world - Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 0.0f, 0.0f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; + Camera camera = {{ 1.0f, 1.0f, 1.0f }, { 4.0f, 1.0f, 4.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 }; // Load skybox model Mesh cube = GenMeshCube(1.0f, 1.0f, 1.0f); diff --git a/examples/models/resources/shaders/cubemap.fs b/examples/models/resources/shaders/cubemap.fs index 09ae62f5..e8e28536 100644 --- a/examples/models/resources/shaders/cubemap.fs +++ b/examples/models/resources/shaders/cubemap.fs @@ -9,7 +9,7 @@ #version 330 // Input vertex attributes (from vertex shader) -in vec3 fragPos; +in vec3 fragPosition; // Input uniform values uniform sampler2D equirectangularMap; @@ -28,7 +28,7 @@ vec2 SampleSphericalMap(vec3 v) void main() { // Normalize local position - vec2 uv = SampleSphericalMap(normalize(fragPos)); + vec2 uv = SampleSphericalMap(normalize(fragPosition)); // Fetch color from texture map vec3 color = texture(equirectangularMap, uv).rgb; diff --git a/examples/models/resources/shaders/cubemap.vs b/examples/models/resources/shaders/cubemap.vs index 6e0bf4e1..5721eaa2 100644 --- a/examples/models/resources/shaders/cubemap.vs +++ b/examples/models/resources/shaders/cubemap.vs @@ -16,12 +16,12 @@ uniform mat4 projection; uniform mat4 view; // Output vertex attributes (to fragment shader) -out vec3 fragPos; +out vec3 fragPosition; void main() { // Calculate fragment position based on model transformations - fragPos = vertexPosition; + fragPosition = vertexPosition; // Calculate final vertex position gl_Position = projection*view*vec4(vertexPosition, 1.0); diff --git a/examples/models/resources/shaders/skybox.fs b/examples/models/resources/shaders/skybox.fs index 0bd2f320..053a2517 100644 --- a/examples/models/resources/shaders/skybox.fs +++ b/examples/models/resources/shaders/skybox.fs @@ -9,7 +9,7 @@ #version 330 // Input vertex attributes (from vertex shader) -in vec3 fragPos; +in vec3 fragPosition; // Input uniform values uniform samplerCube environmentMap; @@ -20,7 +20,7 @@ out vec4 finalColor; void main() { // Fetch color from texture map - vec3 color = texture(environmentMap, fragPos).rgb; + vec3 color = texture(environmentMap, fragPosition).rgb; // Apply gamma correction color = color/(color + vec3(1.0)); diff --git a/examples/models/resources/shaders/skybox.vs b/examples/models/resources/shaders/skybox.vs index f40d615c..dcbe6c3d 100644 --- a/examples/models/resources/shaders/skybox.vs +++ b/examples/models/resources/shaders/skybox.vs @@ -16,12 +16,12 @@ uniform mat4 projection; uniform mat4 view; // Output vertex attributes (to fragment shader) -out vec3 fragPos; +out vec3 fragPosition; void main() { // Calculate fragment position based on model transformations - fragPos = vertexPosition; + fragPosition = vertexPosition; // Remove translation from the view matrix mat4 rotView = mat4(mat3(view)); diff --git a/games/transmission/transmission.c b/games/transmission/transmission.c index d4298e34..91ca28c1 100644 --- a/games/transmission/transmission.c +++ b/games/transmission/transmission.c @@ -70,7 +70,7 @@ int main(void) SetMusicVolume(music, 1.0f); PlayMusicStream(music); - fontMission = LoadFontEx("resources/fonts/traveling_typewriter.ttf", 64, 250, 0); + fontMission = LoadFontEx("resources/fonts/traveling_typewriter.ttf", 64, 0, 250); texButton = LoadTexture("resources/textures/title_ribbon.png"); // UI BUTTON diff --git a/src/rlgl.h b/src/rlgl.h index 5c14f9be..8bee37a3 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -2968,7 +2968,7 @@ Matrix GetMatrixModelview() Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) { Texture2D cubemap = { 0 }; -#if defined(GRAPHICS_API_OPENGL_33) // || defined(GRAPHICS_API_OPENGL_ES2) +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // NOTE: SetShaderDefaultLocations() already setups locations for projection and view Matrix in shader // Other locations should be setup externally in shader before calling the function @@ -2978,22 +2978,31 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); // Flag not supported on OpenGL ES 2.0 #endif - // Setup framebuffer unsigned int fbo, rbo; glGenFramebuffers(1, &fbo); glGenRenderbuffers(1, &rbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glBindRenderbuffer(GL_RENDERBUFFER, rbo); +#if defined(GRAPHICS_API_OPENGL_33) glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, size, size); +#elif defined(GRAPHICS_API_OPENGL_ES2) + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size, size); +#endif glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo); // Set up cubemap to render and attach to framebuffer // NOTE: faces are stored with 16 bit floating point values glGenTextures(1, &cubemap.id); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap.id); - for (unsigned int i = 0; i < 6; i++) - glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, size, size, 0, GL_RGB, GL_FLOAT, NULL); + for (unsigned int i = 0; i < 6; i++) + { +#if defined(GRAPHICS_API_OPENGL_33) + glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB32F, size, size, 0, GL_RGB, GL_FLOAT, NULL); +#elif defined(GRAPHICS_API_OPENGL_ES2) + if (texFloatSupported) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, size, size, 0, GL_RGB, GL_FLOAT, NULL); +#endif + } glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); #if defined(GRAPHICS_API_OPENGL_33) -- cgit v1.2.3 From af4a177af41064db5ebf9a28dcac9eb178cd7fa9 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Wed, 26 Dec 2018 10:34:45 +0100 Subject: Corrected issue with internal buffer Problem aligning provided vertex data to multiples of four, because main buffer is intended to bu used with indexed quads... but also shared with triangles and lines. --- src/rlgl.h | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) (limited to 'src/rlgl.h') diff --git a/src/rlgl.h b/src/rlgl.h index 8bee37a3..632c21f0 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -1102,7 +1102,8 @@ void rlEnd(void) // processed but are placed in a single point to not result in a fragment output... // TODO: System could be improved (a bit) just storing every draw alignment value // and adding it to vertexOffset on drawing... maybe in a future... - int vertexToAlign = draws[drawsCounter - 1].vertexCount%4; + int vertexCount = draws[drawsCounter - 1].vertexCount; + int vertexToAlign = (vertexCount >= 4) ? vertexCount%4 : (4 - vertexCount%4); for (int i = 0; i < vertexToAlign; i++) rlVertex3f(-1, -1, -1); // Make sure vertexCount is the same for vertices, texcoords, colors and normals @@ -1145,7 +1146,7 @@ void rlEnd(void) // Verify internal buffers limits // NOTE: This check is combined with usage of rlCheckBufferLimit() - if ((vertexData[currentBuffer].vCounter/4) >= (MAX_BATCH_ELEMENTS - 4)) + if ((vertexData[currentBuffer].vCounter) >= (MAX_BATCH_ELEMENTS*4 - 4)) { // WARNING: If we are between rlPushMatrix() and rlPopMatrix() and we need to force a rlglDraw(), // we need to call rlPopMatrix() before to recover *currentMatrix (modelview) for the next forced draw call! @@ -1165,7 +1166,7 @@ void rlVertex3f(float x, float y, float z) if (useTransformMatrix) vec = Vector3Transform(vec, transformMatrix); // Verify that MAX_BATCH_ELEMENTS limit not reached - if (vertexData[currentBuffer].vCounter/4 < MAX_BATCH_ELEMENTS) + if (vertexData[currentBuffer].vCounter < (MAX_BATCH_ELEMENTS*4)) { vertexData[currentBuffer].vertices[3*vertexData[currentBuffer].vCounter] = vec.x; vertexData[currentBuffer].vertices[3*vertexData[currentBuffer].vCounter + 1] = vec.y; @@ -1262,7 +1263,7 @@ void rlDisableTexture(void) #else // NOTE: If quads batch limit is reached, // we force a draw call and next batch starts - if (vertexData[currentBuffer].vCounter/4 >= MAX_BATCH_ELEMENTS) rlglDraw(); + if (vertexData[currentBuffer].vCounter >= (MAX_BATCH_ELEMENTS*4)) rlglDraw(); #endif } @@ -1735,7 +1736,7 @@ bool rlCheckBufferLimit(int vCount) { bool overflow = false; #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - if ((vertexData[currentBuffer].vCounter + vCount)/4 >= MAX_BATCH_ELEMENTS) overflow = true; + if ((vertexData[currentBuffer].vCounter + vCount) >= (MAX_BATCH_ELEMENTS*4)) overflow = true; #endif return overflow; } @@ -3013,7 +3014,6 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) // Create projection (transposed) and different views for each face Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, 0.01, 1000.0); - //MatrixTranspose(&fboProjection); Matrix fboViews[6] = { MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }), MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }), @@ -3086,7 +3086,6 @@ Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size) // Create projection (transposed) and different views for each face Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, 0.01, 1000.0); - //MatrixTranspose(&fboProjection); Matrix fboViews[6] = { MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }), MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }), @@ -3163,7 +3162,6 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size) // Create projection (transposed) and different views for each face Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, 0.01, 1000.0); - //MatrixTranspose(&fboProjection); Matrix fboViews[6] = { MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }), MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }), -- cgit v1.2.3 From a072385c6da533331661b25f6ed24fae0f8fd41f Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 29 Dec 2018 00:00:52 +0100 Subject: Corrected issue on draws resetting --- src/rlgl.h | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/rlgl.h') diff --git a/src/rlgl.h b/src/rlgl.h index 632c21f0..7776879a 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -3961,7 +3961,7 @@ static void DrawBuffersDefault(void) if (eyesCount == 2) SetStereoView(eye, matProjection, matModelView); #endif - // Draw quads buffers + // Draw buffers if (vertexData[currentBuffer].vCounter > 0) { // Set current shader and upload current MVP matrix @@ -4047,10 +4047,14 @@ static void DrawBuffersDefault(void) projection = matProjection; modelview = matModelView; - // Reset draws counter - draws[0].mode = RL_QUADS; - draws[0].vertexCount = 0; - draws[0].textureId = defaultTextureId; + // Reset draws array + for (int i = 0; i < MAX_DRAWCALL_REGISTERED; i++) + { + draws[i].mode = RL_QUADS; + draws[i].vertexCount = 0; + draws[i].textureId = defaultTextureId; + } + drawsCounter = 1; // Change to next buffer in the list -- cgit v1.2.3 From 5c614f69755623e346105d17c71697005bd2900c Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sat, 5 Jan 2019 19:24:42 +0100 Subject: Some code tweaks --- examples/models/models_material_pbr.c | 2 +- src/raylib.h | 2 +- src/rlgl.h | 33 +++++++++++++++++++++++++-------- 3 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src/rlgl.h') diff --git a/examples/models/models_material_pbr.c b/examples/models/models_material_pbr.c index a4a10d60..6885f753 100644 --- a/examples/models/models_material_pbr.c +++ b/examples/models/models_material_pbr.c @@ -156,7 +156,7 @@ static Material LoadMaterialPBR(Color albedo, float metalness, float roughness) Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE); mat.maps[MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE); mat.maps[MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE); - mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, cubemap, BRDF_SIZE); + mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE); UnloadTexture(cubemap); UnloadTexture(texHDR); diff --git a/src/raylib.h b/src/raylib.h index e422e6bc..6d4b08d0 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1241,7 +1241,7 @@ RLAPI Matrix GetMatrixModelview(); // Get RLAPI Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size); // Generate cubemap texture from HDR texture RLAPI Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size); // Generate irradiance texture using cubemap data RLAPI Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size); // Generate prefilter texture using cubemap data -RLAPI Texture2D GenTextureBRDF(Shader shader, Texture2D cubemap, int size); // Generate BRDF texture using cubemap data +RLAPI Texture2D GenTextureBRDF(Shader shader, int size); // Generate BRDF texture using cubemap data // Shading begin/end functions RLAPI void BeginShaderMode(Shader shader); // Begin custom shader drawing diff --git a/src/rlgl.h b/src/rlgl.h index 7776879a..2b575614 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -480,7 +480,7 @@ Matrix GetMatrixModelview(); // Get inter Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size); // Generate cubemap texture from HDR texture Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size); // Generate irradiance texture using cubemap data Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size); // Generate prefilter texture using cubemap data -Texture2D GenTextureBRDF(Shader shader, Texture2D cubemap, int size); // Generate BRDF texture using cubemap data +Texture2D GenTextureBRDF(Shader shader, int size); // Generate BRDF texture using cubemap data // Shading begin/end functions void BeginShaderMode(Shader shader); // Begin custom shader drawing @@ -849,7 +849,7 @@ static PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArrays; #if defined(SUPPORT_VR_SIMULATOR) // VR global variables -static VrStereoConfig vrConfig; // VR stereo configuration for simulator +static VrStereoConfig vrConfig = { 0 }; // VR stereo configuration for simulator static bool vrSimulatorReady = false; // VR simulator ready flag static bool vrStereoRender = false; // VR stereo rendering enabled/disabled flag // NOTE: This flag is useful to render data over stereo image (i.e. FPS) @@ -2993,7 +2993,7 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rbo); // Set up cubemap to render and attach to framebuffer - // NOTE: faces are stored with 16 bit floating point values + // NOTE: Faces are stored as 32 bit floating point values glGenTextures(1, &cubemap.id); glBindTexture(GL_TEXTURE_CUBE_MAP, cubemap.id); for (unsigned int i = 0; i < 6; i++) @@ -3012,7 +3012,7 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); - // Create projection (transposed) and different views for each face + // Create projection and different views for each face Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, 0.01, 1000.0); Matrix fboViews[6] = { MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }), @@ -3050,6 +3050,10 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) cubemap.width = size; cubemap.height = size; + cubemap.mipmaps = 1; + cubemap.format = UNCOMPRESSED_R32G32B32; + + // TODO: Texture2D is a GL_TEXTURE_CUBE_MAP, not a GL_TEXTURE_2D! Only cubemap.id makes some sense... #endif return cubemap; } @@ -3216,15 +3220,20 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size) } // Generate BRDF texture using cubemap data -// TODO: OpenGL ES 2.0 does not support GL_RGB16F texture format, neither GL_DEPTH_COMPONENT24 -Texture2D GenTextureBRDF(Shader shader, Texture2D cubemap, int size) +// NOTE: OpenGL ES 2.0 does not support GL_RGB16F texture format, neither GL_DEPTH_COMPONENT24 +Texture2D GenTextureBRDF(Shader shader, int size) { Texture2D brdf = { 0 }; -#if defined(GRAPHICS_API_OPENGL_33) // || defined(GRAPHICS_API_OPENGL_ES2) +#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) // Generate BRDF convolution texture glGenTextures(1, &brdf.id); glBindTexture(GL_TEXTURE_2D, brdf.id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RG16F, size, size, 0, GL_RG, GL_FLOAT, 0); +#if defined(GRAPHICS_API_OPENGL_33) + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, size, size, 0, GL_RG, GL_FLOAT, NULL); +#elif defined(GRAPHICS_API_OPENGL_ES2) + if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, size, size, 0, GL_RG, GL_FLOAT, NULL); +#endif + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); @@ -3236,7 +3245,11 @@ Texture2D GenTextureBRDF(Shader shader, Texture2D cubemap, int size) glGenRenderbuffers(1, &rbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glBindRenderbuffer(GL_RENDERBUFFER, rbo); +#if defined(GRAPHICS_API_OPENGL_33) glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, size, size); +#elif defined(GRAPHICS_API_OPENGL_ES2) + glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, size, size); +#endif glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, brdf.id, 0); glViewport(0, 0, size, size); @@ -3246,6 +3259,10 @@ Texture2D GenTextureBRDF(Shader shader, Texture2D cubemap, int size) // Unbind framebuffer and textures glBindFramebuffer(GL_FRAMEBUFFER, 0); + + // Unload framebuffer but keep color texture + glDeleteRenderbuffers(1, &rbo); + glDeleteFramebuffers(1, &fbo); // Reset viewport dimensions to default glViewport(0, 0, screenWidth, screenHeight); -- cgit v1.2.3 From f4fe7f4d4c6d25e4bcc1ea4b48af07ef49b82720 Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 6 Jan 2019 15:49:29 +0100 Subject: Review BRDF texture generation Actually, that function should be redesigned... --- examples/models/resources/shaders/brdf.fs | 73 ++++++++++++++----------------- src/rlgl.h | 7 ++- 2 files changed, 38 insertions(+), 42 deletions(-) (limited to 'src/rlgl.h') diff --git a/examples/models/resources/shaders/brdf.fs b/examples/models/resources/shaders/brdf.fs index 59ae384a..3e8777d2 100644 --- a/examples/models/resources/shaders/brdf.fs +++ b/examples/models/resources/shaders/brdf.fs @@ -1,12 +1,15 @@ /******************************************************************************************* * -* rPBR [shader] - Bidirectional reflectance distribution function fragment shader +* BRDF LUT Generation - Bidirectional reflectance distribution function fragment shader +* +* REF: https://github.com/HectorMF/BRDFGenerator * * Copyright (c) 2017 Victor Fisac * **********************************************************************************************/ #version 330 + #define MAX_SAMPLES 1024u // Input vertex attributes (from vertex shader) @@ -18,43 +21,30 @@ const float PI = 3.14159265359; // Output fragment color out vec4 finalColor; -float DistributionGGX(vec3 N, vec3 H, float roughness); -float RadicalInverse_VdC(uint bits); vec2 Hammersley(uint i, uint N); -vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness); +float RadicalInverseVdC(uint bits); float GeometrySchlickGGX(float NdotV, float roughness); float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness); +vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness); vec2 IntegrateBRDF(float NdotV, float roughness); -float DistributionGGX(vec3 N, vec3 H, float roughness) -{ - float a = roughness*roughness; - float a2 = a*a; - float NdotH = max(dot(N, H), 0.0); - float NdotH2 = NdotH*NdotH; - - float nom = a2; - float denom = (NdotH2*(a2 - 1.0) + 1.0); - denom = PI*denom*denom; - - return nom/denom; -} - -float RadicalInverse_VdC(uint bits) +float RadicalInverseVdC(uint bits) { - bits = (bits << 16u) | (bits >> 16u); - bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); - bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); - bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); - bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); - return float(bits) * 2.3283064365386963e-10; // / 0x100000000 + bits = (bits << 16u) | (bits >> 16u); + bits = ((bits & 0x55555555u) << 1u) | ((bits & 0xAAAAAAAAu) >> 1u); + bits = ((bits & 0x33333333u) << 2u) | ((bits & 0xCCCCCCCCu) >> 2u); + bits = ((bits & 0x0F0F0F0Fu) << 4u) | ((bits & 0xF0F0F0F0u) >> 4u); + bits = ((bits & 0x00FF00FFu) << 8u) | ((bits & 0xFF00FF00u) >> 8u); + return float(bits) * 2.3283064365386963e-10; // / 0x100000000 } +// Compute Hammersley coordinates vec2 Hammersley(uint i, uint N) { - return vec2(float(i)/float(N), RadicalInverse_VdC(i)); + return vec2(float(i)/float(N), RadicalInverseVdC(i)); } +// Integrate number of importance samples for (roughness and NoV) vec3 ImportanceSampleGGX(vec2 Xi, vec3 N, float roughness) { float a = roughness*roughness; @@ -85,6 +75,7 @@ float GeometrySchlickGGX(float NdotV, float roughness) return nom/denom; } +// Compute the geometry term for the BRDF given roughness squared, NoV, NoL float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) { float NdotV = max(dot(N, V), 0.0); @@ -97,29 +88,31 @@ float GeometrySmith(vec3 N, vec3 V, vec3 L, float roughness) vec2 IntegrateBRDF(float NdotV, float roughness) { - vec3 V = vec3(sqrt(1.0 - NdotV*NdotV), 0.0, NdotV); float A = 0.0; - float B = 0.0; + float B = 0.0; + vec3 V = vec3(sqrt(1.0 - NdotV*NdotV), 0.0, NdotV); vec3 N = vec3(0.0, 0.0, 1.0); - for(uint i = 0u; i < MAX_SAMPLES; i++) + for (int i = 0; i < MAX_SAMPLES; i++) { // Generate a sample vector that's biased towards the preferred alignment direction (importance sampling) - vec2 Xi = Hammersley(i, MAX_SAMPLES); - vec3 H = ImportanceSampleGGX(Xi, N, roughness); - vec3 L = normalize(2.0*dot(V, H)*H - V); - float NdotL = max(L.z, 0.0); - float NdotH = max(H.z, 0.0); - float VdotH = max(dot(V, H), 0.0); + + vec2 Xi = Hammersley(i, MAX_SAMPLES); // Compute a Hammersely coordinate + vec3 H = ImportanceSampleGGX(Xi, N, roughness); // Integrate number of importance samples for (roughness and NoV) + vec3 L = normalize(2.0*dot(V, H)*H - V); // Compute reflection vector L + + float NdotL = max(L.z, 0.0); // Compute normal dot light + float NdotH = max(H.z, 0.0); // Compute normal dot half + float VdotH = max(dot(V, H), 0.0); // Compute view dot half if (NdotL > 0.0) { - float G = GeometrySmith(N, V, L, roughness); - float G_Vis = (G*VdotH)/(NdotH*NdotV); - float Fc = pow(1.0 - VdotH, 5.0); + float G = GeometrySmith(N, V, L, roughness); // Compute the geometry term for the BRDF given roughness squared, NoV, NoL + float GVis = (G*VdotH)/(NdotH*NdotV); // Compute the visibility term given G, VoH, NoH, NoV, NoL + float Fc = pow(1.0 - VdotH, 5.0); // Compute the fresnel term given VoH - A += (1.0 - Fc)*G_Vis; - B += Fc*G_Vis; + A += (1.0 - Fc)*GVis; // Sum the result given fresnel, geometry, visibility + B += Fc*GVis; } } diff --git a/src/rlgl.h b/src/rlgl.h index 2b575614..8c7526fb 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -3221,6 +3221,7 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size) // Generate BRDF texture using cubemap data // NOTE: OpenGL ES 2.0 does not support GL_RGB16F texture format, neither GL_DEPTH_COMPONENT24 +// TODO: Review implementation: https://github.com/HectorMF/BRDFGenerator Texture2D GenTextureBRDF(Shader shader, int size) { Texture2D brdf = { 0 }; @@ -3229,9 +3230,9 @@ Texture2D GenTextureBRDF(Shader shader, int size) glGenTextures(1, &brdf.id); glBindTexture(GL_TEXTURE_2D, brdf.id); #if defined(GRAPHICS_API_OPENGL_33) - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, size, size, 0, GL_RG, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, size, size, 0, GL_RGB, GL_FLOAT, NULL); #elif defined(GRAPHICS_API_OPENGL_ES2) - if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RG, size, size, 0, GL_RG, GL_FLOAT, NULL); + if (texFloatSupported) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, size, size, 0, GL_RGB, GL_FLOAT, NULL); #endif glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); @@ -3269,6 +3270,8 @@ Texture2D GenTextureBRDF(Shader shader, int size) brdf.width = size; brdf.height = size; + brdf.mipmaps = 1; + brdf.format = UNCOMPRESSED_R32G32B32; #endif return brdf; } -- cgit v1.2.3 From 1036de389abb9557cef7f801166a4550e0e90fee Mon Sep 17 00:00:00 2001 From: raysan5 Date: Sun, 6 Jan 2019 21:07:09 +0100 Subject: Some minor tweaks analyzing code Specific textures generation code is quite redundant and not flexible for the user, I'm trying to figure out some easy way to allow raylib users to do the same without needing those functions (very specific and shader dependant). RenderTexture loading and Cubemap textures support must be improved. --- src/rlgl.h | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) (limited to 'src/rlgl.h') diff --git a/src/rlgl.h b/src/rlgl.h index 8c7526fb..384f00f7 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -3004,6 +3004,7 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) if (texFloatSupported) glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, size, size, 0, GL_RGB, GL_FLOAT, NULL); #endif } + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); #if defined(GRAPHICS_API_OPENGL_33) @@ -3033,7 +3034,7 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) glViewport(0, 0, size, size); glBindFramebuffer(GL_FRAMEBUFFER, fbo); - for (unsigned int i = 0; i < 6; i++) + for (int i = 0; i < 6; i++) { SetShaderValueMatrix(shader, shader.locs[LOC_MATRIX_VIEW], fboViews[i]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, cubemap.id, 0); @@ -3048,12 +3049,11 @@ Texture2D GenTextureCubemap(Shader shader, Texture2D skyHDR, int size) glViewport(0, 0, screenWidth, screenHeight); //glEnable(GL_CULL_FACE); + // NOTE: Texture2D is a GL_TEXTURE_CUBE_MAP, not a GL_TEXTURE_2D! cubemap.width = size; cubemap.height = size; cubemap.mipmaps = 1; cubemap.format = UNCOMPRESSED_R32G32B32; - - // TODO: Texture2D is a GL_TEXTURE_CUBE_MAP, not a GL_TEXTURE_2D! Only cubemap.id makes some sense... #endif return cubemap; } @@ -3081,7 +3081,10 @@ Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size) glGenTextures(1, &irradiance.id); glBindTexture(GL_TEXTURE_CUBE_MAP, irradiance.id); for (unsigned int i = 0; i < 6; i++) + { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, size, size, 0, GL_RGB, GL_FLOAT, NULL); + } + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); @@ -3109,7 +3112,7 @@ Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size) glViewport(0, 0, size, size); glBindFramebuffer(GL_FRAMEBUFFER, fbo); - for (unsigned int i = 0; i < 6; i++) + for (int i = 0; i < 6; i++) { SetShaderValueMatrix(shader, shader.locs[LOC_MATRIX_VIEW], fboViews[i]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, irradiance.id, 0); @@ -3125,6 +3128,8 @@ Texture2D GenTextureIrradiance(Shader shader, Texture2D cubemap, int size) irradiance.width = size; irradiance.height = size; + irradiance.mipmaps = 1; + //irradiance.format = UNCOMPRESSED_R16G16B16; #endif return irradiance; } @@ -3154,7 +3159,10 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size) glGenTextures(1, &prefilter.id); glBindTexture(GL_TEXTURE_CUBE_MAP, prefilter.id); for (unsigned int i = 0; i < 6; i++) + { glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16F, size, size, 0, GL_RGB, GL_FLOAT, NULL); + } + glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); @@ -3185,11 +3193,11 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size) #define MAX_MIPMAP_LEVELS 5 // Max number of prefilter texture mipmaps - for (unsigned int mip = 0; mip < MAX_MIPMAP_LEVELS; mip++) + for (int mip = 0; mip < MAX_MIPMAP_LEVELS; mip++) { // Resize framebuffer according to mip-level size. - unsigned int mipWidth = size*(int) powf(0.5f, (float) mip); - unsigned int mipHeight = size* (int) powf(0.5f, (float) mip); + unsigned int mipWidth = size*(int)powf(0.5f, (float)mip); + unsigned int mipHeight = size*(int)powf(0.5f, (float)mip); glBindRenderbuffer(GL_RENDERBUFFER, rbo); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, mipWidth, mipHeight); @@ -3198,7 +3206,7 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size) float roughness = (float)mip/(float)(MAX_MIPMAP_LEVELS - 1); glUniform1f(roughnessLoc, roughness); - for (unsigned int i = 0; i < 6; ++i) + for (int i = 0; i < 6; i++) { SetShaderValueMatrix(shader, shader.locs[LOC_MATRIX_VIEW], fboViews[i]); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, prefilter.id, mip); @@ -3215,6 +3223,8 @@ Texture2D GenTexturePrefilter(Shader shader, Texture2D cubemap, int size) prefilter.width = size; prefilter.height = size; + //prefilter.mipmaps = 1 + (int)floor(log(size)/log(2)); + //prefilter.format = UNCOMPRESSED_R16G16B16; #endif return prefilter; } -- cgit v1.2.3 From 73597332b6fb1fb934c703c32f287bde1f5b3292 Mon Sep 17 00:00:00 2001 From: Marco Lizza Date: Wed, 9 Jan 2019 16:18:00 +0100 Subject: Adding uniform array support for shaders. --- .../Notepad++/raylib_npp_parser/raylib_to_parse.h | 2 ++ src/raylib.h | 2 ++ src/rlgl.h | 42 ++++++++++++++-------- 3 files changed, 32 insertions(+), 14 deletions(-) (limited to 'src/rlgl.h') diff --git a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h index 5dfb5dfe..04a6d0d3 100644 --- a/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h +++ b/projects/Notepad++/raylib_npp_parser/raylib_to_parse.h @@ -376,6 +376,8 @@ RLAPI Texture2D GetTextureDefault(void); // Get RLAPI int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location RLAPI void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) RLAPI void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) +RLAPI void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count); // Set shader uniform value (array of float/vec2/vec3/vec4) +RLAPI void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count); // Set shader uniform value (array of int/ivec2/ivec3/ivec4) RLAPI void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) RLAPI void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) RLAPI void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) diff --git a/src/raylib.h b/src/raylib.h index 430e66db..6c47ae37 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -1213,6 +1213,8 @@ RLAPI Texture2D GetTextureDefault(void); // Get RLAPI int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location RLAPI void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) RLAPI void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) +RLAPI void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count); // Set shader uniform value (array of float/vec2/vec3/vec4) +RLAPI void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count); // Set shader uniform value (array of int/ivec2/ivec3/ivec4) RLAPI void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) RLAPI void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) RLAPI void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) diff --git a/src/rlgl.h b/src/rlgl.h index e1b9a98b..1eac7438 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -478,6 +478,8 @@ Texture2D GetTextureDefault(void); // Get defau int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) +void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count); // Set shader uniform value (array of float/vec2/vec3/vec4) +void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count); // Set shader uniform value (array of int/ivec2/ivec3/ivec4) void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) @@ -2897,35 +2899,47 @@ int GetShaderLocation(Shader shader, const char *uniformName) return location; } -// Set shader uniform value (float) +// Set shader uniform value (float/vec2/vec3/vec4) void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size) +{ + SetShaderValueArray(shader, uniformLoc, value, size, 1); +} + +// Set shader uniform value (int/ivec2/ivec3/ivec4) +void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size) +{ + SetShaderValueArrayi(shader, uniformLoc, value, size, 1); +} + +// Set shader uniform value (array of float/vec2/vec3/vec4) +void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glUseProgram(shader.id); - if (size == 1) glUniform1fv(uniformLoc, 1, value); // Shader uniform type: float - else if (size == 2) glUniform2fv(uniformLoc, 1, value); // Shader uniform type: vec2 - else if (size == 3) glUniform3fv(uniformLoc, 1, value); // Shader uniform type: vec3 - else if (size == 4) glUniform4fv(uniformLoc, 1, value); // Shader uniform type: vec4 - else TraceLog(LOG_WARNING, "Shader value float array size not supported"); + if (size == 1) glUniform1fv(uniformLoc, count, value); // Shader uniform type: float[] + else if (size == 2) glUniform2fv(uniformLoc, count, value); // Shader uniform type: vec2[] + else if (size == 3) glUniform3fv(uniformLoc, count, value); // Shader uniform type: vec3[] + else if (size == 4) glUniform4fv(uniformLoc, count, value); // Shader uniform type: vec4[] + else TraceLog(LOG_WARNING, "Wrong size for shader's uniform value (1 to 4 supported)"); //glUseProgram(0); // Avoid reseting current shader program, in case other uniforms are set #endif } -// Set shader uniform value (int) -void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size) +// Set shader uniform value (array of int/ivec2/ivec3/ivec4) +void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glUseProgram(shader.id); - if (size == 1) glUniform1iv(uniformLoc, 1, value); // Shader uniform type: int - else if (size == 2) glUniform2iv(uniformLoc, 1, value); // Shader uniform type: ivec2 - else if (size == 3) glUniform3iv(uniformLoc, 1, value); // Shader uniform type: ivec3 - else if (size == 4) glUniform4iv(uniformLoc, 1, value); // Shader uniform type: ivec4 - else TraceLog(LOG_WARNING, "Shader value int array size not supported"); + if (size == 1) glUniform1iv(uniformLoc, count, value); // Shader uniform type: int[] + else if (size == 2) glUniform2iv(uniformLoc, count, value); // Shader uniform type: ivec2[] + else if (size == 3) glUniform3iv(uniformLoc, count, value); // Shader uniform type: ivec3[] + else if (size == 4) glUniform4iv(uniformLoc, count, value); // Shader uniform type: ivec4[] + else TraceLog(LOG_WARNING, "Wrong size for shader's uniform value (1 to 4 supported)"); - //glUseProgram(0); + //glUseProgram(0); // Avoid reseting current shader program, in case other uniforms are set #endif } -- cgit v1.2.3 From 55f8dbc755c2e31d2112e71439fef0d31e8090a6 Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 10 Jan 2019 11:25:26 +0100 Subject: WARNING: Redesigned SetShaderValue() --- examples/models/models_material_pbr.c | 20 ++--- examples/models/models_skybox.c | 4 +- examples/models/rlights.h | 10 +-- examples/others/standard_lighting.c | 25 +++--- .../resources/shaders/glsl330/palette-switch.fs | 2 +- examples/shaders/shaders_custom_uniform.c | 2 +- examples/shaders/shaders_palette_switch.c | 24 +++--- examples/shaders/shaders_raymarching.c | 12 +-- src/raylib.h | 23 +++++- src/rlgl.h | 96 ++++++++++++---------- 10 files changed, 120 insertions(+), 98 deletions(-) (limited to 'src/rlgl.h') diff --git a/examples/models/models_material_pbr.c b/examples/models/models_material_pbr.c index 6885f753..f93c7a68 100644 --- a/examples/models/models_material_pbr.c +++ b/examples/models/models_material_pbr.c @@ -64,7 +64,7 @@ int main() // Send to material PBR shader camera view position float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z }; - SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, 3); + SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3); //---------------------------------------------------------------------------------- // Draw @@ -148,9 +148,9 @@ static Material LoadMaterialPBR(Color albedo, float metalness, float roughness) Shader shdrBRDF = LoadShader(PATH_BRDF_VS, PATH_BRDF_FS); // Setup required shader locations - SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1); - SetShaderValuei(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, 1); - SetShaderValuei(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, 1); + SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT); + SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT); + SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT); Texture2D texHDR = LoadTexture("resources/dresden_square.hdr"); Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE); @@ -174,14 +174,14 @@ static Material LoadMaterialPBR(Color albedo, float metalness, float roughness) SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR); // Enable sample usage in shader for assigned textures - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, 1); - SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, 1); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, UNIFORM_INT); + SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, UNIFORM_INT); int renderModeLoc = GetShaderLocation(mat.shader, "renderMode"); - SetShaderValuei(mat.shader, renderModeLoc, (int[1]){ 0 }, 1); + SetShaderValue(mat.shader, renderModeLoc, (int[1]){ 0 }, UNIFORM_INT); // Set up material properties color mat.maps[MAP_ALBEDO].color = albedo; diff --git a/examples/models/models_skybox.c b/examples/models/models_skybox.c index a6b233e3..c7f76ecf 100644 --- a/examples/models/models_skybox.c +++ b/examples/models/models_skybox.c @@ -30,11 +30,11 @@ int main() // Load skybox shader and set required locations // NOTE: Some locations are automatically set at shader loading skybox.material.shader = LoadShader("resources/shaders/skybox.vs", "resources/shaders/skybox.fs"); - SetShaderValuei(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, 1); + SetShaderValue(skybox.material.shader, GetShaderLocation(skybox.material.shader, "environmentMap"), (int[1]){ MAP_CUBEMAP }, UNIFORM_INT); // Load cubemap shader and setup required shader locations Shader shdrCubemap = LoadShader("resources/shaders/cubemap.vs", "resources/shaders/cubemap.fs"); - SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1); + SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT); // Load HDR panorama (sphere) texture Texture2D texHDR = LoadTexture("resources/dresden_square.hdr"); diff --git a/examples/models/rlights.h b/examples/models/rlights.h index 0da3e2cb..19504473 100644 --- a/examples/models/rlights.h +++ b/examples/models/rlights.h @@ -158,20 +158,20 @@ Light CreateLight(int type, Vector3 pos, Vector3 targ, Color color, Shader shade void UpdateLightValues(Shader shader, Light light) { // Send to shader light enabled state and type - SetShaderValuei(shader, light.enabledLoc, (int[1]){ light.enabled }, 1); - SetShaderValuei(shader, light.typeLoc, (int[1]){ light.type }, 1); + SetShaderValue(shader, light.enabledLoc, &light.enabled, UNIFORM_INT); + SetShaderValue(shader, light.typeLoc, &light.type, UNIFORM_INT); // Send to shader light position values float position[3] = { light.position.x, light.position.y, light.position.z }; - SetShaderValue(shader, light.posLoc, position, 3); + SetShaderValue(shader, light.posLoc, position, UNIFORM_VEC3); // Send to shader light target position values float target[3] = { light.target.x, light.target.y, light.target.z }; - SetShaderValue(shader, light.targetLoc, target, 3); + SetShaderValue(shader, light.targetLoc, target, UNIFORM_VEC3); // Send to shader light color values float diff[4] = { (float)light.color.r/(float)255, (float)light.color.g/(float)255, (float)light.color.b/(float)255, (float)light.color.a/(float)255 }; - SetShaderValue(shader, light.colorLoc, diff, 4); + SetShaderValue(shader, light.colorLoc, diff, UNIFORM_VEC4); } #endif // RLIGHTS_IMPLEMENTATION \ No newline at end of file diff --git a/examples/others/standard_lighting.c b/examples/others/standard_lighting.c index 72890436..7034aa35 100644 --- a/examples/others/standard_lighting.c +++ b/examples/others/standard_lighting.c @@ -350,9 +350,6 @@ static void GetShaderLightsLocations(Shader shader) // Set shader uniform values for lights // NOTE: It would be far easier with shader UBOs but are not supported on OpenGL ES 2.0 -// TODO: Replace glUniform1i(), glUniform1f(), glUniform3f(), glUniform4f(): -//SetShaderValue(Shader shader, int uniformLoc, float *value, int size) -//SetShaderValuei(Shader shader, int uniformLoc, int *value, int size) static void SetShaderLightsValues(Shader shader) { int tempInt[8] = { 0 }; @@ -363,20 +360,20 @@ static void SetShaderLightsValues(Shader shader) if (i < lightsCount) { tempInt[0] = lights[i]->enabled; - SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], lights[i]->enabled); + SetShaderValue(shader, lightsLocs[i][0], tempInt, UNIFORM_INT); //glUniform1i(lightsLocs[i][0], lights[i]->enabled); tempInt[0] = lights[i]->type; - SetShaderValuei(shader, lightsLocs[i][1], tempInt, 1); //glUniform1i(lightsLocs[i][1], lights[i]->type); + SetShaderValue(shader, lightsLocs[i][1], tempInt, UNIFORM_INT); //glUniform1i(lightsLocs[i][1], lights[i]->type); tempFloat[0] = (float)lights[i]->diffuse.r/255.0f; tempFloat[1] = (float)lights[i]->diffuse.g/255.0f; tempFloat[2] = (float)lights[i]->diffuse.b/255.0f; tempFloat[3] = (float)lights[i]->diffuse.a/255.0f; - SetShaderValue(shader, lightsLocs[i][5], tempFloat, 4); + SetShaderValue(shader, lightsLocs[i][5], tempFloat, UNIFORM_VEC4); //glUniform4f(lightsLocs[i][5], (float)lights[i]->diffuse.r/255, (float)lights[i]->diffuse.g/255, (float)lights[i]->diffuse.b/255, (float)lights[i]->diffuse.a/255); tempFloat[0] = lights[i]->intensity; - SetShaderValue(shader, lightsLocs[i][6], tempFloat, 1); + SetShaderValue(shader, lightsLocs[i][6], tempFloat, UNIFORM_FLOAT); switch (lights[i]->type) { @@ -385,10 +382,10 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = lights[i]->position.x; tempFloat[1] = lights[i]->position.y; tempFloat[2] = lights[i]->position.z; - SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][2], tempFloat, UNIFORM_VEC3); tempFloat[0] = lights[i]->radius; - SetShaderValue(shader, lightsLocs[i][4], tempFloat, 1); + SetShaderValue(shader, lightsLocs[i][4], tempFloat, UNIFORM_FLOAT); //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z); //glUniform1f(lightsLocs[i][4], lights[i]->radius); @@ -401,7 +398,7 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = direction.x; tempFloat[1] = direction.y; tempFloat[2] = direction.z; - SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][3], tempFloat, UNIFORM_VEC3); //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z); } break; @@ -410,7 +407,7 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = lights[i]->position.x; tempFloat[1] = lights[i]->position.y; tempFloat[2] = lights[i]->position.z; - SetShaderValue(shader, lightsLocs[i][2], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][2], tempFloat, UNIFORM_VEC3); //glUniform3f(lightsLocs[i][2], lights[i]->position.x, lights[i]->position.y, lights[i]->position.z); @@ -420,11 +417,11 @@ static void SetShaderLightsValues(Shader shader) tempFloat[0] = direction.x; tempFloat[1] = direction.y; tempFloat[2] = direction.z; - SetShaderValue(shader, lightsLocs[i][3], tempFloat, 3); + SetShaderValue(shader, lightsLocs[i][3], tempFloat, UNIFORM_VEC3); //glUniform3f(lightsLocs[i][3], direction.x, direction.y, direction.z); tempFloat[0] = lights[i]->coneAngle; - SetShaderValue(shader, lightsLocs[i][7], tempFloat, 1); + SetShaderValue(shader, lightsLocs[i][7], tempFloat, UNIFORM_FLOAT); //glUniform1f(lightsLocs[i][7], lights[i]->coneAngle); } break; default: break; @@ -433,7 +430,7 @@ static void SetShaderLightsValues(Shader shader) else { tempInt[0] = 0; - SetShaderValuei(shader, lightsLocs[i][0], tempInt, 1); //glUniform1i(lightsLocs[i][0], 0); // Light disabled + SetShaderValue(shader, lightsLocs[i][0], tempInt, UNIFORM_INT); //glUniform1i(lightsLocs[i][0], 0); // Light disabled } } } diff --git a/examples/shaders/resources/shaders/glsl330/palette-switch.fs b/examples/shaders/resources/shaders/glsl330/palette-switch.fs index 178c42c5..61b532ed 100644 --- a/examples/shaders/resources/shaders/glsl330/palette-switch.fs +++ b/examples/shaders/resources/shaders/glsl330/palette-switch.fs @@ -16,7 +16,7 @@ out vec4 finalColor; void main() { // Texel color fetching from texture sampler - vec4 texelColor = texture(texture0, fragTexCoord) * fragColor; + vec4 texelColor = texture(texture0, fragTexCoord)*fragColor; // Convert the (normalized) texel color RED component (GB would work, too) // to the palette index by scaling up from [0, 1] to [0, 255]. diff --git a/examples/shaders/shaders_custom_uniform.c b/examples/shaders/shaders_custom_uniform.c index de76a376..fbfd82d0 100644 --- a/examples/shaders/shaders_custom_uniform.c +++ b/examples/shaders/shaders_custom_uniform.c @@ -79,7 +79,7 @@ int main() swirlCenter[1] = screenHeight - mousePosition.y; // Send new value to the shader to be used on drawing - SetShaderValue(shader, swirlCenterLoc, swirlCenter, 2); + SetShaderValue(shader, swirlCenterLoc, swirlCenter, UNIFORM_VEC2); UpdateCamera(&camera); // Update camera //---------------------------------------------------------------------------------- diff --git a/examples/shaders/shaders_palette_switch.c b/examples/shaders/shaders_palette_switch.c index ad09ca73..d0b56190 100644 --- a/examples/shaders/shaders_palette_switch.c +++ b/examples/shaders/shaders_palette_switch.c @@ -1,6 +1,6 @@ /******************************************************************************************* * -* raylib [shaders] example - Apply a postprocessing shader to a scene +* raylib [shaders] example - Color palette switch * * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support, * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version. @@ -9,10 +9,10 @@ * on OpenGL ES 2.0 platforms (Android, Raspberry Pi, HTML5), use #version 100 shaders * raylib comes with shaders ready for both versions, check raylib/shaders install folder * -* This example has been created using raylib 2.x (www.raylib.com) +* This example has been created using raylib 2.3 (www.raylib.com) * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) * -* Copyright (c) 2015 Ramon Santamaria (@raysan5) +* Copyright (c) 2019 Ramon Santamaria (@raysan5) * ********************************************************************************************/ @@ -28,7 +28,7 @@ #define COLORS_PER_PALETTE 8 #define VALUES_PER_COLOR 3 -static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE * VALUES_PER_COLOR] = { +static const int palettes[MAX_PALETTES][COLORS_PER_PALETTE*VALUES_PER_COLOR] = { { 0, 0, 0, 255, 0, 0, @@ -74,7 +74,7 @@ int main() int screenWidth = 800; int screenHeight = 450; - InitWindow(screenWidth, screenHeight, "raylib [shaders] example - palette-switch shader"); + InitWindow(screenWidth, screenHeight, "raylib [shaders] example - color palette switch"); // Load shader to be used on some parts drawing // NOTE 1: Using GLSL 330 shader version, on OpenGL ES 2.0 use GLSL 100 shader version @@ -106,9 +106,10 @@ int main() // Send new value to the shader to be used on drawing. // Note that we are sending RGB triplets w/o the alpha channel *only* if the current // palette index has changed (in order to save performances). - if (currentPalette != paletteIndex) { + if (currentPalette != paletteIndex) + { currentPalette = paletteIndex; - SetShaderValueArrayi(shader, paletteLoc, palettes[currentPalette], VALUES_PER_COLOR, COLORS_PER_PALETTE); + SetShaderValueV(shader, paletteLoc, palettes[currentPalette], UNIFORM_IVEC3, COLORS_PER_PALETTE); } //---------------------------------------------------------------------------------- @@ -126,14 +127,17 @@ int main() int leftover = screenHeight % COLORS_PER_PALETTE; int y = 0; - for (int i = 0; i < COLORS_PER_PALETTE; ++i) { + for (int i = 0; i < COLORS_PER_PALETTE; ++i) + { int height = linesPerRectangle; - if (leftover > 0) { + + if (leftover > 0) + { height += 1; leftover -= 1; } - DrawRectangle(0, y, screenWidth, height, CLITERAL{ i, i, i, 255 }); + DrawRectangle(0, y, screenWidth, height, (Color){ i, i, i, 255 }); y += height; } diff --git a/examples/shaders/shaders_raymarching.c b/examples/shaders/shaders_raymarching.c index d1f9d5f8..79868a2a 100644 --- a/examples/shaders/shaders_raymarching.c +++ b/examples/shaders/shaders_raymarching.c @@ -48,7 +48,7 @@ int main() int resolutionLoc = GetShaderLocation(shader, "resolution"); float resolution[2] = { screenWidth, screenHeight }; - SetShaderValue(shader, resolutionLoc, resolution, 2); + SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2); float runTime = 0.0f; @@ -70,11 +70,11 @@ int main() runTime += deltaTime; // Set shader required uniform values - SetShaderValue(shader, viewEyeLoc, cameraPos, 3); - SetShaderValue(shader, viewCenterLoc, cameraTarget, 3); - SetShaderValue(shader, viewUpLoc, cameraUp, 3); - SetShaderValue(shader, deltaTimeLoc, &deltaTime, 1); - SetShaderValue(shader, runTimeLoc, &runTime, 1); + SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3); + SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3); + SetShaderValue(shader, viewUpLoc, cameraUp, UNIFORM_VEC3); + SetShaderValue(shader, deltaTimeLoc, &deltaTime, UNIFORM_FLOAT); + SetShaderValue(shader, runTimeLoc, &runTime, UNIFORM_FLOAT); //---------------------------------------------------------------------------------- // Draw diff --git a/src/raylib.h b/src/raylib.h index 275a3b63..5e18ae00 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -676,6 +676,23 @@ typedef enum { #define LOC_MAP_DIFFUSE LOC_MAP_ALBEDO #define LOC_MAP_SPECULAR LOC_MAP_METALNESS +// Shader uniform data types +typedef enum { + UNIFORM_BOOL = 0, + UNIFORM_INT, + UNIFORM_UNIT, + UNIFORM_FLOAT, + UNIFORM_IVEC2, + UNIFORM_IVEC3, + UNIFORM_IVEC4, + UNIFORM_UVEC2, + UNIFORM_UVEC3, + UNIFORM_UVEC4, + UNIFORM_VEC2, + UNIFORM_VEC3, + UNIFORM_VEC4, +} ShaderUniformDataType; + // Material map type typedef enum { MAP_ALBEDO = 0, // MAP_DIFFUSE @@ -1229,10 +1246,8 @@ RLAPI Texture2D GetTextureDefault(void); // Get // Shader configuration functions RLAPI int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location -RLAPI void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) -RLAPI void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) -RLAPI void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count); // Set shader uniform value (array of float/vec2/vec3/vec4) -RLAPI void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count); // Set shader uniform value (array of int/ivec2/ivec3/ivec4) +RLAPI void SetShaderValue(Shader shader, int uniformLoc, const void *value, int uniformType); // Set shader uniform value +RLAPI void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int uniformType, int count); // Set shader uniform value vector RLAPI void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) RLAPI void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) RLAPI void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) diff --git a/src/rlgl.h b/src/rlgl.h index 07f18f0f..e40553bc 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -332,6 +332,23 @@ typedef unsigned char byte; LOC_MAP_PREFILTER, LOC_MAP_BRDF } ShaderLocationIndex; + + // Shader uniform data types + typedef enum { + UNIFORM_BOOL = 0, + UNIFORM_INT, + UNIFORM_UNIT, + UNIFORM_FLOAT, + UNIFORM_IVEC2, + UNIFORM_IVEC3, + UNIFORM_IVEC4, + UNIFORM_UVEC2, + UNIFORM_UVEC3, + UNIFORM_UVEC4, + UNIFORM_VEC2, + UNIFORM_VEC3, + UNIFORM_VEC4, + } ShaderUniformDataType; #define LOC_MAP_DIFFUSE LOC_MAP_ALBEDO #define LOC_MAP_SPECULAR LOC_MAP_METALNESS @@ -468,10 +485,8 @@ Texture2D GetTextureDefault(void); // Get defau // Shader configuration functions int GetShaderLocation(Shader shader, const char *uniformName); // Get shader uniform location -void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size); // Set shader uniform value (float) -void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size); // Set shader uniform value (int) -void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count); // Set shader uniform value (array of float/vec2/vec3/vec4) -void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count); // Set shader uniform value (array of int/ivec2/ivec3/ivec4) +void SetShaderValue(Shader shader, int uniformLoc, const void *value, int uniformType); // Set shader uniform value +void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int uniformType, int count); // Set shader uniform value vector void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat); // Set shader uniform value (matrix 4x4) void SetMatrixProjection(Matrix proj); // Set a custom projection matrix (replaces internal projection matrix) void SetMatrixModelview(Matrix view); // Set a custom modelview matrix (replaces internal modelview matrix) @@ -2893,49 +2908,40 @@ int GetShaderLocation(Shader shader, const char *uniformName) return location; } -// Set shader uniform value (float/vec2/vec3/vec4) -void SetShaderValue(Shader shader, int uniformLoc, const float *value, int size) -{ - SetShaderValueArray(shader, uniformLoc, value, size, 1); -} - -// Set shader uniform value (int/ivec2/ivec3/ivec4) -void SetShaderValuei(Shader shader, int uniformLoc, const int *value, int size) +// Set shader uniform value +void SetShaderValue(Shader shader, int uniformLoc, const void *value, int uniformType) { - SetShaderValueArrayi(shader, uniformLoc, value, size, 1); + SetShaderValueV(shader, uniformLoc, value, uniformType, 1); } -// Set shader uniform value (array of float/vec2/vec3/vec4) -void SetShaderValueArray(Shader shader, int uniformLoc, const float *value, int size, int count) +// Set shader uniform value vector +void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int uniformType, int count) { #if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) glUseProgram(shader.id); - if (size == 1) glUniform1fv(uniformLoc, count, value); // Shader uniform type: float[] - else if (size == 2) glUniform2fv(uniformLoc, count, value); // Shader uniform type: vec2[] - else if (size == 3) glUniform3fv(uniformLoc, count, value); // Shader uniform type: vec3[] - else if (size == 4) glUniform4fv(uniformLoc, count, value); // Shader uniform type: vec4[] - else TraceLog(LOG_WARNING, "Wrong size for shader's uniform value (1 to 4 supported)"); - + switch (uniformType) + { + case UNIFORM_BOOL: glUniform1iv(uniformLoc, count, (int *)value); break; + case UNIFORM_INT: glUniform1iv(uniformLoc, count, (int *)value); break; + case UNIFORM_UNIT: glUniform1uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_FLOAT: glUniform1fv(uniformLoc, count, (float *)value); break; + case UNIFORM_IVEC2: glUniform2iv(uniformLoc, count, (int *)value); break; + case UNIFORM_IVEC3: glUniform3iv(uniformLoc, count, (int *)value); break; + case UNIFORM_IVEC4: glUniform4iv(uniformLoc, count, (int *)value); break; + case UNIFORM_UVEC2: glUniform2uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_UVEC3: glUniform3uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_UVEC4: glUniform4uiv(uniformLoc, count, (unsigned int *)value); break; + case UNIFORM_VEC2: glUniform2fv(uniformLoc, count, (float *)value); break; + case UNIFORM_VEC3: glUniform3fv(uniformLoc, count, (float *)value); break; + case UNIFORM_VEC4: glUniform4fv(uniformLoc, count, (float *)value); break; + default: TraceLog(LOG_WARNING, "Shader uniform could not be set data type not recognized"); + } + //glUseProgram(0); // Avoid reseting current shader program, in case other uniforms are set #endif } -// Set shader uniform value (array of int/ivec2/ivec3/ivec4) -void SetShaderValueArrayi(Shader shader, int uniformLoc, const int *value, int size, int count) -{ -#if defined(GRAPHICS_API_OPENGL_33) || defined(GRAPHICS_API_OPENGL_ES2) - glUseProgram(shader.id); - - if (size == 1) glUniform1iv(uniformLoc, count, value); // Shader uniform type: int[] - else if (size == 2) glUniform2iv(uniformLoc, count, value); // Shader uniform type: ivec2[] - else if (size == 3) glUniform3iv(uniformLoc, count, value); // Shader uniform type: ivec3[] - else if (size == 4) glUniform4iv(uniformLoc, count, value); // Shader uniform type: ivec4[] - else TraceLog(LOG_WARNING, "Wrong size for shader's uniform value (1 to 4 supported)"); - - //glUseProgram(0); // Avoid reseting current shader program, in case other uniforms are set -#endif -} // Set shader uniform value (matrix 4x4) void SetShaderValueMatrix(Shader shader, int uniformLoc, Matrix mat) @@ -4286,15 +4292,15 @@ static void SetStereoConfig(VrDeviceInfo hmd) #if defined(SUPPORT_DISTORTION_SHADER) // Update distortion shader with lens and distortion-scale parameters - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftLensCenter"), leftLensCenter, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightLensCenter"), rightLensCenter, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftScreenCenter"), leftScreenCenter, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightScreenCenter"), rightScreenCenter, 2); - - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scale"), scale, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scaleIn"), scaleIn, 2); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "hmdWarpParam"), hmd.lensDistortionValues, 4); - SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "chromaAbParam"), hmd.chromaAbCorrection, 4); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftLensCenter"), leftLensCenter, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightLensCenter"), rightLensCenter, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "leftScreenCenter"), leftScreenCenter, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "rightScreenCenter"), rightScreenCenter, UNIFORM_VEC2); + + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scale"), scale, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "scaleIn"), scaleIn, UNIFORM_VEC2); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "hmdWarpParam"), hmd.lensDistortionValues, UNIFORM_VEC4); + SetShaderValue(vrConfig.distortionShader, GetShaderLocation(vrConfig.distortionShader, "chromaAbParam"), hmd.chromaAbCorrection, UNIFORM_VEC4); #endif // Fovy is normally computed with: 2*atan2(hmd.vScreenSize, 2*hmd.eyeToScreenDistance) -- cgit v1.2.3 From 64fd131ed584d063e4f1ee63050d19ca8413a32a Mon Sep 17 00:00:00 2001 From: Ray Date: Thu, 10 Jan 2019 12:28:23 +0100 Subject: Some improvements on SetShaderValue() - Unsigned int not supported on OpenGL ES 2.0 -> Removed - Reorganized enum -> Removed BOOL (not uniformType) - Support sample2D uniform type --- src/raylib.h | 14 +++++--------- src/rlgl.h | 28 ++++++++++------------------ 2 files changed, 15 insertions(+), 27 deletions(-) (limited to 'src/rlgl.h') diff --git a/src/raylib.h b/src/raylib.h index 5e18ae00..ed863649 100644 --- a/src/raylib.h +++ b/src/raylib.h @@ -678,19 +678,15 @@ typedef enum { // Shader uniform data types typedef enum { - UNIFORM_BOOL = 0, + UNIFORM_FLOAT = 0, + UNIFORM_VEC2, + UNIFORM_VEC3, + UNIFORM_VEC4, UNIFORM_INT, - UNIFORM_UNIT, - UNIFORM_FLOAT, UNIFORM_IVEC2, UNIFORM_IVEC3, UNIFORM_IVEC4, - UNIFORM_UVEC2, - UNIFORM_UVEC3, - UNIFORM_UVEC4, - UNIFORM_VEC2, - UNIFORM_VEC3, - UNIFORM_VEC4, + UNIFORM_SAMPLER2D } ShaderUniformDataType; // Material map type diff --git a/src/rlgl.h b/src/rlgl.h index e40553bc..27b577a3 100644 --- a/src/rlgl.h +++ b/src/rlgl.h @@ -335,19 +335,15 @@ typedef unsigned char byte; // Shader uniform data types typedef enum { - UNIFORM_BOOL = 0, + UNIFORM_FLOAT = 0, + UNIFORM_VEC2, + UNIFORM_VEC3, + UNIFORM_VEC4, UNIFORM_INT, - UNIFORM_UNIT, - UNIFORM_FLOAT, UNIFORM_IVEC2, UNIFORM_IVEC3, UNIFORM_IVEC4, - UNIFORM_UVEC2, - UNIFORM_UVEC3, - UNIFORM_UVEC4, - UNIFORM_VEC2, - UNIFORM_VEC3, - UNIFORM_VEC4, + UNIFORM_SAMPLER2D } ShaderUniformDataType; #define LOC_MAP_DIFFUSE LOC_MAP_ALBEDO @@ -2922,19 +2918,15 @@ void SetShaderValueV(Shader shader, int uniformLoc, const void *value, int unifo switch (uniformType) { - case UNIFORM_BOOL: glUniform1iv(uniformLoc, count, (int *)value); break; - case UNIFORM_INT: glUniform1iv(uniformLoc, count, (int *)value); break; - case UNIFORM_UNIT: glUniform1uiv(uniformLoc, count, (unsigned int *)value); break; case UNIFORM_FLOAT: glUniform1fv(uniformLoc, count, (float *)value); break; - case UNIFORM_IVEC2: glUniform2iv(uniformLoc, count, (int *)value); break; - case UNIFORM_IVEC3: glUniform3iv(uniformLoc, count, (int *)value); break; - case UNIFORM_IVEC4: glUniform4iv(uniformLoc, count, (int *)value); break; - case UNIFORM_UVEC2: glUniform2uiv(uniformLoc, count, (unsigned int *)value); break; - case UNIFORM_UVEC3: glUniform3uiv(uniformLoc, count, (unsigned int *)value); break; - case UNIFORM_UVEC4: glUniform4uiv(uniformLoc, count, (unsigned int *)value); break; case UNIFORM_VEC2: glUniform2fv(uniformLoc, count, (float *)value); break; case UNIFORM_VEC3: glUniform3fv(uniformLoc, count, (float *)value); break; case UNIFORM_VEC4: glUniform4fv(uniformLoc, count, (float *)value); break; + case UNIFORM_INT: glUniform1iv(uniformLoc, count, (int *)value); break; + case UNIFORM_IVEC2: glUniform2iv(uniformLoc, count, (int *)value); break; + case UNIFORM_IVEC3: glUniform3iv(uniformLoc, count, (int *)value); break; + case UNIFORM_IVEC4: glUniform4iv(uniformLoc, count, (int *)value); break; + case UNIFORM_SAMPLER2D: glUniform1iv(uniformLoc, count, (int *)value); break; default: TraceLog(LOG_WARNING, "Shader uniform could not be set data type not recognized"); } -- cgit v1.2.3