Mesh Generator Class
parent
ad936ba4fb
commit
10351c5269
|
@ -14,7 +14,7 @@ find_package(glfw3 REQUIRED)
|
|||
link_directories(GlProject/Libraries/glew/lib)
|
||||
|
||||
add_executable(GlProject
|
||||
GlProject/Main.cpp GlProject/Mesh.cpp GlProject/Mesh.h GlProject/Entity.cpp GlProject/Entity.h GlProject/Shader.cpp GlProject/Shader.h GlProject/Window.cpp GlProject/Window.h GlProject/Camera.cpp GlProject/Camera.h GlProject/Texture.cpp GlProject/Texture.h)
|
||||
GlProject/Main.cpp GlProject/Mesh.cpp GlProject/Mesh.h GlProject/Entity.cpp GlProject/Entity.h GlProject/Shader.cpp GlProject/Shader.h GlProject/Window.cpp GlProject/Window.h GlProject/Camera.cpp GlProject/Camera.h GlProject/Texture.cpp GlProject/Texture.h GlProject/MeshGenerator.cpp GlProject/MeshGenerator.h GlProject/MeshData.cpp GlProject/MeshData.h)
|
||||
|
||||
target_link_libraries(GlProject
|
||||
${OPENGL_gl_LIBRARY}
|
||||
|
|
|
@ -17,6 +17,8 @@
|
|||
#include "Shader.h"
|
||||
#include "Camera.h"
|
||||
#include "Texture.h"
|
||||
#include "MeshData.h"
|
||||
#include "MeshGenerator.h"
|
||||
|
||||
Window* window;
|
||||
Shader* shader;
|
||||
|
@ -84,10 +86,18 @@ void makeEntities() {
|
|||
0, 1, 2, 2, 3, 0,
|
||||
//Bottom Face
|
||||
0+4, 1+4, 2+4, 2+4, 3+4, 0+4,
|
||||
//Front Face
|
||||
0+8, 1+8, 2+8, 2+8, 3+8, 0+8,
|
||||
//Back Face
|
||||
0+12, 1+12, 2+12, 2+12, 3+12, 0+12,
|
||||
//Left Face
|
||||
0+16, 1+16, 2+16, 2+16, 3+16, 0+16,
|
||||
//Right Face
|
||||
0+20, 1+20, 2+20, 2+20, 3+20, 0+20,
|
||||
};
|
||||
|
||||
GLfloat vertices[] = {
|
||||
//Top Face
|
||||
//Top Face
|
||||
-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,
|
||||
|
@ -97,24 +107,66 @@ void makeEntities() {
|
|||
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,
|
||||
//Front Face
|
||||
-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,
|
||||
//Back Face
|
||||
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,
|
||||
//Left Face
|
||||
-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,
|
||||
//Right Face
|
||||
-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,
|
||||
};
|
||||
|
||||
Mesh* mesh = new Mesh();
|
||||
mesh->create(vertices, indices, (sizeof(vertices)/sizeof(*vertices)), (sizeof(indices)/sizeof(*indices)));
|
||||
// Mesh* mesh = new Mesh();
|
||||
// mesh->create(vertices, indices, (sizeof(vertices)/sizeof(*vertices)), (sizeof(indices)/sizeof(*indices)));
|
||||
|
||||
for (auto y = -1; y < 1; y++) {
|
||||
for (auto i = -1; i < 2; i++) {
|
||||
for (auto j = -4; j < -2; j++) {
|
||||
auto meshGen = new MeshGenerator();
|
||||
|
||||
auto *tri = new Entity();
|
||||
tri->create(mesh);
|
||||
tri->setPosition(glm::vec3(i * 1.45, (y + 0.5) * 1.6, j));
|
||||
tri->setScale(0.7);
|
||||
int array[4][4][4] {
|
||||
{ {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} },
|
||||
{ {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} },
|
||||
{ {1, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} },
|
||||
{ {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }};
|
||||
MeshData* m = meshGen->generate(array);
|
||||
|
||||
entities.push_back(tri);
|
||||
}
|
||||
}
|
||||
}
|
||||
Mesh* mesh = new Mesh();
|
||||
|
||||
float* verts = &m->vertices[0];
|
||||
unsigned int* indxs = &m->indices[0];
|
||||
|
||||
mesh->create(verts, indxs, (int)m->vertices.size(), (int)m->indices.size());
|
||||
|
||||
auto *tri = new Entity();
|
||||
tri->create(mesh);
|
||||
tri->setPosition(glm::vec3(0, 0, -5));
|
||||
tri->setScale(0.5);
|
||||
entities.push_back(tri);
|
||||
|
||||
// for (auto y = -1; y < 1; y++) {
|
||||
// for (auto i = -1; i < 2; i++) {
|
||||
// for (auto j = -4; j < -2; j++) {
|
||||
//
|
||||
// auto *tri = new Entity();
|
||||
// tri->create(mesh);
|
||||
// tri->setPosition(glm::vec3(i * 1.45, (y + 0.5) * 1.6, j));
|
||||
// tri->setScale(0.7);
|
||||
//
|
||||
// entities.push_back(tri);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#include "MeshData.h"
|
|
@ -0,0 +1,16 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#ifndef GLPROJECT_MESHDATA_H
|
||||
#define GLPROJECT_MESHDATA_H
|
||||
|
||||
#include <vector>
|
||||
|
||||
struct MeshData {
|
||||
std::vector<float> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
};
|
||||
|
||||
|
||||
#endif //GLPROJECT_MESHDATA_H
|
|
@ -0,0 +1,200 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#include <cstdio>
|
||||
#include "MeshGenerator.h"
|
||||
|
||||
MeshGenerator::MeshGenerator() = default;
|
||||
|
||||
MeshData* MeshGenerator::generate(int blocks[4][4][4]) {
|
||||
std::vector<float> vertices;
|
||||
std::vector<unsigned int> indices;
|
||||
|
||||
int lastIndex = 0;
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
for (int k = 0; k < 4; k++) {
|
||||
if (blocks[i][j][k] == 1) {
|
||||
add_top_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
add_bottom_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
|
||||
add_front_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
add_back_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
|
||||
add_left_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
add_right_face(&vertices, &indices, &lastIndex, &k, &j, &i);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto* m = new MeshData();
|
||||
m->vertices = vertices;
|
||||
m->indices = indices;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_top_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z) {
|
||||
float top_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 top_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
int indexCount = 4;
|
||||
|
||||
for (int i = 0; i < (sizeof(top_vertices) / sizeof(top_vertices[0]) / 5); i++) {
|
||||
vertices->push_back(top_vertices[i*5 + 0] + *x);
|
||||
vertices->push_back(top_vertices[i*5 + 1] + *y);
|
||||
vertices->push_back(top_vertices[i*5 + 2] + *z);
|
||||
vertices->push_back(top_vertices[i*5 + 3]);
|
||||
vertices->push_back(top_vertices[i*5 + 4]);
|
||||
}
|
||||
|
||||
for (auto i : top_indices) {
|
||||
indices->push_back(*lastIndex + i);
|
||||
}
|
||||
|
||||
*lastIndex += indexCount;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_bottom_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z) {
|
||||
float top_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 top_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
int indexCount = 4;
|
||||
|
||||
for (int i = 0; i < (sizeof(top_vertices) / sizeof(top_vertices[0]) / 5); i++) {
|
||||
vertices->push_back(top_vertices[i*5 + 0] + *x);
|
||||
vertices->push_back(top_vertices[i*5 + 1] + *y);
|
||||
vertices->push_back(top_vertices[i*5 + 2] + *z);
|
||||
vertices->push_back(top_vertices[i*5 + 3]);
|
||||
vertices->push_back(top_vertices[i*5 + 4]);
|
||||
}
|
||||
|
||||
for (auto i : top_indices) {
|
||||
indices->push_back(*lastIndex + i);
|
||||
}
|
||||
|
||||
*lastIndex += indexCount;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_front_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float front_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 front_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
int indexCount = 4;
|
||||
|
||||
for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) {
|
||||
vertices->push_back(front_vertices[i*5 + 0] + *x);
|
||||
vertices->push_back(front_vertices[i*5 + 1] + *y);
|
||||
vertices->push_back(front_vertices[i*5 + 2] + *z);
|
||||
vertices->push_back(front_vertices[i*5 + 3]);
|
||||
vertices->push_back(front_vertices[i*5 + 4]);
|
||||
}
|
||||
|
||||
for (auto i : front_indices) {
|
||||
indices->push_back(*lastIndex + i);
|
||||
}
|
||||
|
||||
*lastIndex += indexCount;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_back_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float front_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 front_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
int indexCount = 4;
|
||||
|
||||
for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) {
|
||||
vertices->push_back(front_vertices[i*5 + 0] + *x);
|
||||
vertices->push_back(front_vertices[i*5 + 1] + *y);
|
||||
vertices->push_back(front_vertices[i*5 + 2] + *z);
|
||||
vertices->push_back(front_vertices[i*5 + 3]);
|
||||
vertices->push_back(front_vertices[i*5 + 4]);
|
||||
}
|
||||
|
||||
for (auto i : front_indices) {
|
||||
indices->push_back(*lastIndex + i);
|
||||
}
|
||||
|
||||
*lastIndex += indexCount;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_left_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float front_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 front_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
int indexCount = 4;
|
||||
|
||||
for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) {
|
||||
vertices->push_back(front_vertices[i*5 + 0] + *x);
|
||||
vertices->push_back(front_vertices[i*5 + 1] + *y);
|
||||
vertices->push_back(front_vertices[i*5 + 2] + *z);
|
||||
vertices->push_back(front_vertices[i*5 + 3]);
|
||||
vertices->push_back(front_vertices[i*5 + 4]);
|
||||
}
|
||||
|
||||
for (auto i : front_indices) {
|
||||
indices->push_back(*lastIndex + i);
|
||||
}
|
||||
|
||||
*lastIndex += indexCount;
|
||||
}
|
||||
|
||||
void MeshGenerator::add_right_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z){
|
||||
float front_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 front_indices[] {
|
||||
0, 1, 2, 2, 3, 0
|
||||
};
|
||||
int indexCount = 4;
|
||||
|
||||
for (int i = 0; i < (sizeof(front_vertices) / sizeof(front_vertices[0]) / 5); i++) {
|
||||
vertices->push_back(front_vertices[i*5 + 0] + *x);
|
||||
vertices->push_back(front_vertices[i*5 + 1] + *y);
|
||||
vertices->push_back(front_vertices[i*5 + 2] + *z);
|
||||
vertices->push_back(front_vertices[i*5 + 3]);
|
||||
vertices->push_back(front_vertices[i*5 + 4]);
|
||||
}
|
||||
|
||||
for (auto i : front_indices) {
|
||||
indices->push_back(*lastIndex + i);
|
||||
}
|
||||
|
||||
*lastIndex += indexCount;
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// Created by aurailus on 01/12/18.
|
||||
//
|
||||
|
||||
#ifndef GLPROJECT_MESHGENERATOR_H
|
||||
#define GLPROJECT_MESHGENERATOR_H
|
||||
|
||||
#include "MeshData.h"
|
||||
#include <vector>
|
||||
|
||||
class MeshGenerator {
|
||||
public:
|
||||
MeshGenerator();
|
||||
|
||||
MeshData* generate(int blocks[4][4][4]);
|
||||
private:
|
||||
void add_top_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
void add_bottom_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
|
||||
void add_front_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
void add_back_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
|
||||
void add_left_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
void add_right_face(std::vector<float> *vertices, std::vector<unsigned int> *indices, int *lastIndex, int *x, int *y, int *z);
|
||||
};
|
||||
|
||||
|
||||
#endif //GLPROJECT_MESHGENERATOR_H
|
Loading…
Reference in New Issue