psemek/libs/gfx/source/mesh.cpp

313 lines
6.6 KiB
C++

#include <psemek/gfx/mesh.hpp>
#include <psemek/util/to_string.hpp>
namespace psemek::gfx
{
namespace detail
{
void check_vertex_size(std::size_t vertex_size, std::size_t stride)
{
if (vertex_size != stride)
throw std::runtime_error(util::to_string("Vertex size (", vertex_size, ") not equal to sum of attribute sizes (", stride, ")"));
}
void check_instance_size(std::size_t instance_size, std::size_t stride)
{
if (instance_size != stride)
throw std::runtime_error(util::to_string("Instance size (", instance_size, ") not equal to sum of instanced attribute sizes (", stride, ")"));
}
}
mesh mesh::null()
{
return mesh(0);
}
mesh::mesh()
{
gl::GenVertexArrays(1, &array_);
gl::GenBuffers(1, &buffer_);
}
mesh::mesh(mesh && other)
{
array_ = other.array_;
buffer_ = other.buffer_;
count_ = other.count_;
stride_ = other.stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.count_ = 0;
other.stride_ = 0;
}
mesh & mesh::operator =(mesh && other)
{
if (this == &other) return *this;
if (array_)
{
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
}
array_ = other.array_;
buffer_ = other.buffer_;
count_ = other.count_;
stride_ = other.stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.count_ = 0;
other.stride_ = 0;
return *this;
}
mesh::~mesh()
{
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
}
mesh::mesh(int)
{
array_ = 0;
buffer_ = 0;
}
indexed_mesh indexed_mesh::null()
{
return indexed_mesh(0);
}
indexed_mesh::indexed_mesh()
{
gl::GenVertexArrays(1, &array_);
gl::GenBuffers(1, &buffer_);
gl::GenBuffers(1, &index_buffer_);
}
indexed_mesh::indexed_mesh(indexed_mesh && other)
{
array_ = other.array_;
buffer_ = other.buffer_;
index_buffer_ = other.index_buffer_;
count_ = other.count_;
index_type_ = other.index_type_;
stride_ = other.stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.index_buffer_ = 0;
other.count_ = 0;
other.index_type_ = 0;
other.stride_ = 0;
}
indexed_mesh & indexed_mesh::operator =(indexed_mesh && other)
{
if (this == &other) return *this;
if (array_)
{
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
gl::DeleteBuffers(1, &index_buffer_);
}
array_ = other.array_;
buffer_ = other.buffer_;
index_buffer_ = other.index_buffer_;
count_ = other.count_;
index_type_ = other.index_type_;
stride_ = other.stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.index_buffer_ = 0;
other.count_ = 0;
other.index_type_ = 0;
other.stride_ = 0;
return *this;
}
indexed_mesh::~indexed_mesh()
{
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
gl::DeleteBuffers(1, &index_buffer_);
}
indexed_mesh::indexed_mesh(int)
{
array_ = 0;
buffer_ = 0;
index_buffer_ = 0;
}
instanced_mesh instanced_mesh::null()
{
return instanced_mesh(0);
}
instanced_mesh::instanced_mesh()
{
gl::GenVertexArrays(1, &array_);
gl::GenBuffers(1, &buffer_);
gl::GenBuffers(1, &instance_buffer_);
}
instanced_mesh::instanced_mesh(instanced_mesh && other)
{
array_ = other.array_;
buffer_ = other.buffer_;
instance_buffer_ = other.instance_buffer_;
count_ = other.count_;
instance_count_ = other.instance_count_;
stride_ = other.stride_;
instance_stride_ = other.instance_stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.instance_buffer_ = 0;
other.count_ = 0;
other.instance_count_ = 0;
other.stride_ = 0;
other.instance_stride_ = 0;
}
instanced_mesh & instanced_mesh::operator = (instanced_mesh && other)
{
if (this == &other)
return *this;
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
gl::DeleteBuffers(1, &instance_buffer_);
array_ = other.array_;
buffer_ = other.buffer_;
instance_buffer_ = other.instance_buffer_;
count_ = other.count_;
instance_count_ = other.instance_count_;
stride_ = other.stride_;
instance_stride_ = other.instance_stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.instance_buffer_ = 0;
other.count_ = 0;
other.instance_count_ = 0;
other.stride_ = 0;
other.instance_stride_ = 0;
return *this;
}
instanced_mesh::~instanced_mesh()
{
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
gl::DeleteBuffers(1, &instance_buffer_);
}
instanced_mesh::instanced_mesh(int)
{
array_ = 0;
buffer_ = 0;
instance_buffer_ = 0;
}
instanced_indexed_mesh instanced_indexed_mesh::null()
{
return instanced_indexed_mesh(0);
}
instanced_indexed_mesh::instanced_indexed_mesh()
{
gl::GenVertexArrays(1, &array_);
gl::GenBuffers(1, &buffer_);
gl::GenBuffers(1, &index_buffer_);
gl::GenBuffers(1, &instance_buffer_);
}
instanced_indexed_mesh::instanced_indexed_mesh(instanced_indexed_mesh && other)
{
array_ = other.array_;
buffer_ = other.buffer_;
instance_buffer_ = other.instance_buffer_;
count_ = other.count_;
instance_count_ = other.instance_count_;
stride_ = other.stride_;
instance_stride_ = other.instance_stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.instance_buffer_ = 0;
other.count_ = 0;
other.instance_count_ = 0;
other.stride_ = 0;
other.instance_stride_ = 0;
}
instanced_indexed_mesh & instanced_indexed_mesh::operator = (instanced_indexed_mesh && other)
{
if (this == &other)
return *this;
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
gl::DeleteBuffers(1, &index_buffer_);
gl::DeleteBuffers(1, &instance_buffer_);
array_ = other.array_;
buffer_ = other.buffer_;
instance_buffer_ = other.instance_buffer_;
count_ = other.count_;
instance_count_ = other.instance_count_;
stride_ = other.stride_;
instance_stride_ = other.instance_stride_;
primitive_type_ = other.primitive_type_;
other.array_ = 0;
other.buffer_ = 0;
other.instance_buffer_ = 0;
other.count_ = 0;
other.instance_count_ = 0;
other.stride_ = 0;
other.instance_stride_ = 0;
return *this;
}
instanced_indexed_mesh::~instanced_indexed_mesh()
{
gl::DeleteVertexArrays(1, &array_);
gl::DeleteBuffers(1, &buffer_);
gl::DeleteBuffers(1, &index_buffer_);
gl::DeleteBuffers(1, &instance_buffer_);
}
instanced_indexed_mesh::instanced_indexed_mesh(int)
{
array_ = 0;
buffer_ = 0;
index_buffer_ = 0;
instance_buffer_ = 0;
}
}