Clean up & Optimize MeshGenerator class
parent
5ffd3c6dbb
commit
225862bdd1
|
@ -90,7 +90,7 @@ void MeshGenStream::threadFunction(MeshGenStream::Thread *thread) {
|
||||||
u.vertices = new std::vector<Vertex>();
|
u.vertices = new std::vector<Vertex>();
|
||||||
u.indices = new std::vector<unsigned int>();
|
u.indices = new std::vector<unsigned int>();
|
||||||
|
|
||||||
MeshGenerator().build(u.chunk, thread->atlas, *u.adjacent, *u.vertices, *u.indices);
|
MeshGenerator(*u.vertices, *u.indices, thread->atlas, *u.chunk, *u.adjacent);
|
||||||
|
|
||||||
delete u.adjacent;
|
delete u.adjacent;
|
||||||
|
|
||||||
|
|
|
@ -2,88 +2,60 @@
|
||||||
// Created by aurailus on 01/12/18.
|
// Created by aurailus on 01/12/18.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include <thread>
|
||||||
#include "MeshGenerator.h"
|
#include "MeshGenerator.h"
|
||||||
|
|
||||||
MeshGenerator::MeshGenerator() {
|
MeshGenerator::MeshGenerator(std::vector<Vertex> &vertices, std::vector<unsigned int> &indices,
|
||||||
indOffset = 0;
|
LocalBlockAtlas& atlas, const BlockChunk& chunk, const std::vector<bool>& adj) :
|
||||||
}
|
vertices(vertices),
|
||||||
|
indices(indices),
|
||||||
|
atlas(atlas),
|
||||||
|
chunk(chunk) {
|
||||||
|
|
||||||
LocalBlockDef& blockData(int ind, BlockChunk &chunk, LocalBlockAtlas& atlas) {
|
vertices.reserve(10000);
|
||||||
return atlas.fromIndex(chunk.getBlock(ind));
|
indices.reserve(10000);
|
||||||
}
|
|
||||||
|
|
||||||
LocalBlockDef& blockData(glm::vec3 &pos, BlockChunk &chunk, LocalBlockAtlas &atlas) {
|
|
||||||
return atlas.fromIndex(chunk.getBlock(pos));
|
|
||||||
}
|
|
||||||
|
|
||||||
bool faceOcculudedAt(glm::vec3 &pos, BlockChunk &chunk, LocalBlockAtlas &atlas, std::vector<bool> &bools) {
|
|
||||||
auto off = TransPos::CHUNK_SIZE*TransPos::CHUNK_SIZE; //CHUNK_SIZE ^ 2
|
|
||||||
if (pos.x < 0 || pos.x >= TransPos::CHUNK_SIZE || pos.y < 0 || pos.y >= TransPos::CHUNK_SIZE || pos.z < 0 || pos.z >= TransPos::CHUNK_SIZE) {
|
|
||||||
|
|
||||||
if (pos.x == -1) return bools[off + (int)pos.y * TransPos::CHUNK_SIZE + (int)pos.z];
|
|
||||||
if (pos.x == TransPos::CHUNK_SIZE) return bools[ + (int)pos.y * TransPos::CHUNK_SIZE + (int)pos.z];
|
|
||||||
|
|
||||||
if (pos.y == -1) return bools[off*3 + (int)pos.x * TransPos::CHUNK_SIZE + (int)pos.z];
|
|
||||||
if (pos.y == TransPos::CHUNK_SIZE) return bools[off*2 + (int)pos.x * TransPos::CHUNK_SIZE + (int)pos.z];
|
|
||||||
|
|
||||||
if (pos.z == -1) return bools[off*5 + (int)pos.y * TransPos::CHUNK_SIZE + (int)pos.x];
|
|
||||||
if (pos.z == TransPos::CHUNK_SIZE) return bools[off*4 + (int)pos.y * TransPos::CHUNK_SIZE + (int)pos.x];
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return blockData(pos, chunk, atlas).isCulling();
|
|
||||||
}
|
|
||||||
|
|
||||||
void MeshGenerator::build(const std::shared_ptr<BlockChunk> &chunk, LocalBlockAtlas &atlas, std::vector<bool> &adjacents,
|
|
||||||
std::vector<Vertex> &vertices, std::vector<unsigned int> &indices) {
|
|
||||||
|
|
||||||
Timer t("Mesh Generation");
|
Timer t("Mesh Generation");
|
||||||
|
|
||||||
vertices.reserve(300000);
|
|
||||||
indices.reserve(50000);
|
|
||||||
|
|
||||||
glm::vec3 off;
|
glm::vec3 off;
|
||||||
glm::vec3 vis;
|
glm::vec3 vis;
|
||||||
glm::vec3 check;
|
glm::vec3 check;
|
||||||
|
|
||||||
for (int i = 0; i < (int)pow(TransPos::CHUNK_SIZE, 3); i++) {
|
auto end = (int)pow(TransPos::CHUNK_SIZE, 3);
|
||||||
if (blockData(i, *chunk, atlas).getModel().visible) {
|
for (int i = 0; i < end; i++) {
|
||||||
|
if (getDef(i).getModel().visible) {
|
||||||
VecUtils::indAssignVec(i, off);
|
VecUtils::indAssignVec(i, off);
|
||||||
|
|
||||||
|
// Add TallGrass offsets
|
||||||
|
// TODO: Make this acknowlege MeshMods
|
||||||
vis = off;
|
vis = off;
|
||||||
// if (chunk->getBlock(i) >= 6 && chunk->fromIndex(i) <= 10) {
|
if (chunk.getBlock(i) >= 6 && chunk.getBlock(i) <= 10) {
|
||||||
// vis += glm::vec3((((double)rand() / RAND_MAX) - 0.5f) / 3.f, 0, (((double)rand() / RAND_MAX) - 0.5f) / 3.f);
|
vis += glm::vec3((((double)rand() / RAND_MAX) - 0.5f) / 3.f, 0, (((double)rand() / RAND_MAX) - 0.5f) / 3.f);
|
||||||
// }
|
}
|
||||||
|
|
||||||
//TODO: Better way to do this, surely
|
LocalBlockModel& model = getDef(i).getModel();
|
||||||
|
if (model.visible) {
|
||||||
|
|
||||||
LocalBlockModel& model = blockData(i, *chunk, atlas).getModel();
|
check = off; check.x -= 1;
|
||||||
|
if (!faceOcculudedAt(check, adj)) addFaces(vis, model.parts[XNEG]);
|
||||||
|
|
||||||
check.x = off.x - 1; check.y = off.y; check.z = off.z;
|
check = off; check.x += 1;
|
||||||
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
|
if (!faceOcculudedAt(check, adj)) addFaces(vis, model.parts[XPOS]);
|
||||||
addFaces(vis, vertices, indices, model.parts[XNEG]);
|
|
||||||
|
|
||||||
check.x = off.x + 1; check.y = off.y; check.z = off.z;
|
check = off; check.y -= 1;
|
||||||
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
|
if (!faceOcculudedAt(check, adj)) addFaces(vis, model.parts[YNEG]);
|
||||||
addFaces(vis, vertices, indices, model.parts[XPOS]);
|
|
||||||
|
|
||||||
check.x = off.x; check.y = off.y - 1; check.z = off.z;
|
check = off; check.y += 1;
|
||||||
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
|
if (!faceOcculudedAt(check, adj)) addFaces(vis, model.parts[YPOS]);
|
||||||
addFaces(vis, vertices, indices, model.parts[YNEG]);
|
|
||||||
|
|
||||||
check.x = off.x; check.y = off.y + 1; check.z = off.z;
|
check = off; check.z -= 1;
|
||||||
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
|
if (!faceOcculudedAt(check, adj)) addFaces(vis, model.parts[ZNEG]);
|
||||||
addFaces(vis, vertices, indices, model.parts[YPOS]);
|
|
||||||
|
|
||||||
check.x = off.x; check.y = off.y; check.z = off.z - 1;
|
check = off; check.z += 1;
|
||||||
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
|
if (!faceOcculudedAt(check, adj)) addFaces(vis, model.parts[ZPOS]);
|
||||||
addFaces(vis, vertices, indices, model.parts[ZNEG]);
|
|
||||||
|
|
||||||
check.x = off.x; check.y = off.y; check.z = off.z + 1;
|
addFaces(vis, model.parts[NO_CULL]);
|
||||||
if (!faceOcculudedAt(check, *chunk, atlas, adjacents))
|
}
|
||||||
addFaces(vis, vertices, indices, model.parts[ZPOS]);
|
|
||||||
|
|
||||||
addFaces(vis, vertices, indices, model.parts[NO_CULL]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -91,11 +63,37 @@ void MeshGenerator::build(const std::shared_ptr<BlockChunk> &chunk, LocalBlockAt
|
||||||
indices.shrink_to_fit();
|
indices.shrink_to_fit();
|
||||||
}
|
}
|
||||||
|
|
||||||
void MeshGenerator::addFaces(glm::vec3 &offset, vector<Vertex> &vertices, vector<unsigned int> &indices, vector<LocalMeshPart> &meshParts) {
|
LocalBlockDef& MeshGenerator::getDef(int ind) {
|
||||||
|
return atlas.fromIndex(chunk.getBlock(ind));
|
||||||
|
}
|
||||||
|
|
||||||
|
LocalBlockDef& MeshGenerator::getDef(const glm::vec3 &pos) {
|
||||||
|
return atlas.fromIndex(chunk.getBlock(pos));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool MeshGenerator::faceOcculudedAt(const glm::vec3 &pos, const std::vector<bool> &adj) {
|
||||||
|
auto off = TransPos::CHUNK_SIZE*TransPos::CHUNK_SIZE; //CHUNK_SIZE ^ 2
|
||||||
|
if (pos.x < 0 || pos.x >= TransPos::CHUNK_SIZE || pos.y < 0 || pos.y >= TransPos::CHUNK_SIZE || pos.z < 0 || pos.z >= TransPos::CHUNK_SIZE) {
|
||||||
|
|
||||||
|
if (pos.x == -1) return adj[off + (int)pos.y * TransPos::CHUNK_SIZE + (int)pos.z];
|
||||||
|
if (pos.x == TransPos::CHUNK_SIZE) return adj[(int)pos.y * TransPos::CHUNK_SIZE + (int)pos.z];
|
||||||
|
|
||||||
|
if (pos.y == -1) return adj[off*3 + (int)pos.x * TransPos::CHUNK_SIZE + (int)pos.z];
|
||||||
|
if (pos.y == TransPos::CHUNK_SIZE) return adj[off*2 + (int)pos.x * TransPos::CHUNK_SIZE + (int)pos.z];
|
||||||
|
|
||||||
|
if (pos.z == -1) return adj[off*5 + (int)pos.y * TransPos::CHUNK_SIZE + (int)pos.x];
|
||||||
|
if (pos.z == TransPos::CHUNK_SIZE) return adj[off*4 + (int)pos.y * TransPos::CHUNK_SIZE + (int)pos.x];
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return getDef(pos).isCulling();
|
||||||
|
}
|
||||||
|
|
||||||
|
void MeshGenerator::addFaces(const glm::vec3 &positon, const vector<LocalMeshPart> &meshParts) {
|
||||||
for (const LocalMeshPart& mp : meshParts) {
|
for (const LocalMeshPart& mp : meshParts) {
|
||||||
|
|
||||||
for (const MeshVertex &vertex : mp.vertices) {
|
for (const MeshVertex &vertex : mp.vertices) {
|
||||||
vertices.push_back({{vertex.pos + offset}, 1, {vertex.tex.x, vertex.tex.y, 0, 0}, vertex.nml});
|
vertices.push_back({{vertex.pos + positon}, 1, {vertex.tex.x, vertex.tex.y, 0, 0}, vertex.nml});
|
||||||
}
|
}
|
||||||
|
|
||||||
for (unsigned int index : mp.indices) {
|
for (unsigned int index : mp.indices) {
|
||||||
|
|
|
@ -24,13 +24,20 @@
|
||||||
|
|
||||||
class MeshGenerator {
|
class MeshGenerator {
|
||||||
public:
|
public:
|
||||||
MeshGenerator();
|
MeshGenerator(std::vector<Vertex> &vertices, std::vector<unsigned int> &indices,
|
||||||
void build(const std::shared_ptr<BlockChunk> &chunk, LocalBlockAtlas &atlas, std::vector<bool> &adjacents,
|
LocalBlockAtlas& atlas, const BlockChunk& chunk, const std::vector<bool>& adj);
|
||||||
std::vector<Vertex> &vertices, std::vector<unsigned int> &indices);
|
|
||||||
private:
|
private:
|
||||||
unsigned int indOffset;
|
bool faceOcculudedAt(const glm::vec3 &pos, const std::vector<bool> &adj);
|
||||||
|
void addFaces(const glm::vec3 &positon, const vector<LocalMeshPart> &meshParts);
|
||||||
|
LocalBlockDef& getDef(int ind);
|
||||||
|
LocalBlockDef& getDef(const glm::vec3 &pos);
|
||||||
|
|
||||||
|
unsigned int indOffset = 0;
|
||||||
|
const BlockChunk& chunk;
|
||||||
|
LocalBlockAtlas& atlas;
|
||||||
|
std::vector<Vertex>& vertices;
|
||||||
|
std::vector<unsigned int>& indices;
|
||||||
|
|
||||||
void addFaces(glm::vec3 &offset, vector<Vertex> &vertices, vector<unsigned int> &indices, vector<LocalMeshPart> &meshParts);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif //GLPROJECT_MESHGENERATOR_H
|
#endif //GLPROJECT_MESHGENERATOR_H
|
||||||
|
|
|
@ -13,10 +13,8 @@
|
||||||
|
|
||||||
class ServerPlayer {
|
class ServerPlayer {
|
||||||
public:
|
public:
|
||||||
// const static int ACTIVE_RANGE_H = 30;
|
const static int ACTIVE_RANGE_H = 16;
|
||||||
// const static int ACTIVE_RANGE_V = 12;
|
const static int ACTIVE_RANGE_V = 8;
|
||||||
const static int ACTIVE_RANGE_H = 12;
|
|
||||||
const static int ACTIVE_RANGE_V = 6;
|
|
||||||
|
|
||||||
explicit ServerPlayer(ServerPeer* peer, std::string uuid, std::string username);
|
explicit ServerPlayer(ServerPeer* peer, std::string uuid, std::string username);
|
||||||
|
|
||||||
|
|
|
@ -58,7 +58,8 @@ void Dimension::update() {
|
||||||
auto diffVec = pos - *playerPos;
|
auto diffVec = pos - *playerPos;
|
||||||
float distance = max(abs(diffVec.x), max(abs(diffVec.y), abs(diffVec.z)));
|
float distance = max(abs(diffVec.x), max(abs(diffVec.y), abs(diffVec.z)));
|
||||||
|
|
||||||
if (distance > 16) {
|
//TODO: Don't hard code this number
|
||||||
|
if (distance > 30) {
|
||||||
if (chunk->meshChunk != nullptr) {
|
if (chunk->meshChunk != nullptr) {
|
||||||
meshChunks.erase(chunk->meshChunkIter);
|
meshChunks.erase(chunk->meshChunkIter);
|
||||||
delete chunk->meshChunk;
|
delete chunk->meshChunk;
|
||||||
|
|
|
@ -33,13 +33,13 @@ BlockChunk::BlockChunk(std::vector<int> blocks, glm::vec3 pos) :
|
||||||
renderedEmpty = true;
|
renderedEmpty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
int BlockChunk::getBlock(const glm::vec3& pos) {
|
int BlockChunk::getBlock(const glm::vec3& pos) const {
|
||||||
auto ind = VecUtils::vecToInd(pos);
|
auto ind = VecUtils::vecToInd(pos);
|
||||||
if (ind < 0 || ind >= (int)pow(TransPos::CHUNK_SIZE, 3)) return -1;
|
if (ind < 0 || ind >= (int)pow(TransPos::CHUNK_SIZE, 3)) return -1;
|
||||||
return blocks[ind];
|
return blocks[ind];
|
||||||
}
|
}
|
||||||
|
|
||||||
int BlockChunk::getBlock(int ind) {
|
int BlockChunk::getBlock(int ind) const {
|
||||||
if (ind < 0 || ind >= (int)pow(TransPos::CHUNK_SIZE, 3)) return -1;
|
if (ind < 0 || ind >= (int)pow(TransPos::CHUNK_SIZE, 3)) return -1;
|
||||||
return blocks[ind];
|
return blocks[ind];
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,8 +24,8 @@ public:
|
||||||
bool shouldRender();
|
bool shouldRender();
|
||||||
bool allAdjacentsExist();
|
bool allAdjacentsExist();
|
||||||
|
|
||||||
int getBlock(int ind);
|
int getBlock(int ind) const;
|
||||||
int getBlock(const glm::vec3& pos);
|
int getBlock(const glm::vec3& pos) const;
|
||||||
|
|
||||||
bool setBlock(const glm::vec3& pos, int ind);
|
bool setBlock(const glm::vec3& pos, int ind);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue