vengine  0.1.0
3D graphics engine made with Vulkan
Loading...
Searching...
No Matches
pointLightSystem.cpp
Go to the documentation of this file.
1#define GLM_FORCE_RADIANS
2#define GLM_FORCE_DEPTH_ZERO_TO_ONE
3#include <glm/glm.hpp>
4
7
8
10 glm::vec4 position{};
11 glm::vec4 color{};
12 float radius;
13};
14
15ven::PointLightSystem::PointLightSystem(Device& device, const VkRenderPass renderPass,const VkDescriptorSetLayout globalSetLayout) : m_device{device}
16{
17 createPipelineLayout(globalSetLayout);
18 createPipeline(renderPass);
19}
20
21void ven::PointLightSystem::createPipelineLayout(const VkDescriptorSetLayout globalSetLayout)
22{
23 VkPushConstantRange pushConstantRange{};
24 pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
25 pushConstantRange.offset = 0;
26 pushConstantRange.size = sizeof(PointLightPushConstants);
27
28 const std::vector<VkDescriptorSetLayout> descriptorSetLayouts{globalSetLayout};
29
30 VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
31 pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
32 pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(descriptorSetLayouts.size());
33 pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data();
34 pipelineLayoutInfo.pushConstantRangeCount = 1;
35 pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
36 if (vkCreatePipelineLayout(m_device.device(), &pipelineLayoutInfo, nullptr, &m_pipelineLayout) != VK_SUCCESS)
37 {
38 throw std::runtime_error("Failed to create pipeline layout");
39 }
40}
41
42void ven::PointLightSystem::createPipeline(const VkRenderPass renderPass)
43{
44 PipelineConfigInfo pipelineConfig{};
46 pipelineConfig.attributeDescriptions.clear();
47 pipelineConfig.bindingDescriptions.clear();
48 pipelineConfig.renderPass = renderPass;
49 pipelineConfig.pipelineLayout = m_pipelineLayout;
50 m_shaders = std::make_unique<Shaders>(m_device, std::string(SHADERS_BIN_PATH) + "point_light_vert.spv", std::string(SHADERS_BIN_PATH) + "point_light_frag.spv", pipelineConfig);
51}
52
53void ven::PointLightSystem::render(const FrameInfo &frameInfo) const
54{
55 m_shaders->bind(frameInfo.commandBuffer);
56
57 vkCmdBindDescriptorSets(frameInfo.commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, m_pipelineLayout, 0, 1, &frameInfo.globalDescriptorSet, 0, nullptr);
58
59 for (auto &kv : frameInfo.objects)
60 {
61 Object &object = kv.second;
62 if (object.pointLight == nullptr) continue;
64 push.position = glm::vec4(object.transform3D.translation, 1.F);
65 push.color = glm::vec4(object.color, object.pointLight->lightIntensity);
66 push.radius = object.transform3D.scale.x;
67 vkCmdPushConstants(frameInfo.commandBuffer, m_pipelineLayout, VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT, 0, sizeof(PointLightPushConstants), &push);
68 vkCmdDraw(frameInfo.commandBuffer, 6, 1, 0, 0);
69 }
70
71}
72
74{
75 const auto rotateLight = rotate(glm::mat4(1.F), frameInfo.frameTime, {0.F, -1.F, 0.F});
76 unsigned long lightIndex = 0;
77 for (auto &kv : frameInfo.objects)
78 {
79 Object &object = kv.second;
80 if (object.pointLight == nullptr) continue;
81 assert(lightIndex < MAX_LIGHTS && "Too many lights");
82 object.transform3D.translation = glm::vec3(rotateLight * glm::vec4(object.transform3D.translation, 1.F));
83 ubo.pointLights[lightIndex].position = glm::vec4(object.transform3D.translation, 1.F);
84 ubo.pointLights[lightIndex].color = glm::vec4(object.color, object.pointLight->lightIntensity);
85 lightIndex++;
86 }
87 ubo.numLights = static_cast<int>(lightIndex);
88}
This file contains the constant values used in the project.
This file contains the PointLightSystem class.
PointLightSystem(Device &device, VkRenderPass renderPass, VkDescriptorSetLayout globalSetLayout)
void createPipelineLayout(VkDescriptorSetLayout globalSetLayout)
static void update(const FrameInfo &frameInfo, GlobalUbo &ubo)
void render(const FrameInfo &frameInfo) const
void createPipeline(VkRenderPass renderPass)
static void defaultPipelineConfigInfo(PipelineConfigInfo &configInfo)
Definition shaders.cpp:112
static constexpr std::string_view SHADERS_BIN_PATH
Definition Constant.hpp:15
static constexpr std::size_t MAX_LIGHTS
Definition FrameInfo.hpp:16
Object::Map & objects
Definition FrameInfo.hpp:41
VkCommandBuffer commandBuffer
Definition FrameInfo.hpp:38
VkDescriptorSet globalDescriptorSet
Definition FrameInfo.hpp:40
std::array< PointLight, MAX_LIGHTS > pointLights
Definition FrameInfo.hpp:30