Oxygen Engine
Modern C++ 3D Engine using OpenGL
Loading...
Searching...
No Matches
mesh.h
1#ifndef OE_RENDER_MESH_H
2#define OE_RENDER_MESH_H
3
4#include <vector>
5#include <stdint.h>
6#include <glm/vec2.hpp>
7#include <glm/vec3.hpp>
8#include <glm/vec4.hpp>
9#include "../util/non_copyable.h"
10
11#include "../scene/vertex.h"
12
13namespace oe::scene
14{
15 class CommonMesh;
16}
17
18namespace oe::render
19{
25 template <typename T>
26 int32_t getComponentSize() noexcept
27 {
28 return 0;
29 }
30
31 // Todo put those elsewhere
32 template <>
33 constexpr int32_t getComponentSize<float>() noexcept
34 {
35 return 1;
36 }
37
38 template <>
39 constexpr int32_t getComponentSize<glm::vec2>() noexcept
40 {
41 return 2;
42 }
43
44 template <>
45 constexpr int32_t getComponentSize<glm::vec3>() noexcept
46 {
47 return 3;
48 }
49
50 template <>
51 constexpr int32_t getComponentSize<glm::vec4>() noexcept
52 {
53 return 4;
54 }
55
56 template <>
57 constexpr int32_t getComponentSize<glm::ivec4>() noexcept
58 {
59 return 4;
60 }
61
65 enum class MeshUsage : uint32_t
66 {
68 STREAM = 0x88E0,
69
71 STATIC = 0x88E4,
72
74 DYNAMIC = 0x88E8
75 };
76
78 {
79 public:
87 void renderArrays(const uint32_t count, const uint32_t offset = 0, const uint32_t mode = 0x0004);
88
92 void setSubBufferData(const uint32_t size, const void* data, const uint32_t offset = 0);
93
94 /*
95 * Lock buffer and return buffer adress to update
96 * @return pair with vertex buffer and indices buffer adress
97 *
98 * @note Once you finished, you must call releaseBuffer()
99 */
100 //void* lockBuffer(/*, const uint32_t access = WRITE_ONLY */);
101
102 /*
103 * Release the buffer locked
104 */
105 //void releaseBuffer();
106
112 template <typename V>
114
115 void enableVertexAttribPointer(uint32_t index, size_t offset, size_t total, int32_t componentCount);
116
117 void enableVertexAttribIPointer(uint32_t index, size_t offset, size_t total, int32_t componentCount);
118
125 template <typename VertexType, typename T>
126 void bindVertexAttributeToPointer(const uint32_t index, const size_t offset) noexcept
127 {
128 bindVertexAttributeToPointer<T>(index, offset, sizeof(VertexType));
129 }
130
131 template <typename ComponentType>
132 void bindVertexAttributeToPointer(uint32_t index, size_t offset, size_t total) noexcept
133 {
134 enableVertexAttribPointer(index, offset, total, getComponentSize<ComponentType>());
135 }
136
137 protected:
138 CommonMesh(MeshUsage usage);
139
140 uint32_t _vao = 0;
141 uint32_t _vbo = 0;
142
143 MeshUsage _usage;
144
145 void _prepare_mesh_vao(const size_t vertex_size, const size_t count, const void* data, const bool editable = false);
146
147 uint32_t _prepare_mesh_ebo(const size_t size, const void* data, const bool editable = false);
148
149 };
150
155 class DynamicMesh : public CommonMesh
156 {
157 public:
161 template <typename V>
162 DynamicMesh(const V&, uint32_t max_count, const MeshUsage usage = MeshUsage::DYNAMIC):
163 CommonMesh(usage)
164 {
165 _prepare_mesh_vao(sizeof(V), max_count, nullptr, true);
166 bindVertexAttributes<V>();
167 }
168
172 template <typename V>
173 DynamicMesh(const V&, uint32_t max_vertices_count, uint32_t max_indices_count, const MeshUsage usage = MeshUsage::DYNAMIC):
174 CommonMesh(usage)
175 {
176 _prepare_mesh_vao(sizeof(V), max_vertices_count, nullptr, true);
177 _indices_size = max_indices_count;
178 _ebo = _prepare_mesh_ebo(max_indices_count * sizeof(oe::scene::index_t), nullptr, true);
179 bindVertexAttributes<V>();
180 }
181
187 void render(const uint32_t offset = 0, const uint32_t mode = 0x0004);
188
193 void renderElements(const uint32_t count, const uint32_t offset = 0, const uint32_t mode = 0x0004);
194
198 void setIndicesData(const uint32_t size, const void* data, const uint32_t offset = 0);
199
206 std::pair<void*, void*> getRawBuffers(/*, const uint32_t access = WRITE_ONLY */);
207
208 void releaseRawBuffers();
209
210 protected:
211 uint32_t _indices_size = 0;
212 uint32_t _ebo = 0;
213
214 };
215
235 class Mesh : public CommonMesh
236 {
237 public:
242 template <typename M>
243 Mesh(const M& mesh, const MeshUsage usage = MeshUsage::STATIC):
244 CommonMesh(usage)
245 {
246 const std::vector<typename M::vertex_type>& vertices = mesh.getVertices();
247
248 _prepare_mesh_vao(sizeof(typename M::vertex_type), vertices.size(), vertices.data());
249 bindVertexAttributes<typename M::vertex_type>();
250
251 for (size_t i=0; i<mesh.getSubMeshCount(); ++i)
252 {
253 const std::vector<oe::scene::index_t>& indices = mesh.getIndices(i);
254
255 _ebos.push_back({_prepare_mesh_ebo(indices.size() * sizeof(oe::scene::index_t), indices.data()), indices.size()});
256 }
257 }
258
259 ~Mesh();
260
266 void render(const uint32_t sub_mesh = 0, const uint32_t mode = 0x0004, const uint32_t offset = 0);
267
268 protected:
269 std::vector<std::pair<uint32_t, uint32_t>> _ebos;
270
271 };
272}
273
274#endif
Definition mesh.h:78
void bindVertexAttributeToPointer(const uint32_t index, const size_t offset) noexcept
Definition mesh.h:126
void setSubBufferData(const uint32_t size, const void *data, const uint32_t offset=0)
void renderArrays(const uint32_t count, const uint32_t offset=0, const uint32_t mode=0x0004)
A mesh where the vertices/indices are dynamic.
Definition mesh.h:156
void render(const uint32_t offset=0, const uint32_t mode=0x0004)
DynamicMesh(const V &, uint32_t max_count, const MeshUsage usage=MeshUsage::DYNAMIC)
Definition mesh.h:162
DynamicMesh(const V &, uint32_t max_vertices_count, uint32_t max_indices_count, const MeshUsage usage=MeshUsage::DYNAMIC)
Definition mesh.h:173
void renderElements(const uint32_t count, const uint32_t offset=0, const uint32_t mode=0x0004)
std::pair< void *, void * > getRawBuffers()
void setIndicesData(const uint32_t size, const void *data, const uint32_t offset=0)
GPU mesh wrapper.
Definition mesh.h:236
void render(const uint32_t sub_mesh=0, const uint32_t mode=0x0004, const uint32_t offset=0)
Mesh(const M &mesh, const MeshUsage usage=MeshUsage::STATIC)
Definition mesh.h:243
Prevent class to be copied.
Definition non_copyable.h:12
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures)
Definition opengl.h:10
MeshUsage
Specify how the mesh data will be used.
Definition mesh.h:66
int32_t getComponentSize() noexcept
Definition mesh.h:26
Scene related management (Render-agnostic Geometry, Manger, etc...)
Definition debug.h:19
uint32_t index_t
Definition vertex.h:17