diff options
| author | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-15 13:03:48 -0500 |
|---|---|---|
| committer | Jonathan Bradley <jcb@pikum.xyz> | 2023-11-15 13:03:48 -0500 |
| commit | 1513216876a0409f45d88cbad14ae8b48fca37e2 (patch) | |
| tree | 32b48d966faf36138dd985e4df739a83cddc5f48 /src/window.cpp | |
| parent | 6a30d21c5175c27aeccc8a6ae956c274f3ef9621 (diff) | |
major input refactor and add debug camera
Diffstat (limited to 'src/window.cpp')
| -rw-r--r-- | src/window.cpp | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/src/window.cpp b/src/window.cpp index 0dbd415..4c6b598 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -2,6 +2,7 @@ #define GLFW_INCLUDE_VULKAN #include "asset-manager.hpp" +#include "camera.hpp" #include "ecs.hpp" #include "entities.hpp" #include "event.hpp" @@ -89,7 +90,7 @@ VkSemaphore presentRenderFinishedSemaphores[MAX_FRAMES_IN_FLIGHT]; VkFence presentInFlightFences[MAX_FRAMES_IN_FLIGHT]; UniformBufferObject UBO{ .model = glm::mat4(1), - .view = glm::lookAt(glm::vec3(0), glm::vec3(2, 2, 0), glm::vec3(0, 0, 1)), + .view = glm::lookAt(glm::vec3(0), glm::vec3(2, 2, 0), glm::vec3(0, 1, 0)), .proj = glm::mat4(1), }; VkDeviceMemory uniformBufferMemory; @@ -814,9 +815,27 @@ void UpdatePresentDescriptorSets() { vkUpdateDescriptorSets(vkDevice, MAX_FRAMES_IN_FLIGHT, writeDescriptorSets, 0, nullptr); } -void UpdateCameraProjection() { - UBO.proj = glm::perspective(glm::radians(45.f), Extent.width / (float)Extent.height, 0.1f, 100.f); - UBO.proj[1][1] *= -1; +void UpdateCamera() { + if (bool(ActiveCamera->stale & PKE_CAMERA_STALE_POS)) { + UBO.model = glm::translate(glm::mat4(1.f), ActiveCamera->pos); + } + if (bool(ActiveCamera->stale & PKE_CAMERA_STALE_ROT)) { + if (bool(ActiveCamera->orientation == PKE_CAMERA_ORIENTATION_FREE)) { + UBO.view = glm::mat4_cast(ActiveCamera->rot); + } else if (bool(ActiveCamera->orientation == PKE_CAMERA_ORIENTATION_TARGET)) { + UBO.view = glm::lookAt(glm::vec3(0), ActiveCamera->pos - ActiveCamera->target, glm::vec3(0.f, 1.f, 0.f)); + } + } + if (bool(ActiveCamera->stale & PKE_CAMERA_STALE_ORIENTATION)) { + if (bool(ActiveCamera->type == PKE_CAMERA_TYPE_PERSPECTIVE)) { + UBO.proj = glm::perspective(glm::radians(45.f), Extent.width / (float)Extent.height, 0.1f, 100.f); + UBO.proj[1][1] *= -1; + } else if (bool(ActiveCamera->type == PKE_CAMERA_TYPE_ORTHOGONAL)) { + UBO.proj = glm::ortho(0.f, (float)Extent.width, 0.f, (float)Extent.height, 0.f, 100.f); + UBO.proj[1][1] *= -1; + } + } + ActiveCamera->stale = PkeCameraStaleFlags{0}; } void CreateRenderPass() { @@ -2405,12 +2424,13 @@ void RecreateSwapchain() { } Extent.width = width; Extent.height = height; + ActiveCamera->stale = ActiveCamera->stale | PKE_CAMERA_STALE_ORIENTATION; DetermineMonitor(); vkDeviceWaitIdle(vkDevice); DestroySwapchain(); CreateSwapchain(); UpdatePresentDescriptorSets(); - UpdateCameraProjection(); + UpdateCamera(); CreateFramebuffers(); shouldRecreateSwapchain = false; } @@ -2433,7 +2453,7 @@ void CreateWindow(PKEWindowProperties *wp) { CreateRenderPass(); CreatePresentPipeline(); UpdatePresentDescriptorSets(); - UpdateCameraProjection(); + UpdateCamera(); CreateFramebuffers(); CreateCommandPool(); CreateCommandBuffer(); @@ -2538,6 +2558,7 @@ void Render() { // update uniform buffer { + UpdateCamera(); void *mappedUboMemory; vkMapMemory(vkDevice, uniformBufferMemory, paddedUboBufferSize * imageIndex, sizeof(UniformBufferObject), 0, &mappedUboMemory); memcpy(mappedUboMemory, &UBO, sizeof(UniformBufferObject)); |
