-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint_light_system_broken_25preLightRotate.cpp
More file actions
120 lines (98 loc) · 4.17 KB
/
point_light_system_broken_25preLightRotate.cpp
File metadata and controls
120 lines (98 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "point_light_system.hpp"
// libs
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
// std
#include <array>
#include <cassert>
#include <stdexcept>
namespace lve {
struct PointLightPushConstants {
glm::vec4 position{};
glm::vec4 color{};
float radius;
};
PointLightSystem::PointLightSystem(
LveDevice& device, VkRenderPass renderPass, VkDescriptorSetLayout globalSetLayout)
: lveDevice{ device } {
createPipelineLayout(globalSetLayout);
createPipeline(renderPass);
}
PointLightSystem::~PointLightSystem() {
vkDestroyPipelineLayout(lveDevice.device(), pipelineLayout, nullptr);
}
void PointLightSystem::createPipelineLayout(VkDescriptorSetLayout globalSetLayout) {
VkPushConstantRange pushConstantRange{};
pushConstantRange.stageFlags = VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT;
pushConstantRange.offset = 0;
pushConstantRange.size = sizeof(PointLightPushConstants);
std::vector<VkDescriptorSetLayout> descriptorSetLayouts{ globalSetLayout };
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(descriptorSetLayouts.size());
pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts.data();
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange; //nullptr
if (vkCreatePipelineLayout(lveDevice.device(), &pipelineLayoutInfo, nullptr, &pipelineLayout) !=
VK_SUCCESS) {
throw std::runtime_error("failed to create pipeline layout!");
}
}
void PointLightSystem::createPipeline(VkRenderPass renderPass) {
assert(pipelineLayout != nullptr && "Cannot create pipeline before pipeline layout");
PipelineConfigInfo pipelineConfig{};
LvePipeline::defaultPipelineConfigInfo(pipelineConfig);
pipelineConfig.attributeDescriptions.clear();
pipelineConfig.bindingDescriptions.clear();
pipelineConfig.renderPass = renderPass;
pipelineConfig.pipelineLayout = pipelineLayout;
lvePipeline = std::make_unique<LvePipeline>(
lveDevice,
"shaders/point_light.vert.spv",
"shaders/point_light.frag.spv",
pipelineConfig);
}
void PointLightSystem::update(FrameInfo& frameInfo, GlobalUbo& ubo) {
int lightIndex = 0;
for (auto& kv : frameInfo.gameObjects) {
auto& obj = kv.second;
if (obj.pointLight == nullptr)continue;
//copy light to ubo
ubo.pointLights[lightIndex].position = glm::vec4(obj.transform.translation, 1.f); // the 1 is ignored
ubo.pointLights[lightIndex].color = glm::vec4(obj.color,obj.pointLight->lightIntensity);
lightIndex += 1;
}
ubo.numLights = lightIndex;
}
void PointLightSystem::render(FrameInfo& frameInfo) {
lvePipeline->bind(frameInfo.commandBuffer);
vkCmdBindDescriptorSets(
frameInfo.commandBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout,
0,
1,
&frameInfo.globalDescriptorSet,
0,
nullptr);
for (auto& kv : frameInfo.gameObjects) { //somewhat inefficient
auto& obj = kv.second;
if (obj.pointLight == nullptr) continue;
PointLightPushConstants push{};
push.position = glm::vec4(obj.transform.translation, 1.f);
push.color = glm::vec4(obj.color, obj.pointLight->lightIntensity);
push.radius = obj.transform.scale.x;
vkCmdPushConstants(
frameInfo.commandBuffer,
pipelineLayout,
VK_SHADER_STAGE_VERTEX_BIT | VK_SHADER_STAGE_FRAGMENT_BIT,
0,
sizeof(PointLightPushConstants),
&push
);
}
vkCmdDraw(frameInfo.commandBuffer, 6, 1, 0, 0);
}
} // namespace lve