Create multiple demo programs

This commit is contained in:
Camden Dixie O'Brien
2025-09-23 15:35:26 +01:00
parent 139ff40f12
commit fd1a1e0f5a
3 changed files with 108 additions and 47 deletions

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)

63
demo.c
View File

@@ -3,66 +3,41 @@
#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 1280
#define H 720
#define FOV 90
#define APERTURE 0.5
#define FOCAL_LEN 100.0
#define APERTURE 0.8
#define FOCAL_LEN -1
#define SAMPLES_PER_PIXEL 100
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, 30.0, 60.0 };
static const vec3_t target = { 0.0, 0.0, 10.0 };
static const vec3_t sky = { 0.6, 0.8, 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, 4.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_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 = 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, APERTURE, FOCAL_LEN);
@@ -70,7 +45,7 @@ int main()
const scene_t scene = {
.sky_colour = sky,
.objs = objs,
.obj_count = OBJ_COUNT,
.obj_count = NELEMS(objs),
};
render(&camera, &scene, &img, SAMPLES_PER_PIXEL);

84
rand.c Normal file
View File

@@ -0,0 +1,84 @@
#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 = { 0.6, 0.8, 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_colour = sky,
.objs = objs,
.obj_count = OBJ_COUNT,
};
render(&camera, &scene, &img, SAMPLES_PER_PIXEL);
ff_write(STDOUT_FILENO, img);
return 0;
}