add dummy global illum

This commit is contained in:
Shuo Feng 2024-03-20 02:40:45 -04:00
parent 5cf306cbbe
commit b0fcb58741
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1

View file

@ -99,7 +99,8 @@ void RayTracer::render() {
} }
} }
} }
if (!success)
if (success)
color = accumulate / success; color = accumulate / success;
} else { } else {
Ray ray = getRay(x, y); Ray ray = getRay(x, y);
@ -176,28 +177,48 @@ void writeColor(int i, const Vector3f &color) {
Output::current->b(i, color.z()); Output::current->b(i, color.z());
} }
Vector3f getRandomDirection() {
RETRY_RANDOM:
float x = utils::Random::get();
float y = utils::Random::get();
if (x * x + y * y > 1)
goto RETRY_RANDOM;
return Vector3f(x, y, std::sqrt(1 - x * x - y * y));
}
Vector3f RayTracer::trace(HitRecord hit, int bounce, float prob) const { Vector3f RayTracer::trace(HitRecord hit, int bounce, float prob) const {
float dice = utils::Random::get(); bool notFinish = bounce && (utils::Random::get() > prob);
if (bounce && (dice > prob)) { Vector3f point = hit.point();
return Vector3f(1, 0, 1).array() * trace(hit, bounce - 1, prob).array(); Light *light = singleLightSource();
Vector3f direction;
Geometry *geometry = hit.geometry();
if (notFinish) {
direction = point + getRandomDirection();
} else { } else {
Light *light = singleLightSource(); direction = light->getCenter() - point;
Vector3f point = hit.point(); }
Vector3f direction = (light->getCenter() - point).normalized(); direction.normalize();
Ray shadowRay(point, direction); Ray ray(point, direction);
Geometry *geometry = hit.geometry();
if (notFinish) {
Optional<HitRecord> hitRecord = getHitRecord(ray);
if (hitRecord.hasValue()) {
Vector3f traceColor = trace(hitRecord.value(), bounce - 1, prob);
return traceColor.array() * geometry->cd().array() *
std::max(0.0f, hit.normal().dot(direction));
}
return Vector3f::Zero();
} else {
for (auto g : geometries) for (auto g : geometries)
if (g != geometry && g->intersect(shadowRay).hasValue() && if (g != geometry && g->intersect(ray).hasValue() &&
g->type() == Geometry::Type::SPHERE) g->type() == Geometry::Type::SPHERE)
return Vector3f::Zero(); return Vector3f::Zero();
return geometry->cd().array() * light->id().array() * return geometry->cd().array() * light->id().array() *
std::max(0.0f, hit.normal().dot(direction)); std::max(0.0f, hit.normal().dot(direction));
} }
return Vector3f(1, 1, 1);
} }
utils::Optional<Vector3f> RayTracer::trace(Ray r) const { utils::Optional<Vector3f> RayTracer::trace(Ray r) const {