antialiasing

This commit is contained in:
Shuo Feng 2024-03-21 17:37:00 -04:00
parent 4032014d61
commit 342c9fdef2
Signed by: sfeng
GPG key ID: 1E83AE6CD1C037B1
4 changed files with 31 additions and 21 deletions

View file

@ -91,39 +91,37 @@ void RayTracer::render() {
for (int x = 0; x < width; ++x) { for (int x = 0; x < width; ++x) {
Vector3f color = Scene::current->backgroundColor(); Vector3f color = Scene::current->backgroundColor();
if (Scene::current->globalIllum()) { bool globalIllum = Scene::current->globalIllum();
if (globalIllum || Scene::current->antiAliasing()) {
int success = 0; int success = 0;
Vector3f accumulate = Vector3f::Zero(); Vector3f accumulate = Vector3f::Zero();
for (int j = 0; j < gridHeight; ++j) for (int j = 0; j < gridHeight; ++j)
for (int i = 0; i < gridWidth; ++i) { for (int i = 0; i < gridWidth; ++i) {
if (x != width / 2 || y != height / 2 || i || j)
; // goto DEBUG_COLOR;
Ray ray = getRay(x, y, i, j); Ray ray = getRay(x, y, i, j);
for (int rayNum = 0; rayNum < raysPerPixel; ++rayNum) { for (int rayNum = 0; rayNum < raysPerPixel; ++rayNum) {
utils::Optional<Vector3f> result = trace(ray); Optional<Vector3f> result =
globalIllum ? trace(ray) : trace(ray, x, y);
if (result.hasValue()) { if (result.hasValue()) {
accumulate += result.value(); accumulate += result.value();
success++; success++;
} }
} }
// std::cout << accumulate.transpose() << " (" << success <<
// std::endl;
} }
if (success) if (success) {
color = gammaCorrection(accumulate / success, 1.0f / 2.1f); if (globalIllum)
color = gammaCorrection(accumulate / success, 1.0f / 2.1f);
else
color = accumulate / success;
}
} else { } else {
Ray ray = getRay(x, y); Ray ray = getRay(x, y);
Optional<HitRecord> hitRecord = getHitRecord(ray);
if (hitRecord.hasValue()) { Optional<Vector3f> result = trace(ray, x, y);
HitRecord hit = hitRecord.value(); if (result.hasValue())
color = calculateColor(hit, y * width + x); color = result.value();
}
} }
DEBUG_COLOR:
writeColor(y * width + x, clamp(color)); writeColor(y * width + x, clamp(color));
} }
} }
@ -133,7 +131,7 @@ void RayTracer::render() {
/** /**
* Calculate color using phong model * Calculate color using phong model
*/ */
Vector3f RayTracer::calculateColor(const HitRecord &hit, int i) const { Vector3f RayTracer::calculateColor(const HitRecord &hit) const {
Vector3f result(0, 0, 0); Vector3f result(0, 0, 0);
for (auto light : lights) for (auto light : lights)
result += light->isUse() ? light->illumination(hit, geometries) result += light->isUse() ? light->illumination(hit, geometries)
@ -268,17 +266,25 @@ RETRY_TRACING:
std::max(0.0f, hit.normal().dot(direction)); std::max(0.0f, hit.normal().dot(direction));
} }
utils::Optional<Vector3f> RayTracer::trace(Ray r) const { Optional<Vector3f> RayTracer::trace(Ray r, int x, int y) const {
Optional<HitRecord> hitRecord = getHitRecord(r);
if (hitRecord.hasValue())
return Optional<Vector3f>(calculateColor(hitRecord.value()));
return Optional<Vector3f>::nullopt;
}
Optional<Vector3f> RayTracer::trace(Ray r) const {
Optional<HitRecord> hitRecord = getHitRecord(r); Optional<HitRecord> hitRecord = getHitRecord(r);
if (hitRecord.hasValue()) { if (hitRecord.hasValue()) {
Vector3f color = trace(hitRecord.value(), Scene::current->maxBounce(), Vector3f color = trace(hitRecord.value(), Scene::current->maxBounce(),
Scene::current->probTerminate()); Scene::current->probTerminate());
if (color != Vector3f::Zero()) if (color != Vector3f::Zero())
return utils::Optional<Vector3f>(color); return Optional<Vector3f>(color);
} }
return utils::Optional<Vector3f>::nullopt; return Optional<Vector3f>::nullopt;
} }
namespace camera { namespace camera {

View file

@ -28,8 +28,9 @@ private:
void render(); void render();
Optional<HitRecord> getHitRecord(Ray, const Geometry *) 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 &) const;
Light *singleLightSource() const; Light *singleLightSource() const;
Optional<Vector3f> trace(Ray, int, int) const;
Optional<Vector3f> trace(Ray) const; Optional<Vector3f> trace(Ray) const;
Vector3f trace(HitRecord, int, float) const; Vector3f trace(HitRecord, int, float) const;
}; };

View file

@ -12,6 +12,8 @@ float Scene::fov() { return fov_; }
bool Scene::globalIllum() { return globalIllum_; } bool Scene::globalIllum() { return globalIllum_; }
bool Scene::antiAliasing() { return antialiasing_; }
int Scene::maxBounce() { return maxBounce_; } int Scene::maxBounce() { return maxBounce_; }
float Scene::probTerminate() { return probTerminate_; } float Scene::probTerminate() { return probTerminate_; }

View file

@ -41,6 +41,7 @@ public:
int height(); int height();
float fov(); float fov();
bool globalIllum(); bool globalIllum();
bool antiAliasing();
int maxBounce(); int maxBounce();
float probTerminate(); float probTerminate();
Vector3f ai() const; Vector3f ai() const;