1
0
Fork 0
Univerxel/src/world/Chunk.cpp

67 lines
1.7 KiB
C++
Raw Normal View History

2020-07-10 17:49:16 +00:00
#include "Chunk.hpp"
#include <FastNoiseSIMD.h>
#include "materials.hpp"
#define DENSITY 0.f
#define GRANULARITY 30.f
Chunk::Chunk(const chunk_pos& pos, Generator& rnd) {
const auto [densitySet, materialSet] = rnd.getChunk(pos, CHUNK_LENGTH);
for (size_t i = 0; i < CHUNK_SIZE; i++) {
voxels[i].Density = std::clamp((densitySet[i] + DENSITY) * GRANULARITY, 0.f, 1.f) * UCHAR_MAX;
voxels[i].Material = voxels[i].Density > 0 ? 1 + std::clamp(static_cast<int>(std::lrint((materialSet[i] + 1) / 2 * (materials::count - 1))),
0, materials::count - 1) : 0; //NOTE: map (approx -1, 1) to (1, mat_max)
}
2020-07-23 19:39:08 +00:00
FastNoiseSIMD::FreeNoiseSet(densitySet);
FastNoiseSIMD::FreeNoiseSet(materialSet);
2020-07-10 17:49:16 +00:00
}
Chunk::~Chunk() { }
bool Chunk::update() {
if(upToDate) {
return false;
} else {
upToDate = true;
return true;
}
}
std::optional<ushort> Chunk::getNeighborIdx(ushort idx, Face face) {
switch (face) {
case Face::Forward:
if (idx % CHUNK_LENGTH >= CHUNK_LENGTH - 1)
return {};
return idx + 1;
case Face::Backward:
if (idx % CHUNK_LENGTH <= 0)
return {};
return idx - 1;
case Face::Up:
if ((idx / CHUNK_LENGTH) % CHUNK_LENGTH >= CHUNK_LENGTH - 1)
return {};
return idx + CHUNK_LENGTH;
case Face::Down:
if ((idx / CHUNK_LENGTH) % CHUNK_LENGTH <= 0)
return {};
return idx - CHUNK_LENGTH;
case Face::Right:
if (idx / CHUNK_LENGTH2 >= CHUNK_LENGTH - 1)
return {};
return idx + CHUNK_LENGTH2;
case Face::Left:
if (idx / CHUNK_LENGTH2 <= 0)
return {};
return idx - CHUNK_LENGTH2;
default:
return {};
}
}