From 1ffc687ef56f73ba04f6a2dd0a0ef1f6d9a26b6f Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Fri, 1 Nov 2024 22:28:54 +0100 Subject: [PATCH 01/14] Update rmodels.c - 'fix' for GenMeshSphere artifact When creating a new sphere mesh with high number of slices/rings the top and bottom parts of the generated sphere are removed. This happens because the triangles in those parts, due to high resolution, end up being very small and are removed as part of the 'par_shapes' library's optimization. Adding par_shapes_set_epsilon_degenerate_sphere(0.0); before generating the sphere mesh sets the threshold for removal of small triangles is removed and the sphere is returned to raylib correctly. --- src/rmodels.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/rmodels.c b/src/rmodels.c index ed15d96586c5..40e0182addfe 100644 --- a/src/rmodels.c +++ b/src/rmodels.c @@ -2820,6 +2820,7 @@ Mesh GenMeshSphere(float radius, int rings, int slices) if ((rings >= 3) && (slices >= 3)) { + par_shapes_set_epsilon_degenerate_sphere(0.0); par_shapes_mesh *sphere = par_shapes_create_parametric_sphere(slices, rings); par_shapes_scale(sphere, radius, radius, radius); // NOTE: Soft normals are computed internally From 2f70fd998d57d4e874c73e1f9428b0f51d34e743 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:50:45 +0100 Subject: [PATCH 02/14] Update gbuffer.vs for GLES3 --- .../resources/shaders/glsl100/gbuffer.vs | 60 +++++-------------- 1 file changed, 15 insertions(+), 45 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl100/gbuffer.vs b/examples/shaders/resources/shaders/glsl100/gbuffer.vs index 2791ce5ee127..d9484f273589 100644 --- a/examples/shaders/resources/shaders/glsl100/gbuffer.vs +++ b/examples/shaders/resources/shaders/glsl100/gbuffer.vs @@ -1,60 +1,30 @@ -#version 100 +#version 300 es -// Input vertex attributes -attribute vec3 vertexPosition; -attribute vec2 vertexTexCoord; -attribute vec3 vertexNormal; -attribute vec4 vertexColor; +precision highp float; + +in vec3 vertexPosition; +in vec2 vertexTexCoord; +in vec3 vertexNormal; +in vec4 vertexColor; + +out vec3 fragPosition; +out vec2 fragTexCoord; +out vec3 fragNormal; +out vec4 fragColor; -// Input uniform values uniform mat4 matModel; uniform mat4 matView; uniform mat4 matProjection; -// Output vertex attributes (to fragment shader) -varying vec3 fragPosition; -varying vec2 fragTexCoord; -varying vec3 fragNormal; -varying vec4 fragColor; - - -// https://github.com/glslify/glsl-inverse -mat3 inverse(mat3 m) -{ - float a00 = m[0][0], a01 = m[0][1], a02 = m[0][2]; - float a10 = m[1][0], a11 = m[1][1], a12 = m[1][2]; - float a20 = m[2][0], a21 = m[2][1], a22 = m[2][2]; - - float b01 = a22*a11 - a12*a21; - float b11 = -a22*a10 + a12*a20; - float b21 = a21*a10 - a11*a20; - - float det = a00*b01 + a01*b11 + a02*b21; - - return mat3(b01, (-a22*a01 + a02*a21), (a12*a01 - a02*a11), - b11, (a22*a00 - a02*a20), (-a12*a00 + a02*a10), - b21, (-a21*a00 + a01*a20), (a11*a00 - a01*a10))/det; -} - -// https://github.com/glslify/glsl-transpose -mat3 transpose(mat3 m) -{ - return mat3(m[0][0], m[1][0], m[2][0], - m[0][1], m[1][1], m[2][1], - m[0][2], m[1][2], m[2][2]); -} - void main() { - // Calculate vertex attributes for fragment shader - vec4 worldPos = matModel*vec4(vertexPosition, 1.0); + vec4 worldPos = matModel * vec4(vertexPosition, 1.0); fragPosition = worldPos.xyz; fragTexCoord = vertexTexCoord; fragColor = vertexColor; mat3 normalMatrix = transpose(inverse(mat3(matModel))); - fragNormal = normalMatrix*vertexNormal; + fragNormal = normalMatrix * vertexNormal; - // Calculate final vertex position - gl_Position = matProjection*matView*worldPos; + gl_Position = matProjection * matView * worldPos; } From b51a84977f359a5633e19abfeb7ce7669fe6ea3e Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:52:51 +0100 Subject: [PATCH 03/14] Update gbuffer.fs for GLES3 --- .../resources/shaders/glsl100/gbuffer.fs | 48 ++++++++----------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl100/gbuffer.fs b/examples/shaders/resources/shaders/glsl100/gbuffer.fs index 2945c5ddd379..f6e31357982d 100644 --- a/examples/shaders/resources/shaders/glsl100/gbuffer.fs +++ b/examples/shaders/resources/shaders/glsl100/gbuffer.fs @@ -1,36 +1,26 @@ -#version 100 +#version 300 es -precision mediump float; +precision highp float; -// Input vertex attributes (from vertex shader) -varying vec3 fragPosition; -varying vec2 fragTexCoord; -varying vec3 fragNormal; -varying vec4 fragColor; +layout (location = 0) out vec4 gPosition; +layout (location = 1) out vec4 gNormal; +layout (location = 2) out vec4 gAlbedoSpec; -// TODO: Is there some alternative for GLSL100 -//layout (location = 0) out vec3 gPosition; -//layout (location = 1) out vec3 gNormal; -//layout (location = 2) out vec4 gAlbedoSpec; -//uniform vec3 gPosition; -//uniform vec3 gNormal; -//uniform vec4 gAlbedoSpec; +in vec3 fragPosition; +in vec2 fragTexCoord; +in vec3 fragNormal; +in vec4 fragColor; -// Input uniform values -uniform sampler2D texture0; // Diffuse texture +uniform sampler2D diffuseTexture; uniform sampler2D specularTexture; -void main() -{ - // Store the fragment position vector in the first gbuffer texture - //gPosition = fragPosition; - - // Store the per-fragment normals into the gbuffer - //gNormal = normalize(fragNormal); - - // Store the diffuse per-fragment color - gl_FragColor.rgb = texture2D(texture0, fragTexCoord).rgb; - - // Store specular intensity in gAlbedoSpec's alpha component - gl_FragColor.a = texture2D(specularTexture, fragTexCoord).r; +void main() { + // store the fragment position vector in the first gbuffer texture + gPosition = vec4(fragPosition,1.0); + // also store the per-fragment normals into the gbuffer + gNormal = vec4(normalize(fragNormal),1.0); + // and the diffuse per-fragment color + gAlbedoSpec.rgb = texture(diffuseTexture, fragTexCoord).rgb; + // store specular intensity in gAlbedoSpec's alpha component + gAlbedoSpec.a = texture(specularTexture, fragTexCoord).r; } From 1e3dc61a1f8658e9132292710171f45ddcd5f807 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:53:43 +0100 Subject: [PATCH 04/14] Update deferred_shading.fs for GLES3 --- .../shaders/glsl100/deferred_shading.fs | 35 +++++++++---------- 1 file changed, 16 insertions(+), 19 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl100/deferred_shading.fs b/examples/shaders/resources/shaders/glsl100/deferred_shading.fs index d782792b940e..5a6b2bd33a9d 100644 --- a/examples/shaders/resources/shaders/glsl100/deferred_shading.fs +++ b/examples/shaders/resources/shaders/glsl100/deferred_shading.fs @@ -1,12 +1,11 @@ -#version 100 +#version 300 es + +precision highp float; -precision mediump float; +out vec4 finalColor; -// Input vertex attributes (from vertex shader) -varying vec2 fragTexCoord; -varying vec4 fragColor; +in vec2 texCoord; -// Input uniform values uniform sampler2D gPosition; uniform sampler2D gNormal; uniform sampler2D gAlbedoSpec; @@ -26,34 +25,32 @@ uniform vec3 viewPosition; const float QUADRATIC = 0.032; const float LINEAR = 0.09; -void main() -{ - vec3 fragPosition = texture2D(gPosition, fragTexCoord).rgb; - vec3 normal = texture2D(gNormal, fragTexCoord).rgb; - vec3 albedo = texture2D(gAlbedoSpec, fragTexCoord).rgb; - float specular = texture2D(gAlbedoSpec, fragTexCoord).a; +void main() { + vec3 fragPosition = texture(gPosition, texCoord).rgb; + vec3 normal = texture(gNormal, texCoord).rgb; + vec3 albedo = texture(gAlbedoSpec, texCoord).rgb; + float specular = texture(gAlbedoSpec, texCoord).a; - vec3 ambient = albedo*vec3(0.1); + vec3 ambient = albedo * vec3(0.1f); vec3 viewDirection = normalize(viewPosition - fragPosition); - for (int i = 0; i < NR_LIGHTS; ++i) + for(int i = 0; i < NR_LIGHTS; ++i) { if(lights[i].enabled == 0) continue; vec3 lightDirection = lights[i].position - fragPosition; - vec3 diffuse = max(dot(normal, lightDirection), 0.0)*albedo*lights[i].color.xyz; + vec3 diffuse = max(dot(normal, lightDirection), 0.0) * albedo * lights[i].color.xyz; vec3 halfwayDirection = normalize(lightDirection + viewDirection); float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0); - vec3 specular = specular*spec*lights[i].color.xyz; + vec3 specular = specular * spec * lights[i].color.xyz; // Attenuation float distance = length(lights[i].position - fragPosition); - float attenuation = 1.0/(1.0 + LINEAR * distance + QUADRATIC*distance*distance); + float attenuation = 1.0 / (1.0 + LINEAR * distance + QUADRATIC * distance * distance); diffuse *= attenuation; specular *= attenuation; ambient += diffuse + specular; } - gl_FragColor = vec4(ambient, 1.0); + finalColor = vec4(ambient, 1.0); } - From 135630f64564d5ccd450e8267820415ea296d069 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:55:00 +0100 Subject: [PATCH 05/14] Update deferred_shading.vs for GLES3 --- .../shaders/glsl100/deferred_shading.vs | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl100/deferred_shading.vs b/examples/shaders/resources/shaders/glsl100/deferred_shading.vs index bb108ce09aba..50f94132cd1f 100644 --- a/examples/shaders/resources/shaders/glsl100/deferred_shading.vs +++ b/examples/shaders/resources/shaders/glsl100/deferred_shading.vs @@ -1,16 +1,13 @@ -#version 100 +#version 300 es -// Input vertex attributes -attribute vec3 vertexPosition; -attribute vec2 vertexTexCoord; +precision highp float; -// Output vertex attributes (to fragment shader) -varying vec2 fragTexCoord; +layout (location = 0) in vec3 vertexPosition; +layout (location = 1) in vec2 vertexTexCoord; -void main() -{ - fragTexCoord = vertexTexCoord; +out vec2 texCoord; - // Calculate final vertex position - gl_Position = vec4(vertexPosition, 1.0); +void main() { + gl_Position = vec4(vertexPosition, 1.0); + texCoord = vertexTexCoord; } From 394cf928e21c2fbb687373e58fed27864dfd91e8 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:56:54 +0100 Subject: [PATCH 06/14] Update shaders_deferred_render.c for GLES3 --- examples/shaders/shaders_deferred_render.c | 54 +++++++++++++++++----- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/examples/shaders/shaders_deferred_render.c b/examples/shaders/shaders_deferred_render.c index 13600bc09513..148494be394d 100644 --- a/examples/shaders/shaders_deferred_render.c +++ b/examples/shaders/shaders_deferred_render.c @@ -6,7 +6,7 @@ * * NOTE: This example requires raylib OpenGL 3.3 or OpenGL ES 3.0 * -* Example originally created with raylib 4.5, last time updated with raylib 4.5 +* Example originally created with raylib 4.5, last time updated with raylib 5.6 * * Example contributed by Justin Andreas Lacoste (@27justin) and reviewed by Ramon Santamaria (@raysan5) * @@ -16,6 +16,23 @@ * Copyright (c) 2023 Justin Andreas Lacoste (@27justin) * ********************************************************************************************/ +// the following #define of GLSL_VERSION used only for file access +#if defined(PLATFORM_DESKTOP) + #define GLSL_VERSION 330 + #define GRAPHICS_API_OPENGL_33 +#else // PLATFORM_ANDROID, PLATFORM_WEB + #define GLSL_VERSION 100 + #define GRAPHICS_API_OPENGL_ES3 +#endif + + +#include // Required for: NULL + +#ifdef PLATFORM_WEB + #include +#else + #include +#endif #include "raylib.h" @@ -25,13 +42,6 @@ #define RLIGHTS_IMPLEMENTATION #include "rlights.h" -#if defined(PLATFORM_DESKTOP) - #define GLSL_VERSION 330 -#else // PLATFORM_ANDROID, PLATFORM_WEB - #define GLSL_VERSION 100 -#endif - -#include // Required for: NULL #define MAX_CUBES 30 @@ -54,6 +64,17 @@ typedef enum { DEFERRED_SHADING } DeferredMode; +unsigned int custom_rlLoadTexture(int w,int h, unsigned int format) +{ +unsigned int texture; + glGenTextures(1, &texture); + glBindTexture(GL_TEXTURE_2D, texture); + glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, GL_RGBA, GL_FLOAT, NULL); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); +return texture; +} + //------------------------------------------------------------------------------------ // Program main entry point //------------------------------------------------------------------------------------ @@ -101,14 +122,22 @@ int main(void) // (instead of a detph renderbuffer) to reconstruct world positions in the final render shader via clip-space position, // depth, and the inverse view/projection matrices. - // 16-bit precision ensures OpenGL ES 3 compatibility, though it may lack precision for real scenarios. + // 32-bit precision used for desktop as well as for OpenGL ES 3. + #ifdef PLATFORM_WEB + gBuffer.positionTexture = custom_rlLoadTexture(screenWidth,screenHeight,GL_RGBA32F); + #else // But as mentioned above, the positions could be reconstructed instead of stored. If not targeting OpenGL ES // and you wish to maintain this approach, consider using `RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32`. - gBuffer.positionTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, 1); + gBuffer.positionTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32, 1); + #endif - // Similarly, 16-bit precision is used for normals ensures OpenGL ES 3 compatibility. + // Similarly, 32-bit precision is used for normals also for desktop as well as OpenGL ES 3. + #ifdef PLATFORM_WEB + gBuffer.normalTexture = custom_rlLoadTexture(screenWidth,screenHeight,GL_RGBA32F); + #else // This is generally sufficient, but a 16-bit fixed-point format offer a better uniform precision in all orientations. gBuffer.normalTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, 1); + #endif // Albedo (diffuse color) and specular strength can be combined into one texture. // The color in RGB, and the specular strength in the alpha channel. @@ -132,6 +161,7 @@ int main(void) if (!rlFramebufferComplete(gBuffer.framebuffer)) { TraceLog(LOG_WARNING, "Framebuffer is not complete"); + exit(1); } // Now we initialize the sampler2D uniform's in the deferred shader. @@ -264,10 +294,12 @@ int main(void) rlEnableColorBlend(); EndMode3D(); +#ifdef PLATFORM_DESKTOP // As a last step, we now copy over the depth buffer from our g-buffer to the default framebuffer. rlBindFramebuffer(RL_READ_FRAMEBUFFER, gBuffer.framebuffer); rlBindFramebuffer(RL_DRAW_FRAMEBUFFER, 0); rlBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, 0x00000100); // GL_DEPTH_BUFFER_BIT +#endif rlDisableFramebuffer(); // Since our shader is now done and disabled, we can draw spheres From f4d2428e524e177880a34aabc7826a8a279ae39b Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:58:08 +0100 Subject: [PATCH 07/14] Update shaders_deferred_render.c for GLES3 --- examples/shaders/shaders_deferred_render.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/shaders/shaders_deferred_render.c b/examples/shaders/shaders_deferred_render.c index 148494be394d..b11e23580c44 100644 --- a/examples/shaders/shaders_deferred_render.c +++ b/examples/shaders/shaders_deferred_render.c @@ -294,12 +294,12 @@ int main(void) rlEnableColorBlend(); EndMode3D(); -#ifdef PLATFORM_DESKTOP +//#ifdef PLATFORM_DESKTOP // As a last step, we now copy over the depth buffer from our g-buffer to the default framebuffer. rlBindFramebuffer(RL_READ_FRAMEBUFFER, gBuffer.framebuffer); rlBindFramebuffer(RL_DRAW_FRAMEBUFFER, 0); rlBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, 0x00000100); // GL_DEPTH_BUFFER_BIT -#endif +//#endif rlDisableFramebuffer(); // Since our shader is now done and disabled, we can draw spheres From c9848829d573c530eae67f31b7c679b7db4a66c6 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 15:59:18 +0100 Subject: [PATCH 08/14] Update shaders_deferred_render.c for GLES3 --- examples/shaders/shaders_deferred_render.c | 61 +++++++++++++--------- 1 file changed, 36 insertions(+), 25 deletions(-) diff --git a/examples/shaders/shaders_deferred_render.c b/examples/shaders/shaders_deferred_render.c index b11e23580c44..f0e0cc72c009 100644 --- a/examples/shaders/shaders_deferred_render.c +++ b/examples/shaders/shaders_deferred_render.c @@ -6,7 +6,7 @@ * * NOTE: This example requires raylib OpenGL 3.3 or OpenGL ES 3.0 * -* Example originally created with raylib 4.5, last time updated with raylib 5.6 +* Example originally created with raylib 4.5, last time updated for raylib 5.6 * * Example contributed by Justin Andreas Lacoste (@27justin) and reviewed by Ramon Santamaria (@raysan5) * @@ -16,7 +16,7 @@ * Copyright (c) 2023 Justin Andreas Lacoste (@27justin) * ********************************************************************************************/ -// the following #define of GLSL_VERSION used only for file access +// the following #define of GLSL_VERSION is used only for file access #if defined(PLATFORM_DESKTOP) #define GLSL_VERSION 330 #define GRAPHICS_API_OPENGL_33 @@ -64,12 +64,27 @@ typedef enum { DEFERRED_SHADING } DeferredMode; -unsigned int custom_rlLoadTexture(int w,int h, unsigned int format) +// Load depth texture/renderbuffer (to be attached to fbo) +unsigned int custom_LoadRenderbufferDepth(int width, int height) +{ + unsigned int id = 0; + // GL_DEPTH24_STENCIL8 is the default for GLFW OpenGL context and raylib uses it, you might need to change this for + // different raylib's backends like SDL, RGFW, ... + // Possible formats: GL_DEPTH_COMPONENT16, GL_DEPTH_COMPONENT24, GL_DEPTH_COMPONENT32, GL_DEPTH_COMPONENT32F and GL_DEPTH32F_STENCIL8 (the list might vary depending on the platform the code is run on) + unsigned int glInternalFormat = GL_DEPTH24_STENCIL8; + glGenRenderbuffers(1, &id); + glBindRenderbuffer(GL_RENDERBUFFER, id); + glRenderbufferStorage(GL_RENDERBUFFER, glInternalFormat, width, height); + glBindRenderbuffer(GL_RENDERBUFFER, 0); + return id; +} + +unsigned int custom_LoadTexture(int w,int h, unsigned int opengl_format) { unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); - glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, GL_RGBA, GL_FLOAT, NULL); + glTexImage2D(GL_TEXTURE_2D, 0, opengl_format, w, h, 0, GL_RGBA, GL_FLOAT, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); return texture; @@ -119,24 +134,22 @@ int main(void) rlEnableFramebuffer(gBuffer.framebuffer); // NOTE: Vertex positions are stored in a texture for simplicity. A better approach would use a depth texture - // (instead of a detph renderbuffer) to reconstruct world positions in the final render shader via clip-space position, + // (instead of a depth renderbuffer) to reconstruct world positions in the final render shader via clip-space position, // depth, and the inverse view/projection matrices. - // 32-bit precision used for desktop as well as for OpenGL ES 3. + // 32-bit precision used on desktop as well as for OpenGL ES 3. #ifdef PLATFORM_WEB - gBuffer.positionTexture = custom_rlLoadTexture(screenWidth,screenHeight,GL_RGBA32F); + gBuffer.positionTexture = custom_LoadTexture(screenWidth,screenHeight,GL_RGBA32F); #else - // But as mentioned above, the positions could be reconstructed instead of stored. If not targeting OpenGL ES - // and you wish to maintain this approach, consider using `RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32`. + // But as mentioned above, the positions could be reconstructed instead of stored. gBuffer.positionTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32, 1); #endif - // Similarly, 32-bit precision is used for normals also for desktop as well as OpenGL ES 3. + // Similarly, 32-bit precision is used for normals on desktop as well as OpenGL ES 3. #ifdef PLATFORM_WEB - gBuffer.normalTexture = custom_rlLoadTexture(screenWidth,screenHeight,GL_RGBA32F); + gBuffer.normalTexture = custom_LoadTexture(screenWidth,screenHeight,GL_RGBA32F); #else - // This is generally sufficient, but a 16-bit fixed-point format offer a better uniform precision in all orientations. - gBuffer.normalTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R16G16B16, 1); + gBuffer.normalTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32, 1); #endif // Albedo (diffuse color) and specular strength can be combined into one texture. @@ -152,7 +165,7 @@ int main(void) rlFramebufferAttach(gBuffer.framebuffer, gBuffer.albedoSpecTexture, RL_ATTACHMENT_COLOR_CHANNEL2, RL_ATTACHMENT_TEXTURE2D, 0); // Finally we attach the depth buffer. - gBuffer.depthRenderbuffer = rlLoadTextureDepth(screenWidth, screenHeight, true); + gBuffer.depthRenderbuffer = custom_rlLoadRenderbufferDepth(screenWidth, screenHeight); rlFramebufferAttach(gBuffer.framebuffer, gBuffer.depthRenderbuffer, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0); // Make sure our framebuffer is complete. @@ -168,12 +181,12 @@ int main(void) // We do this by setting the uniform's values to the texture units that // we later bind our g-buffer textures to. rlEnableShader(deferredShader.id); - int texUnitPosition = 0; - int texUnitNormal = 1; - int texUnitAlbedoSpec = 2; - SetShaderValue(deferredShader, rlGetLocationUniform(deferredShader.id, "gPosition"), &texUnitPosition, RL_SHADER_UNIFORM_SAMPLER2D); - SetShaderValue(deferredShader, rlGetLocationUniform(deferredShader.id, "gNormal"), &texUnitNormal, RL_SHADER_UNIFORM_SAMPLER2D); - SetShaderValue(deferredShader, rlGetLocationUniform(deferredShader.id, "gAlbedoSpec"), &texUnitAlbedoSpec, RL_SHADER_UNIFORM_SAMPLER2D); + int texUnitPosition = 0; + int texUnitNormal = 1; + int texUnitAlbedoSpec = 2; + SetShaderValue(deferredShader, rlGetLocationUniform(deferredShader.id, "gPosition"), &texUnitPosition, RL_SHADER_UNIFORM_SAMPLER2D); + SetShaderValue(deferredShader, rlGetLocationUniform(deferredShader.id, "gNormal"), &texUnitNormal, RL_SHADER_UNIFORM_SAMPLER2D); + SetShaderValue(deferredShader, rlGetLocationUniform(deferredShader.id, "gAlbedoSpec"), &texUnitAlbedoSpec, RL_SHADER_UNIFORM_SAMPLER2D); rlDisableShader(); // Assign out lighting shader to model @@ -294,14 +307,13 @@ int main(void) rlEnableColorBlend(); EndMode3D(); -//#ifdef PLATFORM_DESKTOP // As a last step, we now copy over the depth buffer from our g-buffer to the default framebuffer. + // This step is only needed if you plan to continue drawing in the deffer-rendered scene rlBindFramebuffer(RL_READ_FRAMEBUFFER, gBuffer.framebuffer); rlBindFramebuffer(RL_DRAW_FRAMEBUFFER, 0); rlBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, 0x00000100); // GL_DEPTH_BUFFER_BIT -//#endif - rlDisableFramebuffer(); + rlDisableFramebuffer(); // Since our shader is now done and disabled, we can draw spheres // that represent light positions in default forward rendering BeginMode3D(camera); @@ -312,8 +324,7 @@ int main(void) else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f)); } rlDisableShader(); - EndMode3D(); - + EndMode3D(); DrawText("FINAL RESULT", 10, screenHeight - 30, 20, DARKGREEN); } break; case DEFERRED_POSITION: From b638a1e7e1f858a01a63db2451121a8b43bef77c Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:01:24 +0100 Subject: [PATCH 09/14] Update deferred_shading.vs for GLES3 --- .../shaders/resources/shaders/glsl100/deferred_shading.vs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl100/deferred_shading.vs b/examples/shaders/resources/shaders/glsl100/deferred_shading.vs index 50f94132cd1f..3b55bc41e1fd 100644 --- a/examples/shaders/resources/shaders/glsl100/deferred_shading.vs +++ b/examples/shaders/resources/shaders/glsl100/deferred_shading.vs @@ -2,8 +2,8 @@ precision highp float; -layout (location = 0) in vec3 vertexPosition; -layout (location = 1) in vec2 vertexTexCoord; +in vec3 vertexPosition; +in vec2 vertexTexCoord; out vec2 texCoord; From 24d031315703abab6fd670aa9b14fa4c9c8255e4 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 16:26:25 +0100 Subject: [PATCH 10/14] Update shaders_deferred_render.c - correct typo --- examples/shaders/shaders_deferred_render.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/shaders/shaders_deferred_render.c b/examples/shaders/shaders_deferred_render.c index f0e0cc72c009..944096e5c12c 100644 --- a/examples/shaders/shaders_deferred_render.c +++ b/examples/shaders/shaders_deferred_render.c @@ -165,7 +165,7 @@ int main(void) rlFramebufferAttach(gBuffer.framebuffer, gBuffer.albedoSpecTexture, RL_ATTACHMENT_COLOR_CHANNEL2, RL_ATTACHMENT_TEXTURE2D, 0); // Finally we attach the depth buffer. - gBuffer.depthRenderbuffer = custom_rlLoadRenderbufferDepth(screenWidth, screenHeight); + gBuffer.depthRenderbuffer = custom_LoadRenderbufferDepth(screenWidth, screenHeight); rlFramebufferAttach(gBuffer.framebuffer, gBuffer.depthRenderbuffer, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0); // Make sure our framebuffer is complete. From f6cc0e92c8ddb194161076569cd1d709b4135ddc Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:36:07 +0100 Subject: [PATCH 11/14] Update shaders_deferred_render.c for GLES3 --- examples/shaders/shaders_deferred_render.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/examples/shaders/shaders_deferred_render.c b/examples/shaders/shaders_deferred_render.c index 944096e5c12c..e40418c06170 100644 --- a/examples/shaders/shaders_deferred_render.c +++ b/examples/shaders/shaders_deferred_render.c @@ -192,6 +192,15 @@ int main(void) // Assign out lighting shader to model model.materials[0].shader = gbufferShader; cube.materials[0].shader = gbufferShader; + + // add some specular noise to the material + Image tmp_img2=GenImagePerlinNoise(256,256,0,0,8.0); + Texture texture_specular=LoadTextureFromImage(tmp_img2); + UnloadImage(tmp_img2); + + cube.materials[0].maps[MATERIAL_MAP_METALNESS].texture=texture_specular; + + model.materials[0].maps[MATERIAL_MAP_METALNESS].texture=texture_specular; // Create lights //-------------------------------------------------------------------------------------- @@ -204,6 +213,7 @@ int main(void) const float CUBE_SCALE = 0.25; Vector3 cubePositions[MAX_CUBES] = { 0 }; float cubeRotations[MAX_CUBES] = { 0 }; + Color cubeColors[MAX_CUBES] = { 0 }; for (int i = 0; i < MAX_CUBES; i++) { @@ -214,6 +224,9 @@ int main(void) }; cubeRotations[i] = (float)(rand()%360); + + cubeColors[i] = (Color){GetRandomValue(0,128),GetRandomValue(0,128),GetRandomValue(0,128),255}; + } DeferredMode mode = DEFERRED_SHADING; @@ -272,7 +285,7 @@ int main(void) for (int i = 0; i < MAX_CUBES; i++) { Vector3 position = cubePositions[i]; - DrawModelEx(cube, position, (Vector3) { 1, 1, 1 }, cubeRotations[i], (Vector3) { CUBE_SCALE, CUBE_SCALE, CUBE_SCALE }, WHITE); + DrawModelEx(cube, position, (Vector3) { 1, 1, 1 }, cubeRotations[i], (Vector3) { CUBE_SCALE, CUBE_SCALE, CUBE_SCALE }, cubeColors[i]); } rlDisableShader(); @@ -355,7 +368,7 @@ int main(void) .height = screenHeight, }, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, Vector2Zero(), RAYWHITE); - DrawText("ALBEDO TEXTURE", 10, screenHeight - 30, 20, DARKGREEN); + DrawText("ALBEDO*SPECULAR TEXTURE", 10, screenHeight - 30, 20, DARKGREEN); } break; default: break; } @@ -374,6 +387,8 @@ int main(void) UnloadModel(model); // Unload the models UnloadModel(cube); + UnloadTexture(texture_specular); + UnloadShader(deferredShader); // Unload shaders UnloadShader(gbufferShader); From b0646d286777259796606909c07d597e5f5858a6 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:38:28 +0100 Subject: [PATCH 12/14] Update shaders_deferred_render.c for GLES3 From 80f65821f41c1f11efbef30ff82046fbf666af23 Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:40:19 +0100 Subject: [PATCH 13/14] Update deferred_shading.fs for GLES3 --- .../shaders/resources/shaders/glsl100/deferred_shading.fs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl100/deferred_shading.fs b/examples/shaders/resources/shaders/glsl100/deferred_shading.fs index 5a6b2bd33a9d..369316c774f3 100644 --- a/examples/shaders/resources/shaders/glsl100/deferred_shading.fs +++ b/examples/shaders/resources/shaders/glsl100/deferred_shading.fs @@ -29,9 +29,9 @@ void main() { vec3 fragPosition = texture(gPosition, texCoord).rgb; vec3 normal = texture(gNormal, texCoord).rgb; vec3 albedo = texture(gAlbedoSpec, texCoord).rgb; - float specular = texture(gAlbedoSpec, texCoord).a; + float specular = pow(texture(gAlbedoSpec, texCoord).a,8.0); - vec3 ambient = albedo * vec3(0.1f); + vec3 ambient = albedo * vec3(0.03f); vec3 viewDirection = normalize(viewPosition - fragPosition); for(int i = 0; i < NR_LIGHTS; ++i) @@ -41,7 +41,7 @@ void main() { vec3 diffuse = max(dot(normal, lightDirection), 0.0) * albedo * lights[i].color.xyz; vec3 halfwayDirection = normalize(lightDirection + viewDirection); - float spec = pow(max(dot(normal, halfwayDirection), 0.0), 32.0); + float spec = pow(max(dot(normal, halfwayDirection), 0.0), 16.0); vec3 specular = specular * spec * lights[i].color.xyz; // Attenuation From 8e7e9feb64f4bec1f030264502e9e977c2d0a9bf Mon Sep 17 00:00:00 2001 From: MikiZX1 <161243635+MikiZX1@users.noreply.github.com> Date: Sat, 18 Jan 2025 17:40:54 +0100 Subject: [PATCH 14/14] Update gbuffer.fs for GLES3 --- examples/shaders/resources/shaders/glsl100/gbuffer.fs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/shaders/resources/shaders/glsl100/gbuffer.fs b/examples/shaders/resources/shaders/glsl100/gbuffer.fs index f6e31357982d..f18c965618f4 100644 --- a/examples/shaders/resources/shaders/glsl100/gbuffer.fs +++ b/examples/shaders/resources/shaders/glsl100/gbuffer.fs @@ -11,8 +11,10 @@ in vec2 fragTexCoord; in vec3 fragNormal; in vec4 fragColor; -uniform sampler2D diffuseTexture; -uniform sampler2D specularTexture; +uniform vec4 colDiffuse; + +uniform sampler2D texture0; +uniform sampler2D texture1; void main() { // store the fragment position vector in the first gbuffer texture @@ -20,7 +22,7 @@ void main() { // also store the per-fragment normals into the gbuffer gNormal = vec4(normalize(fragNormal),1.0); // and the diffuse per-fragment color - gAlbedoSpec.rgb = texture(diffuseTexture, fragTexCoord).rgb; + gAlbedoSpec.rgb = texture(texture0, fragTexCoord).rgb * colDiffuse.rgb; // store specular intensity in gAlbedoSpec's alpha component - gAlbedoSpec.a = texture(specularTexture, fragTexCoord).r; + gAlbedoSpec.a = pow(texture(texture1, fragTexCoord).r*1.6,16.0); }