Simple Optimizations to the Mesh Generator

- Add basic visibility testing to the mesh generator.
- MeshGenerator::addFaces takes references instead of pointers.
- MeshGenerator: Increase the reserve count.
- Add MeshGenerator::outOfRange function.
- Added Boost as dependency.
- Tweaked window mouse capturing to allow window movement.
master
aurailus 2018-12-14 16:12:04 -08:00
parent 15b4e241ee
commit d0c006d660
6 changed files with 114 additions and 74 deletions

View File

@ -8,12 +8,14 @@ include_directories(Libraries/glfw_linux/include)
include_directories(Libraries/glm)
include_directories(Libraries/stb_image)
include_directories(Libraries/cute)
include_directories(Libraries/boost_1_68_0)
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)
find_package(Boost COMPONENTS system thread)
link_directories(Libraries/glew/lib)
add_executable(GlProject GlProject/Main.cpp GlProject/engine/graphics/Mesh.cpp GlProject/engine/graphics/Mesh.h GlProject/engine/Entity.cpp GlProject/engine/Entity.h GlProject/engine/graphics/Shader.cpp GlProject/engine/graphics/Shader.h GlProject/engine/Window.cpp GlProject/engine/Window.h GlProject/engine/Camera.cpp GlProject/engine/Camera.h GlProject/engine/graphics/Texture.cpp GlProject/engine/graphics/Texture.h GlProject/mesh/MeshGenerator.cpp GlProject/mesh/MeshGenerator.h GlProject/engine/Timer.cpp GlProject/engine/Timer.h GlProject/blocks/BlockAtlas.cpp GlProject/blocks/BlockAtlas.h GlProject/blocks/BlockDef.cpp GlProject/blocks/BlockDef.h GlProject/mesh/MeshPart.cpp GlProject/mesh/MeshPart.h GlProject/mesh/MeshMod.h GlProject/mesh/Vertex.cpp GlProject/mesh/Vertex.h GlProject/mesh/BlockModel.cpp GlProject/mesh/BlockModel.h GlProject/engine/TextureAtlas.cpp GlProject/engine/TextureAtlas.h)
add_executable(GlProject GlProject/Main.cpp GlProject/engine/graphics/Mesh.cpp GlProject/engine/graphics/Mesh.h GlProject/engine/PerlinNoise.cpp GlProject/engine/PerlinNoise.h GlProject/engine/Entity.cpp GlProject/engine/Entity.h GlProject/engine/graphics/Shader.cpp GlProject/engine/graphics/Shader.h GlProject/engine/Window.cpp GlProject/engine/Window.h GlProject/engine/Camera.cpp GlProject/engine/Camera.h GlProject/engine/graphics/Texture.cpp GlProject/engine/graphics/Texture.h GlProject/mesh/MeshGenerator.cpp GlProject/mesh/MeshGenerator.h GlProject/engine/Timer.cpp GlProject/engine/Timer.h GlProject/blocks/BlockAtlas.cpp GlProject/blocks/BlockAtlas.h GlProject/blocks/BlockDef.cpp GlProject/blocks/BlockDef.h GlProject/mesh/MeshPart.cpp GlProject/mesh/MeshPart.h GlProject/mesh/MeshMod.h GlProject/mesh/Vertex.cpp GlProject/mesh/Vertex.h GlProject/mesh/BlockModel.cpp GlProject/mesh/BlockModel.h GlProject/engine/TextureAtlas.cpp GlProject/engine/TextureAtlas.h GlProject/UDP.cpp GlProject/UDP.h)
target_link_libraries(GlProject ${OPENGL_gl_LIBRARY} glfw libGLEW.so)
target_link_libraries(GlProject ${OPENGL_gl_LIBRARY} glfw libGLEW.so pthread)

View File

@ -1,3 +1,5 @@
#pragma clang diagnostic push
#pragma ide diagnostic ignored "OCUnusedMacroInspection"
#define STB_IMAGE_IMPLEMENTATION
#include <iostream>
@ -18,6 +20,8 @@
#include "engine/Entity.h"
#include "engine/Timer.h"
#include "engine/TextureAtlas.h"
#include "UDP.h"
#include "engine/PerlinNoise.h"
Window* window;
Shader* shader;
@ -30,7 +34,6 @@ GLfloat deltaTime = 0.0f;
GLfloat lastTime = 0.0f;
BlockModel* createBlockModel() {
Vertex* leftVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(1.0f, 1.0f)),
@ -41,7 +44,7 @@ BlockModel* createBlockModel() {
0, 1, 2, 2, 3, 0
};
auto* leftPart = new MeshPart(leftVerts, 4, leftInds, 6, "default_grass_side", atlas);
auto* leftPart = new MeshPart(leftVerts, 4, leftInds, 6, "default_cobblestone", atlas);
Vertex* rightVerts = new Vertex[4] {
Vertex(new glm::vec3(1.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
@ -53,7 +56,7 @@ BlockModel* createBlockModel() {
0, 1, 2, 2, 3, 0
};
auto* rightPart = new MeshPart(rightVerts, 4, rightInds, 6, "default_grass_side", atlas);
auto* rightPart = new MeshPart(rightVerts, 4, rightInds, 6, "default_cobblestone", atlas);
Vertex* topVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 1.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
@ -65,7 +68,7 @@ BlockModel* createBlockModel() {
0, 1, 2, 2, 3, 0
};
auto* topPart = new MeshPart(topVerts, 4, topInds, 6, "default_grass_top", atlas);
auto* topPart = new MeshPart(topVerts, 4, topInds, 6, "default_cobblestone", atlas);
Vertex* bottomVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 0.0f)),
@ -77,7 +80,7 @@ BlockModel* createBlockModel() {
0, 1, 2, 2, 3, 0
};
auto* bottomPart = new MeshPart(bottomVerts, 4, bottomInds, 6, "default_dirt", atlas);
auto* bottomPart = new MeshPart(bottomVerts, 4, bottomInds, 6, "default_cobblestone", atlas);
Vertex* frontVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 1.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
@ -89,7 +92,7 @@ BlockModel* createBlockModel() {
0, 1, 2, 2, 3, 0
};
auto* frontPart = new MeshPart(frontVerts, 4, frontInds, 6, "default_grass_side", atlas);
auto* frontPart = new MeshPart(frontVerts, 4, frontInds, 6, "default_cobblestone", atlas);
Vertex* backVerts = new Vertex[4] {
Vertex(new glm::vec3(0.0f, 0.0f, 0.0f), nullptr, new glm::vec2(0.0f, 1.0f)),
@ -101,43 +104,58 @@ BlockModel* createBlockModel() {
0, 1, 2, 2, 3, 0
};
auto* backPart = new MeshPart(backVerts, 4, backInds, 6, "default_grass_side", atlas);
auto* backPart = new MeshPart(backVerts, 4, backInds, 6, "default_cobblestone", atlas);
return new BlockModel(leftPart, rightPart, topPart, bottomPart, frontPart, backPart, nullptr, true, true);
}
void makeEntities(BlockModel* model) {
int array[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
for (int i = 0; i < CHUNK_SIZE; i++) { // NOLINT(modernize-loop-convert)
for (int j = 0; j < CHUNK_SIZE; j++) {
for (int k = 0; k < CHUNK_SIZE; k++) {
// array[i][j][k] = (j < 8) ? 1 : 0;
array[i][j][k] = (int)round(rand()%2);
}
}
}
PerlinNoise p(0);
std::vector<float> vertices;
std::vector<unsigned int> indices;
int VIEW_RANGE = 12;
MeshGenerator mg;
mg.build(array, model, vertices, indices);
for (int i = -VIEW_RANGE; i < VIEW_RANGE; i++) {
for (int j = -VIEW_RANGE; j < VIEW_RANGE; j++) {
for (int k = -8; k < 4; k++) {
auto* mesh = new Mesh();
mesh->create(&vertices, &indices);
int array[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE];
for (int i = -16; i < 16; i++) {
for (int j = -16; j < 16; j++) {
auto *chunk = new Entity();
chunk->create(mesh);
chunk->setPosition(glm::vec3(i * CHUNK_SIZE, 0, j * CHUNK_SIZE));
chunk->setScale(1);
entities.push_back(chunk);
for (int x = 0; x < CHUNK_SIZE; x++) { // NOLINT(modernize-loop-convert)
for (int z = 0; z < CHUNK_SIZE; z++) {
for (int y = 0; y < CHUNK_SIZE; y++) {
int xx = x + i * 16;
int yy = y + k * 16;
int zz = z + j * 16;
double val = p.noise(xx / (double) 16, zz / (double) 16, yy / (double) 16);
array[z][y][x] = (int) (val > 0.5);
}
}
}
std::vector<float> vertices;
std::vector<unsigned int> indices;
MeshGenerator mg;
mg.build(array, model, vertices, indices);
auto *mesh = new Mesh();
mesh->create(&vertices, &indices);
auto *chunk = new Entity();
chunk->create(mesh);
chunk->setPosition(glm::vec3(i * CHUNK_SIZE, k * CHUNK_SIZE, j * CHUNK_SIZE));
chunk->setScale(1);
entities.push_back(chunk);
}
}
}
}
int main() {
int main(int argc, char* argv[]) {
// UDP().start(argc, argv);
// return 0;
Timer boot("Initialization");
//Window

View File

@ -14,6 +14,8 @@ Window::Window(GLint windowWidth, GLint windowHeight) {
centerX = width / 2;
centerY = height / 2;
mouseLocked = true;
for (bool &key : keys) {
key = false;
}
@ -81,17 +83,24 @@ int Window::initialize() {
void Window::update() {
double mouseX, mouseY;
glfwGetCursorPos(mainWindow, &mouseX, &mouseY);
if (glfwGetWindowAttrib(mainWindow, GLFW_FOCUSED)) {
deltaX = mouseX - centerX;
deltaY = centerY - mouseY;
glfwSetCursorPos(mainWindow, centerX, centerY);
}
else {
if (glfwGetWindowAttrib(mainWindow, GLFW_FOCUSED) == GL_FALSE) {
mouseLocked = false;
deltaX = 0;
deltaY = 0;
}
else {
glfwGetCursorPos(mainWindow, &mouseX, &mouseY);
if (!mouseLocked) {
//Allow user to grab window edges without locking the mouse
if (mouseX > 128 && mouseY > 128 && mouseX < bufferWidth - 128 && mouseY < bufferHeight - 128) mouseLocked = true;
}
else {
deltaX = mouseX - centerX;
deltaY = centerY - mouseY;
glfwSetCursorPos(mainWindow, centerX, centerY);
}
}
}
double Window::getDeltaX() {

View File

@ -36,6 +36,8 @@ private:
GLint width, height;
GLint centerX, centerY;
bool mouseLocked;
GLint bufferWidth, bufferHeight;
bool keys[1024];

View File

@ -10,38 +10,46 @@ MeshGenerator::MeshGenerator() {
indOffset = 0;
}
bool outOfRange(glm::vec3 pos) {
return (pos.x < 0 || pos.x > 15 || pos.y < 0 || pos.y > 15 || pos.z < 0 || pos.z > 15);
}
void MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockModel* model,
std::vector<float> &vertices, std::vector<unsigned int> &indices) {
Timer t("Mesh Generation");
vertices.reserve(16384);
indices.reserve(4096);
vertices.reserve(300000);
indices.reserve(50000);
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) {
glm::vec3 off;
if (true) //Left Faces
addFaces(k, j, i, vertices, indices, model->leftFaces);
if (true) //Right Faces
addFaces(k, j, i, vertices, indices, model->rightFaces);
for (int i = 0; i < 4096; i++) {
int idx = i;
int z = idx / (CHUNK_SIZE * CHUNK_SIZE);
idx -= (z * CHUNK_SIZE * CHUNK_SIZE);
int y = idx / CHUNK_SIZE;
int x = idx % CHUNK_SIZE;
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 (blocks[z][y][x] == 1) {
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 (outOfRange(glm::vec3(x - 1, y, z)) || blocks[z][y][x - 1] == 0)
addFaces(x, y, z, &vertices, &indices, &model->leftFaces);
if (true) //Non-culled Faces
addFaces(k, j, i, vertices, indices, model->noCulledFaces);
}
}
if (outOfRange(glm::vec3(x + 1, y, z)) || blocks[z][y][x + 1] == 0)
addFaces(x, y, z, &vertices, &indices, &model->rightFaces);
if (outOfRange(glm::vec3(x, y - 1, z)) || blocks[z][y - 1][x] == 0)
addFaces(x, y, z, &vertices, &indices, &model->bottomFaces);
if (outOfRange(glm::vec3(x, y + 1, z)) || blocks[z][y + 1][x] == 0)
addFaces(x, y, z, &vertices, &indices, &model->topFaces);
if (outOfRange(glm::vec3(x, y, z - 1)) || blocks[z - 1][y][x] == 0)
addFaces(x, y, z, &vertices, &indices, &model->backFaces);
if (outOfRange(glm::vec3(x, y, z + 1)) || blocks[z + 1][y][x] == 0)
addFaces(x, y, z, &vertices, &indices, &model->frontFaces);
}
}
@ -51,28 +59,29 @@ void MeshGenerator::build(int blocks[CHUNK_SIZE][CHUNK_SIZE][CHUNK_SIZE], BlockM
t.elapsedMs();
}
void MeshGenerator::addFaces(int x, int y, int z, vector<float> &vertices, vector<unsigned int> &indices, vector<MeshPart*> meshParts) {
for (MeshPart *mp : meshParts) {
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();
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()) {
unsigned int index = miIterator->next();
indices.push_back(indOffset + index);
indices->push_back(indOffset + index);
}
indOffset += mp->getVertexCount();
}

View File

@ -23,7 +23,7 @@ public:
private:
unsigned int indOffset;
void addFaces(int x, int y, int z, std::vector<float> &vertices, std::vector<unsigned int> &indices, vector<MeshPart*>);
void addFaces(int x, int y, int z, vector<float>* vertices, vector<unsigned int>* indices, vector<MeshPart*>* meshParts);
void cleanup();
};