From dc36b93e4f04893e56250efe2bb448feff63b54c Mon Sep 17 00:00:00 2001 From: yggdrasil75 Date: Sat, 21 Feb 2026 11:05:11 -0500 Subject: [PATCH] some changes for speed and usability with the fast version --- makefile | 2 +- util/grid/grid3eigen.hpp | 40 ++++++++++++++++++++++++++-------------- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/makefile b/makefile index d3ef09d..60b4625 100644 --- a/makefile +++ b/makefile @@ -7,7 +7,7 @@ STB_DIR := ./stb # Compiler and flags CXX := g++ -BASE_CXXFLAGS = -std=c++23 -O3 -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(STB_DIR) +BASE_CXXFLAGS = -std=c++23 -O3 -fopenmp -march=native -I$(IMGUI_DIR) -I$(IMGUI_DIR)/backends -I$(STB_DIR) BASE_CXXFLAGS += `pkg-config --cflags glfw3` CFLAGS = $(BASE_CXXFLAGS) LDFLAGS := -L./imgui -limgui -lGL diff --git a/util/grid/grid3eigen.hpp b/util/grid/grid3eigen.hpp index 1d850fe..73fbcc3 100644 --- a/util/grid/grid3eigen.hpp +++ b/util/grid/grid3eigen.hpp @@ -1177,10 +1177,10 @@ public: Ray oray(origin, direction); float tMin, tMax; - // if (rayBoxIntersect(oray, root_->bounds, tMin, tMax)) { + if (rayBoxIntersect(oray, root_->bounds, tMin, tMax)) { tMax = std::min(tMax, maxDist); voxelTraverseRecursive(root_.get(), tMin, tMax, maxDist, enableLOD, oray, hit, invLodf); - // } + } return hit; } @@ -1255,15 +1255,18 @@ public: frame outFrame(width, height, colorformat); std::vector colorBuffer; - int channels; - channels = 3; - colorBuffer.resize(width * height * channels); + colorBuffer.resize(width * height * 3); - float aspect = static_cast(width) / height; - float fovRad = cam.fovRad(); - float tanHalfFov = tan(fovRad * 0.5f); - float tanfovy = tanHalfFov; - float tanfovx = tanHalfFov * aspect; + const float aspect = static_cast(width) / height; + const float fovRad = cam.fovRad(); + const float tanHalfFov = tan(fovRad * 0.5f); + const float tanfovy = tanHalfFov; + const float tanfovx = tanHalfFov * aspect; + + const PointType globalLightDir = PointType(-3000.0f, 0.0f, 0.0f).normalized(); + const float fogStart = 1000.0f; + const float fogEnd = 4000.0f; + const float minVisibility = 0.2f; #pragma omp parallel for schedule(dynamic) collapse(2) for (int y = 0; y < height; ++y) { @@ -1283,19 +1286,28 @@ public: if (hit != nullptr) { auto obj = hit; + float t = 0.0f; + PointType normal, hitPoint; + + rayCubeIntersect(ray, obj.get(), t, normal, hitPoint); color = obj->color; if (obj->light) { color = color * obj->emittance; + } else { + float diffuse = std::max(0.0f, normal.dot(globalLightDir)); + float ambient = 0.35f; + float intensity = std::min(1.0f, ambient + diffuse * 0.65f); + color = color * intensity; } - PointType normal = -rayDir; + float fogFactor = std::clamp((fogEnd - t) / (fogEnd - fogStart), minVisibility, 1.0f); - // Simple directional lighting - float lightDot = std::max(0.2f, normal.dot(-rayDir)); - color = color * lightDot; + color = color * fogFactor + backgroundColor_ * (1.0f - fogFactor); } + color = color.cwiseMax(0.0f).cwiseMin(1.0f); + colorBuffer[idx ] = color[0]; colorBuffer[idx + 1] = color[1]; colorBuffer[idx + 2] = color[2];