From c39a3beeff5824b47b60ef09e15aae048291728f Mon Sep 17 00:00:00 2001 From: Yggdrasil75 Date: Wed, 14 Jan 2026 08:11:37 -0500 Subject: [PATCH] pushing serialization. need to do it better in a dedicated file later. --- tests/g3test2.cpp | 8 +++ util/grid/grid3.hpp | 100 +++++++++++++++++++++++++++++++++++- util/grid/serialization.hpp | 22 ++++++++ 3 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 util/grid/serialization.hpp diff --git a/tests/g3test2.cpp b/tests/g3test2.cpp index d959ed4..e1b861c 100644 --- a/tests/g3test2.cpp +++ b/tests/g3test2.cpp @@ -59,6 +59,7 @@ void setup(defaults config, VoxelGrid& grid) { } } std::cout << "Noise grid generation complete!" << std::endl; + grid.serializeToFile("output/gridsave.ygg3"); grid.printStats(); } @@ -176,6 +177,13 @@ int main() { defaults config; VoxelGrid grid; bool gridInitialized = false; + auto supposedGrid = VoxelGrid::deserializeFromFile("output/gridsave.ygg3"); + if (supposedGrid) { + grid = std::move(*supposedGrid); + gridInitialized = true; + } + + Camera cam(Vec3f(config.gridWidth/2.0f, config.gridHeight/2.0f, config.gridDepth/2.0f), Vec3f(0,0,1), Vec3f(0,1,0), 80); diff --git a/util/grid/grid3.hpp b/util/grid/grid3.hpp index a8b0c15..fab00aa 100644 --- a/util/grid/grid3.hpp +++ b/util/grid/grid3.hpp @@ -2,6 +2,8 @@ #define GRID3_HPP #include +#include +#include #include "../vectorlogic/vec2.hpp" #include "../vectorlogic/vec3.hpp" #include "../vectorlogic/vec4.hpp" @@ -9,10 +11,13 @@ #include "../output/frame.hpp" #include "../noise/pnoise2.hpp" #include "../vecmat/mat4.hpp" +//#include "serialization.hpp" #include #include #include "../basicdefines.hpp" +constexpr char magic[4] = {'Y', 'G', 'G', '3'}; + struct Voxel { //float active; bool active; @@ -109,7 +114,95 @@ public: VoxelGrid(int w, int h, int d) : gridSize(w,h,d) { voxels.resize(w * h * d); } - + + //bool serializeToFile(const VoxelGrid grid, const std::string& filename); + bool serializeToFile(const std::string& filename) { + std::ofstream file(filename, std::ios::binary); + if (!file.is_open()) { + std::cerr << "failed to open file (serializeToFile): " << filename << std::endl; + return false; + } + + file.write(magic, 4); + + //file.write(reinterpret_cast(&binSize), sizeof(binSize)); + + // Write grid dimensions + int dims[3] = {gridSize.x, gridSize.y, gridSize.z}; + file.write(reinterpret_cast(dims), sizeof(dims)); + + // Write voxel data + size_t voxelCount = voxels.size(); + file.write(reinterpret_cast(&voxelCount), sizeof(voxelCount)); + + // Write each voxel + for (const Voxel& voxel : voxels) { + file.write(reinterpret_cast(&voxel.active), sizeof(voxel.active)); + file.write(reinterpret_cast(&voxel.color.x), sizeof(voxel.color.x)); + file.write(reinterpret_cast(&voxel.color.y), sizeof(voxel.color.y)); + file.write(reinterpret_cast(&voxel.color.z), sizeof(voxel.color.z)); + } + + file.close(); + return !file.fail(); + } + + static std::unique_ptr deserializeFromFile(const std::string& filename) { + VoxelGrid outgrid; + std::ifstream file(filename, std::ios::binary); + if (!file.is_open()) { + std::cerr << "Error: Could not open file for reading: " << filename << std::endl; + return nullptr; + } + + // Read and verify magic number + char filemagic[4]; + file.read(filemagic, 4); + if (std::strncmp(filemagic, "YGG7", 4) != 0) { + std::cerr << "Error: Invalid file format or corrupted file" << std::endl; + return nullptr; + } + + // Read binSize + //file.read(reinterpret_cast(&binSize), sizeof(binSize)); + + // Read grid dimensions + int dims[3]; + file.read(reinterpret_cast(dims), sizeof(dims)); + outgrid.resize(Vec3i(dims[0], dims[1], dims[2])); + //gridSize = Vec3i(dims[0], dims[1], dims[2]); + + // Read voxel count + size_t voxelCount; + file.read(reinterpret_cast(&voxelCount), sizeof(voxelCount)); + + // Verify voxel count matches grid dimensions + size_t expectedCount = static_cast(dims[0]) * dims[1] * dims[2]; + if (voxelCount != expectedCount) { + std::cerr << "Error: Voxel count mismatch. Expected " << expectedCount + << ", found " << voxelCount << std::endl; + return nullptr; + } + + // Resize and read voxels + //voxels.resize(voxelCount); + grid->voxels.resize(voxelCount); + for (size_t i = 0; i < voxelCount; ++i) { + file.read(reinterpret_cast(&grid->voxels[i].active), sizeof(grid->voxels[i].active)); + file.read(reinterpret_cast(&grid->voxels[i].color.x), sizeof(grid->voxels[i].color.x)); + file.read(reinterpret_cast(&grid->voxels[i].color.y), sizeof(grid->voxels[i].color.y)); + file.read(reinterpret_cast(&grid->voxels[i].color.z), sizeof(grid->voxels[i].color.z)); + } + + file.close(); + if (file.fail()) { + std::cerr << "Error: Failed to read from file: " << filename << std::endl; + return nullptr; + } + + return grid; + } + Voxel& get(int x, int y, int z) { return voxels[z * gridSize.x * gridSize.y + y * gridSize.x + x]; } @@ -142,6 +235,10 @@ public: gridSize = Vec3i(newW, newH, newD); } + void resize(Vec3i newsize) { + resize(newsize.x, newsize.y, newsize.z); + } + void set(int x, int y, int z, bool active, Vec3ui8 color) { set(Vec3i(x,y,z), active, color); } @@ -303,7 +400,6 @@ public: return outFrame; } - void printStats() const { int totalVoxels = gridSize.x * gridSize.y * gridSize.z; int activeVoxels = 0; diff --git a/util/grid/serialization.hpp b/util/grid/serialization.hpp new file mode 100644 index 0000000..6e9e260 --- /dev/null +++ b/util/grid/serialization.hpp @@ -0,0 +1,22 @@ +#ifndef GRID3_Serialization +#define GRID3_Serialization + +#include +#include +#include "grid3.hpp" + +constexpr char magic[4] = {'Y', 'G', 'G', '3'}; + +inline bool serializeToFile(const VoxelGrid& grid, const std::string& filename) { + std::ofstream file(filename, std::ios::binary); + if (!file.is_open()) { + std::cerr << "failed to open file (serializeToFile): " << filename << std::endl; + return false; + } + + file.write(magic, 4); + //int dims[3] = {grid.gridSize.x, grid.gridSize.y, grid.gridSize.z}; + +} + +#endif \ No newline at end of file