From 6efd17da5a058e2be730dbf63c5b482ab1921c04 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Tue, 2 Mar 2021 11:48:18 -0600 Subject: [PATCH] Getters/Setters for basic lights --- core/Light.cpp | 34 ++++++++++++++++++++- core/Light.h | 62 ++++++++++++++++++++++++++++++++++++-- core/Shaders/Shader.cpp | 8 ++--- demo/exampleworld/main.lua | 8 ++--- 4 files changed, 100 insertions(+), 12 deletions(-) diff --git a/core/Light.cpp b/core/Light.cpp index 656af4f..9d5f940 100644 --- a/core/Light.cpp +++ b/core/Light.cpp @@ -22,7 +22,37 @@ */ #include "Light.h" -Name Light::GetType() const {return "Light";} +Vector3 Light::GetColor() { + return color; +} + +void Light::SetColor(Vector3 color) { + this->color = color; +} + +float Light::GetAmbient() { + return ambient; +} + +void Light::SetAmbient(float ambient) { + this->ambient = ambient; +} + +float Light::GetDiffuse() { + return diffuse; +} + +void Light::SetDiffuse(float diffuse) { + this->diffuse = diffuse; +} + +float Light::GetSpecular() { + return specular; +} + +void Light::SetSpecular(float specular) { + this->specular = specular; +} Light *Light::Duplicate() { Light *light = static_cast(Spatial::Duplicate()); @@ -42,6 +72,8 @@ Light *Light::Create() { return new Light; } +Name Light::GetType() const {return "Light";} + Name DirectionalLight::GetType() const {return "DirectionalLight";} DirectionalLight::DirectionalLight() { diff --git a/core/Light.h b/core/Light.h index 928e934..097cb03 100644 --- a/core/Light.h +++ b/core/Light.h @@ -27,16 +27,72 @@ #include "types.h" #include "Spatial.h" +/** + All lights have color as well as ambient, diffuse, and specular properties. + In the future, light properties might be a color, but for now they are + strength modulations on the main light color. +*/ class Light : public Spatial { public: - Vector3 color; - float ambient, diffuse, specular; - virtual Name GetType() const; + + /** + Get the color property of this light + @returns The color property + */ + Vector3 GetColor(); + /** + Set the color property of this light + @param color The desired color. + */ + void SetColor(Vector3 color); + + /** Ambient light is how much light is emitted from an object illuminated by + this light with no accounting for incidence angle or the location of the observer. + @returns The ambient intensity + */ + float GetAmbient(); + /** Set the ambient light intensity. + @param ambient The desired ambient intensity for this light. + */ + void SetAmbient(float ambient); + + /** Diffuse light is how much light is reflected by an object illuminated + by this light, accounting for incidence angle of the light but not the + location of the observer. + @returns The diffuse intensity. + */ + float GetDiffuse(); + /** Set the diffuse intensity. + @param diffuse The desired diffuse intensity for this light + */ + void SetDiffuse(float diffuse); + + /** Specular light is how much light is reflected by an object illuminated + by this light, taking into account both the incidence angle of the light + as well as the location of the observer. This is what creates the + "Specular Artifact", the shiny bit on a 3D sphere. + @returns The specular intensity + */ + float GetSpecular(); + /** Set the specular intensity. + @param specular The desired specular intensity for this light. + */ + void SetSpecular(float specular); + virtual Light *Create(); virtual Light *Duplicate(); virtual Light *Instance(); + + virtual Name GetType() const; + +protected: + Vector3 color; + float ambient, diffuse, specular; }; +/** + Directional lights are infinitely far away lights that illuminate the whole world. +*/ class DirectionalLight : public Light { public: Vector3 direction; diff --git a/core/Shaders/Shader.cpp b/core/Shaders/Shader.cpp index 65a485b..248145a 100644 --- a/core/Shaders/Shader.cpp +++ b/core/Shaders/Shader.cpp @@ -75,11 +75,11 @@ void Shader::UpdateMaterial(Material material) { void Shader::UpdateDirectionalLight(DirectionalLight directionalLight) { glUniform3fv(glGetUniformLocation(id, "directionalLight.direction"), 1, glm::value_ptr(directionalLight.direction)); - glUniform3fv(glGetUniformLocation(id, "directionalLight.color"), 1, glm::value_ptr(directionalLight.color)); + glUniform3fv(glGetUniformLocation(id, "directionalLight.color"), 1, glm::value_ptr(directionalLight.GetColor())); - glUniform1f(glGetUniformLocation(id, "directionalLight.ambient"), directionalLight.ambient); - glUniform1f(glGetUniformLocation(id, "directionalLight.diffuse"), directionalLight.diffuse); - glUniform1f(glGetUniformLocation(id, "directionalLight.specular"), directionalLight.specular); + glUniform1f(glGetUniformLocation(id, "directionalLight.ambient"), directionalLight.GetAmbient()); + glUniform1f(glGetUniformLocation(id, "directionalLight.diffuse"), directionalLight.GetDiffuse()); + glUniform1f(glGetUniformLocation(id, "directionalLight.specular"), directionalLight.GetSpecular()); } Name Shader::GetName() const { diff --git a/demo/exampleworld/main.lua b/demo/exampleworld/main.lua index e9da61f..8a631e2 100644 --- a/demo/exampleworld/main.lua +++ b/demo/exampleworld/main.lua @@ -38,10 +38,10 @@ function init() local light = couch.DirectionalLight() light.direction = couch.Vector3(0.0, -1.0, -1.0) - light.color = couch.Vector3(1.0, 1.0, 1.0) - light.ambient = 0.2 - light.diffuse = 1.0 - light.specular = 0.1 + light:SetColor(couch.Vector3(1.0, 1.0, 1.0)) + light:SetAmbient(0.2) + light:SetDiffuse(1.0) + light:SetSpecular(0.1) couch.Node.GetRoot():AddChild(light:Instance()) local skybox = couch.Skybox.FromFiles(