wren
Vulkan-based game engine
Toggle main menu visibility
Loading...
Searching...
No Matches
mesh.hpp
Go to the documentation of this file.
1
#pragma once
2
3
#include <filesystem>
4
#include <optional>
5
#include <
wren/math/matrix.hpp
>
6
#include <
wren/mesh.hpp
>
7
#include <
wren/mesh_loader.hpp
>
8
9
#include "
wren/context.hpp
"
10
11
namespace
wren::scene::components
{
12
13
class
MeshRenderer
{
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 {
24
wren::math::Mat4f
model;
25
wren::math::Vec4f
colour
;
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
) {
34
ubo_
=
vk::Buffer::create
(
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
65
void
colour
(
const
math::Vec4f
&
colour
) {
colour_
=
colour
; }
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
73
wren::math::Vec4f
colour_
;
74
75
std::filesystem::path
mesh_file_
;
76
std::shared_ptr<vk::Buffer>
ubo_
;
77
};
78
79
}
// namespace wren::scene::components
wren::Mesh
Definition
mesh.hpp:57
wren::scene::components::MeshRenderer
Definition
mesh.hpp:13
wren::scene::components::MeshRenderer::bind
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
wren::scene::components::MeshRenderer::mesh_file
auto mesh_file() const
Definition
mesh.hpp:68
wren::scene::components::MeshRenderer::update_mesh
auto update_mesh(const Mesh &mesh)
Definition
mesh.hpp:54
wren::scene::components::MeshRenderer::mesh
auto mesh() const
Definition
mesh.hpp:67
wren::scene::components::MeshRenderer::ubo_
std::shared_ptr< vk::Buffer > ubo_
Definition
mesh.hpp:76
wren::scene::components::MeshRenderer::colour
void colour(const math::Vec4f &colour)
Definition
mesh.hpp:65
wren::scene::components::MeshRenderer::colour_
wren::math::Vec4f colour_
Definition
mesh.hpp:73
wren::scene::components::MeshRenderer::mesh_file_
std::filesystem::path mesh_file_
Definition
mesh.hpp:75
wren::scene::components::MeshRenderer::mesh_
std::optional< Mesh > mesh_
Definition
mesh.hpp:71
wren::scene::components::MeshRenderer::update_mesh
auto update_mesh(const std::filesystem::path &project_root, const std::filesystem::path &mesh_path) -> expected< void >
Definition
mesh.hpp:56
wren::vk::Buffer::create
static auto create(const VmaAllocator &allocator, size_t size, VkBufferUsageFlags usage, const std::optional< VmaAllocationCreateFlags > &flags={}) -> std::shared_ptr< Buffer >
Definition
buffer.cpp:13
matrix.hpp
mesh.hpp
mesh_loader.hpp
wren::scene::components
Definition
collider.cpp:3
wren::expected
std::expected< T, Err > expected
Definition
result.hpp:43
wren::load_mesh
auto load_mesh(const std::filesystem::path &mesh_path) -> expected< Mesh >
Definition
mesh_loader.cpp:65
wren::math::Mat4f
Definition
matrix.hpp:86
wren::math::Vec4f
Definition
vector.hpp:192
context.hpp
TRY_RESULT
#define TRY_RESULT(...)
Definition
result.hpp:120
wren
include
wren
scene
components
mesh.hpp
Generated by
1.17.0