wren
Vulkan-based game engine
Loading...
Searching...
No Matches
mesh.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <filesystem>
4#include <optional>
6#include <wren/mesh.hpp>
8
9#include "wren/context.hpp"
10
12
14 public:
15 auto bind(const std::shared_ptr<Context>& ctx,
16 const std::shared_ptr<vk::Shader>& shader,
17 const ::vk::CommandBuffer& cmd, const math::Mat4f& model_mat) {
18 if (!mesh_.has_value()) return;
19 if (!mesh_->loaded())
20 mesh_->load(ctx->graphics_context->Device(),
21 ctx->graphics_context->allocator());
22
23 struct LOCALS {
25 };
26 LOCALS ubo{};
27
28 ubo.model = model_mat;
29
30 // Load UBO if not there
31 if (ubo_ == nullptr) {
33 ctx->graphics_context->allocator(), sizeof(ubo),
34 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
35 VmaAllocationCreateFlagBits::
36 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
37 }
38
39 ubo_->set_data_raw(&ubo, sizeof(LOCALS));
40
41 ::vk::DescriptorBufferInfo buffer_info(ubo_->get(), 0, sizeof(LOCALS));
42 std::array writes = {::vk::WriteDescriptorSet{
43 {}, 1, 0, ::vk::DescriptorType::eUniformBuffer, {}, buffer_info}};
44
45 cmd.pushDescriptorSetKHR(::vk::PipelineBindPoint::eGraphics,
46 shader->pipeline_layout(), 0, writes);
47
48 mesh_->bind(cmd);
49 mesh_->draw(cmd);
50 }
51
52 auto update_mesh(const std::filesystem::path& project_root,
53 const std::filesystem::path& mesh_path) -> expected<void> {
54 mesh_file_ = mesh_path;
55
56 TRY_RESULT(mesh_, load_mesh(project_root / mesh_path));
57
58 return {};
59 }
60
61 [[nodiscard]] auto mesh() const { return mesh_; }
62 [[nodiscard]] auto mesh_file() const { return mesh_file_; }
63
64 private:
65 std::optional<Mesh> mesh_;
66 std::filesystem::path mesh_file_;
67 std::shared_ptr<vk::Buffer> ubo_;
68};
69
70} // namespace wren::scene::components
auto bind(const std::shared_ptr< Context > &ctx, const std::shared_ptr< vk::Shader > &shader, const ::vk::CommandBuffer &cmd, const math::Mat4f &model_mat)
Definition mesh.hpp:15
auto mesh_file() const
Definition mesh.hpp:62
auto mesh() const
Definition mesh.hpp:61
std::shared_ptr< vk::Buffer > ubo_
Definition mesh.hpp:67
std::filesystem::path mesh_file_
Definition mesh.hpp:66
std::optional< Mesh > mesh_
Definition mesh.hpp:65
auto update_mesh(const std::filesystem::path &project_root, const std::filesystem::path &mesh_path) -> expected< void >
Definition mesh.hpp:52
static auto create(const VmaAllocator &allocator, size_t size, VkBufferUsageFlags usage, const std::optional< VmaAllocationCreateFlags > &flags={}) -> std::shared_ptr< Buffer >
Definition buffer.cpp:13
Definition mesh.hpp:11
std::expected< T, Err > expected
Definition result.hpp:49
auto load_mesh(const std::filesystem::path &mesh_path) -> expected< Mesh >
Definition mesh_loader.cpp:65
Definition matrix.hpp:86
#define TRY_RESULT(...)
Definition result.hpp:103