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 {
26 };
27 LOCALS ubo{};
28
29 ubo.model = model_mat;
30 ubo.colour = colour_;
31
32 // Load UBO if not there
33 if (ubo_ == nullptr) {
35 ctx->graphics_context->allocator(), sizeof(ubo),
36 VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,
37 VmaAllocationCreateFlagBits::
38 VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT);
39 }
40
41 ubo_->set_data_raw(&ubo, sizeof(LOCALS));
42
43 ::vk::DescriptorBufferInfo buffer_info(ubo_->get(), 0, sizeof(LOCALS));
44 std::array writes = {::vk::WriteDescriptorSet{
45 {}, 1, 0, ::vk::DescriptorType::eUniformBuffer, {}, buffer_info}};
46
47 cmd.pushDescriptorSetKHR(::vk::PipelineBindPoint::eGraphics,
48 shader->pipeline_layout(), 0, writes);
49
50 mesh_->bind(cmd);
51 mesh_->draw(cmd);
52 }
53
54 auto update_mesh(const Mesh& mesh) { mesh_ = mesh; }
55
56 auto update_mesh(const std::filesystem::path& project_root,
57 const std::filesystem::path& mesh_path) -> expected<void> {
58 mesh_file_ = mesh_path;
59
60 TRY_RESULT(mesh_, load_mesh(project_root / mesh_path));
61
62 return {};
63 }
64
66
67 [[nodiscard]] auto mesh() const { return mesh_; }
68 [[nodiscard]] auto mesh_file() const { return mesh_file_; }
69
70 private:
71 std::optional<Mesh> mesh_;
72
74
75 std::filesystem::path mesh_file_;
76 std::shared_ptr<vk::Buffer> ubo_;
77};
78
79} // namespace wren::scene::components
Definition mesh.hpp:57
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:68
auto update_mesh(const Mesh &mesh)
Definition mesh.hpp:54
auto mesh() const
Definition mesh.hpp:67
std::shared_ptr< vk::Buffer > ubo_
Definition mesh.hpp:76
void colour(const math::Vec4f &colour)
Definition mesh.hpp:65
wren::math::Vec4f colour_
Definition mesh.hpp:73
std::filesystem::path mesh_file_
Definition mesh.hpp:75
std::optional< Mesh > mesh_
Definition mesh.hpp:71
auto update_mesh(const std::filesystem::path &project_root, const std::filesystem::path &mesh_path) -> expected< void >
Definition mesh.hpp:56
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 collider.cpp:3
std::expected< T, Err > expected
Definition result.hpp:43
auto load_mesh(const std::filesystem::path &mesh_path) -> expected< Mesh >
Definition mesh_loader.cpp:65
Definition matrix.hpp:86
Definition vector.hpp:192
#define TRY_RESULT(...)
Definition result.hpp:120