From 93333821d82bc74919775d0e31a70854f21cd3fd Mon Sep 17 00:00:00 2001 From: Lukian Date: Sat, 8 Feb 2025 15:36:41 +0100 Subject: [PATCH] added basic lighting --- resources/shaders/shader.fs | 25 +++++++++++++++++++++++-- resources/shaders/shader.vs | 8 ++++++-- src/main.cpp | 7 +++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/resources/shaders/shader.fs b/resources/shaders/shader.fs index 9da78b1..8a6e635 100644 --- a/resources/shaders/shader.fs +++ b/resources/shaders/shader.fs @@ -1,11 +1,32 @@ #version 330 core out vec4 FragColor; +in vec3 Normal; in vec2 TexCoords; +in vec3 FragPos; uniform sampler2D texture_diffuse1; +uniform vec3 lightColor; +uniform vec3 lightPos; +uniform vec3 viewPos; void main() -{ - FragColor = texture(texture_diffuse1, TexCoords); +{ + float ambientStrength = 0.1; + vec3 ambient = ambientStrength * lightColor; + + vec3 norm = normalize(Normal); + vec3 lightDir = normalize(lightPos - FragPos); + float diff = max(dot(norm, lightDir), 0.0); + vec3 diffuse = diff * lightColor; + + float specularStrength = 0.5; + vec3 viewDir = normalize(viewPos - FragPos); + vec3 reflectDir = reflect(-lightDir, norm); + float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32); + vec3 specular = specularStrength * spec * lightColor; + + vec3 result = (ambient + diffuse + specular) * vec3(1.0, 0.5, 0.2); + vec4 light = vec4(result, 1.0); + FragColor = texture(texture_diffuse1, TexCoords) * light; } \ No newline at end of file diff --git a/resources/shaders/shader.vs b/resources/shaders/shader.vs index 253b79e..9f56c26 100644 --- a/resources/shaders/shader.vs +++ b/resources/shaders/shader.vs @@ -3,7 +3,9 @@ layout (location = 0) in vec3 aPos; layout (location = 1) in vec3 aNormal; layout (location = 2) in vec2 aTexCoords; +out vec3 Normal; out vec2 TexCoords; +out vec3 FragPos; uniform mat4 model; uniform mat4 view; @@ -11,6 +13,8 @@ uniform mat4 projection; void main() { - TexCoords = aTexCoords; - gl_Position = projection * view * model * vec4(aPos, 1.0); + TexCoords = aTexCoords; + Normal = mat3(transpose(inverse(model))) * aNormal; + FragPos = vec3(model * vec4(aPos, 1.0)); + gl_Position = projection * view * model * vec4(aPos, 1.0); } \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 2035d77..7ecaf33 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,6 +67,9 @@ int main() glm::mat4 model = glm::mat4(1.0f); model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f)); + glm::vec3 lightPos = glm::vec3(1.2f, 1.0f, 2.0f); + glm::vec3 lightColor = glm::vec3(1.0f, 1.0f, 1.0f); + while(!glfwWindowShouldClose(window)) { float currentFrame = glfwGetTime(); @@ -80,6 +83,10 @@ int main() ourShader.use(); + ourShader.setVec3("lightPos", lightPos); + ourShader.setVec3("lightColor", lightColor); + ourShader.setVec3("viewPos", camera.Position); + glm::mat4 view = camera.GetViewMatrix(); glm::mat4 projection = glm::perspective(glm::radians(45.0f), float(width) / float(height), 0.1f, 100.0f); ourShader.setMat4("view", view);