From cb05945e764982357092a40a83c454c2037f303a Mon Sep 17 00:00:00 2001 From: Johnny Cena Date: Wed, 20 Aug 2025 11:17:44 +0200 Subject: [PATCH] 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); +}