cubos::engine::VoxelGrid class final

Represents a voxel object using a 3D grid.

Constructors, destructors, conversion operators

VoxelGrid()
Constructs an empty single-voxel grid.
VoxelGrid(const glm::uvec3& size)
Constructs an empty grid with the given size.
VoxelGrid(const glm::uvec3& size, const std::vector<uint16_t>& indices)
Constructs a grid with the given size and initial data.
VoxelGrid(VoxelGrid&& other) noexcept
Move constructs.

Public functions

auto operator=(const VoxelGrid& rhs) -> VoxelGrid&
Makes this grid a copy of another grid.
void setSize(const glm::uvec3& size)
Resizes the grid. New voxels are initialized to 0.
auto size() const -> const glm::uvec3&
Gets the size of the grid.
void clear()
Sets all voxels to 0.
void set(const glm::ivec3& position, uint16_t mat)
Sets the material index of a voxel.
auto get(const glm::ivec3& position) const -> uint16_t
Gets the material index of a voxel.
auto convert(const VoxelPalette& src, const VoxelPalette& dst, float minSimilarity) -> bool
Converts the material indices of this grid from one palette to another.
auto loadFrom(core::memory::Stream& stream) -> bool
Loads the grid's data from the given stream.
auto writeTo(core::memory::Stream& stream) const -> bool
Writes the grid's data to the given stream.

Function documentation

cubos::engine::VoxelGrid::VoxelGrid(const glm::uvec3& size)

Constructs an empty grid with the given size.

Parameters
size Size of the grid.

cubos::engine::VoxelGrid::VoxelGrid(const glm::uvec3& size, const std::vector<uint16_t>& indices)

Constructs a grid with the given size and initial data.

Parameters
size Size of the grid.
indices Material indices of the voxels.

cubos::engine::VoxelGrid::VoxelGrid(VoxelGrid&& other) noexcept

Move constructs.

Parameters
other Other grid.

VoxelGrid& cubos::engine::VoxelGrid::operator=(const VoxelGrid& rhs)

Makes this grid a copy of another grid.

Parameters
rhs Other grid.
Returns This grid, for chaining.

void cubos::engine::VoxelGrid::setSize(const glm::uvec3& size)

Resizes the grid. New voxels are initialized to 0.

Parameters
size New size of the grid.

const glm::uvec3& cubos::engine::VoxelGrid::size() const

Gets the size of the grid.

Returns Size of the grid.

void cubos::engine::VoxelGrid::set(const glm::ivec3& position, uint16_t mat)

Sets the material index of a voxel.

Parameters
position Voxel coordinates.
mat Material index to set.

uint16_t cubos::engine::VoxelGrid::get(const glm::ivec3& position) const

Gets the material index of a voxel.

Parameters
position Voxel coordinates.
Returns Material index of the voxel.

bool cubos::engine::VoxelGrid::convert(const VoxelPalette& src, const VoxelPalette& dst, float minSimilarity)

Converts the material indices of this grid from one palette to another.

Parameters
src Original palette.
dst New palette.
minSimilarity Minimum similarity between two materials to consider them the same.
Returns Whether the conversion was successful.

For each material, it will search for another material in the second palette which is similar enough to the original one. The conversion fails if no matching index is found.

bool cubos::engine::VoxelGrid::loadFrom(core::memory::Stream& stream)

Loads the grid's data from the given stream.

Parameters
stream Stream to read from.
Returns Whether the stream contained valid data.

Assumes the data is stored in big-endian (network byte order). The first bytes correspond to three uint32_t, which represent the size of the grid (x, y, z). The next bytes correspond to size.x * size.y * size.z uint16_t, which represent the actual voxel materials. The voxel data is indexed by x + y * size.x + z * size.x * size.y.

bool cubos::engine::VoxelGrid::writeTo(core::memory::Stream& stream) const

Writes the grid's data to the given stream.

Parameters
stream Stream to write to.
Returns Whether the write was successful.

Writes in the format specified in loadFrom.