Refactoring to cluster
This commit is contained in:
parent
5d0741635c
commit
efab720485
@ -6,7 +6,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#define CHUNK_VOXEL_SIZE 16
|
#define CLUSTER_SIZE 16
|
||||||
|
|
||||||
#ifdef DEER_RENDER
|
#ifdef DEER_RENDER
|
||||||
#include "DeerRender/Resource.h"
|
#include "DeerRender/Resource.h"
|
||||||
@ -17,39 +17,12 @@ namespace Deer {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Deer {
|
namespace Deer {
|
||||||
|
class VoxelWorldData;
|
||||||
|
|
||||||
struct VoxelType {
|
struct VoxelType {
|
||||||
std::string id;
|
std::string id;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Chunk {
|
|
||||||
Chunk() {
|
|
||||||
Voxel voxel = {.voxelTypeId = 0};
|
|
||||||
voxels_list.push_back(voxel);
|
|
||||||
voxels_reference_map[voxel] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Voxel {
|
|
||||||
uint32_t voxelTypeId;
|
|
||||||
|
|
||||||
bool operator==(const Voxel& other) const noexcept {
|
|
||||||
return voxelTypeId == other.voxelTypeId;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct VoxelHasher {
|
|
||||||
std::size_t operator()(const Voxel& v) const noexcept {
|
|
||||||
return std::hash<uint32_t>{}(v.voxelTypeId);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
std::vector<Chunk::Voxel> voxels_list;
|
|
||||||
std::unordered_map<Chunk::Voxel, uint16_t, VoxelHasher> voxels_reference_map;
|
|
||||||
uint16_t reference_matrix[CHUNK_VOXEL_SIZE][CHUNK_VOXEL_SIZE][CHUNK_VOXEL_SIZE];
|
|
||||||
|
|
||||||
uint16_t getOrCreateVoxelReference(const Voxel& voxel);
|
|
||||||
Voxel getVoxel(uint16_t voxelReference);
|
|
||||||
};
|
|
||||||
|
|
||||||
class VoxelEnvironment {
|
class VoxelEnvironment {
|
||||||
public:
|
public:
|
||||||
VoxelEnvironment();
|
VoxelEnvironment();
|
||||||
@ -59,34 +32,10 @@ namespace Deer {
|
|||||||
|
|
||||||
#ifdef DEER_RENDER
|
#ifdef DEER_RENDER
|
||||||
public:
|
public:
|
||||||
Resource<GPUMesh> buildChunk(int x, int y, int z);
|
Resource<GPUMesh> buildCluster(int x, int y, int z);
|
||||||
Scope<VoxelBuilder> voxelBuilder;
|
Scope<VoxelBuilder> voxelBuilder;
|
||||||
#endif
|
#endif
|
||||||
private:
|
private:
|
||||||
struct ChunkID {
|
Scope<VoxelWorldData> voxelWorldData;
|
||||||
int x, y, z;
|
|
||||||
|
|
||||||
bool operator==(const ChunkID& other) const noexcept {
|
|
||||||
return x == other.x &&
|
|
||||||
y == other.y &&
|
|
||||||
z == other.z;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
struct ChunkIDHasher {
|
|
||||||
std::size_t operator()(const ChunkID& c) const noexcept {
|
|
||||||
std::size_t h = 0;
|
|
||||||
h ^= std::hash<int>{}(c.x) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
|
||||||
h ^= std::hash<int>{}(c.y) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
|
||||||
h ^= std::hash<int>{}(c.z) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
|
||||||
return h;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Chunk* getOrCreateChunk(int x, int y, int z);
|
|
||||||
Chunk* tryGetChunk(int x, int y, int z);
|
|
||||||
|
|
||||||
std::vector<Scope<Chunk>> chunk_list;
|
|
||||||
std::unordered_map<ChunkID, Chunk*, ChunkIDHasher> chunk_map;
|
|
||||||
};
|
};
|
||||||
} // namespace Deer
|
} // namespace Deer
|
||||||
@ -24,12 +24,6 @@ namespace Deer {
|
|||||||
VertexUV(float _u, float _v) : u(_u), v(_v) {}
|
VertexUV(float _u, float _v) : u(_u), v(_v) {}
|
||||||
};
|
};
|
||||||
|
|
||||||
struct VertexLight {
|
|
||||||
uint8_t light;
|
|
||||||
|
|
||||||
VertexLight() = default;
|
|
||||||
VertexLight(int8_t _u) : light(_u) {}
|
|
||||||
};
|
|
||||||
// Vertex normal is represented with a number fromn [-64,64], and then its
|
// Vertex normal is represented with a number fromn [-64,64], and then its
|
||||||
// divided by 64 to know the decimal number
|
// divided by 64 to know the decimal number
|
||||||
struct VertexNormal {
|
struct VertexNormal {
|
||||||
@ -49,7 +43,6 @@ namespace Deer {
|
|||||||
vertexCount = count;
|
vertexCount = count;
|
||||||
}
|
}
|
||||||
void createUVData() { vertexUVData = MakeScope<VertexUV[]>(vertexCount); }
|
void createUVData() { vertexUVData = MakeScope<VertexUV[]>(vertexCount); }
|
||||||
void createLightData() { vertexLightData = MakeScope<VertexLight[]>(vertexCount); }
|
|
||||||
void createIndices(uint32_t count) {
|
void createIndices(uint32_t count) {
|
||||||
indexData = MakeScope<uint32_t[]>(count);
|
indexData = MakeScope<uint32_t[]>(count);
|
||||||
indexCount = count;
|
indexCount = count;
|
||||||
@ -58,7 +51,6 @@ namespace Deer {
|
|||||||
inline VertexPosition* getVertexPosition() const { return vertexPositionsData.get(); }
|
inline VertexPosition* getVertexPosition() const { return vertexPositionsData.get(); }
|
||||||
inline VertexNormal* getVertexNormal() const { return vertexNormalData.get(); }
|
inline VertexNormal* getVertexNormal() const { return vertexNormalData.get(); }
|
||||||
inline VertexUV* getVertexUV() const { return vertexUVData.get(); }
|
inline VertexUV* getVertexUV() const { return vertexUVData.get(); }
|
||||||
inline VertexLight* getVertexLight() const { return vertexLightData.get(); }
|
|
||||||
inline uint32_t* getIndexData() const { return indexData.get(); }
|
inline uint32_t* getIndexData() const { return indexData.get(); }
|
||||||
|
|
||||||
inline uint32_t getVertexCount() const { return vertexCount; }
|
inline uint32_t getVertexCount() const { return vertexCount; }
|
||||||
@ -69,7 +61,6 @@ namespace Deer {
|
|||||||
Scope<VertexPosition[]> vertexPositionsData;
|
Scope<VertexPosition[]> vertexPositionsData;
|
||||||
Scope<VertexNormal[]> vertexNormalData;
|
Scope<VertexNormal[]> vertexNormalData;
|
||||||
Scope<VertexUV[]> vertexUVData;
|
Scope<VertexUV[]> vertexUVData;
|
||||||
Scope<VertexLight[]> vertexLightData;
|
|
||||||
|
|
||||||
uint32_t indexCount = 0;
|
uint32_t indexCount = 0;
|
||||||
Scope<uint32_t[]> indexData;
|
Scope<uint32_t[]> indexData;
|
||||||
|
|||||||
@ -26,7 +26,7 @@ namespace Deer {
|
|||||||
|
|
||||||
class VoxelBuilder {
|
class VoxelBuilder {
|
||||||
public:
|
public:
|
||||||
Resource<GPUMesh> buildChunk(int x, int y, int z);
|
Resource<GPUMesh> buildCluster(int x, int y, int z);
|
||||||
|
|
||||||
VoxelBuilder(VoxelEnvironment* env) : environment(env) {}
|
VoxelBuilder(VoxelEnvironment* env) : environment(env) {}
|
||||||
|
|
||||||
@ -39,7 +39,6 @@ namespace Deer {
|
|||||||
glm::vec3 position;
|
glm::vec3 position;
|
||||||
glm::vec3 normal;
|
glm::vec3 normal;
|
||||||
float extrussion;
|
float extrussion;
|
||||||
float light;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -61,7 +60,6 @@ namespace Deer {
|
|||||||
void clearVoxelData();
|
void clearVoxelData();
|
||||||
|
|
||||||
bool hasBlock(int x, int y, int z);
|
bool hasBlock(int x, int y, int z);
|
||||||
float calculateVertexSunLight(int x, int y, int z);
|
|
||||||
|
|
||||||
std::vector<VertexData> vertices;
|
std::vector<VertexData> vertices;
|
||||||
std::vector<u_int32_t> indices;
|
std::vector<u_int32_t> indices;
|
||||||
@ -71,7 +69,7 @@ namespace Deer {
|
|||||||
int voxelZOffset;
|
int voxelZOffset;
|
||||||
|
|
||||||
VoxelEnvironment* environment;
|
VoxelEnvironment* environment;
|
||||||
VoxelData voxelData[CHUNK_VOXEL_SIZE + 2][CHUNK_VOXEL_SIZE + 2][CHUNK_VOXEL_SIZE + 2];
|
VoxelData voxelData[CLUSTER_SIZE + 2][CLUSTER_SIZE + 2][CLUSTER_SIZE + 2];
|
||||||
|
|
||||||
friend VoxelEnvironment;
|
friend VoxelEnvironment;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#include "DeerCore/Voxel.h"
|
#include "DeerCore/Voxel.h"
|
||||||
|
|
||||||
namespace Deer {
|
namespace Deer {
|
||||||
uint16_t Chunk::getOrCreateVoxelReference(const Voxel& voxel) {
|
uint16_t Cluster::getOrCreateVoxelReference(const Voxel& voxel) {
|
||||||
if (voxels_reference_map.contains(voxel))
|
if (voxels_reference_map.contains(voxel))
|
||||||
return voxels_reference_map[voxel];
|
return voxels_reference_map[voxel];
|
||||||
|
|
||||||
@ -10,7 +10,7 @@ namespace Deer {
|
|||||||
return voxels_list.size() - 1;
|
return voxels_list.size() - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk::Voxel Chunk::getVoxel(uint16_t voxelReference) {
|
Cluster::Voxel Cluster::getVoxel(uint16_t voxelReference) {
|
||||||
return voxels_list[voxelReference];
|
return voxels_list[voxelReference];
|
||||||
}
|
}
|
||||||
} // namespace Deer
|
} // namespace Deer
|
||||||
@ -2,44 +2,44 @@
|
|||||||
|
|
||||||
namespace Deer {
|
namespace Deer {
|
||||||
namespace Voxel {
|
namespace Voxel {
|
||||||
int getChunk(int id) {
|
int getCluster(int id) {
|
||||||
int chunk = id / CHUNK_VOXEL_SIZE;
|
int chunk = id / CLUSTER_SIZE;
|
||||||
|
|
||||||
// Fix truncation toward zero for negatives
|
// Fix truncation toward zero for negatives
|
||||||
if (id < 0 && id % CHUNK_VOXEL_SIZE != 0)
|
if (id < 0 && id % CLUSTER_SIZE != 0)
|
||||||
--chunk;
|
--chunk;
|
||||||
|
|
||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getBlock(int id) {
|
int getBlock(int id) {
|
||||||
int block = id % CHUNK_VOXEL_SIZE;
|
int block = id % CLUSTER_SIZE;
|
||||||
|
|
||||||
// Make remainder positive
|
// Make remainder positive
|
||||||
if (block < 0)
|
if (block < 0)
|
||||||
block += CHUNK_VOXEL_SIZE;
|
block += CLUSTER_SIZE;
|
||||||
|
|
||||||
return block;
|
return block;
|
||||||
}
|
}
|
||||||
} // namespace Voxel
|
} // namespace Voxel
|
||||||
|
|
||||||
Chunk* VoxelEnvironment::getOrCreateChunk(int x, int y, int z) {
|
Cluster* VoxelEnvironment::getOrCreateCluster(int x, int y, int z) {
|
||||||
ChunkID chunkId = {
|
ClusterID chunkId = {
|
||||||
.x = Voxel::getChunk(x),
|
.x = Voxel::getCluster(x),
|
||||||
.y = Voxel::getChunk(y),
|
.y = Voxel::getCluster(y),
|
||||||
.z = Voxel::getChunk(z)};
|
.z = Voxel::getCluster(z)};
|
||||||
|
|
||||||
if (chunk_map.contains(chunkId))
|
if (chunk_map.contains(chunkId))
|
||||||
return chunk_map[chunkId];
|
return chunk_map[chunkId];
|
||||||
|
|
||||||
// Should do stuff but we relax for the moment
|
// Should do stuff but we relax for the moment
|
||||||
chunk_list.push_back(MakeScope<Chunk>());
|
chunk_list.push_back(MakeScope<Cluster>());
|
||||||
Chunk* chunk = chunk_list.back().get();
|
Cluster* chunk = chunk_list.back().get();
|
||||||
chunk_map[chunkId] = chunk;
|
chunk_map[chunkId] = chunk;
|
||||||
|
|
||||||
for (int x = 0; x < CHUNK_VOXEL_SIZE; x++) {
|
for (int x = 0; x < CLUSTER_SIZE; x++) {
|
||||||
for (int y = 0; y < CHUNK_VOXEL_SIZE; y++) {
|
for (int y = 0; y < CLUSTER_SIZE; y++) {
|
||||||
for (int z = 0; z < CHUNK_VOXEL_SIZE; z++) {
|
for (int z = 0; z < CLUSTER_SIZE; z++) {
|
||||||
chunk->reference_matrix[x][y][z] = 0;
|
chunk->reference_matrix[x][y][z] = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -48,11 +48,11 @@ namespace Deer {
|
|||||||
return chunk;
|
return chunk;
|
||||||
}
|
}
|
||||||
|
|
||||||
Chunk* VoxelEnvironment::tryGetChunk(int x, int y, int z) {
|
Cluster* VoxelEnvironment::tryGetCluster(int x, int y, int z) {
|
||||||
ChunkID chunkId = {
|
ClusterID chunkId = {
|
||||||
.x = Voxel::getChunk(x),
|
.x = Voxel::getCluster(x),
|
||||||
.y = Voxel::getChunk(y),
|
.y = Voxel::getCluster(y),
|
||||||
.z = Voxel::getChunk(z)};
|
.z = Voxel::getCluster(z)};
|
||||||
|
|
||||||
if (chunk_map.contains(chunkId))
|
if (chunk_map.contains(chunkId))
|
||||||
return chunk_map[chunkId];
|
return chunk_map[chunkId];
|
||||||
@ -61,9 +61,9 @@ namespace Deer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VoxelEnvironment::modifyVoxel(uint32_t voxelId, int x, int y, int z) {
|
void VoxelEnvironment::modifyVoxel(uint32_t voxelId, int x, int y, int z) {
|
||||||
Chunk* chunk = getOrCreateChunk(x, y, z);
|
Cluster* chunk = getOrCreateCluster(x, y, z);
|
||||||
|
|
||||||
Chunk::Voxel voxel = {.voxelTypeId = voxelId};
|
Cluster::Voxel voxel = {.voxelTypeId = voxelId};
|
||||||
|
|
||||||
uint16_t voxelRef = chunk->getOrCreateVoxelReference(voxel);
|
uint16_t voxelRef = chunk->getOrCreateVoxelReference(voxel);
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ namespace Deer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
uint32_t VoxelEnvironment::getVoxel(int x, int y, int z) {
|
uint32_t VoxelEnvironment::getVoxel(int x, int y, int z) {
|
||||||
Chunk* chunk = tryGetChunk(x, y, z);
|
Cluster* chunk = tryGetCluster(x, y, z);
|
||||||
|
|
||||||
if (!chunk)
|
if (!chunk)
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
34
Deer/src/DeerCore/Voxel/VoxelWorldData.h
Normal file
34
Deer/src/DeerCore/Voxel/VoxelWorldData.h
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
#include "DeerCore/Voxel.h"
|
||||||
|
|
||||||
|
namespace Deer {
|
||||||
|
struct ClusterID {
|
||||||
|
int x, y, z;
|
||||||
|
|
||||||
|
bool operator==(const ClusterID& other) const noexcept {
|
||||||
|
return x == other.x && y == other.y && z == other.z;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct ClusterIDHasher {
|
||||||
|
std::size_t operator()(const ClusterID& c) const noexcept {
|
||||||
|
std::size_t h = 0;
|
||||||
|
h ^= std::hash<int>{}(c.x) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
||||||
|
h ^= std::hash<int>{}(c.y) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
||||||
|
h ^= std::hash<int>{}(c.z) + 0x9e3779b9 + (h << 6) + (h >> 2);
|
||||||
|
return h;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct Cluster {
|
||||||
|
uint16_t matrix_data[CLUSTER_SIZE][CLUSTER_SIZE][CLUSTER_SIZE];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct VoxelWorldData {
|
||||||
|
Cluster* getOrCreate(int x, int y, int z);
|
||||||
|
Cluster* tryGet(int x, int y, int z);
|
||||||
|
|
||||||
|
std::vector<Scope<Cluster>> cluster_list;
|
||||||
|
std::unordered_map<ClusterID, Cluster*, ClusterIDHasher> _map;
|
||||||
|
};
|
||||||
|
}
|
||||||
@ -34,14 +34,6 @@ namespace Deer {
|
|||||||
vertexNormalBuffer->setLayout(normalLayout);
|
vertexNormalBuffer->setLayout(normalLayout);
|
||||||
vertexArray->addVertexBuffer(vertexNormalBuffer);
|
vertexArray->addVertexBuffer(vertexNormalBuffer);
|
||||||
|
|
||||||
if (data.getVertexLight()) {
|
|
||||||
BufferLayout lightLayout({{"v_Light", DataType::Unsigned_Byte, ShaderDataType::FloatingPoint}}, sizeof(VertexLight));
|
|
||||||
Ref<VertexBuffer> vertexLightBuffer = VertexBuffer::create(data.getVertexLight(), data.getVertexCount() * sizeof(VertexLight));
|
|
||||||
vertexLightBuffer->bind();
|
|
||||||
vertexLightBuffer->setLayout(lightLayout);
|
|
||||||
vertexArray->addVertexBuffer(vertexLightBuffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.getVertexUV()) {
|
if (data.getVertexUV()) {
|
||||||
BufferLayout uvLayout({{"v_UV", DataType::Float2, ShaderDataType::FloatingPoint}}, sizeof(VertexUV));
|
BufferLayout uvLayout({{"v_UV", DataType::Float2, ShaderDataType::FloatingPoint}}, sizeof(VertexUV));
|
||||||
Ref<VertexBuffer> vertexUVBuffer = VertexBuffer::create(data.getVertexUV(), data.getVertexCount() * sizeof(VertexUV));
|
Ref<VertexBuffer> vertexUVBuffer = VertexBuffer::create(data.getVertexUV(), data.getVertexCount() * sizeof(VertexUV));
|
||||||
|
|||||||
@ -96,7 +96,7 @@ namespace Deer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Resource<GPUMesh> VoxelBuilder::buildChunk(int x, int y, int z) {
|
Resource<GPUMesh> VoxelBuilder::buildCluster(int x, int y, int z) {
|
||||||
voxelXOffset = x;
|
voxelXOffset = x;
|
||||||
voxelYOffset = y;
|
voxelYOffset = y;
|
||||||
voxelZOffset = z;
|
voxelZOffset = z;
|
||||||
@ -123,7 +123,6 @@ namespace Deer {
|
|||||||
MeshData meshData;
|
MeshData meshData;
|
||||||
meshData.createVertices(vertices.size());
|
meshData.createVertices(vertices.size());
|
||||||
meshData.createIndices(indices.size());
|
meshData.createIndices(indices.size());
|
||||||
meshData.createLightData();
|
|
||||||
|
|
||||||
DEER_CORE_TRACE("{} , {}", meshData.getVertexCount(), meshData.getIndexCount());
|
DEER_CORE_TRACE("{} , {}", meshData.getVertexCount(), meshData.getIndexCount());
|
||||||
for (size_t i = 0; i < meshData.getVertexCount(); i++) {
|
for (size_t i = 0; i < meshData.getVertexCount(); i++) {
|
||||||
@ -135,8 +134,6 @@ namespace Deer {
|
|||||||
meshData.getVertexNormal()[i].x = (uint8_t)(normal.x * 64);
|
meshData.getVertexNormal()[i].x = (uint8_t)(normal.x * 64);
|
||||||
meshData.getVertexNormal()[i].y = (uint8_t)(normal.y * 64);
|
meshData.getVertexNormal()[i].y = (uint8_t)(normal.y * 64);
|
||||||
meshData.getVertexNormal()[i].z = (uint8_t)(normal.z * 64);
|
meshData.getVertexNormal()[i].z = (uint8_t)(normal.z * 64);
|
||||||
|
|
||||||
meshData.getVertexLight()[i].light = (uint8_t)(vertices[i].light * 255.0f);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < meshData.getIndexCount(); i++) {
|
for (size_t i = 0; i < meshData.getIndexCount(); i++) {
|
||||||
@ -151,9 +148,9 @@ namespace Deer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VoxelBuilder::clearVoxelData() {
|
void VoxelBuilder::clearVoxelData() {
|
||||||
for (int x = 0; x < CHUNK_VOXEL_SIZE + 2; x++) {
|
for (int x = 0; x < CLUSTER_SIZE + 2; x++) {
|
||||||
for (int y = 0; y < CHUNK_VOXEL_SIZE + 2; y++) {
|
for (int y = 0; y < CLUSTER_SIZE + 2; y++) {
|
||||||
for (int z = 0; z < CHUNK_VOXEL_SIZE + 2; z++) {
|
for (int z = 0; z < CLUSTER_SIZE + 2; z++) {
|
||||||
voxelData[x][y][z] = VoxelData();
|
voxelData[x][y][z] = VoxelData();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -165,23 +162,10 @@ namespace Deer {
|
|||||||
return voxelId > 0;
|
return voxelId > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
float VoxelBuilder::calculateVertexSunLight(int x, int y, int z) {
|
|
||||||
float acumulatedLight = 0;
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
if (!hasBlock(x + Voxel::voxelCheckOrder[i][0],
|
|
||||||
y + Voxel::voxelCheckOrder[i][1],
|
|
||||||
z + Voxel::voxelCheckOrder[i][2]))
|
|
||||||
acumulatedLight += 1.0f / 4.0f;
|
|
||||||
}
|
|
||||||
if (acumulatedLight < 1)
|
|
||||||
return acumulatedLight;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
void VoxelBuilder::buildVertices() {
|
void VoxelBuilder::buildVertices() {
|
||||||
for (int x = 0; x < CHUNK_VOXEL_SIZE; x++) {
|
for (int x = 0; x < CLUSTER_SIZE; x++) {
|
||||||
for (int y = 0; y < CHUNK_VOXEL_SIZE; y++) {
|
for (int y = 0; y < CLUSTER_SIZE; y++) {
|
||||||
for (int z = 0; z < CHUNK_VOXEL_SIZE; z++) {
|
for (int z = 0; z < CLUSTER_SIZE; z++) {
|
||||||
if (!hasBlock(x, y, z))
|
if (!hasBlock(x, y, z))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
@ -206,20 +190,6 @@ namespace Deer {
|
|||||||
if (face == 2 || face == 3)
|
if (face == 2 || face == 3)
|
||||||
faceData = &flatFaceData;
|
faceData = &flatFaceData;
|
||||||
|
|
||||||
float cornersLight[4];
|
|
||||||
cornersLight[0] = calculateVertexSunLight(x + Voxel::faceOriginVectorInt[face][0],
|
|
||||||
y + Voxel::faceOriginVectorInt[face][1],
|
|
||||||
z + Voxel::faceOriginVectorInt[face][2]);
|
|
||||||
cornersLight[1] = calculateVertexSunLight(x + Voxel::faceOriginVectorInt[face][0] + Voxel::faceRightVector[face][0],
|
|
||||||
y + Voxel::faceOriginVectorInt[face][1] + Voxel::faceRightVector[face][1],
|
|
||||||
z + Voxel::faceOriginVectorInt[face][2] + Voxel::faceRightVector[face][2]);
|
|
||||||
cornersLight[2] = calculateVertexSunLight(x + Voxel::faceOriginVectorInt[face][0] + Voxel::faceUpVectorInt[face][0],
|
|
||||||
y + Voxel::faceOriginVectorInt[face][1] + Voxel::faceUpVectorInt[face][1],
|
|
||||||
z + Voxel::faceOriginVectorInt[face][2] + Voxel::faceUpVectorInt[face][2]);
|
|
||||||
cornersLight[3] = calculateVertexSunLight(x + Voxel::faceOriginVectorInt[face][0] + Voxel::faceUpVectorInt[face][0] + Voxel::faceRightVector[face][0],
|
|
||||||
y + Voxel::faceOriginVectorInt[face][1] + Voxel::faceUpVectorInt[face][1] + Voxel::faceRightVector[face][1],
|
|
||||||
z + Voxel::faceOriginVectorInt[face][2] + Voxel::faceUpVectorInt[face][2] + Voxel::faceRightVector[face][2]);
|
|
||||||
|
|
||||||
for (VoxelVertex& voxelVertex : faceData->vertices) {
|
for (VoxelVertex& voxelVertex : faceData->vertices) {
|
||||||
glm::vec3 vertexPosition = glm::vec3(x, y, z) + Voxel::faceOriginVector[face];
|
glm::vec3 vertexPosition = glm::vec3(x, y, z) + Voxel::faceOriginVector[face];
|
||||||
glm::vec3 up = Voxel::faceUpVector[face];
|
glm::vec3 up = Voxel::faceUpVector[face];
|
||||||
@ -230,14 +200,10 @@ namespace Deer {
|
|||||||
|
|
||||||
glm::vec3 perpendicular = glm::cross(up, right);
|
glm::vec3 perpendicular = glm::cross(up, right);
|
||||||
|
|
||||||
float topLight = Voxel::lerp(voxelVertex.position.x, cornersLight[3], cornersLight[2]);
|
|
||||||
float bottomLight = Voxel::lerp(voxelVertex.position.x, cornersLight[1], cornersLight[0]);
|
|
||||||
|
|
||||||
vertices.push_back({});
|
vertices.push_back({});
|
||||||
VertexData& vv = vertices.back();
|
VertexData& vv = vertices.back();
|
||||||
vv.position = vertexPosition;
|
vv.position = vertexPosition;
|
||||||
vv.extrussion = voxelVertex.position.z;
|
vv.extrussion = voxelVertex.position.z;
|
||||||
vv.light = Voxel::lerp(voxelVertex.position.y, topLight, bottomLight);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (uint32_t index : faceData->triangles) {
|
for (uint32_t index : faceData->triangles) {
|
||||||
@ -245,11 +211,10 @@ namespace Deer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function goes foreach edge, from (0, 0, 0) to (33, 33, 33) then check each edge
|
|
||||||
void VoxelBuilder::buildConnections() {
|
void VoxelBuilder::buildConnections() {
|
||||||
for (int x = 0; x < CHUNK_VOXEL_SIZE + 1; x++) {
|
for (int x = 0; x < CLUSTER_SIZE + 1; x++) {
|
||||||
for (int y = 0; y < CHUNK_VOXEL_SIZE + 1; y++) {
|
for (int y = 0; y < CLUSTER_SIZE + 1; y++) {
|
||||||
for (int z = 0; z < CHUNK_VOXEL_SIZE + 1; z++) {
|
for (int z = 0; z < CLUSTER_SIZE + 1; z++) {
|
||||||
buildAxisConnections(x, y, z);
|
buildAxisConnections(x, y, z);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -364,9 +329,9 @@ namespace Deer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void VoxelBuilder::buildMarchingCubesCorners() {
|
void VoxelBuilder::buildMarchingCubesCorners() {
|
||||||
for (int x = 0; x < CHUNK_VOXEL_SIZE + 1; x++) {
|
for (int x = 0; x < CLUSTER_SIZE + 1; x++) {
|
||||||
for (int y = 0; y < CHUNK_VOXEL_SIZE + 1; y++) {
|
for (int y = 0; y < CLUSTER_SIZE + 1; y++) {
|
||||||
for (int z = 0; z < CHUNK_VOXEL_SIZE + 1; z++) {
|
for (int z = 0; z < CLUSTER_SIZE + 1; z++) {
|
||||||
uint8_t marchingCubeId = 0;
|
uint8_t marchingCubeId = 0;
|
||||||
for (int side = 0; side < CUBE_SIDES; side++) {
|
for (int side = 0; side < CUBE_SIDES; side++) {
|
||||||
bool hasVoxel = hasBlock(x + Voxel::voxelCheckOrder[side][0],
|
bool hasVoxel = hasBlock(x + Voxel::voxelCheckOrder[side][0],
|
||||||
|
|||||||
@ -94,7 +94,7 @@ namespace Deer {
|
|||||||
DEER_CORE_INFO("{}", env.getVoxel(0, 0, 0));
|
DEER_CORE_INFO("{}", env.getVoxel(0, 0, 0));
|
||||||
|
|
||||||
shader.shader = Builtin::simpleShader();
|
shader.shader = Builtin::simpleShader();
|
||||||
mesh.mesh = env.voxelBuilder->buildChunk(0, 0, 0);
|
mesh.mesh = env.voxelBuilder->buildCluster(0, 0, 0);
|
||||||
|
|
||||||
world->execute();
|
world->execute();
|
||||||
}
|
}
|
||||||
|
|||||||
20
imgui.ini
20
imgui.ini
@ -1,6 +1,6 @@
|
|||||||
[Window][DockSpace Demo]
|
[Window][DockSpace Demo]
|
||||||
Pos=0,0
|
Pos=0,0
|
||||||
Size=2560,1371
|
Size=1920,1011
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
|
|
||||||
[Window][Debug##Default]
|
[Window][Debug##Default]
|
||||||
@ -10,36 +10,36 @@ Collapsed=0
|
|||||||
|
|
||||||
[Window][ViewportPanel]
|
[Window][ViewportPanel]
|
||||||
Pos=561,26
|
Pos=561,26
|
||||||
Size=1348,870
|
Size=708,510
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000005,0
|
DockId=0x00000005,0
|
||||||
|
|
||||||
[Window][PropertiesPanel]
|
[Window][PropertiesPanel]
|
||||||
Pos=1911,26
|
Pos=1271,26
|
||||||
Size=649,870
|
Size=649,510
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000006,0
|
DockId=0x00000006,0
|
||||||
|
|
||||||
[Window][ResourceExplorer]
|
[Window][ResourceExplorer]
|
||||||
Pos=0,898
|
Pos=0,538
|
||||||
Size=2560,473
|
Size=1920,473
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000004,0
|
DockId=0x00000004,0
|
||||||
|
|
||||||
[Window][TreePanel]
|
[Window][TreePanel]
|
||||||
Pos=0,26
|
Pos=0,26
|
||||||
Size=559,870
|
Size=559,510
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000001,0
|
DockId=0x00000001,0
|
||||||
|
|
||||||
[Window][VoxelEditor]
|
[Window][VoxelEditor]
|
||||||
Pos=1911,26
|
Pos=1271,26
|
||||||
Size=649,870
|
Size=649,510
|
||||||
Collapsed=0
|
Collapsed=0
|
||||||
DockId=0x00000006,1
|
DockId=0x00000006,1
|
||||||
|
|
||||||
[Docking][Data]
|
[Docking][Data]
|
||||||
DockSpace ID=0x0AC2E849 Window=0xD0388BC8 Pos=0,26 Size=2560,1345 Split=Y
|
DockSpace ID=0x0AC2E849 Window=0xD0388BC8 Pos=0,26 Size=1920,985 Split=Y
|
||||||
DockNode ID=0x00000003 Parent=0x0AC2E849 SizeRef=1920,870 Split=X
|
DockNode ID=0x00000003 Parent=0x0AC2E849 SizeRef=1920,870 Split=X
|
||||||
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=559,985 Selected=0x16E3C1E7
|
DockNode ID=0x00000001 Parent=0x00000003 SizeRef=559,985 Selected=0x16E3C1E7
|
||||||
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1359,985 Split=X
|
DockNode ID=0x00000002 Parent=0x00000003 SizeRef=1359,985 Split=X
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user