vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
engine.cpp
Go to the documentation of this file.
12
13ven::Engine::Engine(const Config& config) : m_state(EDITOR), m_window(config.window.width, config.window.height), m_camera(config.camera.fov, config.camera.near, config.camera.far, config.camera.move_speed, config.camera.look_speed) {
17 const auto framePoolBuilder = DescriptorPool::Builder(m_device)
18 .setMaxSets(1000)
19 .addPoolSize(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1000)
20 .addPoolSize(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, 1000)
21 .setPoolFlags(VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT);
22 for (auto & framePool : m_framePools) {
23 framePool = framePoolBuilder.build();
24 }
26}
27
29{
31 const std::unordered_map<std::string, std::shared_ptr<Model>> modelCache = ModelFactory::loadAll(m_device, "assets/models/");
32 const std::shared_ptr<Texture> defaultTexture = m_sceneManager.getTextureDefault();
33
34 Logger::logExecutionTime("Creating object quad", [&] {
35 m_sceneManager.addObject(ObjectFactory::create(
36 defaultTexture,
37 modelCache.at("assets/models/quad.obj"),
38 "quad",
39 {
40 .translation = {0.F, .5F, 0.F},
41 .scale = {3.F, 1.F, 3.F},
42 .rotation = {0.F, 0.F, 0.F}
43 }));
44 });
45
46 Logger::logExecutionTime("Creating object smooth vase", [&]{
47 m_sceneManager.addObject(ObjectFactory::create(
48 defaultTexture,
49 modelCache.at("assets/models/smooth_vase.obj"),
50 "smooth vase",
51 {
52 .translation = {.5F, .5F, 0.F},
53 .scale = {3.F, 1.5F, 3.F},
54 .rotation = {0.F, 0.F, 0.F}
55 }));
56 });
57 Logger::logExecutionTime("Creating object flat vase", [&]{
58 m_sceneManager.addObject(ObjectFactory::create(
59 defaultTexture,
60 modelCache.at("assets/models/flat_vase.obj"),
61 "flat vase",
62 {
63 .translation = {-.5F, .5F, 0.F},
64 .scale = {3.F, 1.5F, 3.F},
65 .rotation = {0.F, 0.F, 0.F}
66 }));
67 });
68 for (std::size_t i = 0; i < lightColors.size(); i++)
69 {
70 Logger::logExecutionTime("Creating light n" + std::to_string(i), [&] {
71 const glm::mat4 rotateLight = rotate(
72 glm::mat4(1.F),
73 static_cast<float>(i) * glm::two_pi<float>() / 6.0F, // 6 = num of lights
74 {0.F, -1.F, 0.F}
75);
76 m_sceneManager.addLight(LightFactory::create({
77 .translation = glm::vec3(rotateLight * glm::vec4(-1.F, -1.F, -1.F, 1.F)),
78 .scale = { 0.1F, 0.0F, 0.0F },
79 .rotation = { 0.F, 0.F, 0.F }},
80 lightColors.at(i)
81 ));
82 });
83 }
84}
85
87{
88 Clock clock;
89 const EventManager eventManager{};
90 GlobalUbo ubo{};
91 VkCommandBuffer_T *commandBuffer = nullptr;
92 VkDescriptorBufferInfo bufferInfo{};
93 float frameTime = 0.0F;
94 unsigned long frameIndex = 0;
95 const std::unique_ptr globalSetLayout(DescriptorSetLayout::Builder(m_device).addBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_ALL_GRAPHICS).build());
96 std::vector<std::unique_ptr<Buffer>> uboBuffers(MAX_FRAMES_IN_FLIGHT);
97 std::vector<VkDescriptorSet> globalDescriptorSets(MAX_FRAMES_IN_FLIGHT);
98 const ObjectRenderSystem objectRenderSystem(m_device, m_renderer.getSwapChainRenderPass(), globalSetLayout->getDescriptorSetLayout());
99 const PointLightRenderSystem pointLightRenderSystem(m_device, m_renderer.getSwapChainRenderPass(), globalSetLayout->getDescriptorSetLayout());
100
101 for (auto& uboBuffer : uboBuffers)
102 {
103 uboBuffer = std::make_unique<Buffer>(m_device, sizeof(GlobalUbo), 1, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
104 uboBuffer->map();
105 }
106 for (std::size_t i = 0; i < globalDescriptorSets.size(); i++) {
107 bufferInfo = uboBuffers[i]->descriptorInfo();
108 DescriptorWriter(*globalSetLayout, *m_globalPool).writeBuffer(0, &bufferInfo).build(globalDescriptorSets[i]);
109 }
110
111 while (m_state != EXIT)
112 {
113 clock.update();
114 frameTime = clock.getDeltaTime();
115 eventManager.handleEvents(m_window.getGLFWindow(), &m_state, m_camera, m_gui, frameTime);
116 commandBuffer = m_renderer.beginFrame();
117
118 m_camera.setViewXYZ(m_camera.transform.translation, m_camera.transform.rotation);
119 m_camera.setPerspectiveProjection(m_renderer.getAspectRatio());
120
121 if (commandBuffer != nullptr) {
122 frameIndex = m_renderer.getFrameIndex();
123 m_framePools[frameIndex]->resetPool();
124 FrameInfo frameInfo{
125 .frameIndex=frameIndex,
126 .frameTime=frameTime,
127 .commandBuffer=commandBuffer,
128 .camera=m_camera,
129 .globalDescriptorSet=globalDescriptorSets[frameIndex],
130 .frameDescriptorPool=*m_framePools[frameIndex],
131 .objects=m_sceneManager.getObjects(),
132 .lights=m_sceneManager.getLights()
133 };
134 ubo.projection=m_camera.getProjection();
135 ubo.view=m_camera.getView();
136 ubo.inverseView=m_camera.getInverseView();
137 m_sceneManager.updateBuffer(ubo, frameIndex, frameTime);
138 uboBuffers.at(frameIndex)->writeToBuffer(&ubo);
139 uboBuffers.at(frameIndex)->flush();
140 m_renderer.beginSwapChainRenderPass(frameInfo.commandBuffer);
141 objectRenderSystem.render(frameInfo);
142 pointLightRenderSystem.render(frameInfo);
143
144 if (m_gui.getState() != HIDDEN) {
145 m_gui.render(
146 &m_renderer,
147 m_sceneManager,
148 m_camera,
149 m_device.getPhysicalDevice(),
150 ubo,
151 { .deltaTimeMS=clock.getDeltaTimeMS(), .fps=clock.getFPS() }
152 );
153 }
154
155 m_renderer.endSwapChainRenderPass(commandBuffer);
156 m_renderer.endFrame();
157 commandBuffer = nullptr;
158 }
159 if (m_sceneManager.getDestroyState()) {
160 vkDeviceWaitIdle(m_device.device());
161 m_sceneManager.destroyEntity(m_gui.getObjectsToRemove(), m_gui.getLightsToRemove());
162 }
163 }
164 vkDeviceWaitIdle(m_device.device());
165}
This file contains the Clock class.
This file contains the ObjectRenderSystem class.
This file contains the Engine class.
This file contains the EventManager class.
This file contains the Light Factory class.
This file contains the Model Factory class.
This file contains the Object Factory class.
This file contains the Logger class.
This file contains the PointLightRenderSystem class.
This file contains the DescriptorsWriter class.
Class for clock.
Definition Clock.hpp:20
void update()
Definition clock.cpp:3
float getDeltaTime() const
Definition Clock.hpp:38
static constexpr glm::vec4 GREEN_4
Definition Colors.hpp:39
static constexpr glm::vec4 CYAN_4
Definition Colors.hpp:51
static constexpr glm::vec4 RED_4
Definition Colors.hpp:35
static constexpr glm::vec4 BLUE_4
Definition Colors.hpp:43
static constexpr glm::vec4 MAGENTA_4
Definition Colors.hpp:55
static constexpr glm::vec4 YELLOW_4
Definition Colors.hpp:47
std::unique_ptr< DescriptorPool > build() const
Definition Pool.hpp:32
Builder & setPoolFlags(const VkDescriptorPoolCreateFlags flags)
Definition Pool.hpp:35
Builder & setMaxSets(const uint32_t count)
Definition Pool.hpp:36
Builder & addPoolSize(const VkDescriptorType descriptorType, const uint32_t count)
Definition Pool.hpp:34
Class for descriptor writer.
Definition Writer.hpp:19
DescriptorWriter & writeBuffer(uint32_t binding, const VkDescriptorBufferInfo *bufferInfo)
Definition writer.cpp:5
bool build(VkDescriptorSet &set)
Definition writer.cpp:43
VkInstance getInstance() const
Definition Device.hpp:66
std::vector< std::unique_ptr< DescriptorPool > > m_framePools
Definition Engine.hpp:50
Renderer m_renderer
Definition Engine.hpp:48
Device m_device
Definition Engine.hpp:46
void run()
Definition engine.cpp:86
void loadObjects()
Definition engine.cpp:28
Engine(const Config &config)
Definition engine.cpp:13
Window m_window
Definition Engine.hpp:43
std::unique_ptr< DescriptorPool > m_globalPool
Definition Engine.hpp:49
Class for event manager.
void init(GLFWwindow *window, VkInstance instance, const Device *device, VkRenderPass renderPass)
Definition init.cpp:6
static std::unique_ptr< Light > create(const Transform3D &transform=DEFAULT_TRANSFORM, glm::vec4 color=DEFAULT_LIGHT_COLOR)
Definition Light.cpp:5
static void logExecutionTime(const std::string &message, Func &&func)
Definition Logger.hpp:35
static std::unordered_map< std::string, std::shared_ptr< Model > > loadAll(Device &device, const std::string &folderPath)
Definition Model.cpp:3
static std::unique_ptr< Object > create(const std::shared_ptr< Texture > &texture, const std::shared_ptr< Model > &model, const std::string &name, const Transform3D &transform)
Definition Object.cpp:5
Class for object render system.
Definition Object.hpp:23
void render(const FrameInfo &frameInfo) const override
Definition object.cpp:6
Class for point light system.
void render(const FrameInfo &frameInfo) const override
Definition pointLight.cpp:5
VkRenderPass getSwapChainRenderPass() const
Definition Renderer.hpp:36
GLFWwindow * getGLFWindow() const
Definition Window.hpp:41
@ EXIT
Definition Utils.hpp:17
@ EDITOR
Definition Utils.hpp:14
static constexpr int MAX_FRAMES_IN_FLIGHT
Definition SwapChain.hpp:15
@ HIDDEN
Definition Gui.hpp:22
unsigned long frameIndex
Definition FrameInfo.hpp:46