Skip to content

Commit

Permalink
Create a single point light
Browse files Browse the repository at this point in the history
  • Loading branch information
Hjaltesorgenfrei committed Sep 29, 2022
1 parent db8250c commit 0ea573a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
3 changes: 3 additions & 0 deletions src/RenderData.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
19 changes: 18 additions & 1 deletion src/shaders/shader.frag
Original file line number Diff line number Diff line change
@@ -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);
}
23 changes: 13 additions & 10 deletions src/shaders/shader.vert
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}

0 comments on commit 0ea573a

Please sign in to comment.