removed spheres

This commit is contained in:
Yggdrasil75
2026-02-10 13:22:46 -05:00
parent 2db9575bd1
commit d07325932c
3 changed files with 49 additions and 134 deletions

View File

@@ -127,7 +127,7 @@ void createSphere(const defaults& config, const spheredefaults& sconfig, Octree<
pos.z() >= 0 && pos.z() < config.gridSizecube) { pos.z() >= 0 && pos.z() < config.gridSizecube) {
grid.set(1, pos, true, colorVec, voxelSize, true, 1, grid.set(1, pos, true, colorVec, voxelSize, true, 1,
sconfig.light, sconfig.emittance, sconfig.refraction, sconfig.reflection, Octree<int>::Shape::CUBE); sconfig.light, sconfig.emittance, sconfig.refraction, sconfig.reflection);
} }
} }
} }

View File

@@ -29,11 +29,6 @@ public:
using PointType = Eigen::Matrix<float, Dim, 1>; using PointType = Eigen::Matrix<float, Dim, 1>;
using BoundingBox = std::pair<PointType, PointType>; using BoundingBox = std::pair<PointType, PointType>;
enum class Shape {
SPHERE,
CUBE
};
struct NodeData { struct NodeData {
T data; T data;
PointType position; PointType position;
@@ -46,17 +41,16 @@ public:
float emittance; float emittance;
float refraction; float refraction;
float reflection; float reflection;
Shape shape;
NodeData(const T& data, const PointType& pos, bool visible, Eigen::Vector3f color, float size = 0.01f, NodeData(const T& data, const PointType& pos, bool visible, Eigen::Vector3f color, float size = 0.01f,
bool active = true, int objectId = -1, bool light = false, float emittance = 0.0f, bool active = true, int objectId = -1, bool light = false, float emittance = 0.0f,
float refraction = 0.0f, float reflection = 0.0f, Shape shape = Shape::SPHERE) float refraction = 0.0f, float reflection = 0.0f)
: data(data), position(pos), objectId(objectId), active(active), visible(visible), : data(data), position(pos), objectId(objectId), active(active), visible(visible),
color(color), size(size), light(light), emittance(emittance), refraction(refraction), color(color), size(size), light(light), emittance(emittance), refraction(refraction),
reflection(reflection), shape(shape) {} reflection(reflection) {}
NodeData() : objectId(-1), active(false), visible(false), size(0.0f), light(false), NodeData() : objectId(-1), active(false), visible(false), size(0.0f), light(false),
emittance(0.0f), refraction(0.0f), reflection(0.0f), shape(Shape::SPHERE) {} emittance(0.0f), refraction(0.0f), reflection(0.0f) {}
// Helper method to get half-size for cube // Helper method to get half-size for cube
PointType getHalfSize() const { PointType getHalfSize() const {
@@ -215,7 +209,6 @@ private:
} }
} }
auto accumulate = [&](const std::shared_ptr<NodeData>& item) { auto accumulate = [&](const std::shared_ptr<NodeData>& item) {
if (!item || !item->active || !item->visible) return; if (!item || !item->active || !item->visible) return;
avgPos += item->position; avgPos += item->position;
@@ -255,7 +248,6 @@ private:
lod->light = anyLight; lod->light = anyLight;
lod->active = true; lod->active = true;
lod->visible = true; lod->visible = true;
lod->shape = Shape::CUBE;
lod->objectId = -1; lod->objectId = -1;
node->lodData = lod; node->lodData = lod;
@@ -304,7 +296,6 @@ private:
writeVal(out, pt->emittance); writeVal(out, pt->emittance);
writeVal(out, pt->refraction); writeVal(out, pt->refraction);
writeVal(out, pt->reflection); writeVal(out, pt->reflection);
writeVal(out, static_cast<int>(pt->shape));
} }
} else { } else {
// Write bitmask of active children // Write bitmask of active children
@@ -351,7 +342,6 @@ private:
readVal(in, pt->reflection); readVal(in, pt->reflection);
int shapeInt; int shapeInt;
readVal(in, shapeInt); readVal(in, shapeInt);
pt->shape = static_cast<Shape>(shapeInt);
node->points.push_back(pt); node->points.push_back(pt);
} }
} else { } else {
@@ -439,26 +429,6 @@ private:
return tMax >= std::max(0.0f, tMin); return tMax >= std::max(0.0f, tMin);
} }
bool raySphereIntersect(const Ray& ray, const PointType& center, float radiusSq, float& t) const {
PointType oc = ray.origin - center;
float b = 2.0f * oc.dot(ray.dir);
float c = oc.dot(oc) - radiusSq;
float discriminant = b * b - 4 * c;
if (discriminant < 0) return false;
float sqrtDisc = sqrt(discriminant);
float t0 = (-b - sqrtDisc) * 0.5f;
float t1 = (-b + sqrtDisc) * 0.5f;
t = t0;
if (t0 < EPSILON) {
t = t1;
if (t1 < EPSILON) return false;
}
return true;
}
bool rayCubeIntersect(const Ray& ray, const NodeData* cube, bool rayCubeIntersect(const Ray& ray, const NodeData* cube,
float& t, PointType& normal, PointType& hitPoint) const { float& t, PointType& normal, PointType& hitPoint) const {
BoundingBox bounds = cube->getCubeBounds(); BoundingBox bounds = cube->getCubeBounds();
@@ -563,9 +533,9 @@ public:
bool set(const T& data, const PointType& pos, bool visible, Eigen::Vector3f color, float size, bool active, bool set(const T& data, const PointType& pos, bool visible, Eigen::Vector3f color, float size, bool active,
int objectId = -1, bool light = false, float emittance = 0.0f, float refraction = 0.0f, int objectId = -1, bool light = false, float emittance = 0.0f, float refraction = 0.0f,
float reflection = 0.0f, Shape shape = Shape::SPHERE) { float reflection = 0.0f) {
auto pointData = std::make_shared<NodeData>(data, pos, visible, color, size, active, objectId, auto pointData = std::make_shared<NodeData>(data, pos, visible, color, size, active, objectId,
light, emittance, refraction, reflection, shape); light, emittance, refraction, reflection);
if (insertRecursive(root_.get(), pointData, 0)) { if (insertRecursive(root_.get(), pointData, 0)) {
this->size++; this->size++;
return true; return true;
@@ -744,16 +714,11 @@ public:
bool update(const PointType& oldPos, const PointType& newPos, const T& newData, bool newVisible = true, bool update(const PointType& oldPos, const PointType& newPos, const T& newData, bool newVisible = true,
Eigen::Vector3f newColor = Eigen::Vector3f(1.0f, 1.0f, 1.0f), float newSize = 0.01f, bool newActive = true, Eigen::Vector3f newColor = Eigen::Vector3f(1.0f, 1.0f, 1.0f), float newSize = 0.01f, bool newActive = true,
int newObjectId = -2, bool newLight = false, float newEmittance = 0.0f, float newRefraction = 0.0f, int newObjectId = -2, bool newLight = false, float newEmittance = 0.0f, float newRefraction = 0.0f,
float newReflection = 0.0f, Shape newShape = Shape::SPHERE, float tolerance = EPSILON) { float newReflection = 0.0f, float tolerance = EPSILON) {
// Find the existing point
auto pointData = find(oldPos, tolerance); auto pointData = find(oldPos, tolerance);
if (!pointData) return false; if (!pointData) return false;
// If position changed, we need to remove and reinsert
float moveDistSq = (newPos - oldPos).squaredNorm();
if (moveDistSq > tolerance * tolerance) {
// Save the data
T dataCopy = pointData->data; T dataCopy = pointData->data;
bool activeCopy = pointData->active; bool activeCopy = pointData->active;
bool visibleCopy = pointData->visible; bool visibleCopy = pointData->visible;
@@ -764,7 +729,6 @@ public:
float emittanceCopy = pointData->emittance; float emittanceCopy = pointData->emittance;
float refractionCopy = pointData->refraction; float refractionCopy = pointData->refraction;
float reflectionCopy = pointData->reflection; float reflectionCopy = pointData->reflection;
Shape shapeCopy = pointData->shape;
// Remove the old point // Remove the old point
if (!remove(oldPos, tolerance)) { if (!remove(oldPos, tolerance)) {
@@ -781,24 +745,7 @@ public:
newLight ? newLight : lightCopy, newLight ? newLight : lightCopy,
newEmittance > 0 ? newEmittance : emittanceCopy, newEmittance > 0 ? newEmittance : emittanceCopy,
newRefraction >= 0 ? newRefraction : refractionCopy, newRefraction >= 0 ? newRefraction : refractionCopy,
newReflection >= 0 ? newReflection : reflectionCopy, newReflection >= 0 ? newReflection : reflectionCopy);
newShape);
} else {
// Just update properties in place
pointData->data = newData;
pointData->position = newPos; // Minor adjustment within tolerance
pointData->visible = newVisible;
pointData->color = newColor;
pointData->size = newSize;
if (newObjectId != -2) pointData->objectId = newObjectId;
pointData->active = newActive;
pointData->light = newLight;
pointData->emittance = newEmittance;
pointData->refraction = newRefraction;
pointData->reflection = newReflection;
pointData->shape = newShape;
return true;
}
} }
bool move(const PointType& pos, const PointType& newPos) { bool move(const PointType& pos, const PointType& newPos) {
@@ -806,7 +753,7 @@ public:
if (!pointData) return false; if (!pointData) return false;
bool success = set(pointData->data, newPos, pointData->visible, pointData->color, pointData->size, bool success = set(pointData->data, newPos, pointData->visible, pointData->color, pointData->size,
pointData->active, pointData->objectId, pointData->light, pointData->emittance, pointData->active, pointData->objectId, pointData->light, pointData->emittance,
pointData->refraction, pointData->reflection, pointData->shape); pointData->refraction, pointData->reflection);
if (success) { if (success) {
remove(pos); remove(pos);
return true; return true;
@@ -877,14 +824,6 @@ public:
return true; return true;
} }
bool setShape(const PointType& pos, Shape shape, float tolerance = EPSILON) {
auto pointData = find(pos, tolerance);
if (!pointData) return false;
pointData->shape = shape;
return true;
}
std::vector<std::shared_ptr<NodeData>> voxelTraverse(const PointType& origin, const PointType& direction, std::vector<std::shared_ptr<NodeData>> voxelTraverse(const PointType& origin, const PointType& direction,
float maxDist, bool stopAtFirstHit, bool enableLOD = false) const { float maxDist, bool stopAtFirstHit, bool enableLOD = false) const {
std::vector<std::shared_ptr<NodeData>> hits; std::vector<std::shared_ptr<NodeData>> hits;
@@ -924,14 +863,6 @@ public:
if (!pointData->active) continue; if (!pointData->active) continue;
float t; float t;
if (pointData->shape == Shape::SPHERE) {
if (raySphereIntersect(ray, pointData->position, pointData->size * pointData->size, t)) {
if (t >= 0 && t <= maxDist) {
hits.emplace_back(pointData);
if (stopAtFirstHit) return;
}
}
} else {
PointType normal, hitPoint; PointType normal, hitPoint;
if (rayCubeIntersect(ray, pointData.get(), t, normal, hitPoint)) { if (rayCubeIntersect(ray, pointData.get(), t, normal, hitPoint)) {
if (t >= 0 && t <= maxDist) { if (t >= 0 && t <= maxDist) {
@@ -940,7 +871,6 @@ public:
} }
} }
} }
}
} else { } else {
for (int i = 0; i < 8; ++i) { for (int i = 0; i < 8; ++i) {
int childIdx = i ^ raySignMask; int childIdx = i ^ raySignMask;
@@ -1003,30 +933,11 @@ public:
PointType normal; PointType normal;
float t = 0.0f; float t = 0.0f;
// Calculate intersection based on shape // Calculate intersection
if (obj->shape == Shape::SPHERE) {
// Sphere intersection
PointType center = obj->position;
float radius = obj->size;
PointType L_vec = center - rayOrig;
float tca = L_vec.dot(rayDir);
float d2 = L_vec.dot(L_vec) - tca * tca;
float radius2 = radius * radius;
if (d2 <= radius2) {
float thc = std::sqrt(radius2 - d2);
t = tca - thc;
if (t < 0.001f) t = tca + thc;
}
hitPoint = rayOrig + rayDir * t;
normal = (hitPoint - center).normalized();
} else {
PointType cubeNormal; PointType cubeNormal;
if (!rayCubeIntersect(ray, obj.get(), t, normal, hitPoint)) { if (!rayCubeIntersect(ray, obj.get(), t, normal, hitPoint)) {
return globalIllumination ? skylight_ : Eigen::Vector3f::Zero(); return globalIllumination ? skylight_ : Eigen::Vector3f::Zero();
} }
}
Eigen::Vector3f finalColor = globalIllumination ? skylight_ : Eigen::Vector3f::Zero(); Eigen::Vector3f finalColor = globalIllumination ? skylight_ : Eigen::Vector3f::Zero();
@@ -1224,7 +1135,7 @@ public:
PointType rayDir = dir + (right * px) + (up * py); PointType rayDir = dir + (right * px) + (up * py);
rayDir.normalize(); rayDir.normalize();
Eigen::Vector3f color = backgroundColor_; // Start with background color Eigen::Vector3f color = backgroundColor_;
auto hit = fastVoxelTraverse(origin, rayDir, std::numeric_limits<float>::max(), true); auto hit = fastVoxelTraverse(origin, rayDir, std::numeric_limits<float>::max(), true);
if (hit != nullptr) { if (hit != nullptr) {

View File

@@ -1,6 +1,7 @@
#ifndef G3_MATERIALS_HPP #ifndef G3_MATERIALS_HPP
#define G3_MATERIALS_HPP #define G3_MATERIALS_HPP
#include "../../eigen/Eigen/Dense"
#include "colors.hpp" #include "colors.hpp"
struct Material { struct Material {
@@ -15,6 +16,7 @@ struct Material {
float density; float density;
float speedOfSound; float speedOfSound;
float audioAbsorption; float audioAbsorption;
Eigen::Vector3f rgb; //for the fast version.
// Constructor with sensible defaults // Constructor with sensible defaults
Material(float ior = 1.5f, float dispersion = 0.0f, float chromaticity = 550.0f, Material(float ior = 1.5f, float dispersion = 0.0f, float chromaticity = 550.0f,
@@ -39,7 +41,9 @@ struct Material {
float bandwidth = 10.0f + (1.0f - saturation) * 290.0f; float bandwidth = 10.0f + (1.0f - saturation) * 290.0f;
return Material(ior, 0.02f, wavelength, bandwidth, transmission, roughness, emittance, 2.0f, 3000.0f, 0.1f, isLight); Material outerial(ior, 0.02f, wavelength, bandwidth, transmission, roughness, emittance, 2.0f, 3000.0f, 0.1f, isLight);
outerial.rgb = Eigen::Vector3f(r, g, b);
return outerial;
} }
}; };