Browse Source

Started heightmap, some cleanup

testing
totallyfake 1 year ago
parent
commit
a23258a820
10 changed files with 95 additions and 112 deletions
  1. 1
    0
      CMakeLists.txt
  2. 1
    0
      include/chunk.h
  3. 23
    0
      include/heightmap.h
  4. 33
    50
      include/voxelutils.h
  5. 8
    23
      include/world.h
  6. 7
    0
      src/chunk.cpp
  7. 1
    0
      src/graphics/chunkmesh.cpp
  8. 21
    0
      src/heightmap.cpp
  9. 0
    15
      src/heightmap.h
  10. 0
    24
      src/world.cpp

+ 1
- 0
CMakeLists.txt View File

@@ -4,6 +4,7 @@ file (GLOB_RECURSE SOURCES "src/*.cpp" "src/*.c")
include_directories("./include" "libcuckoo/libcuckoo")
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_FLAGS "-O2")
set(CMAKE_C_FLAGS "-fPIC")
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
add_executable(vtk ${SOURCES})


+ 1
- 0
include/chunk.h View File

@@ -38,6 +38,7 @@ public:
void setVoxelType(const int& x, const int& y, const int& z, const unsigned& type, const bool& update = false);
unsigned getVoxelType(const unsigned& x, const unsigned& y, const unsigned& z);
glm::ivec3 getWorldCoords(const int& x, const int& y, const int& z);
unsigned getLightLevel(const glm::ivec3& pos);

void setPos(const glm::ivec3& pos);
glm::ivec3 getPos();

+ 23
- 0
include/heightmap.h View File

@@ -0,0 +1,23 @@
#pragma once

#include <glm/glm.hpp>
#include <array>
#include "concurrentqueue.h"

namespace vtk {

class HeightMap {
public:
HeightMap(const glm::ivec2& pos);
glm::ivec2 getPos();

int getHeight(const glm::ivec2& pos);
bool pushUpdate(const glm::ivec3& pos, const bool& destroyed);
void flushUpdates(); // called from worker thread
protected:
glm::ivec2 mPos;
std::array<int, (16*16)> mHeights; //stores 16*16 heightmap
moodycamel::ConcurrentQueue<glm::ivec3> mUpdateQueue;
};

}

+ 33
- 50
include/voxelutils.h View File

@@ -1,21 +1,3 @@
/*
* =====================================================================================
*
* Filename: voxelutils.h
*
* Description: Positioning and stuff.
*
* Version: 1.0
* Created: 04/03/2014 07:27:40 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/

#pragma once

#include <tuple>
@@ -35,54 +17,55 @@ enum class FaceDirection {
typedef std::tuple<unsigned, unsigned, unsigned> uPos; //used for voxel positions

struct uPosHash : public std::unary_function<uPos, std::size_t> {
std::size_t operator()(const uPos& k) const {
return std::get<0>(k) ^ std::get<1>(k) ^ std::get<2>(k);
}
std::size_t operator()(const uPos& k) const {
return std::get<0>(k) ^ std::get<1>(k) ^ std::get<2>(k);
}
};

struct uPosEqual : public std::binary_function<uPos, uPos, bool> {
bool operator()(const uPos& a, const uPos& b) const {
return (std::get<0>(a) == std::get<0>(b) &&
std::get<1>(a) == std::get<1>(b) &&
std::get<2>(a) == std::get<2>(b));
}
bool operator()(const uPos& a, const uPos& b) const {
return (std::get<0>(a) == std::get<0>(b) &&
std::get<1>(a) == std::get<1>(b) &&
std::get<2>(a) == std::get<2>(b));
}
};


typedef std::tuple<int, int, int> iPos; //used for chunk positions
struct iPosHash : public std::unary_function<iPos, std::size_t> {
std::size_t operator()(const iPos& k) const {
return std::get<0>(k) ^ std::get<1>(k) ^ std::get<2>(k);
}
std::size_t operator()(const iPos& k) const {
return std::get<0>(k) ^ std::get<1>(k) ^ std::get<2>(k);
}
};

struct iPosEqual : public std::binary_function<iPos, iPos, bool> {
bool operator()(const iPos& a, const iPos& b) const {
return (std::get<0>(a) == std::get<0>(b) &&
std::get<1>(a) == std::get<1>(b) &&
std::get<2>(a) == std::get<2>(b));
}
bool operator()(const iPos& a, const iPos& b) const {
return (std::get<0>(a) == std::get<0>(b) &&
std::get<1>(a) == std::get<1>(b) &&
std::get<2>(a) == std::get<2>(b));
}
};

// basic hash function for glm::ivec3
struct ivec3Hash : public std::unary_function<iPos, std::size_t> {
std::size_t operator()(const glm::ivec3& k) const {
std::size_t seed = 3;
for(int i = 0; i < 3; ++i) {
seed ^= k[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
struct ivec3Hash : public std::unary_function<glm::ivec3, std::size_t> {
std::size_t operator()(const glm::ivec3& k) const {
std::size_t seed = 3;
for(int i = 0; i < 3; ++i) {
seed ^= k[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};

/*
struct ivec3Equal : public std::binary_function<iPos, iPos, bool> {
bool operator()(const iPos& a, const iPos& b) const {
return (std::get<0>(a) == std::get<0>(b) &&
std::get<1>(a) == std::get<1>(b) &&
std::get<2>(a) == std::get<2>(b));
}
// hash function for glm::ivec2
struct ivec2Hash : public std::unary_function<glm::ivec2, std::size_t> {
std::size_t operator()(const glm::ivec2& k) const {
std::size_t seed = 3;
for(int i = 0; i < 2; ++i) {
seed ^= k[i] + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
return seed;
}
};
*/

}

+ 8
- 23
include/world.h View File

@@ -1,27 +1,10 @@
/*
* =====================================================================================
*
* Filename: world.h
*
* Description: World manages and draws chunks
*
* Version: 1.0
* Created: 04/03/2014 09:48:12 PM
* Revision: none
* Compiler: gcc
*
* Author: YOUR NAME (),
* Organization:
*
* =====================================================================================
*/

#pragma once

#include "voxelutils.h"
#include "voxelinfo.h"
#include "voxelmath.h"
#include "terraingen.h"
#include "heightmap.h"
#include "graphics/chunkmesh.h"

#include "cuckoohash_map.hh"
@@ -60,21 +43,23 @@ public:
void draw();
void update();

void forceGlobalGeometryUpdate(); //Rebuilds all geometry. Don't do this.

void queueChunkLoadsAroundPoint(const glm::vec3& point, const int& chunkRadius);

//std::unordered_map<glm::ivec3, Chunk*, ivec3Hash> mChunks;
cuckoohash_map<glm::ivec3, Chunk*, ivec3Hash> mChunks;
cuckoohash_map<glm::ivec3, ChunkMesh*, ivec3Hash> mChunkMeshes;
std::vector<iPos> chunkUpdateQueue;
std::deque<glm::ivec3> mChunkUpdateQueue;
cuckoohash_map<glm::ivec3, Chunk*, ivec3Hash> mChunks; //chunks
cuckoohash_map<glm::ivec3, ChunkMesh*, ivec3Hash> mChunkMeshes; //meshes
cuckoohash_map<glm::ivec2, HeightMap*, ivec2Hash> mHeightMaps; //heightmaps
std::deque<glm::ivec3> mChunkLoadQueue;

// mesh update queues
moodycamel::ConcurrentQueue<glm::ivec3> mMeshUpdateQueue; // regular one
moodycamel::ConcurrentQueue<glm::ivec3> mMeshUpdateQueueSoon; // high priority one

//heightmap update queue
moodycamel::ConcurrentQueue<glm::ivec2> mHeightMapUpdateQueue;

unsigned chunkSize;
float voxelSize;


+ 7
- 0
src/chunk.cpp View File

@@ -81,6 +81,13 @@ glm::ivec3 Chunk::getWorldCoords(const int& x, const int& y, const int& z) {
mPos.z * 16 + z);
}

unsigned Chunk::getLightLevel(const glm::ivec3 &pos) {
if (isVoxelSolid(pos.x, pos.y, pos.z)) {
return 0;
}
return 15;
}

void Chunk::setPos(const glm::ivec3& pos) {
mPos = pos;
}

+ 1
- 0
src/graphics/chunkmesh.cpp View File

@@ -54,6 +54,7 @@ bool ChunkMesh::rebuildChunkGeometry() {
// geometry format: x,y,z,u,v,i
mGeometry.clear();
mFaceAttribs.clear();

mFaceCount = 0;
int chunkSize = 16;

+ 21
- 0
src/heightmap.cpp View File

@@ -5,7 +5,28 @@ namespace vtk {
HeightMap::HeightMap(const glm::ivec2& pos) :
mPos(pos)
{
}

glm::ivec2 HeightMap::getPos() {
return mPos;
}

int HeightMap::getHeight(const glm::ivec2 &pos) {
int index = pos.x + (16 * pos.y);
return mHeights[index];
}

bool HeightMap::pushUpdate(const glm::ivec3 &pos, const bool &destroyed) {
return mUpdateQueue.try_enqueue(pos);
}

void HeightMap::flushUpdates() {
glm::ivec3 pos;
while (mUpdateQueue.try_dequeue(pos)) {
int index = pos.x + (16 * pos.y);
mHeights[index] = pos.z;
}
}

}

+ 0
- 15
src/heightmap.h View File

@@ -1,15 +0,0 @@
#pragma once

#include <glm/glm.hpp>

namespace vtk {

class HeightMap {
public:
HeightMap(const glm::ivec2& pos);

protected:
glm::ivec2 mPos;
};

}

+ 0
- 24
src/world.cpp View File

@@ -180,17 +180,6 @@ void World::update() {
getChunk(pos)->setQueuedForMeshRebuild(false);
}
rebuildThreadActive = false;
/*
while (!mChunkUpdateQueue.empty()) {
auto& pos = mChunkUpdateQueue.back();
ChunkMesh* mesh;
mChunkMeshes.find(pos, mesh);
mChunkUpdateQueue.pop_back();
if (!mesh->rebuildChunkGeometry())
queueChunkUpdate(pos);
}
rebuildThreadActive = false;
*/
};

rebuildThreadActive = true;
@@ -212,19 +201,6 @@ void World::update() {
}
}

void World::forceGlobalGeometryUpdate() {
/*
int chunkCount = 1;
chunkCount = 1;
for (auto& i : mChunkMeshes) {
std::cout << "\rUpdating chunk geometry, but better (" << chunkCount << "/" << mChunkMeshes.size() << ")" << std::flush;
i.second.rebuildChunkGeometry();
i.second.updateGeometry();
++chunkCount;
}
*/
}

void World::queueChunkLoadsAroundPoint(const glm::vec3 &point, const int &chunkRadius) {
glm::ivec3 chunkPoint = point / 16.0f;
for (int z = -chunkRadius; z <= chunkRadius; ++z) {

Loading…
Cancel
Save