From c9d87a4a24b472c5ad522c7cf603bff3eaaf67ac Mon Sep 17 00:00:00 2001 From: Christofer Padilla Date: Thu, 22 Aug 2019 22:59:33 -0400 Subject: [PATCH] issue #11: changed the light direction vector to a vec4, now using the w component to determine if a light is a directional light or a point light --- SpacePirates/levels/TestLevel.cpp | 8 +++--- SpacePirates/normal.frag | 42 +++++++++++++++++++------------ 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/SpacePirates/levels/TestLevel.cpp b/SpacePirates/levels/TestLevel.cpp index 9225fd3..1a80b60 100644 --- a/SpacePirates/levels/TestLevel.cpp +++ b/SpacePirates/levels/TestLevel.cpp @@ -88,9 +88,9 @@ void TestLevel::load() { cube->shader.setFloat("material.specular", 0.7f, 0.6f, 0.6f); // red shiny cube->shader.setFloat("material.shininess", 25); - cube->shader.setFloat("dlight.ambient", 0.2f, 0.2f, 0.2f); - cube->shader.setFloat("dlight.diffuse", 1.0f, 1.0f, 1.0f); // darken the light a bit to fit the scene - cube->shader.setFloat("dlight.specular", 1.0f, 1.0f, 1.0f); + cube->shader.setFloat("light.ambient", 0.2f, 0.2f, 0.2f); + cube->shader.setFloat("light.diffuse", 1.0f, 1.0f, 1.0f, 1.0f); // darken the light a bit to fit the scene + cube->shader.setFloat("light.specular", 1.0f, 1.0f, 1.0f); } void TestLevel::update() { @@ -117,7 +117,7 @@ void TestLevel::update() { float lightZ = cos(getTime()) * radius; lamp->setPosition(vec3(lightX, 1.0f, lightZ)); // cout << "lightPos: " << lamp->getPosition().x << ", " << lamp->getPosition().y << ", " << lamp->getPosition().z << endl; - cube->shader.setFloat("dlight.direction", lamp->getPosition().x, lamp->getPosition().y, lamp->getPosition().z); + cube->shader.setFloat("light.direction", lamp->getPosition().x, lamp->getPosition().y, lamp->getPosition().z, 1.0f); vec3 camPos = GameWindow::getInstance()->mainCamera.get()->getPosition(); // cout << "camPos: " << camPos.x << ", " << camPos.y << ", " << camPos.z << endl; diff --git a/SpacePirates/normal.frag b/SpacePirates/normal.frag index 8b51443..c6be60f 100644 --- a/SpacePirates/normal.frag +++ b/SpacePirates/normal.frag @@ -8,8 +8,9 @@ struct Material { float shininess; }; -struct DirectionalLight { - vec3 direction; +struct Light { + // directional light if w == 0 + vec4 direction; vec3 ambient; vec3 diffuse; @@ -17,12 +18,12 @@ struct DirectionalLight { }; in vec2 TexCoords; -in vec3 FragPos; -in vec3 Normal; - +in vec3 FragPos; +in vec3 Normal; + uniform vec3 viewPos; uniform Material material; -uniform DirectionalLight dlight; +uniform Light light; uniform float time; void main() @@ -32,20 +33,29 @@ void main() vec3 textSpec = texture(material.specular, TexCoords).rgb; // ambient - vec3 ambient = dlight.ambient * textDiff; - - // diffuse + vec3 ambient = light.ambient * textDiff; + + vec3 lightDir; + // check if this is a directional light + if (light.direction.w == 0.0) { + lightDir = normalize(-light.direction.xyz); + } // else if it's a point light + else if (light.direction.w == 1.0) { + lightDir = normalize(light.direction.xyz); + } + + // diffuse vec3 norm = normalize(Normal); //vec3 lightDir = normalize(light.position - FragPos); - vec3 lightDir = normalize(dlight.direction); + //vec3 lightDir = normalize(light.direction.xyz); float diff = max(dot(norm, lightDir), 0.0); - vec3 diffuse = dlight.diffuse * diff * textDiff; - + vec3 diffuse = light.diffuse * diff * textDiff; + // specular vec3 viewDir = normalize(viewPos - FragPos); - vec3 reflectDir = reflect(-lightDir, norm); + vec3 reflectDir = reflect(-lightDir, norm); float spec = pow(max(dot(viewDir, reflectDir), 0.0), material.shininess); - vec3 specular = dlight.specular * (spec * textSpec); + vec3 specular = light.specular * (spec * textSpec); vec3 textEmit; @@ -54,7 +64,7 @@ void main() } else { textEmit = texture(material.emission, vec2(TexCoords.x, TexCoords.y + time)).rgb; } - + vec3 result = ambient + diffuse + specular + textEmit; FragColor = vec4(result, 1.0); -} \ No newline at end of file +}