From b92c9d17efff6340dad64bd920900a130166ae11 Mon Sep 17 00:00:00 2001 From: 0undefined Date: Tue, 11 Nov 2025 00:17:37 +0100 Subject: Add some lighting Ambient & Diffuse, shadowmapping et. al. TBD --- resources/shader.frag | 17 ++++++++++++++++- resources/shader.vert | 13 ++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) (limited to 'resources') diff --git a/resources/shader.frag b/resources/shader.frag index f81277d..0b81464 100644 --- a/resources/shader.frag +++ b/resources/shader.frag @@ -2,10 +2,25 @@ // Ouput data in vec2 UV; +in vec3 FragmentPos; +in vec3 Normal; out vec3 color; uniform sampler2D textureSampler; void main() { - color = texture(textureSampler, UV).rgb; + vec3 light_color_ambient = vec3(0.4, 0.6, 0.9); + vec3 light_color_diffuse = vec3(0.8, 1.0, 1.0); + + vec3 lightpos = vec3(16, 26, 32); + + float ambient_strength = 0.3; + vec3 ambient = ambient_strength * light_color_ambient; + + vec3 norm = normalize(Normal); + vec3 light_dir = normalize(lightpos - FragmentPos); + + float diff = max(dot(norm, light_dir), 0.0); + vec3 diffuse = diff * light_color_diffuse; + color = (ambient + diffuse) * texture(textureSampler, UV).rgb; } diff --git a/resources/shader.vert b/resources/shader.vert index e5aff9a..50e8218 100644 --- a/resources/shader.vert +++ b/resources/shader.vert @@ -3,21 +3,20 @@ // Input vertex data, different for all executions of this shader. layout(location = 0) in vec3 vertexPosition_modelspace; layout(location = 1) in vec2 vertexUV; +layout(location = 2) in vec3 vNorm; out vec2 UV; +out vec3 FragmentPos; +out vec3 Normal; uniform mat4 MVP; +uniform mat4 modelPosition; void main() { gl_Position = MVP * vec4(vertexPosition_modelspace, 1); UV = vertexUV; - - //fragmentcolor.x = vertexPosition_modelspace.x; //= vertexColor; - //fragmentcolor.y = vertexPosition_modelspace.y; //= vertexColor; - //fragmentcolor.z = vertexPosition_modelspace.z; //= vertexColor; - - //fragmentcolor += 1; - //fragmentcolor /= 2; + FragmentPos = vec3(modelPosition * vec4(vertexPosition_modelspace,1)); + Normal = vNorm; } -- cgit v1.3