Add gfx::mesh::load_raw for loading buffers from raw data
This commit is contained in:
parent
e4f0675a1d
commit
697de5c422
2 changed files with 61 additions and 32 deletions
|
|
@ -121,6 +121,8 @@ namespace psemek::gfx
|
|||
|
||||
// Non-indexed vertex data
|
||||
|
||||
void load_raw(void const * vertices, std::size_t vertex_size, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW);
|
||||
|
||||
template <typename Vertex>
|
||||
void load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW);
|
||||
|
||||
|
|
@ -135,6 +137,10 @@ namespace psemek::gfx
|
|||
|
||||
// Indexed vertex data
|
||||
|
||||
void load_raw(void const * vertices, std::size_t vertex_size, std::size_t vertex_count,
|
||||
void const * indices, GLenum index_type, std::size_t index_count,
|
||||
GLenum primitive_type, GLenum usage = gl::STREAM_DRAW);
|
||||
|
||||
template <typename Vertex, typename Index>
|
||||
void load(Vertex const * vertices, std::size_t vertex_count, Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage = gl::STREAM_DRAW);
|
||||
|
||||
|
|
@ -229,18 +235,7 @@ namespace psemek::gfx
|
|||
template <typename Vertex>
|
||||
void mesh::load(Vertex const * vertices, std::size_t count, GLenum primitive_type, GLenum usage)
|
||||
{
|
||||
assert(info_.stride_ == sizeof(Vertex));
|
||||
|
||||
if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n)
|
||||
assert((count % (*n)) == 0);
|
||||
|
||||
assert(vertex_buffer_);
|
||||
|
||||
vertex_buffer_.load(vertices, count, usage);
|
||||
info_.vertex_count_ = count;
|
||||
info_.index_count_ = 0;
|
||||
info_.indexed_ = false;
|
||||
info_.primitive_type_ = primitive_type;
|
||||
load_raw(vertices, sizeof(Vertex), count, primitive_type, usage);
|
||||
}
|
||||
|
||||
template <typename Vertex>
|
||||
|
|
@ -265,26 +260,7 @@ namespace psemek::gfx
|
|||
template <typename Vertex, typename Index>
|
||||
void mesh::load(Vertex const * vertices, std::size_t vertex_count, Index const * indices, std::size_t index_count, GLenum primitive_type, GLenum usage)
|
||||
{
|
||||
assert(info_.stride_ == sizeof(Vertex));
|
||||
|
||||
if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n)
|
||||
assert((index_count % (*n)) == 0);
|
||||
|
||||
assert(vertex_buffer_);
|
||||
|
||||
if (!index_buffer_)
|
||||
{
|
||||
index_buffer_ = buffer{};
|
||||
array_.bind();
|
||||
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, index_buffer_.id());
|
||||
}
|
||||
vertex_buffer_.load(vertices, vertex_count, usage);
|
||||
index_buffer_.load(indices, index_count, usage);
|
||||
info_.vertex_count_ = vertex_count;
|
||||
info_.index_count_ = index_count;
|
||||
info_.indexed_ = true;
|
||||
info_.primitive_type_ = primitive_type;
|
||||
info_.index_type_ = detail::index_type_to_gl_enum_v<Index>;
|
||||
load_raw(vertices, sizeof(Vertex), vertex_count, indices, detail::index_type_to_gl_enum_v<Index>, index_count, primitive_type, usage);
|
||||
}
|
||||
|
||||
template <typename Vertex, typename Index>
|
||||
|
|
|
|||
|
|
@ -89,6 +89,59 @@ namespace psemek::gfx
|
|||
}
|
||||
}
|
||||
|
||||
void mesh::load_raw(void const * vertices, std::size_t vertex_size, std::size_t count, GLenum primitive_type, GLenum usage)
|
||||
{
|
||||
assert(info_.stride_ == vertex_size);
|
||||
|
||||
if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n)
|
||||
assert((count % (*n)) == 0);
|
||||
|
||||
assert(vertex_buffer_);
|
||||
|
||||
vertex_buffer_.load(vertices, vertex_size * count, usage);
|
||||
info_.vertex_count_ = count;
|
||||
info_.index_count_ = 0;
|
||||
info_.indexed_ = false;
|
||||
info_.primitive_type_ = primitive_type;
|
||||
}
|
||||
|
||||
static std::size_t index_size(GLenum type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case gl::UNSIGNED_BYTE: return 1;
|
||||
case gl::UNSIGNED_SHORT: return 2;
|
||||
case gl::UNSIGNED_INT: return 4;
|
||||
default: throw std::runtime_error("Unknown undex type");
|
||||
}
|
||||
}
|
||||
|
||||
void mesh::load_raw(void const * vertices, std::size_t vertex_size, std::size_t vertex_count,
|
||||
void const * indices, GLenum index_type, std::size_t index_count,
|
||||
GLenum primitive_type, GLenum usage)
|
||||
{
|
||||
assert(info_.stride_ == vertex_size);
|
||||
|
||||
if (auto n = detail::get_primitive_type_vertex_count(primitive_type); n)
|
||||
assert((index_count % (*n)) == 0);
|
||||
|
||||
assert(vertex_buffer_);
|
||||
|
||||
if (!index_buffer_)
|
||||
{
|
||||
index_buffer_ = buffer{};
|
||||
array_.bind();
|
||||
gl::BindBuffer(gl::ELEMENT_ARRAY_BUFFER, index_buffer_.id());
|
||||
}
|
||||
vertex_buffer_.load(vertices, vertex_size * vertex_count, usage);
|
||||
index_buffer_.load(indices, index_size(index_type) * index_count, usage);
|
||||
info_.vertex_count_ = vertex_count;
|
||||
info_.index_count_ = index_count;
|
||||
info_.indexed_ = true;
|
||||
info_.primitive_type_ = primitive_type;
|
||||
info_.index_type_ = index_type;
|
||||
}
|
||||
|
||||
void mesh::draw() const
|
||||
{
|
||||
draw(0, is_indexed() ? index_count() : vertex_count(), instance_count());
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue