opynsim
Unofficial C++ Documentation
Loading...
Searching...
No Matches
render_queue.h
Go to the documentation of this file.
1#pragma once
2
15
16#include <cstddef>
17#include <iterator>
18#include <limits>
19#include <optional>
20#include <ranges>
21#include <type_traits>
22#include <vector>
23
24namespace osc
25{
26 // Represents what's queued up whenever a caller calls `graphics::draw`
27 class RenderQueue final {
28 public:
29 using handle_type = size_t;
30 using handle_iterator = std::vector<handle_type>::iterator;
31 using handle_const_iterator = std::vector<handle_type>::const_iterator;
32 using handle_subrange = std::ranges::subrange<handle_iterator>;
33 using handle_const_subrange = std::ranges::subrange<handle_const_iterator>;
34 using size_type = size_t;
36
37 friend bool operator==(const RenderQueue&, const RenderQueue&) = default;
38
40 const Mesh& mesh,
41 const Material& material)
42 {
44 }
46 const Mesh& mesh,
47 const Transform& transform,
48 const Material& material)
49 {
50 return emplace(mesh, matrix4x4_cast(transform), material);
51 }
53 const Mesh& mesh,
54 const Transform& transform,
55 const Material& material,
56 const MaterialPropertyBlock& material_prop_block)
57 {
58 return emplace(mesh, matrix4x4_cast(transform), material, material_prop_block);
59 }
61 const Mesh& mesh,
62 const Transform& transform,
63 const Material& material,
64 size_t submesh_index)
65 {
66 return emplace(mesh, matrix4x4_cast(transform), material, submesh_index);
67 }
69 const Mesh& mesh,
70 const Transform& transform,
71 const Material& material,
72 const MaterialPropertyBlock& material_prop_block,
73 size_t submesh_index)
74 {
75 return emplace(mesh, matrix4x4_cast(transform), material, material_prop_block, submesh_index);
76 }
77 handle_type emplace(const Mesh& mesh, const Matrix4x4& transform, const Material& material)
78 {
79 materials_.push_back(material);
80 material_property_blocks_.push_back(blank_property_block_);
81 meshes_.push_back(mesh);
82 maybe_submesh_indices_.emplace_back();
83 model_matrices_.push_back(transform);
84 return handles_.emplace_back(handles_.size());
85 }
87 const Mesh& mesh,
88 const Matrix4x4& transform,
89 const Material& material,
90 const MaterialPropertyBlock& material_prop_block)
91 {
92 materials_.push_back(material);
93 material_property_blocks_.push_back(material_prop_block);
94 meshes_.push_back(mesh);
95 maybe_submesh_indices_.emplace_back();
96 model_matrices_.push_back(transform);
97 return handles_.emplace_back(handles_.size());
98 }
100 const Mesh& mesh,
101 const Matrix4x4& transform,
102 const Material& material,
103 size_t submesh_index)
104 {
105 materials_.push_back(material);
106 material_property_blocks_.push_back(blank_property_block_);
107 meshes_.push_back(mesh);
108 maybe_submesh_indices_.emplace_back(submesh_index);
109 model_matrices_.push_back(transform);
110 return handles_.emplace_back(handles_.size());
111 }
113 const Mesh& mesh,
114 const Matrix4x4& transform,
115 const Material& material,
116 const MaterialPropertyBlock& material_prop_block,
117 size_t submesh_index)
118 {
119 materials_.push_back(material);
120 material_property_blocks_.push_back(material_prop_block);
121 meshes_.push_back(mesh);
122 maybe_submesh_indices_.emplace_back(submesh_index);
123 model_matrices_.push_back(transform);
124 return handles_.emplace_back(handles_.size());
125 }
126
127 void clear()
128 {
129 materials_.clear();
130 material_property_blocks_.clear();
131 meshes_.clear();
132 maybe_submesh_indices_.clear();
133 model_matrices_.clear();
134 handles_.clear();
135 }
136
137 [[nodiscard]] bool empty() const { return materials_.empty(); }
138
139 const Material& material(handle_type id) const { return materials_[id]; }
140 Material& material(handle_type id) { return materials_[id]; }
141
142 const MaterialPropertyBlock& material_property_block(handle_type id) const { return material_property_blocks_[id]; }
143 MaterialPropertyBlock& material_property_block(handle_type id) { return material_property_blocks_[id]; }
144
145 const Mesh& mesh(handle_type id) const { return meshes_[id]; }
146 Mesh& mesh(handle_type id) { return meshes_[id]; }
147
148 const submesh_index_type& maybe_submesh_index(handle_type id) const { return maybe_submesh_indices_[id]; }
149 submesh_index_type& maybe_submesh_index(handle_type id) { return maybe_submesh_indices_[id]; }
150
151 const Matrix4x4& model_matrix(handle_type id) const { return model_matrices_[id]; }
152 Matrix4x4& model_matrix(handle_type id) { return model_matrices_[id]; }
153
155 {
156 if (const auto local_centroid = mesh(id).centroid()) {
157 return transform_point(model_matrix(id), *local_centroid);
158 } else {
159 return Vector3{0.0f}; // Just something for scene sorting
160 }
161 }
162
163 bool is_opaque(handle_type id) const { return not material(id).is_transparent(); }
164 bool is_depth_tested(handle_type id) const { return material(id).is_depth_tested(); }
167
168 const auto& handles() const { return handles_; }
169 auto& handles() { return handles_; }
170
171 size_type size() const { return handles_.size(); }
172
173 private:
174 MaterialPropertyBlock blank_property_block_;
175 std::vector<Material> materials_;
176 std::vector<MaterialPropertyBlock> material_property_blocks_;
177 std::vector<Mesh> meshes_;
178 std::vector<submesh_index_type> maybe_submesh_indices_;
179 std::vector<Matrix4x4> model_matrices_;
180 std::vector<handle_type> handles_;
181 };
182}
Definition material_property_block.h:20
Definition material.h:21
bool is_depth_tested() const
bool is_transparent() const
Definition mesh.h:33
Definition render_queue.h:27
handle_type emplace(const Mesh &mesh, const Matrix4x4 &transform, const Material &material, const MaterialPropertyBlock &material_prop_block)
Definition render_queue.h:86
MaterialPropertyBlock & material_property_block(handle_type id)
Definition render_queue.h:143
const Matrix4x4 & model_matrix(handle_type id) const
Definition render_queue.h:151
handle_type emplace(const Mesh &mesh, const Matrix4x4 &transform, const Material &material, const MaterialPropertyBlock &material_prop_block, size_t submesh_index)
Definition render_queue.h:112
handle_type emplace(const Mesh &mesh, const Material &material)
Definition render_queue.h:39
std::ranges::subrange< handle_iterator > handle_subrange
Definition render_queue.h:32
std::ranges::subrange< handle_const_iterator > handle_const_subrange
Definition render_queue.h:33
std::vector< handle_type >::const_iterator handle_const_iterator
Definition render_queue.h:31
handle_type emplace(const Mesh &mesh, const Matrix4x4 &transform, const Material &material, size_t submesh_index)
Definition render_queue.h:99
bool is_depth_tested(handle_type id) const
Definition render_queue.h:164
Vector3 world_space_centroid(handle_type id) const
Definition render_queue.h:154
submesh_index_type & maybe_submesh_index(handle_type id)
Definition render_queue.h:149
Matrix3x3 normal_matrix3x3(handle_type id) const
Definition render_queue.h:165
size_t handle_type
Definition render_queue.h:29
handle_type emplace(const Mesh &mesh, const Transform &transform, const Material &material, const MaterialPropertyBlock &material_prop_block, size_t submesh_index)
Definition render_queue.h:68
bool empty() const
Definition render_queue.h:137
const auto & handles() const
Definition render_queue.h:168
Material & material(handle_type id)
Definition render_queue.h:140
size_t size_type
Definition render_queue.h:34
std::vector< handle_type >::iterator handle_iterator
Definition render_queue.h:30
const Mesh & mesh(handle_type id) const
Definition render_queue.h:145
const Material & material(handle_type id) const
Definition render_queue.h:139
Mesh & mesh(handle_type id)
Definition render_queue.h:146
auto & handles()
Definition render_queue.h:169
handle_type emplace(const Mesh &mesh, const Transform &transform, const Material &material, size_t submesh_index)
Definition render_queue.h:60
bool is_opaque(handle_type id) const
Definition render_queue.h:163
Matrix4x4 & model_matrix(handle_type id)
Definition render_queue.h:152
void clear()
Definition render_queue.h:127
friend bool operator==(const RenderQueue &, const RenderQueue &)=default
const submesh_index_type & maybe_submesh_index(handle_type id) const
Definition render_queue.h:148
const MaterialPropertyBlock & material_property_block(handle_type id) const
Definition render_queue.h:142
Matrix4x4 normal_matrix4x4(handle_type id) const
Definition render_queue.h:166
handle_type emplace(const Mesh &mesh, const Matrix4x4 &transform, const Material &material)
Definition render_queue.h:77
size_type size() const
Definition render_queue.h:171
handle_type emplace(const Mesh &mesh, const Transform &transform, const Material &material, const MaterialPropertyBlock &material_prop_block)
Definition render_queue.h:52
handle_type emplace(const Mesh &mesh, const Transform &transform, const Material &material)
Definition render_queue.h:45
Definition value_or_sentinel.h:16
Definition custom_decoration_generator.h:5
constexpr Matrix< T, 4, 4 > matrix4x4_cast(const Qua< T > &q)
Definition quaternion_functions.h:149
Vector3 transform_point(const Matrix4x4 &mat, const Vector3 &point)
Definition matrix_functions.h:540
Matrix< T, 3, 3 > normal_matrix(const Matrix< T, 4, 4 > &m)
Definition matrix_functions.h:502
Matrix< T, 4, 4 > normal_matrix4x4(const Matrix< T, 4, 4 > &m)
Definition matrix_functions.h:523
constexpr Matrix4x4 identity< Matrix4x4 >()
Definition matrix4x4.h:390
Definition transform.h:11