Compare commits

...

10 Commits

Author SHA1 Message Date
Camden Dixie O'Brien
506027c194 Tweak demo scene 2025-09-23 15:02:21 +01:00
Camden Dixie O'Brien
53198edaa4 Allow negative sky colour to be specified 2025-09-23 14:19:30 +01:00
Camden Dixie O'Brien
a8c69c2386 Create multiple demo programs 2025-09-23 14:18:57 +01:00
Camden Dixie O'Brien
9db19eb2e9 Add area light material 2025-09-23 03:17:30 +01:00
Camden Dixie O'Brien
35c6d4a784 Allow -1 to be passed for auto focal length 2025-09-23 03:17:13 +01:00
Camden Dixie O'Brien
a6b70503f7 Set absorbed colour in material scatter function 2025-09-23 00:14:15 +01:00
Camden Dixie O'Brien
209ec3430f Add camera aperture / depth-of-field 2025-09-23 00:09:47 +01:00
Camden Dixie O'Brien
4f6cbb008f Add PNGs to .gitignore 2025-09-23 00:09:38 +01:00
Camden Dixie O'Brien
d3edbad772 Add rng_xy() 2025-09-23 00:09:38 +01:00
Camden Dixie O'Brien
d90b72e047 Rename rng_disc() to rng_plusminus() 2025-09-23 00:09:27 +01:00
12 changed files with 190 additions and 62 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
build/
*.ff
*.png

View File

@@ -20,8 +20,10 @@ add_library(batomorph
configure_target(batomorph)
target_include_directories(batomorph PUBLIC include)
add_executable(demo
demo.c
)
add_executable(demo demo.c)
configure_target(demo)
target_link_libraries(demo PRIVATE batomorph m)
add_executable(rand rand.c)
configure_target(rand)
target_link_libraries(rand PRIVATE batomorph m)

70
demo.c
View File

@@ -3,71 +3,51 @@
#include <unistd.h>
#define W 800
#define H 600
#define OBJ_COUNT 32
#define MAX_DIST 100.0
#define MAX_RAD 10.0
#define MIN_RAD 1.0
#define W 1920
#define H 1080
#define FOV 90
#define SAMPLES_PER_PIXEL 100
#define APERTURE 0.8
#define FOCAL_LEN -1
#define SAMPLES_PER_PIXEL 1000
static const vec3_t camera_pos = { -100.0, 25.0, -100.0 };
static const vec3_t target = { 0.0, 0.0, 0.0 };
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
static const vec3_t sky = { 0.6, 0.7, 1.0 };
static const vec3_t camera_pos = { 80.0, 20.0, 60.0 };
static const vec3_t target = { 0.0, 0.0, 10.0 };
static const vec3_t sky_pos = { 0.4, 0.7, 1.0 };
static const vec3_t sky_neg = { 1.0, 1.0, 1.0 };
static const material_t floor = LAMBERTIAN(1.0, 0.94, 0.80);
static const material_t light = AREA_LIGHT(1.0, 0.8, 0.5, 3.0);
static const material_t white = LAMBERTIAN(1.0, 1.0, 1.0);
static const material_t gold = REFLECTIVE(1.0, 0.9, 0.5, 0.0);
static const material_t silver = REFLECTIVE(0.9, 0.9, 0.9, 0.0);
static const material_t bronze = REFLECTIVE(0.9, 0.7, 0.5, 0.0);
static const material_t glass = DIELECTRIC(1.5);
static const material_t metals[] = { gold, silver, bronze };
static obj_t objs[OBJ_COUNT] = {
[0] = SPHERE(0.0, -100000.0, 0.0, 100000.0, floor),
static obj_t objs[] = {
SPHERE(0.0, -100'000.0, 0.0, 100'000.0, floor),
SPHERE(-3000.0, 3000.0, -2000.0, 1000.0, light),
SPHERE(0.0, 10.0, -33.0, 10.0, gold),
SPHERE(0.0, 10.0, -11.0, 10.0, glass),
SPHERE(0.0, 10.0, 11.0, 10.0, silver),
SPHERE(0.0, 10.0, 33.0, 10.0, white),
};
static pix_t pixbuf[W * H];
static void rand_obj(obj_t *out, rng_t *rng)
{
material_t material;
const uint32_t rand = rng_uint32(rng);
if (rand % 2 == 0) {
const double r = rng_canon(rng);
const double g = rng_canon(rng);
const double b = rng_canon(rng);
material = (material_t)LAMBERTIAN(r, g, b);
} else if (rand % 3 == 0) {
material = glass;
} else {
material = metals[rng_uint32(rng) % 3];
}
const double r = MIN_RAD + (MAX_RAD - MIN_RAD) * rng_canon(rng);
const double x = MAX_DIST * rng_disc(rng);
const double z = MAX_DIST * rng_disc(rng);
*out = (obj_t)SPHERE(x, r, z, r, material);
}
int main()
{
rng_t rng = rng_init(0);
for (int i = 1; i < OBJ_COUNT; ++i)
rand_obj(objs + i, &rng);
img_t img = { .pix = pixbuf };
camera_t camera = camera_init(camera_pos, target, FOV, W, H);
camera_t camera
= camera_init(camera_pos, target, FOV, W, H, APERTURE, FOCAL_LEN);
const scene_t scene = {
.sky_colour = sky,
.sky_pos = sky_pos,
.sky_neg = sky_neg,
.objs = objs,
.obj_count = OBJ_COUNT,
.obj_count = NELEMS(objs),
};
render(&camera, &scene, &img, SAMPLES_PER_PIXEL);

View File

@@ -8,11 +8,13 @@
typedef struct {
vec3_t pos;
vec3_t pix_origin, x_step, y_step;
vec3_t u_hat, v_hat;
uint32_t img_width, img_height;
double aperture;
} camera_t;
camera_t camera_init(
vec3_t pos, vec3_t target, double fov, uint32_t img_width,
uint32_t img_height);
uint32_t img_height, double aperture, double focal_len);
#endif

View File

@@ -22,6 +22,14 @@
.params = { .dielectric = { .eta = e } }, \
}
#define AREA_LIGHT(r, g, b, l) \
{ \
.scatter = scatter_area_light, \
.params = { \
.area_light = { .colour = { r, g, b }, .luminosity = l }, \
}, \
}
typedef struct {
vec3_t point, normal;
double t;
@@ -41,10 +49,16 @@ typedef struct {
double eta;
} dielectric_params_t;
typedef struct {
vec3_t colour;
double luminosity;
} area_light_params_t;
typedef union {
lambertian_params_t lambertian;
reflective_params_t reflective;
dielectric_params_t dielectric;
area_light_params_t area_light;
} material_params_t;
typedef bool scatter_fn_t(
@@ -65,5 +79,8 @@ bool scatter_reflective(
bool scatter_dielectric(
material_params_t params, hit_t hit, rng_t *rng, ray_t *ray,
vec3_t *atten_out);
bool scatter_area_light(
material_params_t params, hit_t hit, rng_t *rng, ray_t *ray,
vec3_t *atten_out);
#endif

View File

@@ -12,8 +12,9 @@ typedef struct {
rng_t rng_init(unsigned seed);
uint32_t rng_uint32(rng_t *rng);
double rng_canon(rng_t *rng);
double rng_disc(rng_t *rng);
double rng_plusminus(rng_t *rng);
vec3_t rng_vec3(rng_t *rng);
vec3_t rng_xy(rng_t *rng);
vec3_t rng_gaussian_xy(rng_t *rng, double stddev);
#endif

View File

@@ -4,7 +4,7 @@
#include "obj.h"
typedef struct {
vec3_t sky_colour;
vec3_t sky_pos, sky_neg;
obj_t *objs;
unsigned obj_count;
} scene_t;

86
rand.c Normal file
View File

@@ -0,0 +1,86 @@
#include "ff.h"
#include "render.h"
#include <unistd.h>
#define W 1280
#define H 720
#define OBJ_COUNT 32
#define MAX_DIST 100.0
#define MAX_RAD 10.0
#define MIN_RAD 1.0
#define FOV 90
#define APERTURE 0.8
#define FOCAL_LEN 100
#define SAMPLES_PER_PIXEL 100
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
static const vec3_t camera_pos = { -100.0, 30.0, -100.0 };
static const vec3_t target = { 0.0, 0.0, 0.0 };
static const vec3_t sky_pos = { 0.6, 0.8, 1.0 };
static const vec3_t sky_neg = { 1.0, 1.0, 1.0 };
static const material_t floor = LAMBERTIAN(1.0, 0.94, 0.80);
static const material_t light = AREA_LIGHT(1.0, 0.8, 0.1, 4.0);
static const material_t gold = REFLECTIVE(1.0, 0.9, 0.5, 0.0);
static const material_t silver = REFLECTIVE(0.9, 0.9, 0.9, 0.0);
static const material_t bronze = REFLECTIVE(0.9, 0.7, 0.5, 0.0);
static const material_t glass = DIELECTRIC(1.5);
static const material_t metals[] = { gold, gold, silver, silver, bronze };
static obj_t objs[OBJ_COUNT] = {
SPHERE(0.0, -100'000.0, 0.0, 100'000.0, floor),
SPHERE(-3000.0, 3000.0, -2000.0, 1000.0, light),
};
static pix_t pixbuf[W * H];
static void rand_obj(obj_t *out, rng_t *rng)
{
material_t material;
const double rand = rng_canon(rng);
if (rand < 0.75) {
const double r = rng_canon(rng);
const double g = rng_canon(rng);
const double b = rng_canon(rng);
material = (material_t)LAMBERTIAN(r, g, b);
} else if (rand < 0.925) {
material = glass;
} else {
material = metals[rng_uint32(rng) % NELEMS(metals)];
}
const double r = MIN_RAD + (MAX_RAD - MIN_RAD) * rng_canon(rng);
const double x = MAX_DIST * rng_plusminus(rng);
const double z = MAX_DIST * rng_plusminus(rng);
*out = (obj_t)SPHERE(x, r, z, r, material);
}
int main()
{
rng_t rng = rng_init(0);
for (int i = 2; i < OBJ_COUNT; ++i)
rand_obj(objs + i, &rng);
img_t img = { .pix = pixbuf };
camera_t camera
= camera_init(camera_pos, target, FOV, W, H, APERTURE, FOCAL_LEN);
const scene_t scene = {
.sky_pos = sky_pos,
.sky_neg = sky_neg,
.objs = objs,
.obj_count = OBJ_COUNT,
};
render(&camera, &scene, &img, SAMPLES_PER_PIXEL);
ff_write(STDOUT_FILENO, img);
return 0;
}

View File

@@ -10,10 +10,10 @@ static const vec3_t up = { 0.0, 1.0, 0.0 };
camera_t camera_init(
vec3_t pos, vec3_t target, double fov, uint32_t img_width,
uint32_t img_height)
uint32_t img_height, double aperture, double focal_len)
{
const vec3_t target_disp = vec3_sub(target, pos);
const double focal_len = vec3_len(target_disp);
if (focal_len = -1)
focal_len = vec3_len(vec3_sub(target, pos));
const double fov_rad = M_PI * fov / 180.0;
const double aspect = (double)img_width / (double)img_height;
@@ -27,7 +27,9 @@ camera_t camera_init(
const vec3_t u = vec3_scale(u_hat, viewport_width);
const vec3_t v = vec3_scale(v_hat, -viewport_height);
const vec3_t topleft = vec3_sub(target, vec3_scale(vec3_add(u, v), 0.5));
const vec3_t topleft = vec3_sub(
vec3_add(pos, vec3_scale(w_hat, focal_len)),
vec3_scale(vec3_add(u, v), 0.5));
const vec3_t x_step = vec3_scale(u, 1.0 / (double)img_width);
const vec3_t y_step = vec3_scale(v, 1.0 / (double)img_height);
@@ -39,7 +41,10 @@ camera_t camera_init(
.pix_origin = pix_origin,
.x_step = x_step,
.y_step = y_step,
.u_hat = u_hat,
.v_hat = v_hat,
.img_width = img_width,
.img_height = img_height,
.aperture = aperture,
};
}

View File

@@ -68,3 +68,16 @@ bool scatter_dielectric(
return true;
}
bool scatter_area_light(
material_params_t params, hit_t hit, rng_t *rng, ray_t *ray,
vec3_t *atten_out)
{
(void)hit;
(void)rng;
(void)ray;
*atten_out
= vec3_scale(params.area_light.colour, params.area_light.luminosity);
return false;
}

View File

@@ -45,14 +45,17 @@ static vec3_t trace(ray_t ray, const scene_t *scene, rng_t *rng)
if (hit.t == DBL_MAX) {
const double a = (ray.dir.y + 1.0) / 2.0;
const vec3_t bg = vec3_add(
vec3_scale(scene->sky_colour, a), vec3_scale(white, 1 - a));
vec3_scale(scene->sky_pos, a),
vec3_scale(scene->sky_neg, 1 - a));
return vec3_hadamard(colour, bg);
}
vec3_t atten;
if (!material.scatter(material.params, hit, rng, &ray, &atten))
return black;
const bool scattered
= material.scatter(material.params, hit, rng, &ray, &atten);
colour = vec3_hadamard(colour, atten);
if (!scattered)
return colour;
}
return black;
@@ -94,9 +97,19 @@ static int render_thread(void *arg)
vec3_scale(camera->y_step, jitter.y));
const vec3_t jittered_pix = vec3_add(pix, offset);
vec3_t source = camera->pos;
if (camera->aperture != 0.0) {
const vec3_t pos
= vec3_scale(rng_xy(&slice->rng), camera->aperture);
const vec3_t offset = vec3_add(
vec3_scale(camera->u_hat, pos.x),
vec3_scale(camera->v_hat, pos.y));
source = vec3_add(source, offset);
}
const ray_t ray = {
.orig = camera->pos,
.dir = vec3_unit(vec3_sub(jittered_pix, camera->pos)),
.orig = source,
.dir = vec3_unit(vec3_sub(jittered_pix, source)),
};
const vec3_t sample = trace(ray, slice->scene, &slice->rng);

View File

@@ -30,17 +30,25 @@ double rng_canon(rng_t *rng)
return (double)rng_uint32(rng) / (double)UINT32_MAX;
}
double rng_disc(rng_t *rng)
double rng_plusminus(rng_t *rng)
{
return 2.0 * rng_canon(rng) - 1.0;
}
vec3_t rng_vec3(rng_t *rng)
{
const vec3_t v = { rng_disc(rng), rng_disc(rng), rng_disc(rng) };
const vec3_t v
= { rng_plusminus(rng), rng_plusminus(rng), rng_plusminus(rng) };
return vec3_unit(v);
}
vec3_t rng_xy(rng_t *rng)
{
const double theta = 2.0 * M_PI * rng_canon(rng);
const double mag = rng_canon(rng);
return (vec3_t) { .x = mag * cos(theta), .y = mag * sin(theta) };
}
vec3_t rng_gaussian_xy(rng_t *rng, double stddev)
{
const double r1 = rng_canon(rng);