vengine  0.1.0
3D graphics engine made with Vulkan
Loading...
Searching...
No Matches
engine.cpp
Go to the documentation of this file.
1#include <chrono>
2#include <cmath>
3
4#define GLM_FORCE_RADIANS
5#define GLM_FORCE_DEPTH_ZERO_TO_ONE
6#include <glm/glm.hpp>
7#include <glm/gtc/constants.hpp>
8
9#include "VEngine/Engine.hpp"
14
15
17{
18 std::shared_ptr model = Model::createModelFromFile(m_device, "models/flat_vase.obj");
19
20 Object flatVase = Object::createObject();
21 flatVase.model = model;
22 flatVase.transform3D.translation = {-.5F, .5F, 0.F};
23 flatVase.transform3D.scale = {3.F, 1.5F, 3.F};
24 m_objects.emplace(flatVase.getId(), std::move(flatVase));
25
26 model = Model::createModelFromFile(m_device, "models/smooth_vase.obj");
27 Object smoothVase = Object::createObject();
28 smoothVase.model = model;
29 smoothVase.transform3D.translation = {.5F, .5F, 0.F};
30 smoothVase.transform3D.scale = {3.F, 1.5F, 3.F};
31 m_objects.emplace(smoothVase.getId(), std::move(smoothVase));
32
33 model = Model::createModelFromFile(m_device, "models/quad.obj");
35 floor.model = model;
36 floor.transform3D.translation = {0.F, .5F, 0.F};
37 floor.transform3D.scale = {3.F, 1.F, 3.F};
38 m_objects.emplace(floor.getId(), std::move(floor));
39
40 std::vector<glm::vec3> lightColors{
41 {1.F, .1F, .1F},
42 {.1F, .1F, 1.F},
43 {.1F, 1.F, .1F},
44 {1.F, 1.F, .1F},
45 {.1F, 1.F, 1.F},
46 {1.F, 1.F, 1.F}
47 };
48
49 for (std::size_t i = 0; i < lightColors.size(); i++)
50 {
51 Object pointLight = Object::makePointLight(0.2F);
52 pointLight.color = lightColors[i];
53 auto rotateLight = rotate(glm::mat4(1.F), (static_cast<float>(i) * glm::two_pi<float>()) / static_cast<float>(lightColors.size()), {0.F, -1.F, 0.F});
54 pointLight.transform3D.translation = glm::vec3(rotateLight * glm::vec4(-1.F, -1.F, -1.F, 1.F));
55 m_objects.emplace(pointLight.getId(), std::move(pointLight));
56 }
57}
58
59ven::Engine::Engine(const uint32_t width, const uint32_t height, const std::string &title) : m_window(width, height, title)
60{
65}
66
68{
69 GlobalUbo ubo{};
70 Camera camera{};
71 FrameCounter frameCounter{};
72 KeyboardController cameraController{};
73 std::chrono::duration<float> deltaTime{};
74 float frameTime = NAN;
75 int frameIndex = 0;
76 Object viewerObject = Object::createObject();
77 std::chrono::time_point<std::chrono::system_clock> newTime;
78 std::chrono::time_point<std::chrono::system_clock> currentTime = std::chrono::high_resolution_clock::now();
79 std::unique_ptr<DescriptorSetLayout> globalSetLayout = DescriptorSetLayout::Builder(m_device).addBinding(0, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_ALL_GRAPHICS).build();
80 std::vector<std::unique_ptr<Buffer>> uboBuffers(SwapChain::MAX_FRAMES_IN_FLIGHT);
81 std::vector<VkDescriptorSet> globalDescriptorSets(SwapChain::MAX_FRAMES_IN_FLIGHT);
82 RenderSystem renderSystem(m_device, m_renderer.getSwapChainRenderPass(), globalSetLayout->getDescriptorSetLayout());
83 PointLightSystem pointLightSystem(m_device, m_renderer.getSwapChainRenderPass(), globalSetLayout->getDescriptorSetLayout());
84
85 for (auto & uboBuffer : uboBuffers)
86 {
87 uboBuffer = std::make_unique<Buffer>(m_device, sizeof(GlobalUbo), 1, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
88 uboBuffer->map();
89 }
90 for (std::size_t i = 0; i < globalDescriptorSets.size(); i++) {
91 VkDescriptorBufferInfo bufferInfo = uboBuffers[i]->descriptorInfo();
92 DescriptorWriter(*globalSetLayout, *m_globalPool).writeBuffer(0, &bufferInfo).build(globalDescriptorSets[i]);
93 }
94 camera.setViewTarget(glm::vec3(-1.F, -2.F, -2.F), glm::vec3(0.F, 0.F, 2.5F));
95 viewerObject.transform3D.translation.z = -2.5F;
96
97 while (glfwWindowShouldClose(m_window.getGLFWindow()) == 0)
98 {
99 glfwPollEvents();
100
101 newTime = std::chrono::high_resolution_clock::now();
102 deltaTime = newTime - currentTime;
103 currentTime = newTime;
104 frameTime = deltaTime.count();
105 frameCounter.update(frameTime);
106
107 cameraController.moveInPlaneXZ(m_window.getGLFWindow(), frameTime, viewerObject);
108 camera.setViewYXZ(viewerObject.transform3D.translation, viewerObject.transform3D.rotation);
109 camera.setPerspectiveProjection(glm::radians(50.0F), m_renderer.getAspectRatio(), 0.1F, 100.F);
110
111 if (VkCommandBuffer_T *commandBuffer = m_renderer.beginFrame())
112 {
113 frameIndex = (m_renderer.getFrameIndex());
114 FrameInfo frameInfo{frameIndex, frameTime, commandBuffer, camera, globalDescriptorSets[static_cast<unsigned long>(frameIndex)], m_objects};
115
116 ubo.projection = camera.getProjection();
117 ubo.view = camera.getView();
118 ubo.inverseView = camera.getInverseView();
119 PointLightSystem::update(frameInfo, ubo);
120 uboBuffers[static_cast<unsigned long>(frameIndex)]->writeToBuffer(&ubo);
121 uboBuffers[static_cast<unsigned long>(frameIndex)]->flush();
122
123 m_renderer.beginSwapChainRenderPass(commandBuffer);
124 renderSystem.renderObjects(frameInfo);
125 pointLightSystem.render(frameInfo);
127 m_renderer.endFrame();
128 }
129 }
130 vkDeviceWaitIdle(m_device.device());
131}
132
134{
135 VkApplicationInfo appInfo{};
136 appInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
137 appInfo.pApplicationName = "VEngine App";
138 appInfo.applicationVersion = VK_MAKE_API_VERSION(0, 1, 0, 0);
139 appInfo.pEngineName = "VEngine";
140 appInfo.engineVersion = VK_MAKE_API_VERSION(0, 1, 0, 0);
141 appInfo.apiVersion = VK_API_VERSION_1_0;
142 VkInstanceCreateInfo createInfo{};
143 createInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
144 createInfo.pApplicationInfo = &appInfo;
145 uint32_t glfwExtensionCount = 0;
146 const char** glfwExtensions = glfwGetRequiredInstanceExtensions(&glfwExtensionCount);
147 createInfo.enabledExtensionCount = glfwExtensionCount;
148 createInfo.ppEnabledExtensionNames = glfwExtensions;
149
150 if (vkCreateInstance(&createInfo, nullptr, &m_instance) != VK_SUCCESS)
151 {
152 throw std::runtime_error("Failed to create Vulkan instance");
153 }
154}
This file contains the Engine class.
This file contains the FrameCounter class.
This file contains the PointLightSystem class.
This file contains the RenderSystem class.
std::unique_ptr< DescriptorPool > build() const
Builder & addPoolSize(VkDescriptorType descriptorType, uint32_t count)
Builder & setMaxSets(uint32_t count)
Builder & addBinding(uint32_t binding, VkDescriptorType descriptorType, VkShaderStageFlags stageFlags, uint32_t count=1)
std::unique_ptr< DescriptorSetLayout > build() const
Class for descriptor writer.
DescriptorWriter & writeBuffer(uint32_t binding, const VkDescriptorBufferInfo *bufferInfo)
bool build(VkDescriptorSet &set)
Object::Map m_objects
Definition Engine.hpp:43
void createInstance()
Definition engine.cpp:133
void mainLoop()
Definition engine.cpp:67
void createSurface()
Definition Engine.hpp:49
Device m_device
Definition Engine.hpp:39
void loadObjects()
Definition engine.cpp:16
Engine(uint32_t=DEFAULT_WIDTH, uint32_t=DEFAULT_HEIGHT, const std::string &title=DEFAULT_TITLE.data())
Definition engine.cpp:59
std::unique_ptr< DescriptorPool > m_globalPool
Definition Engine.hpp:42
static std::unique_ptr< Model > createModelFromFile(Device &device, const std::string &filename)
Definition model.cpp:92
Transform3DComponent transform3D
Definition Object.hpp:55
static Object makePointLight(float intensity=10.F, float radius=0.1F, glm::vec3 color=glm::vec3(1.F))
Definition object.cpp:67
std::shared_ptr< Model > model
Definition Object.hpp:53
glm::vec3 color
Definition Object.hpp:54
id_t getId() const
Definition Object.hpp:51
static Object createObject()
Definition Object.hpp:40
Class for point light system.
static void update(const FrameInfo &frameInfo, GlobalUbo &ubo)
void render(const FrameInfo &frameInfo) const
Class for render system.
void renderObjects(const FrameInfo &frameInfo) const
static void endSwapChainRenderPass(VkCommandBuffer commandBuffer)
Definition renderer.cpp:123
static constexpr int MAX_FRAMES_IN_FLIGHT
Definition SwapChain.hpp:20