- Added vector constructor for Mesh

- Removed MeshData type and made MeshGenerator::build require pointers to vectors
- Removed addBlockModel, added addFaces, properly handle BlockModels in MeshGenerator
- Added cleanup method & deconstructor to MeshPart
master
aurailus 2018-12-05 22:36:01 -08:00
parent a01bdedda3
commit ba4e8c6431
7 changed files with 70 additions and 212 deletions

View File

@ -57,12 +57,14 @@ void makeEntities(BlockModel* model) {
}
}
auto meshGen = new MeshGenerator();
MeshData *m = meshGen->build(array, model);
delete meshGen;
std::vector<float> vertices;
std::vector<unsigned int> indices;
MeshGenerator mg;
mg.build(array, model, vertices, indices);
Mesh* mesh = new Mesh();
mesh->create(&m->vertices[0], &m->indices[0], (int)m->vertices.size(), (int)m->indices.size());
mesh->create(&vertices, &indices);
for (int i = -16; i < 16; i++) {
for (int j = -16; j < 16; j++) {

View File

@ -3,7 +3,6 @@
//
#include "Mesh.h"
#include <iostream>
Mesh::Mesh() {
VAO = 0;
@ -12,7 +11,11 @@ Mesh::Mesh() {
indCount = 0;
}
void Mesh::create(GLfloat *vertices, unsigned int *indices, unsigned int vertCount, unsigned int indCount) {
void Mesh::create(std::vector<float>* vertices, std::vector<unsigned int>* indices) {
create(&(*vertices)[0], &(*indices)[0], (unsigned int)vertices->size(), (unsigned int)indices->size());
}
void Mesh::create(float *vertices, unsigned int *indices, unsigned int vertCount, unsigned int indCount) {
this->indCount = indCount;
glGenVertexArrays(1, &VAO);

View File

@ -6,12 +6,15 @@
#define GLPROJECT_MESH_H
#include <GL/glew.h>
#include <iostream>
#include <vector>
class Mesh {
public:
Mesh();
void create(GLfloat *vertices, unsigned int *indices, unsigned int vertCount, unsigned int indCount);
void create(std::vector<float>* vertices, std::vector<unsigned int>* indices);
void create(float *vertices, unsigned int *indices, unsigned int vertCount, unsigned int indCount);
void draw();
void cleanup();

View File

@ -7,219 +7,77 @@
#include "../engine/Timer.h"
MeshGenerator::MeshGenerator() {
vertices = new std::vector<float>;
indices = new std::vector<unsigned int>;
indOffset = 0;
}
MeshData* MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockModel* model) {
void MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockModel* model,
std::vector<float> &vertices, std::vector<unsigned int> &indices) {
auto t = Timer("MeshGen");
vertices->reserve(32768);
indices->reserve(8192);
// addBlockModel(&k, &j, &i, model);
vertices.reserve(16384);
indices.reserve(4096);
for (int i = 0; i < CHUNK_SIZE; i++) {
for (int j = 0; j < CHUNK_SIZE; j++) {
for (int k = 0; k < CHUNK_SIZE; k++) {
if (blocks[i][j][k] == 1) {
addBlockModel(&k, &j, &i, model);
// add_top_face(&k, &j, &i);
// add_bottom_face(&k, &j, &i);
//
// add_front_face(&k, &j, &i);
// add_back_face(&k, &j, &i);
//
// add_left_face(&k, &j, &i);
// add_right_face(&k, &j, &i);
if (true) //Left Faces
addFaces(k, j, i, vertices, indices, model->leftFaces);
if (true) //Right Faces
addFaces(k, j, i, vertices, indices, model->rightFaces);
if (true) //Top Faces
addFaces(k, j, i, vertices, indices, model->topFaces);
if (true) //Bottom Faces
addFaces(k, j, i, vertices, indices, model->bottomFaces);
if (true) //Front Faces
addFaces(k, j, i, vertices, indices, model->frontFaces);
if (true) //Back Faces
addFaces(k, j, i, vertices, indices, model->backFaces);
if (true) //Non-culled Faces
addFaces(k, j, i, vertices, indices, model->noCulledFaces);
}
}
}
}
vertices->shrink_to_fit();
indices->shrink_to_fit();
auto* m = new MeshData();
m->vertices = *vertices;
m->indices = *indices;
vertices.shrink_to_fit();
indices.shrink_to_fit();
t.elapsed();
return m;
}
void MeshGenerator::addBlockModel(int* x, int* y, int* z, BlockModel* model) {
for (MeshPart* mp : model->topFaces) {
MeshVertexIter* mvIterator = mp->getVertexIterator();
void MeshGenerator::addFaces(int x, int y, int z, vector<float> &vertices, vector<unsigned int> &indices, vector<MeshPart*> meshParts) {
for (MeshPart *mp : meshParts) {
MeshVertexIter *mvIterator = mp->getVertexIterator();
while (mvIterator->hasNext()) {
Vertex* vertex = mvIterator->next();
Vertex *vertex = mvIterator->next();
vertices->push_back(vertex->pos->x + *x);
vertices->push_back(vertex->pos->y + *y);
vertices->push_back(vertex->pos->z + *z);
vertices.push_back(vertex->pos->x + x);
vertices.push_back(vertex->pos->y + y);
vertices.push_back(vertex->pos->z + z);
vertices->push_back(vertex->tex->x);
vertices->push_back(vertex->tex->y);
vertices.push_back(vertex->tex->x);
vertices.push_back(vertex->tex->y);
vertices->push_back(vertex->nml->x);
vertices->push_back(vertex->nml->y);
vertices->push_back(vertex->nml->z);
vertices.push_back(vertex->nml->x);
vertices.push_back(vertex->nml->y);
vertices.push_back(vertex->nml->z);
}
MeshIndexIter* miIterator = mp->getIndexIterator();
while(miIterator->hasNext()) {
MeshIndexIter *miIterator = mp->getIndexIterator();
while (miIterator->hasNext()) {
unsigned int index = miIterator->next();
indices->push_back(indOffset + index);
indices.push_back(indOffset + index);
}
indOffset += mp->getVertexCount();
}
}
void MeshGenerator::add_vertices(std::vector<float> *verts, std::vector<unsigned int> *inds, int *x, int *y, int *z) {
//How many values there are per vertex
const unsigned int VERT_SIZE = 5;
//Total vertices in the array
int numOfVertices = (int)(verts->size() / VERT_SIZE);
glm::vec3 normal;
for (int i = 0; i < numOfVertices; i++) {
//Vertices
vertices->push_back((*verts)[i*VERT_SIZE + 0] + *x);
vertices->push_back((*verts)[i*VERT_SIZE + 1] + *y);
vertices->push_back((*verts)[i*VERT_SIZE + 2] + *z);
//TexCoords
vertices->push_back((*verts)[i*VERT_SIZE + 3]);
vertices->push_back((*verts)[i*VERT_SIZE + 4]);
//Normals
if (i % 4 == 0) {
normal = glm::triangleNormal(
glm::vec3((*verts)[(i )*VERT_SIZE + 0], (*verts)[(i )*VERT_SIZE + 1], (*verts)[(i )*VERT_SIZE + 2]),
glm::vec3((*verts)[(i+1)*VERT_SIZE + 0], (*verts)[(i+1)*VERT_SIZE + 1], (*verts)[(i+1)*VERT_SIZE + 2]),
glm::vec3((*verts)[(i+2)*VERT_SIZE + 0], (*verts)[(i+2)*VERT_SIZE + 1], (*verts)[(i+2)*VERT_SIZE + 2]));
}
vertices->push_back(normal.x);
vertices->push_back(normal.y);
vertices->push_back(normal.z);
}
for (auto i : *inds) {
indices->push_back(indOffset + i);
}
indOffset += numOfVertices;
}
void MeshGenerator::add_top_face(int *x, int *y, int *z) {
float face_vertices[] {
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f
};
unsigned int face_indices[] {
0, 1, 2, 2, 3, 0
};
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
add_vertices(&vec, &vec2, x, y, z);
}
void MeshGenerator::add_bottom_face(int *x, int *y, int *z) {
float face_vertices[] {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
};
unsigned int face_indices[] {
0, 1, 2, 2, 3, 0
};
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
add_vertices(&vec, &vec2, x, y, z);
}
void MeshGenerator::add_front_face(int *x, int *y, int *z){
float face_vertices[] {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
};
unsigned int face_indices[] {
0, 1, 2, 2, 3, 0
};
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
add_vertices(&vec, &vec2, x, y, z);
}
void MeshGenerator::add_back_face(int *x, int *y, int *z){
float face_vertices[] {
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
};
unsigned int face_indices[] {
0, 1, 2, 2, 3, 0
};
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
add_vertices(&vec, &vec2, x, y, z);
}
void MeshGenerator::add_left_face(int *x, int *y, int *z){
float face_vertices[] {
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
};
unsigned int face_indices[] {
0, 1, 2, 2, 3, 0
};
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
add_vertices(&vec, &vec2, x, y, z);
}
void MeshGenerator::add_right_face(int *x, int *y, int *z){
float face_vertices[] {
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
};
unsigned int face_indices[] {
0, 1, 2, 2, 3, 0
};
std::vector<float> vec(std::begin(face_vertices), std::end(face_vertices));
std::vector<unsigned int> vec2(std::begin(face_indices), std::end(face_indices));
add_vertices(&vec, &vec2, x, y, z);
}
void MeshGenerator::cleanup() {
}

View File

@ -18,35 +18,16 @@ struct MeshData;
class MeshGenerator {
public:
MeshGenerator();
MeshData* build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockModel* model);
void build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockModel* model,
std::vector<float> &vertices, std::vector<unsigned int> &indices);
~MeshGenerator();
private:
std::vector<float>* vertices;
std::vector<unsigned int>* indices;
unsigned int indOffset;
//TODO:: Replace the pointers to ints to non-pointer shorts(?)
void addBlockModel(int* x, int* y, int* z, BlockModel* model);
void add_vertices(std::vector<float> *verts, std::vector<unsigned int> *inds, int *x, int *y, int *z);
void add_top_face(int *x, int *y, int *z);
void add_bottom_face(int *x, int *y, int *z);
void add_front_face(int *x, int *y, int *z);
void add_back_face(int *x, int *y, int *z);
void add_left_face(int *x, int *y, int *z);
void add_right_face(int *x, int *y, int *z);
void addFaces(int x, int y, int z, std::vector<float> &vertices, std::vector<unsigned int> &indices, vector<MeshPart*>);
void cleanup();
};
//Temporary class for storing Vertices and Indices.
//TODO: Replace this class with pointers to new Vertices and Indices in the build method.
struct MeshData {
std::vector<float> vertices;
std::vector<unsigned int> indices;
};
#endif //GLPROJECT_MESHGENERATOR_H

View File

@ -85,6 +85,15 @@ MeshIndexIter* MeshPart::getIndexIterator() {
return new MeshIndexIter(this);
}
void MeshPart::cleanup() {
delete [] vertices;
delete [] indices;
}
MeshPart::~MeshPart() {
cleanup();
}
//Print information about this MeshPart into std::cout.
void MeshPart::debug() {
std::cout << "Debugging MeshPart" << std::endl << "Vertices:" << std::endl;

View File

@ -30,12 +30,14 @@ public:
MeshIndexIter* getIndexIterator();
void debug();
void cleanup();
~MeshPart();
private:
void construct(Vertex* vertices, int vSize, unsigned int* indices, int iSize, const char* texture, MeshMod meshMod, float modValue);
float modValue;
//TODO: Free these values from memory
Vertex* vertices;
int vSize;
unsigned int* indices;