Skip to content

Examples

Pain67 edited this page May 24, 2025 · 2 revisions

Basic Example

#include "fVoxel.hpp"

int main() {

//.....

// Create an instance of the world
fVoxelWorld World;

// Set the world properties
// These properties needs to be set before world init
// They cannot be changed afterwards.
// NOTE: if world is loading, these will be overriten by saved values
World.SetChunkVoxelSize(33,255,33); // Number of Voxels per chunk
World.SetRegionSize(33,33); // Number of chunks per region
World.SetWorldSize(32,32); // Number of Chunks that can simultaneously exist in the world

// Once World is set up we can create a World
// The argument expects an absolute path to a folder
// this is where the World data will be saved
// NOTE: based on the content of the folder, this will either created a
// New empty world, or load the world that already present in the folder.
World.CreateWorld("/path/to/save/folder/")


// There are some properties of the world that can be change "on-the-fly"
World.SetVoxelSize(1.0F,1.0F,1.0F); // Sets the size of a single voxel in world space - used when determining where each voxel mesh needs to be
World.SetTextureSteps(0.5F, 0.5F); // Sets the texture coordinates offset. Texturing the World works based on texture atlases

// This will make the world use a simple cube as the voxel mesh
// This needs to be called after Voxel size has been set as this function
// Will generate the "cube" using the current voxel size
World.UseDefaultVoxelMesh();

// Now that we have a world that is initialised and ready,
// We still needs to give it some Voxeldefinitions
// Create 4 example voxel
std::vector<fVoxelBlock> VList;
VList.push_back(fVoxelBlock(0,"Block0",{0,0},0));
VList.push_back(fVoxelBlock(1,"Block1",{1,0},0));
VList.push_back(fVoxelBlock(2,"Block2",{0,1},0));
VList.push_back(fVoxelBlock(3,"Block3",{1,1},0));

World.SetVoxelList(VList);

// At this point we can start spawning chunks to work with
// This will create a chunk at position 0,0 in world space
World.SpawnChunk(0,0);

// To generate the chunk data, a pointer to the chunk can be obtained:
// The resulting value can be a "nullptr" indicating that the requested chunk not found
fVoxelChunk* ChunkPtr = World.GetChunkPtr(0,0);
if (ChunkPtr == nullptr) {
    // TODO: handle unable to get chunk data pointer.
}

// Also get the chunk size for the iteration
fVector3ui ChunkSize = World.GetChunkSize();

// Now we have access to the chunk data, just iterate each voxel and set it to a random value
for (fUInt Z = 0; Z < ChunkSize.Z; Z++) {
    for (fUInt X = 0; X < ChunkSize.X; X++) {
        // Generate a random heigh at this X,Z column
        fUInt CurrH = rand() % ChunkSize.Y;

        // Use this height to only "spawn" voxels up to
        // Any voxel above this will keeps its default value of F_UINT_MAX indicating it is "non-existing"
        for (fUInt Y = 0; Y < CurrH; Y++) {
            // Get an index for the Voxel we aim to set
            // This can be calculated manually but this function
            // for easy of use and to avoid issues in axis orders
            fUInt Index = ChunkPtr->GetVoxelIndex(X,Y,Z);

            // Set the value of the voxels to a random entry in the FVoxelList (Currently contains 4 elements).
            ChunkPtr->BlockList[Index] = rand() % 4;
        }
    }
}

// Now we generated the voxel for this chunk, we can mark it as such
// NOTE: at this point this variable is not internally used
ChunkPtr->isVoxelGenerated = true;

// Also need to make sure that the updated chunk will be saved
// Since we get a pointer to the chunk, the engine has no way of knowing
// if we made any changes so we have to manually mark it as changed
ChunkPtr->isModified = true;


// After Voxels generated, we can create a mesh for this chunk
// mesh generated from the World perspective to enable chunk borders to only
// render faces if needed based on the surroundings chunks
fProcMesh ChunkMesh;
if (!World.GenerateChunkMesh(0,0, ChunkMesh)) {
    // TODO: Handle unable to generate chunk mesh.
}

// All done, ChunkMesh ready to be used / rendered as needed
//.....
// TODO: Rest of the code
//.....

// Once allpication finished working with the world, save all outstanding changes
World.SaveWorld();

// Clean up world and deallocate any memory currently in use
World.UnloadWorld();

// At this point, all resources freed and another world can be created / loaded
// or application can terminated safely
return 0;
}

Clone this wiki locally