From a0936c92a9c254ede4abf3eb7860d1d0172de1a1 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Thu, 4 Mar 2021 15:10:48 -0600 Subject: [PATCH] Point light specular intensity and shininess --- demo/exampleworld/main.lua | 3 +++ shaders/flat.vert | 18 ++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/demo/exampleworld/main.lua b/demo/exampleworld/main.lua index a5a78a6..e4e287b 100644 --- a/demo/exampleworld/main.lua +++ b/demo/exampleworld/main.lua @@ -190,6 +190,9 @@ function init_point_lights() pointLight:SetSpecular(0.1) local lightBox = couch.Mesh.FromFile("../resources/cube.obj") lightBox:UniformScale(0.5); + local material = lightBox:GetMaterial(0) + material.ambient = color + lightBox:SetMaterial(0, material) pointLight:AddChild(lightBox); couch.Node.GetRoot():AddChild(pointLight:Instance()) end diff --git a/shaders/flat.vert b/shaders/flat.vert index dd7295c..a81b4f1 100644 --- a/shaders/flat.vert +++ b/shaders/flat.vert @@ -60,18 +60,22 @@ float calcDiffuseIntensity(in vec3 direction, in vec3 normal) { return max(diff, 0.0); } +float calcSpecularIntensity(in vec3 direction, in vec3 normal, in int shininess) { + vec3 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz); + vec3 reflectionDir = reflect(normalize(direction), normalize(normal)); + float spec = dot(viewDir, reflectionDir); + spec = max(spec, 0.0); + spec = pow(spec, shininess); + return spec; +} + void calcDirectionalLight(in vec3 normal) { AMBIENT += directionalLight.ambient * directionalLight.color * material.ambient; vec3 direction = -(VIEW * vec4(directionalLight.direction, 0.0)).xyz; DIFFUSE += directionalLight.diffuse * directionalLight.color * material.diffuse * calcDiffuseIntensity(direction, normal); - vec3 viewDir = normalize((VIEW * MODEL * vec4(pos, 1.0)).xyz); - vec3 reflectionDir = reflect(normalize(direction), normalize(normal)); - float spec = dot(viewDir, reflectionDir); - spec = max(spec, 0.0); - spec = pow(spec, material.shininess); - SPECULAR += directionalLight.color * (spec * material.specular); + SPECULAR += directionalLight.color * material.specular * calcSpecularIntensity(direction, normal, material.shininess); } void calcPointLight(in vec3 normal, in PointLight pointLight) { @@ -82,6 +86,8 @@ void calcPointLight(in vec3 normal, in PointLight pointLight) { AMBIENT += pointLight.color * material.ambient * pointLight.ambient * attenuation; DIFFUSE += pointLight.color * pointLight.diffuse * material.diffuse * calcDiffuseIntensity(direction, normal); + + SPECULAR += pointLight.color * material.specular * calcSpecularIntensity(direction, normal, material.shininess); } void main() {