wren
Vulkan-based game engine
Loading...
Searching...
No Matches
mesh.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <vk_mem_alloc.h>
4#include <vulkan/vulkan_core.h>
5
6#include <vulkan/vulkan.hpp>
9#include <wren/vk/buffer.hpp>
10#include <wren/vk/shader.hpp>
11
12#include "utils/device.hpp"
13
14namespace wren {
15
19
25
26const std::array kTriangleVertices = {
27 Vertex{.pos = {0.0f, -0.5f, 0.0f}, .normal = {1.0f, 0.0f, 0.0f}},
28 Vertex{.pos = {0.5f, 0.5f, 0.0f}, .normal = {0.0f, 1.0f, 0.0f}},
29 Vertex{.pos = {-0.5f, 0.5f, 0.0f}, .normal = {0.0f, 0.0f, 1.0f}},
30};
31
32const std::array kQuadVertices = {
33 Vertex{
34 .pos = {-0.5f, -0.5f, 0.0f},
35 .normal = {1.0f, 0.0f, 0.0f},
36 .colour = {1, 1, 1, 1},
37 },
38 Vertex{
39 .pos = {0.5f, -0.5f, 0.0f},
40 .normal = {0.0f, 1.0f, 0.0f},
41 .colour = {1, 1, 1, 1},
42 },
43 Vertex{
44 .pos = {0.5f, 0.5f, 0.0f},
45 .normal = {0.0f, 0.0f, 1.0f},
46 .colour = {1, 1, 1, 1},
47 },
48 Vertex{
49 .pos = {-0.5f, 0.5f, 0.0f},
50 .normal = {1.0f, 1.0f, 1.0f},
51 .colour = {1, 1, 1, 1},
52 },
53};
54
55const std::vector<uint16_t> kQuadIndices = {0, 2, 1, 0, 3, 2};
56
57class Mesh {
58 public:
59 static auto create_quad() -> Mesh {
60 return {std::vector<Vertex>{kQuadVertices.begin(), kQuadVertices.end()},
61 std::vector<uint16_t>{kQuadIndices.begin(), kQuadIndices.end()}};
62 }
63
64 Mesh() = default;
65
66 Mesh(const vulkan::Device& device, VmaAllocator allocator);
67 Mesh(const std::vector<Vertex>& vertices,
68 const std::vector<uint16_t>& indices);
69
70 void load(const vulkan::Device& device, VmaAllocator allocator);
71
72 void shader(const std::shared_ptr<vk::Shader>& shader) { shader_ = shader; }
73 void draw(const ::vk::CommandBuffer& cmd) const;
74 void bind(const ::vk::CommandBuffer& cmd) const;
75
76 [[nodiscard]] auto loaded() const { return loaded_; }
77
78 private:
79 bool loaded_ = false;
80
81 std::shared_ptr<vk::Shader> shader_;
82 std::vector<Vertex> vertices_;
83 std::vector<uint16_t> indices_;
84 std::shared_ptr<vk::Buffer> index_buffer_;
85 std::shared_ptr<vk::Buffer> vertex_buffer_;
86 std::shared_ptr<vk::Buffer> uniform_buffer_;
87};
88
89} // namespace wren
static auto create_quad() -> Mesh
Definition mesh.hpp:59
std::shared_ptr< vk::Buffer > uniform_buffer_
Definition mesh.hpp:86
void bind(const ::vk::CommandBuffer &cmd) const
Definition mesh.cpp:79
bool loaded_
Definition mesh.hpp:79
std::shared_ptr< vk::Shader > shader_
Definition mesh.hpp:81
void load(const vulkan::Device &device, VmaAllocator allocator)
Definition mesh.cpp:19
std::shared_ptr< vk::Buffer > index_buffer_
Definition mesh.hpp:84
void shader(const std::shared_ptr< vk::Shader > &shader)
Definition mesh.hpp:72
auto loaded() const
Definition mesh.hpp:76
std::vector< Vertex > vertices_
Definition mesh.hpp:82
void draw(const ::vk::CommandBuffer &cmd) const
Definition mesh.cpp:75
std::shared_ptr< vk::Buffer > vertex_buffer_
Definition mesh.hpp:85
std::vector< uint16_t > indices_
Definition mesh.hpp:83
Mesh()=default
Definition device.hpp:9
Definition editor_scene.hpp:5
const std::array kQuadVertices
Definition mesh.hpp:32
const std::array kTriangleVertices
Definition mesh.hpp:26
const std::vector< uint16_t > kQuadIndices
Definition mesh.hpp:55
Definition mesh.hpp:16
wren::math::Mat4f model
Definition mesh.hpp:17
Definition mesh.hpp:20
wren::math::Vec4f colour
Definition mesh.hpp:23
wren::math::Vec3f normal
Definition mesh.hpp:22
wren::math::Vec3f pos
Definition mesh.hpp:21
Definition matrix.hpp:86
Definition vector.hpp:174
Definition vector.hpp:192