add dummy global illum

This commit is contained in:
Shuo Feng 2024-03-20 03:27:01 -04:00
parent b0fcb58741
commit bdbd81091c
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
2 changed files with 27 additions and 22 deletions

View file

@ -1,6 +1,7 @@
#include "RayTracer.h" #include "RayTracer.h"
#include "HitRecord.h" #include "HitRecord.h"
#include "Light.h" #include "Light.h"
#include "Optional.h"
#include "Output.h" #include "Output.h"
#include "Parser.h" #include "Parser.h"
#include "Progress.h" #include "Progress.h"
@ -132,11 +133,13 @@ Vector3f RayTracer::calculateColor(const HitRecord &hit, int i) const {
/** /**
* Find the nearest geometry to intersect * Find the nearest geometry to intersect
*/ */
Optional<HitRecord> RayTracer::getHitRecord(Ray r) const { Optional<HitRecord> RayTracer::getHitRecord(Ray r, const Geometry *self,
bool notSphere) const {
priority_queue<HitRecord> records; priority_queue<HitRecord> records;
for (auto g : geometries) { for (auto g : geometries) {
Optional<float> t = g->intersect(r); Optional<float> t = g->intersect(r);
if (t.hasValue()) if (t.hasValue() && g != self)
if (!notSphere || notSphere && g->type() != Geometry::Type::SPHERE)
records.push(HitRecord(t.value(), r, g)); records.push(HitRecord(t.value(), r, g));
} }
@ -149,6 +152,14 @@ Optional<HitRecord> RayTracer::getHitRecord(Ray r) const {
return Optional<HitRecord>::nullopt; return Optional<HitRecord>::nullopt;
} }
Optional<HitRecord> RayTracer::getHitRecord(Ray r, const Geometry *g) const {
return getHitRecord(r, g, false);
}
Optional<HitRecord> RayTracer::getHitRecord(Ray r) const {
return getHitRecord(r, nullptr, false);
}
Light *RayTracer::singleLightSource() const { Light *RayTracer::singleLightSource() const {
for (auto light : lights) for (auto light : lights)
if (light->isUse()) if (light->isUse())
@ -194,31 +205,23 @@ Vector3f RayTracer::trace(HitRecord hit, int bounce, float prob) const {
Vector3f direction; Vector3f direction;
Geometry *geometry = hit.geometry(); Geometry *geometry = hit.geometry();
if (notFinish) { if (notFinish)
direction = point + getRandomDirection(); direction = point + getRandomDirection();
} else { else
direction = light->getCenter() - point; direction = light->getCenter() - point;
}
direction.normalize(); direction.normalize();
Ray ray(point, direction); Ray ray(point, direction);
if (notFinish) { Optional<HitRecord> hitRecord = getHitRecord(ray, geometry, !notFinish);
Optional<HitRecord> hitRecord = getHitRecord(ray); Vector3f traceColor = Vector3f::Zero();
if (hitRecord.hasValue()) { if (notFinish && hitRecord.hasValue())
Vector3f traceColor = trace(hitRecord.value(), bounce - 1, prob); traceColor = trace(hitRecord.value(), bounce - 1, prob);
else if (!notFinish)
traceColor = light->id();
return traceColor.array() * geometry->cd().array() * return traceColor.array() * geometry->cd().array() *
std::max(0.0f, hit.normal().dot(direction)); std::max(0.0f, hit.normal().dot(direction));
}
return Vector3f::Zero();
} else {
for (auto g : geometries)
if (g != geometry && g->intersect(ray).hasValue() &&
g->type() == Geometry::Type::SPHERE)
return Vector3f::Zero();
return geometry->cd().array() * light->id().array() *
std::max(0.0f, hit.normal().dot(direction));
}
} }
utils::Optional<Vector3f> RayTracer::trace(Ray r) const { utils::Optional<Vector3f> RayTracer::trace(Ray r) const {

View file

@ -24,6 +24,8 @@ private:
void parse(); void parse();
void render(); void render();
Optional<HitRecord> getHitRecord(Ray, const Geometry *, bool) const;
Optional<HitRecord> getHitRecord(Ray, const Geometry *) const;
Optional<HitRecord> getHitRecord(Ray) const; Optional<HitRecord> getHitRecord(Ray) const;
Vector3f calculateColor(const HitRecord &, int) const; Vector3f calculateColor(const HitRecord &, int) const;
Light *singleLightSource() const; Light *singleLightSource() const;