Partial meshing is broken

main
Brett 2023-02-13 23:50:23 -05:00
parent 9bcded0383
commit c955c07ab4
5 changed files with 61 additions and 35 deletions

View File

@ -79,3 +79,11 @@
1 812 1676349209335934338 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1 1 812 1676349209335934338 CMakeFiles/FinalProject.dir/src/main.cpp.o d6fd815a69105af1
1 912 1676349209435937096 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86 1 912 1676349209435937096 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
912 1010 1676349209535939851 FinalProject e2e3313cdc6f5890 912 1010 1676349209535939851 FinalProject e2e3313cdc6f5890
1 856 1676350055707248541 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
856 932 1676350055783250634 FinalProject e2e3313cdc6f5890
1 880 1676350137201492095 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
880 960 1676350137281494299 FinalProject e2e3313cdc6f5890
1 897 1676350155746002613 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
897 973 1676350155818004597 FinalProject e2e3313cdc6f5890
1 876 1676350201339257745 CMakeFiles/FinalProject.dir/src/world/chunk/world.cpp.o a64a67d7d4fe0e86
876 961 1676350201423260057 FinalProject e2e3313cdc6f5890

View File

@ -1,3 +1,3 @@
Start testing: Feb 13 23:33 EST Start testing: Feb 13 23:50 EST
---------------------------------------------------------- ----------------------------------------------------------
End testing: Feb 13 23:33 EST End testing: Feb 13 23:50 EST

View File

@ -9,23 +9,23 @@ void fp::world::generateFullMesh(mesh_storage* mesh, fp::chunk* chunk) {
// checks to outside the bounds of the chunk should not have faces added. this will be handled by the partial mesh! // checks to outside the bounds of the chunk should not have faces added. this will be handled by the partial mesh!
bool outside = false; bool outside = false;
for (int i = 1; i < CHUNK_SIZE-1; i++) { for (int i = 1; i < CHUNK_SIZE - 1; i++) {
for (int j = 1; j < CHUNK_SIZE-1; j++) { for (int j = 1; j < CHUNK_SIZE - 1; j++) {
for (int k = 1; k < CHUNK_SIZE-1; k++) { for (int k = 1; k < CHUNK_SIZE - 1; k++) {
auto block = chunk->storage->get({i, j, k}); auto block = chunk->storage->get({i, j, k});
// opaque visibility is always 0. Non-zero values (true) are what we care about since opaque blocks are completely hidden // opaque visibility is always 0. Non-zero values (true) are what we care about since opaque blocks are completely hidden
if (!fp::registry::get(block).visibility) { if (!fp::registry::get(block).visibility) {
if (fp::registry::get(chunk->storage->getBounded(outside, {i-1, j, k})).visibility && !outside) if (fp::registry::get(chunk->storage->getBounded(outside, {i - 1, j, k})).visibility && !outside)
mesh->addFace(X_NEG, {i, j, k}); mesh->addFace(X_NEG, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i+1, j, k})).visibility && !outside) if (fp::registry::get(chunk->storage->getBounded(outside, {i + 1, j, k})).visibility && !outside)
mesh->addFace(X_POS, {i, j, k}); mesh->addFace(X_POS, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j-1, k})).visibility && !outside) if (fp::registry::get(chunk->storage->getBounded(outside, {i, j - 1, k})).visibility && !outside)
mesh->addFace(Y_NEG, {i, j, k}); mesh->addFace(Y_NEG, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j+1, k})).visibility && !outside) if (fp::registry::get(chunk->storage->getBounded(outside, {i, j + 1, k})).visibility && !outside)
mesh->addFace(Y_POS, {i, j, k}); mesh->addFace(Y_POS, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j, k-1})).visibility && !outside) if (fp::registry::get(chunk->storage->getBounded(outside, {i, j, k - 1})).visibility && !outside)
mesh->addFace(Z_NEG, {i, j, k}); mesh->addFace(Z_NEG, {i, j, k});
if (fp::registry::get(chunk->storage->getBounded(outside, {i, j, k+1})).visibility && !outside) if (fp::registry::get(chunk->storage->getBounded(outside, {i, j, k + 1})).visibility && !outside)
mesh->addFace(Z_POS, {i, j, k}); mesh->addFace(Z_POS, {i, j, k});
} }
} }
@ -35,31 +35,48 @@ void fp::world::generateFullMesh(mesh_storage* mesh, fp::chunk* chunk) {
chunk->dirtiness = PARTIAL_MESH; chunk->dirtiness = PARTIAL_MESH;
} }
inline void checkEdgeFaces(
fp::mesh_storage* mesh, fp::chunk* chunk, fp::chunk* neighbour, fp::face face, const fp::block_pos& pos, const fp::block_pos& neighbour_pos
) {
auto block = chunk->storage->get(pos);
if (!fp::registry::get(block).visibility) {
if (fp::registry::get(neighbour->storage->get(neighbour_pos)).visibility)
mesh->addFace(face, pos);
}
}
void fp::world::generateEdgeMesh(mesh_storage* mesh, fp::chunk* chunk) { void fp::world::generateEdgeMesh(mesh_storage* mesh, fp::chunk* chunk) {
BLT_TRACE("NOPE");
// don't try to regen the chunk mesh unless there is a chance all neighbours are not null // don't try to regen the chunk mesh unless there is a chance all neighbours are not null
if (chunk->status != chunk_update_status::NEIGHBOUR_CREATE) if (chunk->status != chunk_update_status::NEIGHBOUR_CREATE)
return; return;
chunk_neighbours neighbours {}; chunk_neighbours neighbours{};
getNeighbours(chunk->pos, neighbours); getNeighbours(chunk->pos, neighbours);
BLT_TRACE("GOODBYE");
// if none of the neighbours exist we cannot continue! // if none of the neighbours exist we cannot continue!
for (auto* neighbour : neighbours.neighbours){ for (auto* neighbour : neighbours.neighbours) {
if (!neighbour) if (!neighbour)
return; return;
} }
for (int i = 0; i < CHUNK_SIZE; i++){ for (int i = 0; i < CHUNK_SIZE; i++) {
for (int j = 0; j < CHUNK_SIZE; j++){ for (int j = 0; j < CHUNK_SIZE; j++) {
auto block = chunk->storage->get({0, i, j}); checkEdgeFaces(mesh, chunk, neighbours[X_NEG], X_NEG, {0, i, j}, {CHUNK_SIZE - 1, i, j});
if (!fp::registry::get(block).visibility) { checkEdgeFaces(mesh, chunk, neighbours[X_POS], X_POS, {CHUNK_SIZE-1, i, j}, {0, i, j});
auto neighbour = neighbours[X_NEG]->storage->get({CHUNK_SIZE-1, i, j});
if (fp::registry::get(neighbour).visibility) checkEdgeFaces(mesh, chunk, neighbours[Y_NEG], Y_NEG, {i, 0, j}, {i, CHUNK_SIZE - 1, j});
mesh->addFace(X_NEG, {0, i, j}); checkEdgeFaces(mesh, chunk, neighbours[Y_POS], Y_POS, {i, CHUNK_SIZE-1, j}, {i, 0, j});
}
checkEdgeFaces(mesh, chunk, neighbours[Z_NEG], Z_NEG, {i, j, 0}, {i, j, CHUNK_SIZE - 1});
checkEdgeFaces(mesh, chunk, neighbours[Z_POS], Z_POS, {i, j, CHUNK_SIZE-1}, {i, j, 0});
} }
} }
BLT_TRACE("HELLO");
chunk->status = NONE; chunk->status = NONE;
chunk->dirtiness = REFRESH; chunk->dirtiness = REFRESH;
} }
@ -70,7 +87,8 @@ void fp::world::generateChunkMesh(fp::chunk* chunk) {
if (chunk->dirtiness == FULL_MESH) { // full chunk mesh if (chunk->dirtiness == FULL_MESH) { // full chunk mesh
generateFullMesh(chunk->mesh, chunk); generateFullMesh(chunk->mesh, chunk);
} else if (chunk->dirtiness == PARTIAL_MESH){ // partial chunk mesh (had null neighbours) }
if (chunk->dirtiness == PARTIAL_MESH) { // partial chunk mesh (had null neighbours)
generateEdgeMesh(chunk->mesh, chunk); generateEdgeMesh(chunk->mesh, chunk);
} }
@ -90,11 +108,11 @@ void fp::world::render(fp::shader& shader) {
if (chunk == nullptr) if (chunk == nullptr)
continue; continue;
if (chunk->dirtiness > REFRESH){ if (chunk->dirtiness > REFRESH) {
generateChunkMesh(chunk); generateChunkMesh(chunk);
} }
if (chunk->dirtiness == REFRESH){ if (chunk->dirtiness == REFRESH) {
auto& vertices = chunk->mesh->getVertices(); auto& vertices = chunk->mesh->getVertices();
BLT_INFO("Chunk [%d, %d, %d] mesh updated with %d vertices and %d indices taking (%d, %d) bytes!", BLT_INFO("Chunk [%d, %d, %d] mesh updated with %d vertices and %d indices taking (%d, %d) bytes!",
@ -106,18 +124,18 @@ void fp::world::render(fp::shader& shader) {
chunk->render_size = vertices.size() / 3; chunk->render_size = vertices.size() / 3;
// delete the memory from the CPU. // delete the memory from the CPU.
delete(chunk->mesh); delete (chunk->mesh);
chunk->mesh = nullptr; chunk->mesh = nullptr;
chunk->dirtiness = OKAY; chunk->dirtiness = OKAY;
} }
if (chunk->render_size > 0){ if (chunk->render_size > 0) {
blt::mat4x4 translation {}; blt::mat4x4 translation{};
translation.translate((float)chunk->pos.x * CHUNK_SIZE, (float)chunk->pos.y * CHUNK_SIZE, (float)chunk->pos.z * CHUNK_SIZE); translation.translate((float) chunk->pos.x * CHUNK_SIZE, (float) chunk->pos.y * CHUNK_SIZE, (float) chunk->pos.z * CHUNK_SIZE);
shader.setMatrix("translation", translation); shader.setMatrix("translation", translation);
chunk->chunk_vao->bind(); chunk->chunk_vao->bind();
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, (int)chunk->render_size); glDrawArrays(GL_TRIANGLES, 0, (int) chunk->render_size);
glDisableVertexAttribArray(0); glDisableVertexAttribArray(0);
} }
} }