mirror of
https://github.com/vonhyou/ray-tracer-comp371.git
synced 2025-06-08 07:22:01 +00:00
add dummy global illum
This commit is contained in:
parent
5cf306cbbe
commit
b0fcb58741
1 changed files with 34 additions and 13 deletions
|
@ -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 RayTracer::trace(HitRecord hit, int bounce, float prob) const {
|
Vector3f getRandomDirection() {
|
||||||
float dice = utils::Random::get();
|
RETRY_RANDOM:
|
||||||
if (bounce && (dice > prob)) {
|
float x = utils::Random::get();
|
||||||
return Vector3f(1, 0, 1).array() * trace(hit, bounce - 1, prob).array();
|
float y = utils::Random::get();
|
||||||
} else {
|
if (x * x + y * y > 1)
|
||||||
Light *light = singleLightSource();
|
goto RETRY_RANDOM;
|
||||||
Vector3f point = hit.point();
|
|
||||||
Vector3f direction = (light->getCenter() - point).normalized();
|
|
||||||
Ray shadowRay(point, direction);
|
|
||||||
|
|
||||||
|
return Vector3f(x, y, std::sqrt(1 - x * x - y * y));
|
||||||
|
}
|
||||||
|
|
||||||
|
Vector3f RayTracer::trace(HitRecord hit, int bounce, float prob) const {
|
||||||
|
bool notFinish = bounce && (utils::Random::get() > prob);
|
||||||
|
Vector3f point = hit.point();
|
||||||
|
Light *light = singleLightSource();
|
||||||
|
Vector3f direction;
|
||||||
Geometry *geometry = hit.geometry();
|
Geometry *geometry = hit.geometry();
|
||||||
|
|
||||||
|
if (notFinish) {
|
||||||
|
direction = point + getRandomDirection();
|
||||||
|
} else {
|
||||||
|
direction = light->getCenter() - point;
|
||||||
|
}
|
||||||
|
direction.normalize();
|
||||||
|
Ray ray(point, direction);
|
||||||
|
|
||||||
|
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 {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue