-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpoint_light_system.cpp
More file actions
112 lines (99 loc) · 4.47 KB
/
point_light_system.cpp
File metadata and controls
112 lines (99 loc) · 4.47 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
#pragma once
#include "point_light_system.hpp"
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
#include <glm/gtc/constants.hpp>
#include <stdexcept>
#include <array>
#include <cassert>
#include <map>
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>desciptorSetLayouts{ globalSetLayout };
VkPipelineLayoutCreateInfo pipelineLayoutInfo{};
pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
pipelineLayoutInfo.setLayoutCount = static_cast<uint32_t>(desciptorSetLayouts.size());
pipelineLayoutInfo.pSetLayouts = desciptorSetLayouts.data();
pipelineLayoutInfo.pushConstantRangeCount = 1;
pipelineLayoutInfo.pPushConstantRanges = &pushConstantRange;
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);
LvePipeline::enableAlphaBlending(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) {
auto rotateLight = glm::rotate(glm::mat4(1.f), 0.5f * frameInfo.frameTime, { 0.f, -1.f, 0.f });
int lightIndex = 0;
for (auto& keyValue : frameInfo.gameObjects) {
auto& gameObject = keyValue.second;
if (gameObject.pointLight == nullptr) continue;
assert(lightIndex < MAX_POINT_LIGHTS && "Point lights exceed maximum specified");
gameObject.transform.translation = glm::vec3(rotateLight * glm::vec4(gameObject.transform.translation, 1.f));
ubo.pointLights[lightIndex].position = glm::vec4(gameObject.transform.translation, 1.0f);
ubo.pointLights[lightIndex].color = glm::vec4(gameObject.color, gameObject.pointLight->lightIntensity);
lightIndex++;
}
ubo.numPointLights = lightIndex;
}
void PointLightSystem::render(FrameInfo& frameInfo) {
std::map<float, LveGameObject::id_t> sortedPointLightsToCameraDistance;
for (auto& keyValue : frameInfo.gameObjects) {
auto& gameObject = keyValue.second;
if (gameObject.pointLight == nullptr) continue;
float distance = glm::dot(frameInfo.camera.getPosition() - gameObject.transform.translation, frameInfo.camera.getPosition() - gameObject.transform.translation);
sortedPointLightsToCameraDistance[distance] = gameObject.getId();
}
lvePipeline->bind(frameInfo.commandBuffer);
vkCmdBindDescriptorSets(
frameInfo.commandBuffer,
VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout,
0,
1,
&frameInfo.globalDescriptorSet,
0,
nullptr
);
for (auto it = sortedPointLightsToCameraDistance.rbegin(); it != sortedPointLightsToCameraDistance.rend(); it++) {
auto& gameObject = frameInfo.gameObjects.at(it->second);
PointLightPushConstants push{};
push.position = glm::vec4(gameObject.transform.translation, 1.0f);
push.color = glm::vec4(gameObject.color, gameObject.pointLight->lightIntensity);
push.radius = gameObject.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);
}
}
}