From 1e34058ac8c5929e9dedb544b9a1c84a6c13435a Mon Sep 17 00:00:00 2001 From: iurii Date: Sat, 31 Aug 2024 22:27:54 +0300 Subject: phong shader model --- res/shaders/colors.fs | 12 +++++++++++- res/shaders/colors.vs | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'res/shaders') diff --git a/res/shaders/colors.fs b/res/shaders/colors.fs index d434370..2184d03 100644 --- a/res/shaders/colors.fs +++ b/res/shaders/colors.fs @@ -8,17 +8,27 @@ in vec3 FragPos; uniform vec3 object_color; uniform vec3 light_color; uniform vec3 light_position; +uniform vec3 view_position; void main() { + // ambient float ambient_stength = 0.1; vec3 ambient = ambient_stength * light_color; + // diffuse vec3 norm = normalize(Normal); vec3 light_dir = normalize(light_position - FragPos); float diff = max(dot(norm, light_dir), 0.0); vec3 diffuse = diff * light_color; + + // specular + float specular_strength = 0.5; + vec3 view_dir = normalize(view_position - FragPos); + vec3 reflect_dir = reflect(-light_dir, norm); + float spec = pow(max(dot(view_dir, reflect_dir), 0.0), 32); + vec3 specular = specular_strength * spec * light_color; - vec3 result = (ambient + diffuse) * object_color; + vec3 result = (ambient + diffuse + specular) * object_color; FragColor = vec4(result, 1.0); } diff --git a/res/shaders/colors.vs b/res/shaders/colors.vs index 323b2ad..15cc265 100644 --- a/res/shaders/colors.vs +++ b/res/shaders/colors.vs @@ -12,7 +12,7 @@ out vec3 FragPos; void main() { FragPos = vec3(model * vec4(a_pos, 1.0)); - Normal = a_normal; + Normal = mat3(transpose(inverse(model))) * a_normal; gl_Position = projection * view * vec4(FragPos, 1.0); } -- cgit 1.4.1-2-gfad0