StapleGL
Header-only C++20 OpenGL wrapper
Loading...
Searching...
No Matches
vertex_buffer_layout.hpp
Go to the documentation of this file.
1
18#pragma once
19
20#include <span>
21#include <string>
22#include <string_view>
23#include <vector>
24
25#include "shader_data_type.hpp"
26
27namespace staplegl {
38 std::string name;
39 std::size_t element_count { 1 };
40 std::uint32_t offset {};
42
43 vertex_attribute() = default;
44 ~vertex_attribute() = default;
45
46 vertex_attribute(shader_data_type::u_type in_type, std::string_view in_name)
47 : name { in_name }
48 , type { in_type }
49 {
50 }
51
52 vertex_attribute(shader_data_type::shader_array_type in_type, std::string_view in_name, size_t element_count)
53 : name { in_name }
55 , type { static_cast<shader_data_type::u_type>(in_type) }
56 {
57 }
58
59 vertex_attribute(const vertex_attribute&) noexcept = default;
60 auto operator=(const vertex_attribute&) noexcept -> vertex_attribute& = default;
61
62 vertex_attribute(vertex_attribute&&) noexcept = default;
63 auto operator=(vertex_attribute&&) noexcept -> vertex_attribute& = default;
64};
65
79
80private:
81 std::vector<vertex_attribute> m_attributes;
82 std::size_t m_stride {};
83
84public:
92 vertex_buffer_layout(std::initializer_list<vertex_attribute> attributes)
93 : m_attributes { attributes }
94 {
95 for (auto& attribute : m_attributes) {
96 attribute.offset = m_stride;
97 m_stride += shader_data_type::size(attribute.type) * attribute.element_count;
98 }
99 }
100
106 [[nodiscard]] constexpr auto stride() const noexcept -> std::size_t { return m_stride; }
107
113 [[nodiscard]] constexpr auto stride_elements() const noexcept -> std::size_t { return m_stride / sizeof(float); }
114
120 [[nodiscard]] auto get_attributes() const noexcept -> std::span<const vertex_attribute>
121 {
122 return { m_attributes };
123 }
124
131 [[nodiscard]] auto operator[](std::size_t index) const -> const vertex_attribute& { return m_attributes[index]; }
132};
133
134} // namespace staplegl
constexpr auto stride() const noexcept -> std::size_t
Get the stride of the vertex buffer layout.
vertex_buffer_layout(std::initializer_list< vertex_attribute > attributes)
Construct a new vertex buffer layout object.
std::vector< vertex_attribute > m_attributes
constexpr auto stride_elements() const noexcept -> std::size_t
Get the stride of the vertex buffer layout in elements (assuming float-only data).
auto get_attributes() const noexcept -> std::span< const vertex_attribute >
Get the data of the vertex buffer layout as a non-owning view.
auto operator[](std::size_t index) const -> const vertex_attribute &
Get a single attribute from the vertex buffer layout.
u_type
Enumerator that represents a data type.
static constexpr auto size(u_type type) -> std::size_t
Get the size of the shader data type.
shader_array_type
Enumerator that represents an array of a given data type.
Shader data type definitions.
vertex_attribute(const vertex_attribute &) noexcept=default
auto operator=(const vertex_attribute &) noexcept -> vertex_attribute &=default
vertex_attribute(vertex_attribute &&) noexcept=default
vertex_attribute(shader_data_type::u_type in_type, std::string_view in_name)
vertex_attribute(shader_data_type::shader_array_type in_type, std::string_view in_name, size_t element_count)
shader_data_type::u_type type