vengine  0.0.1
3D graphics engine
Loading...
Searching...
No Matches
render.cpp
Go to the documentation of this file.
1#include <imgui_impl_glfw.h>
2#include <imgui_impl_vulkan.h>
3
4#include <glm/gtc/type_ptr.hpp>
5
10
12{
13 ImGui_ImplVulkan_Shutdown();
14 ImGui_ImplGlfw_Shutdown();
15 ImGui::DestroyContext();
16}
17
18void ven::Gui::render(Renderer* renderer, SceneManager& sceneManager, Camera& camera, const VkPhysicalDevice physicalDevice, GlobalUbo& ubo, const ClockData& clockData)
19{
20 VkPhysicalDeviceProperties deviceProperties;
21 vkGetPhysicalDeviceProperties(physicalDevice, &deviceProperties);
22 ImGui_ImplVulkan_NewFrame();
23 ImGui_ImplGlfw_NewFrame();
24 ImGui::NewFrame();
25 renderFrameWindow(clockData);
26
27 rendererSection(renderer, ubo);
28 cameraSection(camera);
29 lightsSection(sceneManager);
30 objectsSection(sceneManager);
31 inputsSection(*m_io);
32 devicePropertiesSection(deviceProperties);
33
34 ImGui::End();
35 ImGui::Render();
36 ImGui_ImplVulkan_RenderDrawData(ImGui::GetDrawData(), renderer->getCurrentCommandBuffer());
37}
38
40{
41 ImGui::SetNextWindowPos(ImVec2(0.0F, 0.0F), ImGuiCond_Always, ImVec2(0.0F, 0.0F));
42 ImGui::Begin("Application Info", nullptr, ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing | ImGuiWindowFlags_NoNav);
43 ImGui::Text("FPS: %.1f", clockData.fps);
44 ImGui::Text("Frame time: %.3fms", clockData.deltaTimeMS);
45 ImGui::End();
46}
47
49{
50 ImGui::SetNextWindowPos(ImVec2(0.0F, 45.0F), ImGuiCond_Always, ImVec2(0.0F, 0.0F));
51 ImGui::Begin("Editor tools");
52 if (ImGui::CollapsingHeader("Renderer")) {
53 ImGui::Text("Aspect Ratio: %.2f", renderer->getAspectRatio());
54
55 if (ImGui::BeginTable("ClearColorTable", 2)) {
56 ImGui::TableNextColumn();
57 std::array<float, 4> clearColor = renderer->getClearColor();
58
59 if (ImGui::ColorEdit4("Clear Color", clearColor.data())) {
60 const VkClearColorValue clearColorValue = {{clearColor[0], clearColor[1], clearColor[2], clearColor[3]}};
61 renderer->setClearValue(clearColorValue);
62 }
63
64 ImGui::TableNextColumn();
65 static int item_current = 0;
66
67 if (ImGui::Combo("Color Presets##clearColor",
68 &item_current,
69 [](void*, const int idx, const char** out_text) -> bool {
70 if (idx < 0 || idx >= static_cast<int>(std::size(Colors::COLOR_PRESETS_VK))) { return false; }
71 *out_text = Colors::COLOR_PRESETS_VK.at(static_cast<unsigned long>(idx)).first;
72 return true;
73 },
74 nullptr,
75 std::size(Colors::COLOR_PRESETS_VK))) {
76 renderer->setClearValue(Colors::COLOR_PRESETS_VK.at(static_cast<unsigned long>(item_current)).second);
77 }
78
79 ImGui::TableNextColumn();
80 ImGui::ColorEdit4("Ambient Light Color", glm::value_ptr(ubo.ambientLightColor));
81 ImGui::TableNextColumn();
82 if (ImGui::Combo("Color Presets##ambientColor",
83 &item_current,
84 [](void*, const int idx, const char** out_text) -> bool {
85 if (idx < 0 || idx >= static_cast<int>(std::size(Colors::COLOR_PRESETS_4))) { return false; }
86 *out_text = Colors::COLOR_PRESETS_4.at(static_cast<unsigned long>(idx)).first;
87 return true;
88 },
89 nullptr,
90 std::size(Colors::COLOR_PRESETS_4))) {
91 ubo.ambientLightColor = Colors::COLOR_PRESETS_4.at(static_cast<unsigned long>(item_current)).second;
92 }
93
94 ImGui::TableNextColumn();
95 ImGui::SliderFloat(("Intensity##" + std::to_string(0)).c_str(), &ubo.ambientLightColor.a, 0.0F, 1.0F);
96 ImGui::TableNextColumn();
97 if (ImGui::Button("Reset##ambientIntensity")) { ubo.ambientLightColor.a = DEFAULT_AMBIENT_LIGHT_INTENSITY; }
98
99 ImGui::EndTable();
100 }
101
102 static bool fullscreen = false;
103 if (ImGui::Checkbox("Fullscreen", &fullscreen)) {
104 renderer->getWindow().setFullscreen(fullscreen, renderer->getWindow().getExtent().width, renderer->getWindow().getExtent().height);
105 }
106 }
107}
108
110{
111 if (ImGui::CollapsingHeader("Camera")) {
112 float fov = camera.getFov();
113 float tnear = camera.getNear();
114 float tfar = camera.getFar();
115 if (ImGui::BeginTable("CameraTable", 2)) {
116 ImGui::TableNextColumn();
117 ImGui::DragFloat3("Position", glm::value_ptr(camera.transform.translation), 0.1F);
118 ImGui::TableNextColumn();
119 if (ImGui::Button("Reset##position")) { camera.transform.translation = DEFAULT_POSITION; }
120
121 ImGui::TableNextColumn();
122 ImGui::DragFloat3("Rotation", glm::value_ptr(camera.transform.rotation), 0.1F);
123 ImGui::TableNextColumn();
124 if (ImGui::Button("Reset##rotation")) { camera.transform.rotation = DEFAULT_ROTATION; }
125
126 ImGui::TableNextColumn();
127 if (ImGui::SliderFloat("FOV", &fov, glm::radians(0.1F), glm::radians(180.0F))) { camera.setFov(fov); }
128 ImGui::TableNextColumn();
129 if (ImGui::Button("Reset##fov")) { camera.setFov(DEFAULT_FOV); }
130
131 ImGui::TableNextColumn();
132 if (ImGui::SliderFloat("Near", &tnear, 0.001F, 10.0F)) { camera.setNear(tnear); }
133 ImGui::TableNextColumn();
134 if (ImGui::Button("Reset##near")) { camera.setNear(DEFAULT_NEAR); }
135
136 ImGui::TableNextColumn();
137 if (ImGui::SliderFloat("Far", &tfar, 1.F, 1000.0F)) { camera.setFar(tfar); }
138 ImGui::TableNextColumn();
139 if (ImGui::Button("Reset##far")) { camera.setFar(DEFAULT_FAR); }
140
141 ImGui::TableNextColumn();
142 float moveSpeed = camera.getMoveSpeed();
143 if (ImGui::SliderFloat("Move speed", &moveSpeed, 0.1F, 10.0F)) { camera.setMoveSpeed(moveSpeed); }
144 ImGui::TableNextColumn();
145 if (ImGui::Button("Reset##moveSpeed")) { camera.setMoveSpeed(DEFAULT_MOVE_SPEED); }
146
147 ImGui::TableNextColumn();
148 float lookSpeed = camera.getLookSpeed();
149 if (ImGui::SliderFloat("Look speed", &lookSpeed, 0.1F, 10.0F)) { camera.setLookSpeed(lookSpeed); }
150 ImGui::TableNextColumn();
151 if (ImGui::Button("Reset##lookSpeed")) { camera.setLookSpeed(DEFAULT_LOOK_SPEED); }
152
153 ImGui::EndTable();
154 }
155 }
156}
157
159{
160 if (ImGui::CollapsingHeader("Objects")) {
161 bool open = false;
162 for (Object::Map& objects = sceneManager.getObjects(); auto& [id, object] : objects) {
163 ImGui::PushStyleColor(ImGuiCol_Text, { Colors::GRAY_4.r, Colors::GRAY_4.g, Colors::GRAY_4.b, 1.0F });
164 open = ImGui::TreeNode(std::string(object.getName() + " [" + std::to_string(object.getId()) + "]").c_str());
165 ImGui::PopStyleColor(1);
166 if (open) {
167 if (ImGui::Button(("Delete##" + object.getName()).c_str())) {
168 m_objectsToRemove.push_back(id);
169 sceneManager.setDestroyState(true);
170 }
171 ImGui::SameLine();
172 if (ImGui::Button(("Duplicate##" + object.getName()).c_str())) {
174 }
175 ImGui::Text("Address: %p", static_cast<void*>(&object));
176 ImGui::DragFloat3(("Position##" + object.getName()).c_str(), glm::value_ptr(object.transform.translation), 0.1F);
177 ImGui::DragFloat3(("Rotation##" + object.getName()).c_str(), glm::value_ptr(object.transform.rotation), 0.1F);
178 ImGui::DragFloat3(("Scale##" + object.getName()).c_str(), glm::value_ptr(object.transform.scale), 0.1F);
179 ImGui::TreePop();
180 }
181 }
182 }
183}
184
186{
187
188 if (ImGui::CollapsingHeader("Lights")) {
189 bool open = false;
190 float tempIntensity = m_intensity;
191 float tempShininess = m_shininess;
192 Light::Map& lights = sceneManager.getLights();
193
194 if (ImGui::BeginTable("LightTable", 2)) {
195 ImGui::TableNextColumn();
196 if (ImGui::SliderFloat("Global Intensity", &tempIntensity, 0.0F, 5.F)) {
197 m_intensity = tempIntensity;
198 for (auto&[fst, snd] : lights) {
199 snd.color.a = m_intensity;
200 }
201 }
202 ImGui::TableNextColumn();
203 if (ImGui::Button("Reset")) {
204 m_intensity = DEFAULT_LIGHT_INTENSITY;
205 tempIntensity = m_intensity;
206 for (auto&[fst, snd] : lights) {
207 snd.color.a = m_intensity;
208 }
209 }
210
211 ImGui::TableNextColumn();
212 if (ImGui::SliderFloat("Global Shininess", &tempShininess, 0.0F, 512.F)) {
213 m_shininess = tempShininess;
214 for (auto&[fst, snd] : lights) {
215 snd.setShininess(m_shininess);
216 }
217 }
218
219 ImGui::TableNextColumn();
220 if (ImGui::Button("Reset")) {
221 m_shininess = DEFAULT_SHININESS;
222 tempShininess = m_shininess;
223 for (auto&[fst, snd] : lights) {
224 snd.setShininess(m_shininess);
225 }
226 }
227
228 ImGui::EndTable();
229 }
230
231 for (auto& [id, light] : lights) {
232 ImGui::PushStyleColor(ImGuiCol_Text, {light.color.r, light.color.g, light.color.b, 1.0F});
233 open = ImGui::TreeNode(std::string(light.getName() + " [" + std::to_string(light.getId()) + "]").c_str());
234 ImGui::PopStyleColor(1);
235 if (open) {
236 if (ImGui::Button(("Delete##" + light.getName()).c_str())) {
237 m_lightsToRemove.push_back(id);
238 sceneManager.setDestroyState(true);
239 }
240 ImGui::SameLine();
241 if (ImGui::Button(("Duplicate##" + light.getName()).c_str())) {
243 }
244 ImGui::Text("Address: %p", static_cast<void*>(&light));
245 ImGui::DragFloat3(("Position##" + std::to_string(light.getId())).c_str(), glm::value_ptr(light.transform.translation), 0.1F);
246 ImGui::DragFloat3(("Rotation##" + std::to_string(light.getId())).c_str(), glm::value_ptr(light.transform.rotation), 0.1F);
247 ImGui::DragFloat3(("Scale##" + std::to_string(light.getId())).c_str(), glm::value_ptr(light.transform.scale), 0.1F);
248 if (ImGui::BeginTable("ColorTable", 2)) {
249 ImGui::TableNextColumn();
250 ImGui::ColorEdit4(("Color##" + std::to_string(light.getId())).c_str(), glm::value_ptr(light.color));
251 ImGui::TableNextColumn();
252 static int item_current = 0;
253 if (ImGui::Combo("Color Presets",
254 &item_current,
255 [](void*, const int idx, const char** out_text) -> bool {
256 if (idx < 0 || idx >= static_cast<int>(std::size(Colors::COLOR_PRESETS_3))) { return false; }
257 *out_text = Colors::COLOR_PRESETS_3.at(static_cast<unsigned long>(idx)).first;
258 return true;
259 },
260 nullptr,
261 std::size(Colors::COLOR_PRESETS_3))) {
262 light.color = {Colors::COLOR_PRESETS_3.at(static_cast<unsigned long>(item_current)).second, light.color.a};
263 }
264 ImGui::EndTable();
265
266 ImGui::SliderFloat(("Intensity##" + std::to_string(light.getId())).c_str(), &light.color.a, 0.0F, 5.F);
267 ImGui::SameLine();
268 if (ImGui::Button(("Reset##" + std::to_string(light.getId())).c_str())) { light.color.a = DEFAULT_LIGHT_INTENSITY; }
269 float shininess = light.getShininess();
270 if (ImGui::SliderFloat("Shininess", &shininess, 0.0F, 512.F)) {
271 light.setShininess(shininess);
272 }
273 ImGui::SameLine();
274 if (ImGui::Button("Reset##shininess")) { light.setShininess(DEFAULT_SHININESS); }
275 }
276 ImGui::TreePop();
277 }
278 }
279 }
280}
281
282void ven::Gui::inputsSection(const ImGuiIO& io)
283{
284 if (ImGui::CollapsingHeader("Input")) {
285 ImGui::IsMousePosValid() ? ImGui::Text("Mouse pos: (%g, %g)", io.MousePos.x, io.MousePos.y) : ImGui::Text("Mouse pos: <INVALID>");
286 ImGui::Text("Mouse delta: (%g, %g)", io.MouseDelta.x, io.MouseDelta.y);
287 ImGui::Text("Mouse down:");
288 for (int i = 0; i < static_cast<int>(std::size(io.MouseDown)); i++) {
289 if (ImGui::IsMouseDown(i)) {
290 ImGui::SameLine();
291 ImGui::Text("b%d (%.02f secs)", i, io.MouseDownDuration[i]);
292 }
293 }
294 ImGui::Text("Mouse wheel: %.1f", io.MouseWheel);
295 ImGui::Text("Keys down:");
296 for (auto key = static_cast<ImGuiKey>(0); key < ImGuiKey_NamedKey_END; key = static_cast<ImGuiKey>(key + 1)) {
297 if (funcs::IsLegacyNativeDupe(key) || !ImGui::IsKeyDown(key)) { continue; }
298 ImGui::SameLine();
299 ImGui::Text((key < ImGuiKey_NamedKey_BEGIN) ? "\"%s\"" : "\"%s\" %d", ImGui::GetKeyName(key), key);
300 }
301 }
302}
303
304void ven::Gui::devicePropertiesSection(VkPhysicalDeviceProperties deviceProperties)
305{
306 if (ImGui::CollapsingHeader("Device Properties")) {
307 if (ImGui::BeginTable("DevicePropertiesTable", 2)) {
308
309 ImGui::TableNextColumn(); ImGui::Text("Device Name: %s", deviceProperties.deviceName);
310 ImGui::TableNextColumn(); ImGui::Text("API Version: %d.%d.%d", VK_VERSION_MAJOR(deviceProperties.apiVersion), VK_VERSION_MINOR(deviceProperties.apiVersion), VK_VERSION_PATCH(deviceProperties.apiVersion));
311 ImGui::TableNextColumn(); ImGui::Text("Driver Version: %d.%d.%d", VK_VERSION_MAJOR(deviceProperties.driverVersion), VK_VERSION_MINOR(deviceProperties.driverVersion), VK_VERSION_PATCH(deviceProperties.driverVersion));
312 ImGui::TableNextColumn(); ImGui::Text("Vendor ID: %d", deviceProperties.vendorID);
313 ImGui::TableNextColumn(); ImGui::Text("Device ID: %d", deviceProperties.deviceID);
314 ImGui::TableNextColumn(); ImGui::Text("Device Type: %d", deviceProperties.deviceType);
315 ImGui::TableNextColumn(); ImGui::Text("Discrete Queue Priorities: %d", deviceProperties.limits.discreteQueuePriorities);
316 ImGui::TableNextColumn(); ImGui::Text("Max Push Constants Size: %d", deviceProperties.limits.maxPushConstantsSize);
317 ImGui::TableNextColumn(); ImGui::Text("Max Memory Allocation Count: %d", deviceProperties.limits.maxMemoryAllocationCount);
318 ImGui::TableNextColumn(); ImGui::Text("Max Image Dimension 1D: %d", deviceProperties.limits.maxImageDimension1D);
319 ImGui::TableNextColumn(); ImGui::Text("Max Image Dimension 2D: %d", deviceProperties.limits.maxImageDimension2D);
320 ImGui::TableNextColumn(); ImGui::Text("Max Image Dimension 3D: %d", deviceProperties.limits.maxImageDimension3D);
321 ImGui::TableNextColumn(); ImGui::Text("Max Image Dimension Cube: %d", deviceProperties.limits.maxImageDimensionCube);
322 ImGui::TableNextColumn(); ImGui::Text("Max Image Array Layers: %d", deviceProperties.limits.maxImageArrayLayers);
323 ImGui::TableNextColumn(); ImGui::Text("Max Texel Buffer Elements: %d", deviceProperties.limits.maxTexelBufferElements);
324 ImGui::TableNextColumn(); ImGui::Text("Max Uniform Buffer Range: %d", deviceProperties.limits.maxUniformBufferRange);
325 ImGui::TableNextColumn(); ImGui::Text("Max Storage Buffer Range: %d", deviceProperties.limits.maxStorageBufferRange);
326 ImGui::EndTable();
327 }
328 }
329}
This file contains the Light Factory class.
This file contains the Object Factory class.
This file contains the ImGuiWindowManager class.
Class for camera.
Definition Camera.hpp:28
Transform3D transform
Definition Camera.hpp:60
float getFov() const
Definition Camera.hpp:54
float getNear() const
Definition Camera.hpp:55
void setMoveSpeed(const float moveSpeed)
Definition Camera.hpp:48
float getMoveSpeed() const
Definition Camera.hpp:57
float getLookSpeed() const
Definition Camera.hpp:58
void setNear(const float near)
Definition Camera.hpp:46
void setLookSpeed(const float lookSpeed)
Definition Camera.hpp:49
void setFov(const float fov)
Definition Camera.hpp:45
float getFar() const
Definition Camera.hpp:56
void setFar(const float far)
Definition Camera.hpp:47
static constexpr std::array< std::pair< const char *, glm::vec4 >, 20 > COLOR_PRESETS_4
Definition Colors.hpp:130
static constexpr glm::vec4 GRAY_4
Definition Colors.hpp:63
static constexpr std::array< std::pair< const char *, glm::vec3 >, 20 > COLOR_PRESETS_3
Definition Colors.hpp:107
static constexpr std::array< std::pair< const char *, VkClearColorValue >, 20 > COLOR_PRESETS_VK
Definition Colors.hpp:153
static void renderFrameWindow(const ClockData &clockData)
Definition render.cpp:39
static void cameraSection(Camera &camera)
Definition render.cpp:109
void lightsSection(SceneManager &sceneManager)
Definition render.cpp:185
static void rendererSection(Renderer *renderer, GlobalUbo &ubo)
Definition render.cpp:48
static void inputsSection(const ImGuiIO &io)
Definition render.cpp:282
static void cleanup()
Definition render.cpp:11
static void devicePropertiesSection(VkPhysicalDeviceProperties deviceProperties)
Definition render.cpp:304
void render(Renderer *renderer, SceneManager &sceneManager, Camera &camera, VkPhysicalDevice physicalDevice, GlobalUbo &ubo, const ClockData &clockData)
Definition render.cpp:18
void objectsSection(SceneManager &sceneManager)
Definition render.cpp:158
static std::unique_ptr< Light > duplicate(const Light &cpyLight)
Definition Light.hpp:35
std::unordered_map< unsigned int, Light > Map
Definition Light.hpp:32
static std::unique_ptr< Object > duplicate(const Object &objSrc)
Definition Object.hpp:31
std::unordered_map< unsigned int, Object > Map
Definition Object.hpp:28
Class for renderer.
Definition Renderer.hpp:24
Window & getWindow() const
Definition Renderer.hpp:49
std::array< float, 4 > getClearColor() const
Definition Renderer.hpp:42
float getAspectRatio() const
Definition Renderer.hpp:37
void setClearValue(const VkClearColorValue clearColorValue=DEFAULT_CLEAR_COLOR, const VkClearDepthStencilValue clearDepthValue=DEFAULT_CLEAR_DEPTH)
Definition Renderer.hpp:51
VkCommandBuffer getCurrentCommandBuffer() const
Definition Renderer.hpp:39
Class for object manager.
Definition Manager.hpp:19
void setDestroyState(const bool state)
Definition Manager.hpp:46
Light::Map & getLights()
Definition Manager.hpp:41
Object::Map & getObjects()
Definition Manager.hpp:40
glm::vec3 translation
VkExtent2D getExtent() const
Definition Window.hpp:43
void setFullscreen(bool fullscreen, uint32_t width, uint32_t height)
Definition window.cpp:42
static constexpr glm::vec3 DEFAULT_POSITION
Definition Camera.hpp:13
static constexpr float DEFAULT_FOV
Definition Camera.hpp:16
static constexpr float DEFAULT_LOOK_SPEED
Definition Camera.hpp:21
static constexpr float DEFAULT_SHININESS
Definition Light.hpp:18
static constexpr float DEFAULT_AMBIENT_LIGHT_INTENSITY
Definition FrameInfo.hpp:18
static constexpr float DEFAULT_MOVE_SPEED
Definition Camera.hpp:20
static constexpr float DEFAULT_NEAR
Definition Camera.hpp:17
static constexpr float DEFAULT_LIGHT_INTENSITY
Definition Light.hpp:16
static constexpr glm::vec3 DEFAULT_ROTATION
Definition Camera.hpp:14
static constexpr float DEFAULT_FAR
Definition Camera.hpp:18
glm::vec4 ambientLightColor
Definition FrameInfo.hpp:39
float deltaTimeMS
Definition Gui.hpp:33