From c4f1f3f019af92166e667a87164a1b9a1fd21618 Mon Sep 17 00:00:00 2001 From: AmyFoxie Date: Sat, 2 Dec 2023 12:52:27 +0100 Subject: [PATCH] fix the path loading --- src/engine/Camera.cpp | 29 ++++++++++++++--------------- src/engine/vk/VkDescriptorSet.cpp | 22 ++++++++++++++++------ src/engine/vk/VkDescriptorSet.h | 4 +++- src/engine/vk/VkImage.cpp | 2 +- src/engine/vk/test/main.cpp | 17 +++++++++++------ 5 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/engine/Camera.cpp b/src/engine/Camera.cpp index 5a3f593..261daa7 100644 --- a/src/engine/Camera.cpp +++ b/src/engine/Camera.cpp @@ -12,12 +12,17 @@ namespace Vixen { clearColor(clearColor) {} glm::mat4 Camera::view() const { - // const auto &reverse = glm::conjugate(rotation); - // glm::mat4 rot = glm::toMat4(reverse); - // glm::mat4 translation = glm::translate({1.0}, -position); + // glm::vec3 front; + // front.x = static_cast(cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y))); + // front.y = static_cast(sin(glm::radians(rotation.y))); + // front.z = static_cast(sin(glm::radians(rotation.x)) * cos(glm::radians(rotation.y))); // - // return rot * translation; - return glm::lookAt( + // front = normalize(front); + // const auto &right = normalize(cross(front, glm::vec3(0.0f, 1.0f, 0.0f))); + // const auto &up = normalize(cross(right, front)); + // + // return lookAt(position, position + front, up); + return lookAt( position, glm::vec3(0, 0, 0), {0.0f, 1.0f, 0.0f} @@ -25,17 +30,11 @@ namespace Vixen { } glm::mat4 Camera::perspective(const float aspectRatio) const { - // return glm::perspective( - // glm::radians(fieldOfView), - // aspectRatio, - // nearPlane, - // farPlane - // ); return glm::perspective( - glm::radians(45.0f), - 1.7f, - 0.1f, - 10.0f + glm::radians(fieldOfView), + aspectRatio, + nearPlane, + farPlane ); } diff --git a/src/engine/vk/VkDescriptorSet.cpp b/src/engine/vk/VkDescriptorSet.cpp index 66e4621..243f424 100644 --- a/src/engine/vk/VkDescriptorSet.cpp +++ b/src/engine/vk/VkDescriptorSet.cpp @@ -43,13 +43,19 @@ namespace Vixen::Vk { ); } - void VkDescriptorSet::updateUniformBuffer(const uint32_t binding, const VkBuffer& buffer) const { + void VkDescriptorSet::updateUniformBuffer( + const uint32_t binding, + const VkBuffer& buffer, + const uint32_t offset, + const uint32_t size + ) const { + if (offset + size > buffer.getSize()) + throw std::runtime_error("Offset plus size is greater than buffer size"); + const VkDescriptorBufferInfo bufferInfo{ .buffer = buffer.getBuffer(), - // TODO: This can't be hardcoded in the future to deal with buffers storing - // multiple descriptors (or other things), will need a better solution - .offset = 0, - .range = buffer.getSize() + .offset = offset, + .range = size }; const VkWriteDescriptorSet write{ @@ -68,7 +74,11 @@ namespace Vixen::Vk { vkUpdateDescriptorSets(device->getDevice(), 1, &write, 0, nullptr); } - void VkDescriptorSet::updateCombinedImageSampler(const uint32_t binding, const VkSampler &sampler, const VkImageView &view) const { + void VkDescriptorSet::updateCombinedImageSampler( + const uint32_t binding, + const VkSampler& sampler, + const VkImageView& view + ) const { const VkDescriptorImageInfo imageInfo{ .sampler = sampler.getSampler(), .imageView = view.getImageView(), diff --git a/src/engine/vk/VkDescriptorSet.h b/src/engine/vk/VkDescriptorSet.h index 46b6c4d..8792225 100644 --- a/src/engine/vk/VkDescriptorSet.h +++ b/src/engine/vk/VkDescriptorSet.h @@ -33,7 +33,9 @@ namespace Vixen::Vk { void updateUniformBuffer( uint32_t binding, - const VkBuffer& buffer + const VkBuffer& buffer, + uint32_t offset, + uint32_t size ) const; void updateCombinedImageSampler( diff --git a/src/engine/vk/VkImage.cpp b/src/engine/vk/VkImage.cpp index e437740..c20aadd 100644 --- a/src/engine/vk/VkImage.cpp +++ b/src/engine/vk/VkImage.cpp @@ -84,7 +84,7 @@ namespace Vixen::Vk { VkImage VkImage::from(const std::shared_ptr& device, const std::string& path) { FreeImage_Initialise(); - const auto& format = FreeImage_GetFileType(path.c_str(), 0); + const auto& format = FreeImage_GetFileType(path.c_str(), static_cast(path.length())); if (format == FIF_UNKNOWN) error("Failed to determine image format, possibly unsupported format?"); diff --git a/src/engine/vk/test/main.cpp b/src/engine/vk/test/main.cpp index 24bed9c..aca00e5 100644 --- a/src/engine/vk/test/main.cpp +++ b/src/engine/vk/test/main.cpp @@ -109,7 +109,7 @@ int main() { } ); - auto camera = Vixen::Camera(glm::vec3{2.0f, 2.0f, 2.0f}); + auto camera = Vixen::Camera(glm::vec3{1.0f, 1.0f, 1.0f}); const std::vector sizes{ { @@ -134,12 +134,12 @@ int main() { camera.perspective(static_cast(width) / static_cast(height)) }; - auto descriptorPool = std::make_shared(vixen.device, sizes, 2); + auto descriptorPool = std::make_shared(vixen.device, sizes, 1); auto mvp = Vixen::Vk::VkDescriptorSet(vixen.device, descriptorPool, *program.getDescriptorSetLayout()); - mvp.updateUniformBuffer(0, uniformBuffer); + mvp.updateUniformBuffer(0, uniformBuffer, 0, uniformBuffer.getSize()); auto image = std::make_shared( - Vixen::Vk::VkImage::from(vixen.device, "texture.jpg")); + Vixen::Vk::VkImage::from(vixen.device, "../../src/engine/vk/test/texture.jpg")); auto view = Vixen::Vk::VkImageView(image, VK_IMAGE_ASPECT_COLOR_BIT); auto sampler = Vixen::Vk::VkSampler(vixen.device); @@ -161,8 +161,13 @@ int main() { const double& now = glfwGetTime(); double deltaTime = now - lastFrame; lastFrame = now; - ubo.model = glm::rotate(ubo.model, static_cast(deltaTime) * glm::radians(90.0f), - glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.model = rotate(ubo.model, static_cast(deltaTime) * glm::radians(90.0f), + glm::vec3(0.0f, 0.0f, 1.0f)); + ubo.view = camera.view(); + ubo.projection = camera.perspective( + static_cast(vixen.swapchain.getExtent().width) / + static_cast(vixen.swapchain.getExtent().height) + ); uniformBuffer.write(reinterpret_cast(&ubo), sizeof(UniformBufferObject), 0); renderer->render(buffer, vertices.size(), indices.size(), descriptorSets);