summaryrefslogtreecommitdiff
path: root/mesh.cpp
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2021-01-17 18:54:26 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2021-01-17 18:54:26 +0000
commit43a87590f45aa6e55724d30d0c2d0d34b407a57e (patch)
tree21ce8e8886f8aa58b159419b7d885f57d9a37580 /mesh.cpp
parentInitial commit (diff)
downloadilt-43a87590f45aa6e55724d30d0c2d0d34b407a57e.tar.bz2
ilt-43a87590f45aa6e55724d30d0c2d0d34b407a57e.tar.xz
ilt-43a87590f45aa6e55724d30d0c2d0d34b407a57e.zip
First cut modernizing and sanitizing
Diffstat (limited to 'mesh.cpp')
-rw-r--r--mesh.cpp63
1 files changed, 29 insertions, 34 deletions
diff --git a/mesh.cpp b/mesh.cpp
index 19f3a82..8c304b3 100644
--- a/mesh.cpp
+++ b/mesh.cpp
@@ -1,43 +1,36 @@
#include "mesh.h"
-#include "debugTimer.h"
-#include "util.h"
-#include <algorithm>
-#include <fstream>
-#include <iostream>
-#include <map>
-#include <stdlib.h>
-
-Mesh::Mesh(const std::string & fileName)
-{
- InitMesh(OBJModel(fileName).ToIndexedModel());
-}
+#include "obj_loader.h"
+#include "vertex.hpp"
+#include <glm/glm.hpp>
+#include <memory>
+#include <vector>
-void
-Mesh::InitMesh(const IndexedModel & model)
-{
- m_numIndices = model.indices.size();
+Mesh::Mesh(const std::string & fileName) : Mesh(OBJModel(fileName).ToIndexedModel()) { }
+Mesh::Mesh(const IndexedModel & model) :
+ m_vertexArrayObject {}, m_vertexArrayBuffers {}, m_numIndices {model.indices.size()}
+{
glGenVertexArrays(1, &m_vertexArrayObject);
glBindVertexArray(m_vertexArrayObject);
- glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
+ glGenBuffers(NUM_BUFFERS, m_vertexArrayBuffers.data());
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[POSITION_VB]);
glBufferData(
GL_ARRAY_BUFFER, sizeof(model.positions[0]) * model.positions.size(), &model.positions[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[TEXCOORD_VB]);
glBufferData(
GL_ARRAY_BUFFER, sizeof(model.texCoords[0]) * model.texCoords.size(), &model.texCoords[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
+ glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexArrayBuffers[NORMAL_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(model.normals[0]) * model.normals.size(), &model.normals[0], GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
- glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
+ glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, nullptr);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_vertexArrayBuffers[INDEX_VB]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(model.indices[0]) * model.indices.size(), &model.indices[0],
@@ -46,25 +39,28 @@ Mesh::InitMesh(const IndexedModel & model)
glBindVertexArray(0);
}
-Mesh::Mesh(Vertex * vertices, unsigned int numVertices, unsigned int * indices, unsigned int numIndices)
-{
- IndexedModel model;
+Mesh::Mesh(Vertex * vertices, unsigned int numVertices, unsigned int * indices, unsigned int numIndices) :
+ Mesh {[vertices, numVertices, indices, numIndices]() {
+ IndexedModel model;
- for (unsigned int i = 0; i < numVertices; i++) {
- model.positions.push_back(*vertices[i].GetPos());
- model.texCoords.push_back(*vertices[i].GetTexCoord());
- model.normals.push_back(*vertices[i].GetNormal());
- }
+ for (unsigned int i = 0; i < numVertices; i++) {
+ model.positions.push_back(vertices[i].GetPos());
+ model.texCoords.push_back(vertices[i].GetTexCoord());
+ model.normals.push_back(vertices[i].GetNormal());
+ }
- for (unsigned int i = 0; i < numIndices; i++)
- model.indices.push_back(indices[i]);
+ for (unsigned int i = 0; i < numIndices; i++) {
+ model.indices.push_back(indices[i]);
+ }
- InitMesh(model);
+ return model;
+ }()}
+{
}
Mesh::~Mesh()
{
- glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers);
+ glDeleteBuffers(NUM_BUFFERS, m_vertexArrayBuffers.data());
glDeleteVertexArrays(1, &m_vertexArrayObject);
}
@@ -73,8 +69,7 @@ Mesh::Draw()
{
glBindVertexArray(m_vertexArrayObject);
- // glDrawElements(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, 0);
- glDrawElementsBaseVertex(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, 0, 0);
+ glDrawElementsBaseVertex(GL_TRIANGLES, m_numIndices, GL_UNSIGNED_INT, nullptr, 0);
glBindVertexArray(0);
}