1#ifndef OE_RENDER_MESH_H
2#define OE_RENDER_MESH_H
4#include <glue_chan/glue_chan.h>
7#include "../scene/mesh.h"
8#include "../core/renderer/opengl.h"
14 uint32_t& vertex_array;
25 void enableVertexAttribPointer(
const uint32_t index,
const size_t offset,
const int32_t componentCount)
28 glEnableVertexArrayAttrib(vertex_array, index);
29 glVertexArrayAttribFormat(vertex_array, index, componentCount, GL_FLOAT,
false, offset);
30 glVertexArrayAttribBinding(vertex_array, index, 0);
33 void enableVertexAttribIPointer(
const uint32_t index,
const size_t offset,
const int32_t componentCount)
36 glEnableVertexArrayAttrib(vertex_array, index);
37 glVertexArrayAttribIFormat(vertex_array, index, componentCount, GL_INT, offset);
38 glVertexArrayAttribBinding(vertex_array, index, 0);
55 template <typename VertexType, typename T>
67 template <
typename VertexType,
typename T>
87 template <
typename SceneMeshType>
88 Mesh(
const SceneMeshType& scene_mesh,
bool editable =
false):
90 _index_size(sizeof(typename SceneMeshType::index_type))
94 const auto& vertices = scene_mesh.vertices;
95 const auto& indices = scene_mesh.indices;
97 _common_construct<SceneMeshType>(
98 vertices.size(), vertices.data(),
99 indices.size(), indices.data(),
110 template <
typename SceneMeshType>
111 Mesh(
const SceneMeshType&, uint32_t max_vertices_count, uint32_t max_indices_count):
112 _index_size(sizeof(typename SceneMeshType::index_type))
116 _common_construct<SceneMeshType>(
117 max_vertices_count,
nullptr,
118 max_indices_count,
nullptr,
124 Mesh& operator=(
const Mesh&) =
delete;
128 _index_type(other._index_type),
129 _index_size(other._index_size),
143 const uint32_t buffers[2] = {_vbo, _ebo};
144 glDeleteBuffers(2, buffers);
146 glDeleteVertexArrays(1, &_vao);
155 glNamedBufferSubData(_vbo, offset, size, data);
161 void setIndicesData(
const uint32_t size,
const void* data,
const uint32_t offset = 0)
163 glNamedBufferSubData(_ebo, offset, size, data);
175 glBindVertexArray(_vao);
179 glMapNamedBuffer(_vbo, GL_WRITE_ONLY),
180 glMapNamedBuffer(_ebo, GL_WRITE_ONLY)
191 glUnmapNamedBuffer(_vbo);
192 glUnmapNamedBuffer(_ebo);
202 void renderArrays(
const uint32_t count,
const uint32_t offset, uint32_t mode)
204 glBindVertexArray(_vao);
205 glDrawArrays(mode, offset, count);
215 void renderElements(
const uint32_t count,
const uint32_t offset,
const uint32_t mode)
217 glBindVertexArray(_vao);
218 glDrawElements(mode, count, GL_UNSIGNED_INT,
reinterpret_cast<uint32_t*
>(offset));
228 assert(
primitives.size() &&
"If this triggers, you forgot to add primitives to mesh, maybe you meant to renderArrays instead?");
229 assert(activeprimitives.size() ==
primitives.size());
232 std::vector<oe::scene::Primitive> primitive_render_calls;
235 bool last_primitive_active =
false;
238 for (
const auto& is_active : activeprimitives)
240 const auto& primitive_data =
primitives.at(i);
242 if (last_primitive_active)
246 current_packed_primitive.size += primitive_data.size;
250 primitive_render_calls.push_back(current_packed_primitive);
252 current_packed_primitive.offset = 0;
253 current_packed_primitive.size = 0;
260 current_packed_primitive.offset = primitive_data.offset;
261 current_packed_primitive.size = primitive_data.size;
265 last_primitive_active = is_active;
270 if (last_primitive_active)
273 primitive_render_calls.push_back(current_packed_primitive);
276 glBindVertexArray(_vao);
278 for (
const auto& it : primitive_render_calls)
280 glDrawElements(mode, it.size, _index_type,
reinterpret_cast<void*
>(it.offset * _index_size));
290 assert(
primitives.size() &&
"If this triggers, you forgot to add primitives to mesh, maybe you meant to renderArrays instead?");
292 glBindVertexArray(_vao);
294 size_t total_size = 0;
298 total_size += it.size;
301 glDrawElements(mode, total_size, _index_type,
nullptr);
310 uint32_t _index_type = 0;
311 uint8_t _index_size = 0;
317 template <
typename SceneMeshType>
318 void _common_construct(
319 const size_t vertices_count,
const void* vertices_data,
320 const size_t indices_count,
const void* indices_data,
323 _index_type = (_index_size == 1) ? GL_UNSIGNED_BYTE
324 : (_index_size == 2) ? GL_UNSIGNED_SHORT
328 glCreateVertexArrays(1, &_vao);
330 std::array<uint32_t, 2> buffers;
332 glCreateBuffers(2, buffers.data());
340 size_t vertex_size =
sizeof(
typename SceneMeshType::vertex_type);
344 glNamedBufferStorage(_vbo, vertex_size * vertices_count, vertices_data, GL_DYNAMIC_STORAGE_BIT | GL_MAP_WRITE_BIT);
345 glNamedBufferStorage(_ebo, _index_size * indices_count, indices_data, GL_DYNAMIC_STORAGE_BIT | GL_MAP_WRITE_BIT);
349 glNamedBufferStorage(_vbo, vertex_size * vertices_count, vertices_data, 0);
350 glNamedBufferStorage(_ebo, _index_size * indices_count, indices_data, 0);
353 glVertexArrayVertexBuffer(_vao, 0, _vbo, 0, vertex_size);
354 glVertexArrayElementBuffer(_vao, _ebo);
static bool hasContext()
Check if this thread has an OpenGL context.
Definition opengl.h:88
void renderAll(uint32_t mode=default_mesh_rendering_mode)
Definition mesh.h:288
std::pair< void *, void * > getRawBuffers()
Get raw GPU buffers of the mesh (to write data).
Definition mesh.h:173
void setIndicesData(const uint32_t size, const void *data, const uint32_t offset=0)
Definition mesh.h:161
void setSubBufferData(const uint32_t size, const void *data, const uint32_t offset=0)
Definition mesh.h:153
Mesh(const SceneMeshType &, uint32_t max_vertices_count, uint32_t max_indices_count)
Allocate a mesh to be dynamically filled.
Definition mesh.h:111
Mesh(const SceneMeshType &scene_mesh, bool editable=false)
Construct a mesh from an existing scene mesh.
Definition mesh.h:88
void render(const std::vector< bool > &activeprimitives, uint32_t mode=default_mesh_rendering_mode)
Ask the GPU to render specific primitives of the mesh.
Definition mesh.h:226
void renderElements(const uint32_t count, const uint32_t offset, const uint32_t mode)
Definition mesh.h:215
void renderArrays(const uint32_t count, const uint32_t offset, uint32_t mode)
Definition mesh.h:202
void releaseRawBuffers()
Release raw buffers got by getRawBuffers.
Definition mesh.h:189
std::vector< oe::scene::Primitive > primitives
mesh parts
Definition mesh.h:307
Render related abstractions (Shader, Framebuffer, Cubemaps, Textures).
Definition opengl.h:12
constexpr const uint8_t default_mesh_rendering_mode
Default way to render the mesh vertices as filled triangles.
Definition mesh.h:77
int32_t getComponentSize() noexcept
Return the number of components in T.
void bindVertexAttributeToIPointer(const uint32_t index, const size_t offset) noexcept
Enable and bind a vertex attribute of type T to the location, and fill value from offset in structure...
Definition mesh.h:68
void bindVertexAttributeToPointer(const uint32_t index, const size_t offset) noexcept
Enable and bind a vertex attribute of type T to the location, and fill value from offset in structure...
Definition mesh.h:56
void bindVertexAttributes()
Bind vertex array attributes to stored vertex array based on the vertex type.
Part of a mesh (used to separate mesh materials for example).
Definition mesh.h:20