Skip to content

Commit 89e81b9

Browse files
committed
added a lighting descriptor
1 parent df17e14 commit 89e81b9

File tree

4 files changed

+53
-11
lines changed

4 files changed

+53
-11
lines changed

shaders/triangle.frag

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,38 @@
11
#version 450
22

3-
layout(location = 1) in vec3 vNormal;
3+
// input from vertex shader
4+
layout(location = 1) in vec3 inNormal;
5+
6+
// Uniform buffer for lighting data
7+
layout(binding = 1) uniform LightUBO {
8+
vec4 lightDirection;
9+
vec4 lightColor;
10+
float lightIntensity;
11+
} ubo;
412

513
// Final output color
614
layout(location = 0) out vec4 outColor;
715

816
void main() {
9-
// Normalize the incoming normal vector to ensure unit vector.
10-
vec3 normal = normalize(vNormal);
1117

12-
// map normal components in (-1, 1) to (0, 1)
13-
vec3 normalColor = (normal + 1.0) * 0.5;
18+
// Define a base color for object
19+
vec3 objectColor = vec3(0.8, 0.8, 0.8);
20+
21+
// Ensure the normal is a unit vector
22+
vec3 normal = normalize(inNormal);
23+
24+
// The direction vector from the uniform is pointing *to* the light.
25+
// We use it directly for the dot product.
26+
vec3 lightDir = normalize(ubo.lightDirection.xyz);
27+
28+
// Calculate the diffuse factor (Lambertian reflectance)
29+
// max(..., 0.0) ensures that we don't have negative light
30+
float diffuseFactor = max(dot(normal, lightDir), 0.0);
31+
32+
// Calculate the final diffuse light color
33+
vec3 diffuse = ubo.lightColor.rgb * ubo.lightIntensity * diffuseFactor;
34+
35+
// The final color is the object's base color modulated by the diffuse light
36+
outColor = vec4(objectColor * diffuse, 1.0);
1437

15-
outColor = vec4(normalColor, 1.0);
1638
}

src/core/Uniforms.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ struct SceneUBO {
66
glm::mat4 projection;
77
};
88

9-
struct LightingUBO {
10-
glm::vec4 lightPosition;
11-
glm::vec4 lightColor;
12-
float lightIntensity;
9+
struct DirectionalLightUBO
10+
{
11+
glm::vec4 lightPosition = glm::vec4(-0.5f, 1.0f, -0.5f, 0.0f);
12+
glm::vec4 lightColor = glm::vec4(1.0f, 1.0f, 1.0f, 1.0f);
13+
float lightIntensity = 1.0f;
1314
};
1415

1516
struct CompositeUBO {

src/vulkan/VulkanRenderer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ VulkanRenderer::VulkanRenderer(const RendererConfig& config, Window& window, Cam
2121
m_uniformManager = std::make_unique<UniformManager>(*m_allocator, m_frameManager->getFramesInFlightCount());
2222
m_uniformManager->registerUBO<SceneUBO>("scene");
2323
m_uniformManager->registerUBO<CompositeUBO>("composite");
24+
m_uniformManager->registerUBO<DirectionalLightUBO>("lighting");
2425

2526
createPipelineAndDescriptors();
2627
setupUI();
@@ -60,6 +61,8 @@ void VulkanRenderer::createPipelineAndDescriptors()
6061

6162
const std::vector bindings = {
6263
vk::DescriptorSetLayoutBinding(0, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eVertex),
64+
vk::DescriptorSetLayoutBinding(1, vk::DescriptorType::eUniformBuffer, 1, vk::ShaderStageFlagBits::eFragment)
65+
6366
};
6467
m_descriptorSet = std::make_unique<DescriptorSet>(m_context->device(), 2, bindings);
6568
const std::vector setLayouts = {m_descriptorSet->getLayout()};
@@ -266,17 +269,31 @@ void VulkanRenderer::drawFrame()
266269
ubo.view = m_camera.getViewMatrix();
267270
ubo.projection = m_camera.getProjectionMatrix();
268271

272+
m_uniformManager->update<DirectionalLightUBO>(frameIdx, m_light);
273+
269274
m_uniformManager->update<SceneUBO>(frameIdx, ubo);
270275

276+
// Get descriptor info for both UBOs
271277
vk::DescriptorBufferInfo sceneBufferInfo = m_uniformManager->getDescriptorInfo<SceneUBO>(frameIdx);
278+
vk::DescriptorBufferInfo lightBufferInfo = m_uniformManager->getDescriptorInfo<DirectionalLightUBO>(frameIdx);
279+
280+
// Create write for Scene UBO at binding 0
272281
vk::WriteDescriptorSet sceneWrite{};
273282
sceneWrite.dstSet = m_descriptorSet->getCurrentSet(frameIdx);
274283
sceneWrite.dstBinding = 0;
275284
sceneWrite.descriptorType = vk::DescriptorType::eUniformBuffer;
276285
sceneWrite.descriptorCount = 1;
277286
sceneWrite.pBufferInfo = &sceneBufferInfo;
278287

279-
m_descriptorSet->updateSet({sceneWrite});
288+
// Create write for Light UBO at binding 1
289+
vk::WriteDescriptorSet lightWrite{};
290+
lightWrite.dstSet = m_descriptorSet->getCurrentSet(frameIdx);
291+
lightWrite.dstBinding = 1; // Target binding 1
292+
lightWrite.descriptorType = vk::DescriptorType::eUniformBuffer;
293+
lightWrite.descriptorCount = 1;
294+
lightWrite.pBufferInfo = &lightBufferInfo;
295+
296+
m_descriptorSet->updateSet({sceneWrite, lightWrite});
280297

281298
beginCommandBuffer(cmd);
282299

src/vulkan/VulkanRenderer.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "VulkanContext.hpp"
1818
#include "Mesh.hpp"
1919
#include "MeshGenerators.hpp"
20+
#include "../core/Uniforms.hpp"
2021

2122
namespace reactor
2223
{
@@ -79,6 +80,7 @@ class VulkanRenderer
7980
std::vector<vk::ImageView> m_depthViews;
8081
std::vector<vk::DescriptorSet> m_depthImageDescriptorSets;
8182

83+
DirectionalLightUBO m_light;
8284
std::vector<RenderObject> m_objects;
8385

8486
void createCoreVulkanObjects();

0 commit comments

Comments
 (0)