done with trying to copy treexy. it wasnt better after I implemented it. going to switch back to my method.

This commit is contained in:
Yggdrasil75
2026-01-13 14:56:59 -05:00
parent 082682a339
commit 40c761edb7
3 changed files with 75 additions and 117 deletions

View File

@@ -1,9 +1,41 @@
// test_voxel_render.cpp
#include "../util/grid/grid33.hpp"
//#include "../util/grid/treexy/treexy_serialization.hpp"
#include "../util/output/bmpwriter.hpp"
#include "../util/noise/pnoise2.hpp"
#include "../util/timing_decorator.cpp"
#include <random>
#include <iostream>
struct configuration {
float threshold = 0.1;
int gridWidth = 128;
int gridHeight = 128;
int gridDepth = 128;
PNoise2 noise = PNoise2(42);
};
void setup(configuration& config, VoxelGrid<Vec3ui8, 2, 3>& grid) {
TIME_FUNCTION;
uint8_t thresh = config.threshold * 255;
for (int z = 0; z < config.gridDepth; ++z) {
if (z % 64 == 0) {
std::cout << "Processing layer " << z << " of " << config.gridDepth << std::endl;
}
for (int y = 0; y < config.gridHeight; ++y) {
for (int x = 0; x < config.gridWidth; ++x) {
uint8_t r = std::clamp(config.noise.permute(Vec3f(static_cast<float>(x) / config.gridWidth / 64, static_cast<float>(y) / config.gridHeight / 64, static_cast<float>(z) / config.gridDepth / 64)), 0.f, 1.f) * 255;
uint8_t g = std::clamp(config.noise.permute(Vec3f(static_cast<float>(x) / config.gridWidth / 32, static_cast<float>(y) / config.gridHeight / 32, static_cast<float>(z) / config.gridDepth / 32)), 0.f, 1.f) * 255;
uint8_t b = std::clamp(config.noise.permute(Vec3f(static_cast<float>(x) / config.gridWidth / 16, static_cast<float>(y) / config.gridHeight / 16, static_cast<float>(z) / config.gridDepth / 16)), 0.f, 1.f) * 255;
uint8_t a = std::clamp(config.noise.permute(Vec3f(static_cast<float>(x) / config.gridWidth / 8 , static_cast<float>(y) / config.gridHeight / 8 , static_cast<float>(z) / config.gridDepth / 8 )), 0.f, 1.f) * 255;
if (a > thresh) {
bool wasOn = grid.setVoxelColor(Vec3d(x,y,z), Vec3ui8(r,g,b));
}
}
}
}
}
int main() {
// Initialize random number generator
std::random_device rd;
@@ -13,33 +45,9 @@ int main() {
// Create a voxel grid with 0.1 unit resolution
VoxelGrid<Vec3ui8, 2, 3> voxelGrid(0.1);
std::cout << "Placing 10 random colored voxels..." << std::endl;
// Place 10 random colored voxels
for (int i = 0; i < 10; ++i) {
// Generate random position
double x = pos_dist(gen);
double y = pos_dist(gen);
double z = pos_dist(gen);
// Generate random color
uint8_t r = static_cast<uint8_t>(color_dist(gen));
uint8_t g = static_cast<uint8_t>(color_dist(gen));
uint8_t b = static_cast<uint8_t>(color_dist(gen));
Vec3d worldPos(x, y, z);
Vec3ui8 color(r, g, b);
// Set voxel color
bool wasOn = voxelGrid.setVoxelColor(worldPos, color);
std::cout << "Voxel " << i + 1 << ": "
<< "Pos(" << x << ", " << y << ", " << z << ") "
<< "Color(RGB:" << static_cast<int>(r) << ","
<< static_cast<int>(g) << "," << static_cast<int>(b) << ") "
<< (wasOn ? "(overwritten)" : "(new)") << std::endl;
}
configuration config;
setup(config, voxelGrid);
std::cout << "\nMemory usage: " << voxelGrid.getMemoryUsage() << " bytes" << std::endl;
@@ -53,15 +61,16 @@ int main() {
std::vector<uint8_t> imageBuffer;
// Render with orthographic projection (view along Z axis)
voxelGrid.renderProjectedToRGBBuffer(imageBuffer, width, height,
Vec3d(0, 0, 1), // View direction (looking along Z)
Vec3d(0, 1, 0)); // Up direction
Vec3d camPos = Vec3d(config.gridDepth, config.gridHeight, config.gridWidth * 2);
Vec3d lookAt = Vec3d(config.gridDepth / 2, config.gridHeight / 2, config.gridWidth / 2);
Vec3d viewDir = (lookAt-camPos).normalized();
voxelGrid.renderToRGB(imageBuffer, width, height, camPos, viewDir, Vec3d(0, 1, 0), 80.f);
std::cout << "Image buffer size: " << imageBuffer.size() << " bytes" << std::endl;
std::cout << "Expected size: " << (width * height * 3) << " bytes" << std::endl;
// Save to BMP using BMPWriter
std::string filename = "voxel_render.bmp";
std::string filename = "output/voxel_render.bmp";
// Create a frame object from the buffer
frame renderFrame(width, height, frame::colormap::RGB);
@@ -71,33 +80,8 @@ int main() {
if (BMPWriter::saveBMP(filename, renderFrame)) {
std::cout << "Successfully saved to: " << filename << std::endl;
// Also save using the direct vector interface as backup
if (BMPWriter::saveBMP("voxel_render_direct.bmp", imageBuffer, width, height)) {
std::cout << "Also saved direct version: voxel_render_direct.bmp" << std::endl;
}
} else {
std::cout << "Failed to save BMP!" << std::endl;
// Try alternative save method
std::cout << "Trying alternative save method..." << std::endl;
// Convert to Vec3ui8 format
std::vector<Vec3ui8> pixelsVec3;
pixelsVec3.reserve(width * height);
for (size_t i = 0; i < imageBuffer.size(); i += 3) {
pixelsVec3.push_back(Vec3ui8(
imageBuffer[i],
imageBuffer[i + 1],
imageBuffer[i + 2]
));
}
if (BMPWriter::saveBMP("voxel_render_vec3.bmp", pixelsVec3, width, height)) {
std::cout << "Saved Vec3ui8 version: voxel_render_vec3.bmp" << std::endl;
} else {
std::cout << "All save methods failed!" << std::endl;
}
}
// Test accessor functionality
@@ -132,6 +116,7 @@ int main() {
});
std::cout << "\nTotal voxels in grid: " << voxelCount << std::endl;
FunctionTimer::printStats(FunctionTimer::Mode::ENHANCED);
return 0;
}