From 1442d81549b813cddff511c6e9465c1d90f0f724 Mon Sep 17 00:00:00 2001 From: 4VRDriver <44267643+4VRDriver@users.noreply.github.com> Date: Sun, 6 Sep 2020 22:32:46 +0200 Subject: [PATCH] Light attenuation --- res/shaders/basic.fs | 19 +++++++++++++++---- src/Controller.cpp | 10 ++++++++++ src/Window.cpp | 2 +- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/res/shaders/basic.fs b/res/shaders/basic.fs index 7afec01..9d52cea 100644 --- a/res/shaders/basic.fs +++ b/res/shaders/basic.fs @@ -17,10 +17,15 @@ uniform Material u_material; struct Light { vec3 position; + //vec3 direction; vec3 ambient; vec3 diffuse; vec3 specular; + + float K_c; + float K_l; + float K_q; }; uniform Light u_light; @@ -29,10 +34,13 @@ uniform vec3 u_viewPosition; void main() { + vec3 vecLightToFragment = u_light.position - v_fragmentPosition; + vec3 lightDir = normalize(vecLightToFragment); + //vec3 lightDir = normalize(-u_light.direction); + // Diffuse lighting vec3 diffuseColor0 = vec3(texture(u_material.texture_diffuse0, v_texCoord)); vec3 normal = normalize(u_normalMatrix * v_normal); - vec3 lightDir = normalize(u_light.position - v_fragmentPosition); float diff = max(dot(normal, lightDir), 0.0f); vec3 diffuse = u_light.diffuse * diff * diffuseColor0; @@ -45,9 +53,12 @@ void main() { float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_material.shininess); vec3 specular = u_light.specular * spec * vec3(texture(u_material.texture_specular0, v_texCoord)); - //vec4 texColor1 = texture(u_material.u_texture_diffuse0, v_texCoord); - //vec4 texColor2 = texture(u_material.u_texture_diffuse1, v_texCoord); - //f_color = texColor1; + float distanceLightFragment = length(vecLightToFragment); + float attenuation = 1.0 / (u_light.K_c + u_light.K_l * distanceLightFragment + u_light.K_q * distanceLightFragment * distanceLightFragment); + + ambient *= attenuation; + diffuse *= attenuation; + specular *= attenuation; f_color = vec4((ambient + diffuse + specular), 1.0f); } diff --git a/src/Controller.cpp b/src/Controller.cpp index fb5e1a3..14b3128 100644 --- a/src/Controller.cpp +++ b/src/Controller.cpp @@ -52,11 +52,15 @@ void Controller::run() { //Model model_backpack("res/models/backpack.obj"); Model model_container("res/models/container.obj"); Model model_cube("res/models/cube.obj"); + Model model_sphere("res/models/sphere.obj"); //Entity backpack1(&model_backpack, &shaderProgram); + Entity sphere(&model_sphere, &shaderProgram); Entity cube(&model_container, &shaderProgram); Entity lightSource(&model_cube, &lightProgram); + sphere.translate(glm::vec3(3.0f, 0.0f, 2.0f)); + lightSource.translate(glm::vec3(-5.0f, 1.0f, 0.0f)); lightSource.scale(0.2f); @@ -66,15 +70,21 @@ void Controller::run() { shaderProgram.bind(); shaderProgram.setUniform("u_light.position", lightSource.getPosition()); + //shaderProgram.setUniform("u_light.direction", glm::vec3(-0.2f, -1.0f, -0.3f)); shaderProgram.setUniform("u_light.ambient", ambientColor); shaderProgram.setUniform("u_light.diffuse", diffuseColor); shaderProgram.setUniform("u_light.specular", glm::vec3(1.0f, 1.0f, 1.0f)); + shaderProgram.setUniform("u_light.K_c", 1.0f); + shaderProgram.setUniform("u_light.K_l", 0.09f); + shaderProgram.setUniform("u_light.K_q", 0.032f); + shaderProgram.setUniform("u_material.shininess", 32.0f); shaderProgram.unbind(); scene.push_back(cube); scene.push_back(lightSource); + scene.push_back(sphere); camera->translate(glm::vec3(0.0f, 0.0f, 7.5f)); diff --git a/src/Window.cpp b/src/Window.cpp index b98d7ed..4779a45 100644 --- a/src/Window.cpp +++ b/src/Window.cpp @@ -8,7 +8,7 @@ Window::Window() { width = INIT_WINDOW_WIDTH; height = INIT_WINDOW_HEIGHT; - window = glfwCreateWindow(width, height, "Fall-Fever", NULL, NULL); + window = glfwCreateWindow(width, height, "OpenGL", NULL, NULL); if(!window) { std::cout << "Failed to create window" << std::endl; }