voxelcore/src/voxels/VoxelsVolume.hpp
2025-12-29 20:41:47 +03:00

162 lines
3.7 KiB
C++

#pragma once
#include "constants.hpp"
#include "typedefs.hpp"
#include "voxel.hpp"
#include <cstring>
class VoxelsVolume {
public:
static inline constexpr size_t MAX_VOXELS = CHUNK_VOL * 2;
VoxelsVolume(int w, int h, int d)
: VoxelsVolume(0, 0, 0, w, h, d) {}
VoxelsVolume(int x, int y, int z, int w, int h, int d)
: x(x), y(y), z(z), w(w), h(h), d(d) {
std::memset(voxels.get(), 0xFF, sizeof(voxel) * w * h * d);
}
void setPosition(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
int getX() const {
return x;
}
int getY() const {
return y;
}
int getZ() const {
return z;
}
int getW() const {
return w;
}
int getH() const {
return h;
}
int getD() const {
return d;
}
voxel* getVoxels() {
return voxels.get();
}
light_t* getLights() {
return lights.get();
}
inline blockid_t pickBlockId(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {
return BLOCK_VOID;
}
return voxels[vox_index(bx - x, by - y, bz - z, w, d)].id;
}
inline voxel pickBlock(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {
return {BLOCK_VOID, {}};
}
return voxels[vox_index(bx - x, by - y, bz - z, w, d)];
}
inline light_t pickLight(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {
return 0;
}
return lights[vox_index(bx - x, by - y, bz - z, w, d)];
}
private:
int x, y, z;
int w, h, d;
std::unique_ptr<voxel[]> voxels;
std::unique_ptr<light_t[]> lights;
};
template <int w, int h, int d>
class StaticVoxelsVolume {
public:
static inline constexpr size_t size = w * h * d;
static inline constexpr int width = w;
static inline constexpr int height = h;
static inline constexpr int depth = d;
StaticVoxelsVolume()
: StaticVoxelsVolume(0, 0, 0) {}
StaticVoxelsVolume(int x, int y, int z)
: x(x), y(y), z(z) {
std::memset(voxels, 0xFF, sizeof(voxel) * size);
}
void setPosition(int x, int y, int z) {
this->x = x;
this->y = y;
this->z = z;
}
int getX() const {
return x;
}
int getY() const {
return y;
}
int getZ() const {
return z;
}
voxel* getVoxels() {
return voxels;
}
light_t* getLights() {
return lights;
}
inline blockid_t pickBlockId(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {
return BLOCK_VOID;
}
return voxels[vox_index(bx - x, by - y, bz - z, w, d)].id;
}
inline voxel pickBlock(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {
return {BLOCK_VOID, {}};
}
return voxels[vox_index(bx - x, by - y, bz - z, w, d)];
}
inline light_t pickLight(int bx, int by, int bz) const {
if (bx < x || by < y || bz < z || bx >= x + w || by >= y + h ||
bz >= z + d) {
return 0;
}
return lights[vox_index(bx - x, by - y, bz - z, w, d)];
}
private:
int x, y, z;
voxel voxels[size];
light_t lights[size];
};