From 0ea573aa004f06dfdf152c9295142ba148f1894c Mon Sep 17 00:00:00 2001 From: Hjalte Sorgenfrei Mac Dalland Date: Thu, 29 Sep 2022 15:37:05 +0200 Subject: [PATCH] Create a single point light --- src/RenderData.h | 3 +++ src/Renderer.cpp | 2 +- src/shaders/shader.frag | 19 ++++++++++++++++++- src/shaders/shader.vert | 23 +++++++++++++---------- 4 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/RenderData.h b/src/RenderData.h index 6bc86a2..624dae0 100644 --- a/src/RenderData.h +++ b/src/RenderData.h @@ -9,6 +9,9 @@ struct UniformBufferObject { glm::mat4 view; glm::mat4 proj; glm::mat4 projView; + glm::vec4 ambientLightColor{1.f, 1.f, 1.f, .04f}; // w is intensity + glm::vec4 lightPosition{2.f, 1.f, 0.0f, 0.0f}; // w is ignored + glm::vec4 lightColor{1.f}; // w is light intensity }; class RenderData { diff --git a/src/Renderer.cpp b/src/Renderer.cpp index 444678c..9f28fd4 100644 --- a/src/Renderer.cpp +++ b/src/Renderer.cpp @@ -791,7 +791,7 @@ void Renderer::createDescriptorSetLayout() { .binding = 0, .descriptorType = vk::DescriptorType::eUniformBuffer, .descriptorCount = 1, - .stageFlags = vk::ShaderStageFlagBits::eVertex, + .stageFlags = vk::ShaderStageFlagBits::eAllGraphics, .pImmutableSamplers = nullptr }; diff --git a/src/shaders/shader.frag b/src/shaders/shader.frag index 3915b59..0b81ae6 100644 --- a/src/shaders/shader.frag +++ b/src/shaders/shader.frag @@ -1,14 +1,31 @@ #version 450 #extension GL_EXT_nonuniform_qualifier : enable +layout(set = 0, binding = 0) uniform UniformBufferObject { + mat4 view; + mat4 proj; + mat4 projView; + vec4 ambientLightColor; // w is intensity + vec4 lightPosition; // w is ignored + vec4 lightColor; // w is intensity +} ubo; + layout(set = 1, binding = 0) uniform sampler2D texSampler[]; layout(location = 0) in vec3 fragColor; layout(location = 1) in vec2 fragTexCoord; layout(location = 2) in flat uint materialIndex; +layout(location = 3) in vec3 fragPosWorld; +layout(location = 4) in vec3 fragNormalWorld; layout(location = 0) out vec4 outColor; void main() { - outColor = texture(texSampler[int(materialIndex)], fragTexCoord.xy) * vec4(fragColor, 1.0); // Shitty shadows + vec3 directionToLight = ubo.lightPosition.xyz - fragPosWorld; + float attenuation = 1.0 / dot(directionToLight, directionToLight); // distance squared + + vec3 lightColor = ubo.lightColor.xyz * ubo.lightColor.w * attenuation; + vec3 ambientLight = ubo.ambientLightColor.xyz * ubo.ambientLightColor.w; + vec3 diffuseLight = lightColor * max(dot(normalize(fragNormalWorld), normalize(directionToLight)), 0); + outColor = texture(texSampler[int(materialIndex)], fragTexCoord.xy) * vec4((diffuseLight + ambientLight) * fragColor, 1.0); } \ No newline at end of file diff --git a/src/shaders/shader.vert b/src/shaders/shader.vert index d0ecb3e..dc3106a 100644 --- a/src/shaders/shader.vert +++ b/src/shaders/shader.vert @@ -4,6 +4,9 @@ layout(set = 0, binding = 0) uniform UniformBufferObject { mat4 view; mat4 proj; mat4 projView; + vec4 ambientLightColor; // w is intensity + vec4 lightPosition; // w is ignored + vec4 lightColor; // w is intensity } ubo; layout (push_constant) uniform constants { @@ -20,20 +23,20 @@ layout(location = 4) in uint inMaterialIndex; layout(location = 0) out vec3 fragColor; layout(location = 1) out vec2 fragTexCoord; layout(location = 2) out uint materialIndex; - -const vec3 DIRECTION_TO_LIGHT = normalize(vec3(1.0, 3.0, -1.0)); -const float AMBIENT = 0.02; +layout(location = 3) out vec3 fragPosWorld; +layout(location = 4) out vec3 fragNormalWorld; void main() { + vec4 positionWorld = pushConstants.modelMatrix * vec4(inPosition, 1.0); + + gl_Position = ubo.projView * positionWorld; + fragColor = inColor; + fragTexCoord = inTexCoord; + materialIndex = inMaterialIndex; + fragPosWorld = positionWorld.xyz; // Diffuse shading does not work correctly if model is scaled non Uniformly // https://paroj.github.io/gltut/Illumination/Tut09%20Normal%20Transformation.html // TODO: Fix this if it becomes necessary // Can be done by computing the value on the CPU and sending it as a Push Constant - vec3 normalWorldSpace = normalize((pushConstants.modelMatrix * vec4(inNormal, 0.0)).xyz); - float lightIntensity = AMBIENT + max(dot(normalWorldSpace, DIRECTION_TO_LIGHT), 0); - - gl_Position = ubo.projView * pushConstants.modelMatrix * vec4(inPosition, 1); - fragColor = inColor * lightIntensity; - fragTexCoord = inTexCoord; - materialIndex = inMaterialIndex; + fragNormalWorld = normalize((pushConstants.modelMatrix * vec4(inNormal, 0.0)).xyz); }