Cleanup Mesh class
This commit is contained in:
parent
2843761be0
commit
d09b53b5c0
3 changed files with 38 additions and 35 deletions
|
@ -40,20 +40,33 @@ Mesh::Mesh(vector<float> vertices, vector<uint16_t> indices, VertexAttributes at
|
|||
if (attributes.stride == 0) {
|
||||
throw invalid_argument("stride in attributes must not be zero");
|
||||
}
|
||||
_vertexCount = _vertices.size() / attributes.stride / sizeof(float);
|
||||
_vertexCount = _vertices.size() / (attributes.stride / sizeof(float));
|
||||
|
||||
computeAABB();
|
||||
}
|
||||
|
||||
void Mesh::computeAABB() {
|
||||
_aabb.reset();
|
||||
|
||||
auto coordsPtr = reinterpret_cast<uint8_t *>(&_vertices[0]) + _attributes.offCoords;
|
||||
|
||||
for (size_t i = 0; i < _vertexCount; ++i) {
|
||||
_aabb.expand(glm::make_vec3(reinterpret_cast<const float *>(coordsPtr)));
|
||||
coordsPtr += _attributes.stride;
|
||||
}
|
||||
}
|
||||
|
||||
void Mesh::init() {
|
||||
if (_inited) return;
|
||||
|
||||
glGenBuffers(1, &_vbo);
|
||||
glGenBuffers(1, &_ibo);
|
||||
glGenBuffers(1, &_vboId);
|
||||
glGenBuffers(1, &_iboId);
|
||||
|
||||
glGenVertexArrays(1, &_vao);
|
||||
glBindVertexArray(_vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
||||
glGenVertexArrays(1, &_vaoId);
|
||||
glBindVertexArray(_vaoId);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, _vboId);
|
||||
glBufferData(GL_ARRAY_BUFFER, _vertices.size() * sizeof(float), &_vertices[0], GL_STATIC_DRAW);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ibo);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _iboId);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, _indices.size() * sizeof(uint16_t), &_indices[0], GL_STATIC_DRAW);
|
||||
|
||||
if (_attributes.offCoords != -1) {
|
||||
|
@ -99,46 +112,38 @@ Mesh::~Mesh() {
|
|||
}
|
||||
|
||||
void Mesh::deinit() {
|
||||
if (_inited) {
|
||||
glDeleteVertexArrays(1, &_vao);
|
||||
glDeleteBuffers(1, &_ibo);
|
||||
glDeleteBuffers(1, &_vbo);
|
||||
_inited = false;
|
||||
}
|
||||
if (!_inited) return;
|
||||
|
||||
glDeleteVertexArrays(1, &_vaoId);
|
||||
glDeleteBuffers(1, &_iboId);
|
||||
glDeleteBuffers(1, &_vboId);
|
||||
|
||||
_inited = false;
|
||||
}
|
||||
|
||||
static GLenum getModeGL(Mesh::DrawMode mode) {
|
||||
switch (mode) {
|
||||
case Mesh::DrawMode::Lines:
|
||||
return GL_LINES;
|
||||
case Mesh::DrawMode::Triangles:
|
||||
return GL_TRIANGLES;
|
||||
case Mesh::DrawMode::TriangleStrip:
|
||||
return GL_TRIANGLE_STRIP;
|
||||
default:
|
||||
return GL_TRIANGLES;
|
||||
throw invalid_argument("Unsupported draw mode: " + to_string(static_cast<int>(mode)));
|
||||
}
|
||||
}
|
||||
|
||||
void Mesh::draw() {
|
||||
glBindVertexArray(_vao);
|
||||
glBindVertexArray(_vaoId);
|
||||
glDrawElements(getModeGL(_mode), static_cast<GLsizei>(_indices.size()), GL_UNSIGNED_SHORT, nullptr);
|
||||
}
|
||||
|
||||
void Mesh::drawInstanced(int count) {
|
||||
glBindVertexArray(_vao);
|
||||
glBindVertexArray(_vaoId);
|
||||
glDrawElementsInstanced(getModeGL(_mode), static_cast<GLsizei>(_indices.size()), GL_UNSIGNED_SHORT, nullptr, count);
|
||||
}
|
||||
|
||||
void Mesh::computeAABB() {
|
||||
_aabb.reset();
|
||||
|
||||
auto coordsPtr = reinterpret_cast<uint8_t *>(&_vertices[0]) + _attributes.offCoords;
|
||||
|
||||
for (size_t i = 0; i < _vertexCount; ++i) {
|
||||
_aabb.expand(glm::make_vec3(reinterpret_cast<const float *>(coordsPtr)));
|
||||
coordsPtr += _attributes.stride;
|
||||
}
|
||||
}
|
||||
|
||||
glm::vec2 Mesh::getFaceCenterUV(int faceIdx) const {
|
||||
if (_mode != DrawMode::Triangles) {
|
||||
throw logic_error("Unsupported draw mode: " + to_string(static_cast<int>(_mode)));
|
||||
|
|
|
@ -31,8 +31,7 @@ namespace reone {
|
|||
namespace graphics {
|
||||
|
||||
/**
|
||||
* Polygonal mesh, containing vertex and index data. Renders itself,
|
||||
* but does not manage textures and shaders.
|
||||
* Polygon mesh. Consists of vertices and faces.
|
||||
*/
|
||||
class Mesh : boost::noncopyable {
|
||||
public:
|
||||
|
@ -51,8 +50,6 @@ public:
|
|||
void draw();
|
||||
void drawInstanced(int count);
|
||||
|
||||
void computeAABB();
|
||||
|
||||
/**
|
||||
* Used for lightmapping grass.
|
||||
*
|
||||
|
@ -78,11 +75,13 @@ private:
|
|||
|
||||
// OpenGL
|
||||
|
||||
uint32_t _vbo { 0 };
|
||||
uint32_t _ibo { 0 };
|
||||
uint32_t _vao { 0 };
|
||||
uint32_t _vboId { 0 };
|
||||
uint32_t _iboId { 0 };
|
||||
uint32_t _vaoId { 0 };
|
||||
|
||||
// END OpenGL
|
||||
|
||||
void computeAABB();
|
||||
};
|
||||
|
||||
} // namespace graphics
|
||||
|
|
|
@ -985,7 +985,6 @@ MdlReader::MeshHeader MdlReader::readMeshHeader() {
|
|||
|
||||
void MdlReader::loadMesh(const MeshHeader &header, vector<float> &&vertices, vector<uint16_t> &&indices, VertexAttributes &&attributes, ModelNode &node) {
|
||||
auto mesh = make_unique<Mesh>(vertices, indices, attributes);
|
||||
mesh->computeAABB();
|
||||
|
||||
node._mesh = make_unique<ModelNode::Trimesh>();
|
||||
node._mesh->mesh = move(mesh);
|
||||
|
|
Loading…
Reference in a new issue