add inital anti-aliasing

This commit is contained in:
zongor 2025-07-24 15:48:02 -04:00
parent 5bbfb17684
commit cefd653d4e
3 changed files with 17 additions and 6 deletions

View File

@ -4,6 +4,7 @@
#include "common.h" #include "common.h"
#include "hittable.h" #include "hittable.h"
class Camera { class Camera {
public: public:
double aspect_ratio = 1.0; // Ratio of image width over height double aspect_ratio = 1.0; // Ratio of image width over height

View File

@ -1,6 +1,7 @@
#ifndef CANVAS_H #ifndef CANVAS_H
#define CANVAS_H #define CANVAS_H
#include "interval.h"
#include "vec3.h" #include "vec3.h"
#include <cstdint> #include <cstdint>
@ -13,11 +14,10 @@ struct Rgb {
}; };
inline Rgb to_rgb(const Color &c) { inline Rgb to_rgb(const Color &c) {
auto clamp = [](double x) { return x < 0.0 ? 0.0 : (x > 1.0 ? 1.0 : x); }; static const interval intensity(0.000, 0.999);
return {static_cast<uint8_t>(256 * intensity.clamp(c.x())),
return {static_cast<uint8_t>(255.999 * clamp(c.x())), static_cast<uint8_t>(256 * intensity.clamp(c.y())),
static_cast<uint8_t>(255.999 * clamp(c.y())), static_cast<uint8_t>(256 * intensity.clamp(c.z()))};
static_cast<uint8_t>(255.999 * clamp(c.z()))};
} }
struct Canvas { struct Canvas {
@ -32,7 +32,7 @@ struct Canvas {
pixels[y * width + x] = color; pixels[y * width + x] = color;
} }
} }
void set_pixel(int x, int y, Color color) { void set_pixel(int x, int y, Color color) {
if (x >= 0 && x < width && y >= 0 && y < height) { if (x >= 0 && x < width && y >= 0 && y < height) {
pixels[y * width + x] = to_rgb(color); pixels[y * width + x] = to_rgb(color);

View File

@ -1,6 +1,8 @@
#ifndef INTERVAL_H #ifndef INTERVAL_H
#define INTERVAL_H #define INTERVAL_H
#include "common.h"
class interval { class interval {
public: public:
double min, max; double min, max;
@ -14,6 +16,14 @@ public:
bool contains(double x) const { return min <= x && x <= max; } bool contains(double x) const { return min <= x && x <= max; }
bool surrounds(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); static inline const interval empty = interval(+infinity, -infinity);