From 7bc711a3ad513263b6471e6843a455484e4e08c8 Mon Sep 17 00:00:00 2001 From: yggdrasil75 Date: Fri, 2 Jan 2026 05:15:08 -0500 Subject: [PATCH] fixed 1 direction --- tests/g3test2.cpp | 33 +++++++++++------ util/grid/grid3.hpp | 9 +++-- util/vectorlogic/vec3.hpp | 75 ++++++++++++++++++++++++--------------- 3 files changed, 75 insertions(+), 42 deletions(-) diff --git a/tests/g3test2.cpp b/tests/g3test2.cpp index fe3c27b..667ab27 100644 --- a/tests/g3test2.cpp +++ b/tests/g3test2.cpp @@ -5,23 +5,28 @@ #include "../util/grid/grid3.hpp" #include "../util/output/bmpwriter.hpp" #include "../util/noise/pnoise2.hpp" +#include "../util/timing_decorator.cpp" // Constants -const size_t GRID_SIZE = 1024; +const size_t GRID_SIZE = 128; const size_t RENDER_WIDTH = 512; const size_t RENDER_HEIGHT = 512; // Function to generate grayscale noise grid void generateNoiseGrid(VoxelGrid& grid, PNoise2& noise) { + TIME_FUNCTION; std::cout << "Generating 1024x1024x1024 noise grid..." << std::endl; // Generate Perlin noise in the grid + #pragma omp parallel for for (size_t z = 0; z < GRID_SIZE; ++z) { if (z % 64 == 0) { std::cout << "Processing layer " << z << " of " << GRID_SIZE << std::endl; } + #pragma omp parallel for for (size_t y = 0; y < GRID_SIZE; ++y) { + #pragma omp parallel for for (size_t x = 0; x < GRID_SIZE; ++x) { // Create 3D noise coordinates (scaled for better frequency) float scale = 0.05f; // Controls noise frequency @@ -34,12 +39,16 @@ void generateNoiseGrid(VoxelGrid& grid, PNoise2& noise) { // Higher threshold = sparser voxels float threshold = 0.3f; float active = (normalizedNoise > threshold) ? normalizedNoise : 0.0f; + //float active = 1; // Create grayscale color based on noise value uint8_t grayValue = static_cast(normalizedNoise * 255); Vec3ui8 color(grayValue, grayValue, grayValue); - - grid.set(x, y, z, active, color); + #pragma omp critical + if (active > threshold) { + grid.set(x, y, z, active, color); + //std::cout << "setting a voxel to color: " << color << " with alpha of " << active << std::endl; + } } } } @@ -50,7 +59,7 @@ void generateNoiseGrid(VoxelGrid& grid, PNoise2& noise) { // Function to render grid from a specific viewpoint bool renderView(const std::string& filename, VoxelGrid& grid, const Vec3f& position, const Vec3f& direction, const Vec3f& up = Vec3f(0, 1, 0)) { - + TIME_FUNCTION; Camera cam(position, direction, up); std::vector renderBuffer; @@ -92,45 +101,46 @@ int main() { // Camera distance from center (outside the grid) float cameraDistance = GRID_SIZE * 2.0f; + float cameraDir = GRID_SIZE * 0.5f; // Render from 4 cardinal directions (looking at center) std::cout << "\nRendering cardinal views..." << std::endl; // North view (looking from positive Z towards center) - renderView("north_view.bmp", grid, + renderView("output/north_view.bmp", grid, gridCenter + Vec3f(0, 0, cameraDistance), Vec3f(0, 0, -1)); // Look towards negative Z // South view (looking from negative Z towards center) - renderView("south_view.bmp", grid, + renderView("output/south_view.bmp", grid, gridCenter + Vec3f(0, 0, -cameraDistance), Vec3f(0, 0, 1)); // Look towards positive Z // East view (looking from positive X towards center) - renderView("east_view.bmp", grid, + renderView("output/east_view.bmp", grid, gridCenter + Vec3f(cameraDistance, 0, 0), Vec3f(-1, 0, 0)); // Look towards negative X // West view (looking from negative X towards center) - renderView("west_view.bmp", grid, + renderView("output/west_view.bmp", grid, gridCenter + Vec3f(-cameraDistance, 0, 0), Vec3f(1, 0, 0)); // Look towards positive X // Zenith view (looking from above) std::cout << "\nRendering zenith and nadir views..." << std::endl; - renderView("zenith_view.bmp", grid, + renderView("output/zenith_view.bmp", grid, gridCenter + Vec3f(0, cameraDistance, 0), Vec3f(0, -1, 0), // Look down Vec3f(0, 0, -1)); // Adjust up vector for proper orientation // Nadir view (looking from below) - renderView("nadir_view.bmp", grid, + renderView("output/nadir_view.bmp", grid, gridCenter + Vec3f(0, -cameraDistance, 0), Vec3f(0, 1, 0), // Look up Vec3f(0, 0, 1)); // Adjust up vector // Optional: Create a front view (alternative to north) - renderView("front_view.bmp", grid, + renderView("output/front_view.bmp", grid, gridCenter + Vec3f(0, 0, cameraDistance), Vec3f(0, 0, -1), Vec3f(0, 1, 0)); @@ -151,4 +161,5 @@ int main() { std::cerr << "Error: " << e.what() << std::endl; return 1; } + FunctionTimer::printStats(FunctionTimer::Mode::ENHANCED); } \ No newline at end of file diff --git a/util/grid/grid3.hpp b/util/grid/grid3.hpp index 7997456..849d150 100644 --- a/util/grid/grid3.hpp +++ b/util/grid/grid3.hpp @@ -147,7 +147,7 @@ public: tDelta.x = std::abs(1.0 / rayDir.x); tDelta.y = std::abs(1.0 / rayDir.y); tDelta.z = std::abs(1.0 / rayDir.z); - tMax = mix((rayOrigin - currentVoxel) / -rayDir, ((currentVoxel + 1) - rayOrigin) / rayDir, rayDir > 0); + tMax = mix(((rayOrigin - currentVoxel.toFloat()) / -rayDir).toFloat(), (((currentVoxel.toFloat() + 1) - rayOrigin) / rayDir).toFloat(), rayDir.mask([](float x, float value) { return x > 0; }, 0)); if (!inGrid(rayOrigin)) { /* The initialization phase begins by identifying the voxel in which the ray origin, → @@ -179,9 +179,9 @@ public: if (tEntry < 0.0) tEntry = 0.0; if (tEntry > 0.0) { - rayOrigin = rayOrigin + rayDir + tEntry; + rayOrigin = rayOrigin + rayDir * tEntry; currentVoxel = rayOrigin.floorToT(); - tMax = mix(((currentVoxel + 1) - rayOrigin) / rayDir, (rayOrigin - currentVoxel) / -rayDir, rayDir > 0 ); + tMax = mix(((currentVoxel.toFloat() + 1) - rayOrigin) / rayDir, (rayOrigin - currentVoxel) / -rayDir, rayDir.mask([](float x, float value) { return x > 0; }, 0) ); } } @@ -238,6 +238,9 @@ public: } } } + if (aalpha > EPSILON) { + std::cout << "hit at: " << currentVoxel << " with value of " << aalpha << std::endl; + } return hit; } diff --git a/util/vectorlogic/vec3.hpp b/util/vectorlogic/vec3.hpp index 1e22e78..6892a01 100644 --- a/util/vectorlogic/vec3.hpp +++ b/util/vectorlogic/vec3.hpp @@ -27,19 +27,23 @@ public: } // Arithmetic operations - Vec3 operator+(const Vec3& other) const { + template + Vec3 operator+(const Vec3& other) const { return Vec3(x + other.x, y + other.y, z + other.z); } - Vec3 operator-(const Vec3& other) const { + template + Vec3 operator-(const Vec3& other) const { return Vec3(x - other.x, y - other.y, z - other.z); } - Vec3 operator*(const Vec3& other) const { + template + Vec3 operator*(const Vec3& other) const { return Vec3(x * other.x, y * other.y, z * other.z); } - Vec3 operator/(const Vec3& other) const { + template + Vec3 operator/(const Vec3& other) const { return Vec3(x / other.x, y / other.y, z / other.z); } @@ -233,6 +237,17 @@ public: return x >= other.x || y >= other.y || z >= other.z; } + template + Vec3 mask(CompareFunc comp, T value) const { + return Vec3(comp(x, value), comp(y, value), comp(z, value)); + } + + template + Vec3 mask(CompareFunc comp, const Vec3& other) const { + return Vec3(comp(x, other.x), comp(y, other.y), comp(z, other.z)); + } + + Vec3 abs() const { return Vec3(std::abs(x), std::abs(y), std::abs(z)); } @@ -248,6 +263,10 @@ public: Vec3 floorToT() const { return Vec3(static_cast(std::floor(x)), static_cast(std::floor(x)), static_cast(std::floor(z))); } + + Vec3 toFloat() const { + return Vec3(static_cast(x), static_cast(y), static_cast(z)); + } Vec3 ceil() const { return Vec3(std::ceil(x), std::ceil(y), std::ceil(z)); @@ -292,33 +311,33 @@ public: } // Template friend operators to allow different scalar types - template - friend Vec3 operator+(S scalar, const Vec3& vec) { - return Vec3(static_cast(scalar + vec.x), - static_cast(scalar + vec.y), - static_cast(scalar + vec.z)); - } + // template + // friend Vec3 operator+(S scalar, const Vec3& vec) { + // return Vec3(static_cast(scalar + vec.x), + // static_cast(scalar + vec.y), + // static_cast(scalar + vec.z)); + // } - template - friend Vec3 operator-(S scalar, const Vec3& vec) { - return Vec3(static_cast(scalar - vec.x), - static_cast(scalar - vec.y), - static_cast(scalar - vec.z)); - } + // template + // friend Vec3 operator-(S scalar, const Vec3& vec) { + // return Vec3(static_cast(scalar - static_cast(vec.x)), + // static_cast(scalar - static_cast(vec.y)), + // static_cast(scalar - static_cast(vec.z))); + // } - template - friend Vec3 operator*(S scalar, const Vec3& vec) { - return Vec3(static_cast(scalar * vec.x), - static_cast(scalar * vec.y), - static_cast(scalar * vec.z)); - } + // template + // friend Vec3 operator*(S scalar, const Vec3& vec) { + // return Vec3(static_cast(scalar * vec.x), + // static_cast(scalar * vec.y), + // static_cast(scalar * vec.z)); + // } - template - friend Vec3 operator/(S scalar, const Vec3& vec) { - return Vec3(static_cast(scalar / vec.x), - static_cast(scalar / vec.y), - static_cast(scalar / vec.z)); - } + // template + // friend Vec3 operator/(S scalar, const Vec3& vec) { + // return Vec3(static_cast(scalar / vec.x), + // static_cast(scalar / vec.y), + // static_cast(scalar / vec.z)); + // } Vec3 reflect(const Vec3& normal) const { return *this - 2.0f * this->dot(normal) * normal;