From 35085524d330d40f2c566bef1c4512b88cf571e3 Mon Sep 17 00:00:00 2001 From: Daniel Date: Thu, 10 Aug 2023 10:54:30 +0200 Subject: [PATCH] added directional light --- src/Engine/main.cpp | 26 ++++++++++++++++--------- src/Engine/shaders/default/default.frag | 5 +++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/Engine/main.cpp b/src/Engine/main.cpp index 78fc509..afea0c6 100644 --- a/src/Engine/main.cpp +++ b/src/Engine/main.cpp @@ -199,13 +199,8 @@ int main() for (size_t i = 0; i < allShaders.size(); i++) { auto shader = allShaders[i]; - auto model = allObjects[i].GetModelMatrix(); + glm::mat4 model = allObjects[i].GetModelMatrix(); - if(i == 0) - { - float angle = 20.0f * currentFrameTime; - model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); - } shader->Use(); shader->SetMatrix("viewMatrix", cam.GetViewMatrix()); // more or less the camera @@ -220,7 +215,7 @@ int main() lightColour.z = sin(glfwGetTime() * 1.3f); glm::vec3 diffuseColour = glm::vec3(1.0f);//lightColour * glm::vec3(0.5f); - glm::vec3 ambientColour = glm::vec3(0.2f);//diffuseColour * glm::vec3(0.2f); + glm::vec3 ambientColour = diffuseColour * glm::vec3(0.05f); lighting->Use(); lighting->SetInt("material.diffuseMap", 0); @@ -230,7 +225,7 @@ int main() lighting->SetFloat("light.ambientIntensity", ambientColour); lighting->SetFloat("light.diffuseIntensity", diffuseColour); lighting->SetFloat("light.specularIntensity", 1.0f, 1.0f, 1.0f); - lighting->SetFloat("lightPosition", lightPosition); + lighting->SetFloat("light.direction", -0.2f, -1.0f, -0.3f); lighting->SetFloat("viewPosition", cam.Position); diffuseMap.BindTexture(); @@ -239,7 +234,20 @@ int main() for (size_t i = 0; i < allObjects.size(); i++) { allShaders[i]->Use(); - allObjects[i].Draw(); + + for (size_t j = 0; j < 10; j++) + { + allObjects[i].Position = cubePositions[j]; + glm::mat4 model = allObjects[i].GetModelMatrix(); + if (i == 0) + { + float angle = 20.0f * j;// * currentFrameTime; + model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); + } + + allShaders[i]->SetMatrix("modelMatrix", model); + allObjects[i].Draw(); + } } glfwSwapBuffers(window); diff --git a/src/Engine/shaders/default/default.frag b/src/Engine/shaders/default/default.frag index 8b445a6..76eeced 100644 --- a/src/Engine/shaders/default/default.frag +++ b/src/Engine/shaders/default/default.frag @@ -5,7 +5,8 @@ struct Material { float shininess; }; struct Light { - vec3 position; + // vec3 position; + vec3 direction; vec3 ambientIntensity; vec3 diffuseIntensity; @@ -28,7 +29,7 @@ void main() vec3 ambientLight = light.ambientIntensity * vec3(texture(material.diffuseMap, TexCoords)); - vec3 lightDir = normalize(lightPosition - FragmentPos); + vec3 lightDir = normalize(-light.direction); //normalize(lightPosition - FragmentPos); float diff = max(dot(normal, lightDir), 0.0); vec3 diffuseLight = light.diffuseIntensity * diff * vec3(texture(material.diffuseMap, TexCoords));