From 0203beb367aa5fb99ba884b7a8e97b80f03acbf5 Mon Sep 17 00:00:00 2001 From: Austin Morlan Date: Thu, 20 Sep 2018 20:06:27 -0700 Subject: [PATCH] Modify lighting equations --- include/geometry.h | 2 +- include/light.h | 2 +- src/engine.cpp | 4 +- src/light.cpp | 105 ++++++++++----------------------------------- src/loader.cpp | 2 +- 5 files changed, 28 insertions(+), 87 deletions(-) diff --git a/include/geometry.h b/include/geometry.h index dbe03a7..32d1c67 100644 --- a/include/geometry.h +++ b/include/geometry.h @@ -31,7 +31,7 @@ struct Material ColorF32 ambient; ColorF32 diffuse; ColorF32 specular; - float specularExp; + float glossiness; float opacity; }; diff --git a/include/light.h b/include/light.h index c13c17e..959738b 100644 --- a/include/light.h +++ b/include/light.h @@ -19,7 +19,7 @@ struct Light // PUBLIC FUNCTIONS -ColorF32 ComputeLight(Vertex &vert, Material &material, Light &light, Camera &camera); +ColorF32 ComputeLight(Point &position, Vector &normal, Material &material, Light &light, Camera &camera); #define LIGHT_H diff --git a/src/engine.cpp b/src/engine.cpp index d6bc82d..a7f3365 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -70,7 +70,7 @@ int EngineInit(char *objFilename, char *mtlFilename) // Light configuration - light.position = Point(-100.0f, 200.0f, 0.0f); + light.position = Point(-300.0f, 200.0f, 0.0f); light.color = {1.0f, 1.0f, 1.0f, 1.0f}; light.intensity = 2.0f; light.falloffConstant = 1.0f; @@ -338,7 +338,7 @@ static void LightMesh(void) { Vertex &vert = verts.data[face.vertIndex[i]]; - vert.color = ComputeLight(vert, material, light, camera); + vert.color = ComputeLight(vert.position, vert.normal, material, light, camera); } } } diff --git a/src/light.cpp b/src/light.cpp index 824e96e..da9eeb4 100644 --- a/src/light.cpp +++ b/src/light.cpp @@ -3,103 +3,44 @@ #include "light.h" -// PRIVATE PROTOTYPES -static ColorF32 ComputeAmbientLight( - Material &material, ColorF32 &intensity); - -static ColorF32 ComputeDiffuseLight( - Material &material, ColorF32 &intensity, Vertex &vert, Light &light); - -static ColorF32 ComputeSpecularLight( - Material &material, ColorF32 &intensity, Vertex &vert, Light &light, - Camera &camera); - - // PUBLIC FUNCTIONS ColorF32 ComputeLight( - Vertex &vert, Material &material, Light &light, Camera &camera) + Point &position, Vector &normal, Material &material, Light &light, Camera &camera) { - // Distance from light to vertex - Vector direction = light.position - vert.position; + // Point light intensity is a function of the falloff factors and the distance + Vector direction = light.position - position; + VectorNormalize(direction); float distance = VectorLength(direction); - - // Point light intensity is a function of the falloff factors and the distance ColorF32 intensity = (light.intensity * light.color) - / (light.falloffConstant + light.falloffLinear * distance); + / (light.falloffConstant + (light.falloffLinear * distance)); - // Compute ambient light - ColorF32 ambientLight = ComputeAmbientLight(material, intensity); + // Diffuse Light = Kr * I * (alpha + (1 - alpha) * n.d) + float dotNormalDirection = MAX(VectorDot(normal, direction), 0.0f); + float alpha = 0.2f; + + ColorF32 diffuseLight = + material.diffuse + * intensity + * (alpha + (1.0f - alpha) * dotNormalDirection); - // Compute diffuse light - ColorF32 diffuseLight = ComputeDiffuseLight(material, intensity, vert, light); + // Specular Light = Ks * I * (r.v)^sp + Vector view = camera.position - position; + VectorNormalize(view); + Vector reflection = (2.0f * dotNormalDirection * normal) - direction; + float dotReflectionView = MAX(VectorDot(reflection, view), 0.0f); - - // Compute specular light - ColorF32 specularLight = ComputeSpecularLight(material, intensity, vert, light, camera); + ColorF32 specularLight = + material.specular + * intensity + * powf(dotReflectionView, material.glossiness); // Total light is sum of all lights - ColorF32 result = ambientLight + diffuseLight + specularLight; - - - return result; -} - - -// PRIVATE FUNCTIONS -static ColorF32 ComputeAmbientLight( - Material &material, ColorF32 &intensity) -{ - // light = Kd * I - ColorF32 result = material.ambient * intensity; - - - return result; -} - -static ColorF32 ComputeDiffuseLight( - Material &material, ColorF32 &intensity, Vertex &vert, Light &light) -{ - // Vector from the light to the vertex - Vector direction = light.position - vert.position; - VectorNormalize(direction); - float normalDotDirection = VectorDot(vert.normal, direction); - - - // light = Kr * I * n.d - ColorF32 result = material.diffuse * intensity * normalDotDirection; - - - return result; -} - -static ColorF32 ComputeSpecularLight( - Material &material, ColorF32 &intensity, Vertex &vert, Light &light, - Camera &camera) -{ - // Vector from the camera to the vertex - Vector view = camera.position - vert.position; - VectorNormalize(view); - - - // Vector from the light to the vertex - Vector direction = light.position - vert.position; - VectorNormalize(direction); - - - // Reflection vector of the light vector across the vertex normal - float normalDotDirection = VectorDot(vert.normal, direction); - Vector reflection = (2.0f * normalDotDirection * vert.normal) - direction; - float reflectDotView = MAX(VectorDot(reflection, view), 0.0f); - - - // light = Ks * I * (r.v)^sp - ColorF32 result = - material.specular * intensity * powf(reflectDotView, material.specularExp); + ColorF32 result = diffuseLight + specularLight; return result; diff --git a/src/loader.cpp b/src/loader.cpp index feaa075..5f544b8 100644 --- a/src/loader.cpp +++ b/src/loader.cpp @@ -157,7 +157,7 @@ int ParseMTL(char *filename, EngineMemory &memory) else if (strcmp(type, "Ns") == 0) { sscanf( data, "%f", - &materials.data[materialIndex].specularExp); + &materials.data[materialIndex].glossiness); } else if (strcmp(type, "Ka") == 0)