From cefd653d4ebd055fdccff1ba4d4a34ae87bb0b3c Mon Sep 17 00:00:00 2001 From: zongor Date: Thu, 24 Jul 2025 15:48:02 -0400 Subject: [PATCH] add inital anti-aliasing --- src/camera.h | 1 + src/canvas.h | 12 ++++++------ src/interval.h | 10 ++++++++++ 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/camera.h b/src/camera.h index 2767505..9e52110 100644 --- a/src/camera.h +++ b/src/camera.h @@ -4,6 +4,7 @@ #include "common.h" #include "hittable.h" + class Camera { public: double aspect_ratio = 1.0; // Ratio of image width over height diff --git a/src/canvas.h b/src/canvas.h index 0c886b7..0717e36 100644 --- a/src/canvas.h +++ b/src/canvas.h @@ -1,6 +1,7 @@ #ifndef CANVAS_H #define CANVAS_H +#include "interval.h" #include "vec3.h" #include @@ -13,11 +14,10 @@ struct Rgb { }; inline Rgb to_rgb(const Color &c) { - auto clamp = [](double x) { return x < 0.0 ? 0.0 : (x > 1.0 ? 1.0 : x); }; - - return {static_cast(255.999 * clamp(c.x())), - static_cast(255.999 * clamp(c.y())), - static_cast(255.999 * clamp(c.z()))}; + static const interval intensity(0.000, 0.999); + return {static_cast(256 * intensity.clamp(c.x())), + static_cast(256 * intensity.clamp(c.y())), + static_cast(256 * intensity.clamp(c.z()))}; } struct Canvas { @@ -32,7 +32,7 @@ struct Canvas { pixels[y * width + x] = color; } } - + void set_pixel(int x, int y, Color color) { if (x >= 0 && x < width && y >= 0 && y < height) { pixels[y * width + x] = to_rgb(color); diff --git a/src/interval.h b/src/interval.h index 452d254..deee645 100644 --- a/src/interval.h +++ b/src/interval.h @@ -1,6 +1,8 @@ #ifndef INTERVAL_H #define INTERVAL_H +#include "common.h" + class interval { public: double min, max; @@ -14,6 +16,14 @@ public: bool contains(double x) const { return min <= x && x <= max; } bool surrounds(double x) const { return min < x && x < max; } + + double clamp(double x) const { + if (x < min) + return min; + if (x > max) + return max; + return x; + } }; static inline const interval empty = interval(+infinity, -infinity);