cubos::engine::VoxelPalette class final

Holds a palette of materials. Supports up to 65535 materials.

Instead of storing the materials themselves in voxel data, Cubos uses palettes, and stores the index of the material in the palette instead.

This allows for more efficient storage of voxel data, since now instead of storing the whole material per each voxel, we just store a 16-bit integer.

Public types

class Iterator
Used to iterate over materials on the palette.

Constructors, destructors, conversion operators

VoxelPalette(std::vector<VoxelMaterial>&& materials)
Constructs a palette with the given materials.
VoxelPalette() defaulted
Constructs an empty palette.

Public functions

auto data() const -> const VoxelMaterial*
Gets a pointer to the array of materials on the palette.
auto size() const -> uint16_t
Gets the number of materials in the palette, excluding the empty material.
auto get(uint16_t index) const -> const VoxelMaterial&
Gets the material with the given index.
void set(uint16_t index, const VoxelMaterial& material)
Sets the material at the given index.
auto find(const VoxelMaterial& material) const -> uint16_t
Searches for the index of the material most similar with the given material.
auto add(const VoxelMaterial& material, float similarity = 1.0F) -> uint16_t
Adds a material to the palette, if one not similar enough already exists.
auto push(const VoxelMaterial& material) -> uint16_t
Pushes a material to the palette without checking for uniqueness.
void merge(const VoxelPalette& palette, float similarity = 1.0F)
Merges another palette into this one.
auto begin() -> Iterator
Returns an iterator to the first material.
auto end() -> Iterator
Returns an iterator to the last material.
auto loadFrom(core::memory::Stream& stream) -> bool
Loads the palette's data from the given stream.
auto writeTo(core::memory::Stream& stream) const -> bool
Writes the palette's data to the given stream.

Function documentation

cubos::engine::VoxelPalette::VoxelPalette(std::vector<VoxelMaterial>&& materials)

Constructs a palette with the given materials.

Parameters
materials Materials to add to the palette.

const VoxelMaterial* cubos::engine::VoxelPalette::data() const

Gets a pointer to the array of materials on the palette.

Returns Pointer to the array of materials on the palette.

uint16_t cubos::engine::VoxelPalette::size() const

Gets the number of materials in the palette, excluding the empty material.

Returns Number of materials in the palette.

const VoxelMaterial& cubos::engine::VoxelPalette::get(uint16_t index) const

Gets the material with the given index.

Parameters
index Index of the material to get (1-based, 0 is empty).
Returns Material at the given index.

void cubos::engine::VoxelPalette::set(uint16_t index, const VoxelMaterial& material)

Sets the material at the given index.

Parameters
index Index of the material to set (1-based, 0 is empty).
material Material to set.

uint16_t cubos::engine::VoxelPalette::find(const VoxelMaterial& material) const

Searches for the index of the material most similar with the given material.

Parameters
material Material to compare with.
Returns Index of the material.

uint16_t cubos::engine::VoxelPalette::add(const VoxelMaterial& material, float similarity = 1.0F)

Adds a material to the palette, if one not similar enough already exists.

Parameters
material Material to add.
similarity Minimum similarity for a material to be considered similar enough.
Returns Index of the material in the palette (1-based, 0 is empty).

uint16_t cubos::engine::VoxelPalette::push(const VoxelMaterial& material)

Pushes a material to the palette without checking for uniqueness.

Parameters
material Material to push.
Returns Size of the palette.

void cubos::engine::VoxelPalette::merge(const VoxelPalette& palette, float similarity = 1.0F)

Merges another palette into this one.

Parameters
palette Palette to merge.
similarity Minimum similarity for two materials to be merged.

Iterator cubos::engine::VoxelPalette::begin()

Returns an iterator to the first material.

Returns Iterator.

Iterator cubos::engine::VoxelPalette::end()

Returns an iterator to the last material.

Returns Iterator.

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

Loads the palette'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 an uint16_t, which represents the number of materials in the palette. The next bytes correspond to numMaterials * 4 floats (each material is represented by 4 floats (r, g, b, a)), which represents the actual palette data.

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

Writes the palette'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.