diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-01-31 13:54:54 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2021-02-04 19:29:49 +0000 |
commit | eb9369ff236d4be52fe5c1e4d2331007b54f7177 (patch) | |
tree | f0750889075b8f46f592a4e74fe5d02fe4324b6a /gfx/models/mesh.cpp | |
parent | Collection can be shared/unique pointers (diff) | |
download | ilt-eb9369ff236d4be52fe5c1e4d2331007b54f7177.tar.bz2 ilt-eb9369ff236d4be52fe5c1e4d2331007b54f7177.tar.xz ilt-eb9369ff236d4be52fe5c1e4d2331007b54f7177.zip |
Create mesh direct from vertices and indices
Diffstat (limited to 'gfx/models/mesh.cpp')
-rw-r--r-- | gfx/models/mesh.cpp | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/gfx/models/mesh.cpp b/gfx/models/mesh.cpp index 967f3fd..90de8db 100644 --- a/gfx/models/mesh.cpp +++ b/gfx/models/mesh.cpp @@ -1,7 +1,6 @@ #include "mesh.h"
#include "obj_loader.h"
#include "vertex.hpp"
-#include <glm/glm.hpp>
#include <memory>
#include <vector>
@@ -39,23 +38,30 @@ Mesh::Mesh(const IndexedModel & model) : glBindVertexArray(0);
}
-Mesh::Mesh(Vertex * vertices, unsigned int numVertices, unsigned int * indices, unsigned int numIndices) :
- Mesh {[vertices, numVertices, indices, numIndices]() {
- IndexedModel model;
+Mesh::Mesh(std::span<Vertex> vertices, std::span<unsigned int> indices) :
+ m_vertexArrayObject {}, m_vertexArrayBuffers {}, m_numIndices {indices.size()}
+{
+ glGenVertexArrays(1, &m_vertexArrayObject);
+ glBindVertexArray(m_vertexArrayObject);
- for (unsigned int i = 0; i < numVertices; i++) {
- model.positions.push_back(vertices[i].pos);
- model.texCoords.push_back(vertices[i].texCoord);
- model.normals.push_back(vertices[i].normal);
- }
+ glGenBuffers(2, m_vertexArrayBuffers.data());
- for (unsigned int i = 0; i < numIndices; i++) {
- model.indices.push_back(indices[i]);
- }
+ glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[0]);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
- return model;
- }()}
-{
+ glEnableVertexAttribArray(0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, pos));
+
+ glEnableVertexAttribArray(1);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, texCoord));
+
+ glEnableVertexAttribArray(2);
+ glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, normal));
+
+ glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[1]);
+ glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), indices.data(), GL_STATIC_DRAW);
+
+ glBindVertexArray(0);
}
Mesh::~Mesh()
|