From cb05945e764982357092a40a83c454c2037f303a Mon Sep 17 00:00:00 2001 From: Johnny Cena Date: Wed, 20 Aug 2025 11:17:44 +0200 Subject: [PATCH 1/3] add missing shaders --- .../shaders/glsl100/voxel_lighting.fs | 65 +++++++++++++++++++ .../shaders/glsl100/voxel_lighting.vs | 28 ++++++++ .../shaders/glsl120/voxel_lighting.fs | 61 +++++++++++++++++ .../shaders/glsl120/voxel_lighting.vs | 23 +++++++ 4 files changed, 177 insertions(+) create mode 100644 examples/models/resources/shaders/glsl100/voxel_lighting.fs create mode 100644 examples/models/resources/shaders/glsl100/voxel_lighting.vs create mode 100644 examples/models/resources/shaders/glsl120/voxel_lighting.fs create mode 100644 examples/models/resources/shaders/glsl120/voxel_lighting.vs diff --git a/examples/models/resources/shaders/glsl100/voxel_lighting.fs b/examples/models/resources/shaders/glsl100/voxel_lighting.fs new file mode 100644 index 000000000..ba02ad21e --- /dev/null +++ b/examples/models/resources/shaders/glsl100/voxel_lighting.fs @@ -0,0 +1,65 @@ +#ifdef GL_ES +precision mediump float; +#endif + +// Input from vertex shader +varying vec3 fragPosition; +varying vec4 fragColor; +varying vec3 fragNormal; + +// Uniforms +uniform vec4 colDiffuse; +uniform vec4 ambient; +uniform vec3 viewPos; + +#define MAX_LIGHTS 4 +#define LIGHT_DIRECTIONAL 0 +#define LIGHT_POINT 1 + +struct Light { + int enabled; + int type; + vec3 position; + vec3 target; + vec4 color; +}; + +uniform Light lights[MAX_LIGHTS]; + +void main() +{ + vec3 lightDot = vec3(0.0); + vec3 normal = normalize(fragNormal); + vec3 viewD = normalize(viewPos - fragPosition); + vec3 specular = vec3(0.0); + + for (int i = 0; i < MAX_LIGHTS; i++) + { + if (lights[i].enabled == 1) + { + vec3 light = vec3(0.0); + + if (lights[i].type == LIGHT_DIRECTIONAL) + light = -normalize(lights[i].target - lights[i].position); + + if (lights[i].type == LIGHT_POINT) + light = normalize(lights[i].position - fragPosition); + + float NdotL = max(dot(normal, light), 0.0); + lightDot += lights[i].color.rgb * NdotL; + + if (NdotL > 0.0) + { + float specCo = pow(max(0.0, dot(viewD, reflect(-light, normal))), 16.0); + specular += specCo; + } + } + } + + vec4 finalColor = (fragColor * ((colDiffuse + vec4(specular, 1.0)) * vec4(lightDot, 1.0))); + finalColor += fragColor * (ambient / 10.0) * colDiffuse; + + finalColor = pow(finalColor, vec4(1.0/2.2)); // gamma correction + + gl_FragColor = finalColor; +} diff --git a/examples/models/resources/shaders/glsl100/voxel_lighting.vs b/examples/models/resources/shaders/glsl100/voxel_lighting.vs new file mode 100644 index 000000000..e8951eb75 --- /dev/null +++ b/examples/models/resources/shaders/glsl100/voxel_lighting.vs @@ -0,0 +1,28 @@ +#ifdef GL_ES +precision mediump float; +#endif + +// Input vertex attributes +attribute vec3 vertexPosition; +attribute vec3 vertexNormal; +attribute vec4 vertexColor; +// attribute vec2 vertexTexCoord; + +// Input uniform values +uniform mat4 mvp; +uniform mat4 matModel; +uniform mat4 matNormal; + +// Output to fragment shader +varying vec3 fragPosition; +varying vec4 fragColor; +varying vec3 fragNormal; + +void main() +{ + fragPosition = vec3(matModel * vec4(vertexPosition, 1.0)); + fragColor = vertexColor; + fragNormal = normalize(vec3(matNormal * vec4(vertexNormal, 1.0))); + + gl_Position = mvp * vec4(vertexPosition, 1.0); +} diff --git a/examples/models/resources/shaders/glsl120/voxel_lighting.fs b/examples/models/resources/shaders/glsl120/voxel_lighting.fs new file mode 100644 index 000000000..dab171950 --- /dev/null +++ b/examples/models/resources/shaders/glsl120/voxel_lighting.fs @@ -0,0 +1,61 @@ +// Input from vertex shader +varying vec3 fragPosition; +varying vec4 fragColor; +varying vec3 fragNormal; + +// Uniforms +uniform vec4 colDiffuse; +uniform vec4 ambient; +uniform vec3 viewPos; + +#define MAX_LIGHTS 4 +#define LIGHT_DIRECTIONAL 0 +#define LIGHT_POINT 1 + +struct Light { + int enabled; + int type; + vec3 position; + vec3 target; + vec4 color; +}; + +uniform Light lights[MAX_LIGHTS]; + +void main() +{ + vec3 lightDot = vec3(0.0); + vec3 normal = normalize(fragNormal); + vec3 viewD = normalize(viewPos - fragPosition); + vec3 specular = vec3(0.0); + + for (int i = 0; i < MAX_LIGHTS; i++) + { + if (lights[i].enabled == 1) + { + vec3 light = vec3(0.0); + + if (lights[i].type == LIGHT_DIRECTIONAL) + light = -normalize(lights[i].target - lights[i].position); + + if (lights[i].type == LIGHT_POINT) + light = normalize(lights[i].position - fragPosition); + + float NdotL = max(dot(normal, light), 0.0); + lightDot += lights[i].color.rgb * NdotL; + + if (NdotL > 0.0) + { + float specCo = pow(max(0.0, dot(viewD, reflect(-light, normal))), 16.0); + specular += specCo; + } + } + } + + vec4 finalColor = (fragColor * ((colDiffuse + vec4(specular, 1.0)) * vec4(lightDot, 1.0))); + finalColor += fragColor * (ambient / 10.0) * colDiffuse; + + finalColor = pow(finalColor, vec4(1.0/2.2)); // gamma correction + + gl_FragColor = finalColor; +} diff --git a/examples/models/resources/shaders/glsl120/voxel_lighting.vs b/examples/models/resources/shaders/glsl120/voxel_lighting.vs new file mode 100644 index 000000000..2a5930fd9 --- /dev/null +++ b/examples/models/resources/shaders/glsl120/voxel_lighting.vs @@ -0,0 +1,23 @@ +// Input vertex attributes +attribute vec3 vertexPosition; +attribute vec3 vertexNormal; +attribute vec4 vertexColor; + +// Uniforms +uniform mat4 mvp; +uniform mat4 matModel; +uniform mat4 matNormal; + +// Output to fragment shader +varying vec3 fragPosition; +varying vec4 fragColor; +varying vec3 fragNormal; + +void main() +{ + fragPosition = vec3(matModel * vec4(vertexPosition, 1.0)); + fragColor = vertexColor; + fragNormal = normalize(vec3(matNormal * vec4(vertexNormal, 1.0))); + + gl_Position = mvp * vec4(vertexPosition, 1.0); +} From df200b942c2a9fa3f33583f2d42c4291cde3f740 Mon Sep 17 00:00:00 2001 From: Johnny Cena Date: Wed, 20 Aug 2025 12:16:57 +0200 Subject: [PATCH 2/3] add versions to new shaders --- examples/models/resources/shaders/glsl100/voxel_lighting.fs | 1 + examples/models/resources/shaders/glsl100/voxel_lighting.vs | 1 + examples/models/resources/shaders/glsl120/voxel_lighting.fs | 1 + examples/models/resources/shaders/glsl120/voxel_lighting.vs | 1 + 4 files changed, 4 insertions(+) diff --git a/examples/models/resources/shaders/glsl100/voxel_lighting.fs b/examples/models/resources/shaders/glsl100/voxel_lighting.fs index ba02ad21e..b9a57cfc9 100644 --- a/examples/models/resources/shaders/glsl100/voxel_lighting.fs +++ b/examples/models/resources/shaders/glsl100/voxel_lighting.fs @@ -1,3 +1,4 @@ +#version 100 #ifdef GL_ES precision mediump float; #endif diff --git a/examples/models/resources/shaders/glsl100/voxel_lighting.vs b/examples/models/resources/shaders/glsl100/voxel_lighting.vs index e8951eb75..165365528 100644 --- a/examples/models/resources/shaders/glsl100/voxel_lighting.vs +++ b/examples/models/resources/shaders/glsl100/voxel_lighting.vs @@ -1,3 +1,4 @@ +#version 100 #ifdef GL_ES precision mediump float; #endif diff --git a/examples/models/resources/shaders/glsl120/voxel_lighting.fs b/examples/models/resources/shaders/glsl120/voxel_lighting.fs index dab171950..eb01f96bb 100644 --- a/examples/models/resources/shaders/glsl120/voxel_lighting.fs +++ b/examples/models/resources/shaders/glsl120/voxel_lighting.fs @@ -1,3 +1,4 @@ +#version 120 // Input from vertex shader varying vec3 fragPosition; varying vec4 fragColor; diff --git a/examples/models/resources/shaders/glsl120/voxel_lighting.vs b/examples/models/resources/shaders/glsl120/voxel_lighting.vs index 2a5930fd9..75163c7f8 100644 --- a/examples/models/resources/shaders/glsl120/voxel_lighting.vs +++ b/examples/models/resources/shaders/glsl120/voxel_lighting.vs @@ -1,3 +1,4 @@ +#version 120 // Input vertex attributes attribute vec3 vertexPosition; attribute vec3 vertexNormal; From a99649b455a9fb05c825776cf02a75302fbe4dac Mon Sep 17 00:00:00 2001 From: Johnny Cena Date: Wed, 20 Aug 2025 12:18:51 +0200 Subject: [PATCH 3/3] remove unneeded checks --- examples/models/resources/shaders/glsl100/voxel_lighting.fs | 3 +-- examples/models/resources/shaders/glsl100/voxel_lighting.vs | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/examples/models/resources/shaders/glsl100/voxel_lighting.fs b/examples/models/resources/shaders/glsl100/voxel_lighting.fs index b9a57cfc9..f23d9292d 100644 --- a/examples/models/resources/shaders/glsl100/voxel_lighting.fs +++ b/examples/models/resources/shaders/glsl100/voxel_lighting.fs @@ -1,7 +1,6 @@ #version 100 -#ifdef GL_ES + precision mediump float; -#endif // Input from vertex shader varying vec3 fragPosition; diff --git a/examples/models/resources/shaders/glsl100/voxel_lighting.vs b/examples/models/resources/shaders/glsl100/voxel_lighting.vs index 165365528..12f53ed32 100644 --- a/examples/models/resources/shaders/glsl100/voxel_lighting.vs +++ b/examples/models/resources/shaders/glsl100/voxel_lighting.vs @@ -1,7 +1,6 @@ #version 100 -#ifdef GL_ES + precision mediump float; -#endif // Input vertex attributes attribute vec3 vertexPosition;