Browse Source

proper updates with new mesher

testing
Michael Porter 2 years ago
parent
commit
ebcc149607
5 changed files with 51 additions and 7 deletions
  1. 6
    0
      include/graphics/chunkmesh.h
  2. 1
    0
      include/world.h
  3. 21
    6
      src/graphics/chunkmesh.cpp
  4. 1
    1
      src/scenes/testscene.cpp
  5. 22
    0
      src/world.cpp

+ 6
- 0
include/graphics/chunkmesh.h View File

@@ -2,6 +2,7 @@

#include <memory>
#include <glm/glm.hpp>
#include <vector>

namespace vtk {
class World;
@@ -12,6 +13,7 @@ namespace vtk {
void rebuildChunkGeometry();
void rebuildChunkLighting();
void draw();
bool updateGeometry(); //puts the geometry into the VBOs because OGL can't into threading

protected:
//position of chunk since chunk does NOT have to exist to rebuild
@@ -21,10 +23,14 @@ namespace vtk {
glm::ivec3 mLinkedChunkPos;
World& mLinkedWorld;

std::vector<float> geometry;
bool mLocked;
bool mUpdated;
unsigned mVAO;
unsigned mGeometryTexVBO;
unsigned mLightVBO;
int mFaceCount;
int mGeometryFaceCount;
};
}

+ 1
- 0
include/world.h View File

@@ -57,6 +57,7 @@ public:
std::unordered_map<iPos, Chunk*, iPosHash, iPosEqual> chunks;
std::unordered_map<glm::ivec3, ChunkMesh, ivec3Hash> mChunkMeshes;
std::vector<iPos> chunkUpdateQueue;
std::vector<glm::ivec3> mChunkGeometryUpdateQueue;

unsigned chunkSize;
float voxelSize;

+ 21
- 6
src/graphics/chunkmesh.cpp View File

@@ -7,12 +7,14 @@
#include <vector>
#include <GL/glew.h>
#include <GL/gl.h>
#include <iostream>


namespace vtk {
ChunkMesh::ChunkMesh(World& world, glm::ivec3 chunkPos) :
mLinkedWorld(world),
mLinkedChunkPos(chunkPos)
mLinkedChunkPos(chunkPos),
mLocked(false)
{
glGenBuffers(1, &mGeometryTexVBO);
glGenBuffers(1, &mLightVBO);
@@ -34,6 +36,8 @@ namespace vtk {
}

void ChunkMesh::rebuildChunkGeometry() {
if (mLocked) return;
mLocked = true;
//get the chunk from position
auto chunk = mLinkedWorld.getChunk(mLinkedChunkPos);

@@ -102,7 +106,7 @@ namespace vtk {
};

// geometry format: x,y,z,u,v,i
std::vector<float> geometry;
geometry.clear();
mFaceCount = 0;
int chunkSize = 16; // TODO: make this not hardcoded

@@ -203,9 +207,8 @@ namespace vtk {
}
}

glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * geometry.size(), geometry.data(), GL_STATIC_DRAW);
mUpdated = true;
mLocked = false;
}

void ChunkMesh::rebuildChunkLighting() {
@@ -213,7 +216,19 @@ namespace vtk {
}

void ChunkMesh::draw() {
updateGeometry();
glBindVertexArray(mVAO);
glDrawArrays(GL_TRIANGLES, 0, mFaceCount * 6);
glDrawArrays(GL_TRIANGLES, 0, mGeometryFaceCount * 6);
}

bool ChunkMesh::updateGeometry() {
if (mUpdated) {
mUpdated = false;
glBindBuffer(GL_ARRAY_BUFFER, mGeometryTexVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * geometry.size(), geometry.data(), GL_STATIC_DRAW);
mGeometryFaceCount = mFaceCount;
return true;
}
return false;
}
}

+ 1
- 1
src/scenes/testscene.cpp View File

@@ -129,7 +129,7 @@ void TestScene::init() {
for (int j = 0; j < 8; j++) {
for (int k = 0; k < 8; k++) {
std::cout << "\rGenerating chunks (" << chunkCount << "/" << 8*8*8 << ")" << std::flush;
world.generateChunk(i-1,j,k);
world.generateChunk(i,j,k);
chunkCount++;
}
}

+ 22
- 0
src/world.cpp View File

@@ -159,14 +159,35 @@ void World::draw() {

void World::update() {
while (!chunkUpdateQueue.empty()) {
/*
auto chunk = chunks[chunkUpdateQueue.back()];
chunkUpdateQueue.pop_back();
std::thread(&ChunkRenderer::updateGeometry, &chunk->renderer).detach();
*/
auto& pos = chunkUpdateQueue.back();
auto posVec = glm::ivec3(std::get<0>(pos), std::get<1>(pos), std::get<2>(pos));
auto& mesh = mChunkMeshes.find(posVec)->second;
chunkUpdateQueue.pop_back();
//mesh.rebuildChunkGeometry();
std::thread(&ChunkMesh::rebuildChunkGeometry, &mesh).detach();
mChunkGeometryUpdateQueue.push_back(posVec);
}

/*
for (auto& i : chunks) {
i.second->renderer.updateVertexData();
}

auto it = mChunkGeometryUpdateQueue.begin();
while (it != mChunkGeometryUpdateQueue.end()) {
if (mChunkMeshes.find(*it)->second.updateGeometry()) {
std::cout << "updated chunk " << it->x << "," << it->y << "," << it->z;
mChunkGeometryUpdateQueue.erase(it);
}
else ++it;
}
*/

}

void World::forceGlobalGeometryUpdate() {
@@ -183,6 +204,7 @@ void World::forceGlobalGeometryUpdate() {
for (auto& i : mChunkMeshes) {
std::cout << "\rUpdating chunk geometry, but better (" << chunkCount << "/" << mChunkMeshes.size() << ")" << std::flush;
i.second.rebuildChunkGeometry();
i.second.updateGeometry();
++chunkCount;
}
}

Loading…
Cancel
Save