Implement Gaussian anti-aliasing

This commit is contained in:
Camden Dixie O'Brien
2025-09-23 15:35:26 +01:00
parent 259f0922b3
commit d8a2b8a3d8
3 changed files with 49 additions and 16 deletions

View File

@@ -4,12 +4,14 @@
#include "rng.h"
#include <float.h>
#include <stdio.h>
#include <threads.h>
#define MAX_ITER 10
#define MIN_T 1e-6
#define NSAMPLES 100
#define SAMPLE_WEIGHT (1.0 / (double)NSAMPLES)
#define SAMPLE_STDDEV 0.333
#define NTHREADS 40
@@ -76,18 +78,23 @@ static int render_thread(void *arg)
= vec3_add(camera->pix_origin, vec3_scale(camera->y_step, y));
for (unsigned x = 0; x < w; ++x) {
const vec3_t pix = vec3_add(row, vec3_scale(camera->x_step, x));
const ray_t ray = {
.orig = camera->pos,
.dir = vec3_unit(pix),
};
vec3_t colour = black;
for (unsigned i = 0; i < NSAMPLES; ++i) {
const double weight = 1.0 / NSAMPLES;
const vec3_t jitter
= rng_gaussian_xy(&slice->rng, SAMPLE_STDDEV);
const vec3_t offset = vec3_add(
vec3_scale(camera->x_step, jitter.x),
vec3_scale(camera->y_step, jitter.y));
const ray_t ray = {
.orig = camera->pos,
.dir = vec3_unit(vec3_add(pix, offset)),
};
const vec3_t sample = trace(
ray, slice->scene, slice->scene_count, &slice->rng);
colour = vec3_add(colour, vec3_scale(sample, weight));
colour = vec3_add(colour, vec3_scale(sample, SAMPLE_WEIGHT));
}
setpix(colour, slice->pixels + (w * y + x));