defocus-modules/lib/raytracer.c

472 lines
16 KiB
C
Raw Permalink Normal View History

#include <defocus/defocus.h>
2023-07-12 22:57:49 +02:00
#include <defocus/df_math.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
2023-06-20 23:28:33 +02:00
#include "pcg/pcg_basic.h"
/* ********************* *
* *
* Image functions *
* *
* ********************* */
#define MAX_IMAGES 1024
struct
{
struct
{
stbi_uc *pixels;
int w;
int h;
} images[MAX_IMAGES];
unsigned int image_count;
} _image_table;
DF_API df_image_handle df_load_image(const char *path, int *w, int *h)
{
if (_image_table.image_count == MAX_IMAGES)
return 0;
int width, height, c;
stbi_uc *pixels = stbi_load(path, &width, &height, &c, 4);
if (!pixels) {
return 0;
}
df_image_handle handle = (_image_table.image_count + 1);
_image_table.image_count++;
_image_table.images[handle].pixels = pixels;
_image_table.images[handle].w = width;
_image_table.images[handle].h = height;
if (w) *w = width;
if (h) *h = height;
return handle;
}
2023-06-20 23:28:33 +02:00
/* *************************** *
* *
* Builtin camera models *
* *
* *************************** */
2023-07-12 22:57:49 +02:00
DF_API df_pinhole_camera_data
df_create_pinhole_camera_data(df_v3 look_from, df_v3 look_at, df_v3 up, float vfov, float aspect)
2023-06-20 23:28:33 +02:00
{
2023-07-12 22:57:49 +02:00
float theta = DF_DEGREES_TO_RADIANS(vfov);
float h = tanf(theta * .5f);
float viewport_height = 2.f * h;
float viewport_width = aspect * viewport_height;
df_v3 view_vec = df_sub_v3(look_from, look_at);
df_v3 w = df_normalize_v3(view_vec);
df_v3 u = df_normalize_v3(df_cross(up, w));
df_v3 v = df_cross(w, u);
df_v3 horizontal = df_mul_v3(viewport_width, u);
df_v3 vertical = df_mul_v3(viewport_height, v);
df_v3 lower_left = {
.x = look_from.x - horizontal.x / 2.f - vertical.x / 2.f - w.x,
.y = look_from.y - horizontal.y / 2.f - vertical.y / 2.f - w.y,
.z = look_from.z - horizontal.z / 2.f - vertical.z / 2.f - w.z,
};
2023-06-20 23:28:33 +02:00
return (df_pinhole_camera_data){
2023-07-12 22:57:49 +02:00
.origin = look_from,
.horizontal = horizontal,
.vertical = vertical,
.lower_left = lower_left,
2023-06-20 23:28:33 +02:00
};
}
DF_API df_ray df_pinhole_camera(float u, float v, unsigned int sample_idx, void *camera_data)
{
DF_UNUSED(sample_idx);
df_pinhole_camera_data *data = camera_data;
2023-07-12 22:57:49 +02:00
df_v3 origin = data->origin;
df_v3 horizontal = data->horizontal;
df_v3 vertical = data->vertical;
2023-06-20 23:28:33 +02:00
df_v3 target = {
2023-07-12 22:57:49 +02:00
data->lower_left.x + u * horizontal.x + v * vertical.x - origin.x,
data->lower_left.y + u * horizontal.y + v * vertical.y - origin.y,
data->lower_left.z + u * horizontal.z + v * vertical.z - origin.z,
2023-06-20 23:28:33 +02:00
};
return (df_ray){
.origin = origin,
.dir = target,
};
}
static df_v2 random_point_on_disk(unsigned int i)
{
for (unsigned int j = 0; j < i; ++j)
pcg32_random();
while (1) {
df_v2 p = {.x = (float)pcg32_boundedrand(1024) / 1023.f, .y = (float)pcg32_boundedrand(1024) / 1023.f};
2023-07-12 22:57:49 +02:00
if (df_len_v2(p) <= 1.f)
2023-06-20 23:28:33 +02:00
return p;
}
}
2023-07-12 22:57:49 +02:00
DF_API df_thin_lense_camera_data df_create_thin_lense_camera_data(
df_v3 look_from, df_v3 look_at, df_v3 up, float vfov, float aspect, float aperture, float focal_distance)
2023-06-20 23:28:33 +02:00
{
2023-07-12 22:57:49 +02:00
float theta = DF_DEGREES_TO_RADIANS(vfov);
float h = tanf(theta * .5f);
float viewport_height = 2.f * h;
float viewport_width = aspect * viewport_height;
df_v3 view_vec = df_sub_v3(look_from, look_at);
df_v3 w = df_normalize_v3(view_vec);
df_v3 u = df_normalize_v3(df_cross(up, w));
df_v3 v = df_cross(w, u);
df_v3 horizontal = df_mul_v3(viewport_width, u);
df_v3 vertical = df_mul_v3(viewport_height, v);
df_v3 lower_left = {
.x = look_from.x - horizontal.x / 2.f - vertical.x / 2.f - focal_distance * w.x,
.y = look_from.y - horizontal.y / 2.f - vertical.y / 2.f - focal_distance * w.y,
.z = look_from.z - horizontal.z / 2.f - vertical.z / 2.f - focal_distance * w.z,
};
2023-06-20 23:28:33 +02:00
return (df_thin_lense_camera_data){
2023-07-12 22:57:49 +02:00
.origin = look_from,
.horizontal = horizontal,
.vertical = vertical,
.lower_left = lower_left,
.lens_radius = aperture / 2.f,
2023-06-20 23:28:33 +02:00
};
}
DF_API df_ray df_thin_lense_camera(float u, float v, unsigned int sample_idx, void *camera_data)
{
df_thin_lense_camera_data *data = camera_data;
df_v2 lensp = random_point_on_disk(sample_idx);
lensp.x *= data->lens_radius;
lensp.y *= data->lens_radius;
df_v3 offset = {u * lensp.x, v * lensp.y, .z = 0.f};
2023-07-12 22:57:49 +02:00
df_v3 origin = df_add_v3(data->origin, offset);
df_v3 horizontal = data->horizontal;
df_v3 vertical = data->vertical;
2023-06-20 23:28:33 +02:00
2023-07-12 22:57:49 +02:00
df_v3 target = {
data->lower_left.x + u * horizontal.x + v * vertical.x - origin.x,
data->lower_left.y + u * horizontal.y + v * vertical.y - origin.y,
data->lower_left.z + u * horizontal.z + v * vertical.z - origin.z,
};
return (df_ray){
.origin = origin,
.dir = target,
};
2023-06-20 23:28:33 +02:00
}
/* ********************************* *
* *
* Intersection test functions *
2023-06-20 23:28:33 +02:00
* *
* ********************************* */
typedef struct
{
float ray_t;
float img_u;
float img_v;
df_image_handle image;
} df_hit;
/* -1 => does not hit, >= 0 => hit (sphere index) */
static float sphere_test(float ray_origin_x,
float ray_origin_y,
float ray_origin_z,
float ray_dx,
float ray_dy,
float ray_dz,
const df_sphere *spheres,
unsigned int sphere_count)
{
float result = -1.f;
for (unsigned int i = 0; i < sphere_count; ++i) {
float delta_x = ray_origin_x - spheres[i].center_x;
float delta_y = ray_origin_y - spheres[i].center_y;
float delta_z = ray_origin_z - spheres[i].center_z;
float a = ray_dx * ray_dx + ray_dy * ray_dy + ray_dz * ray_dz;
float b = (delta_x * ray_dx + delta_y * ray_dy + delta_z * ray_dz);
float c = (delta_x * delta_x + delta_y * delta_y + delta_z * delta_z) - (spheres[i].radius * spheres[i].radius);
float discriminant = b * b - a * c;
/* we can get the hit location t as: (-b - sqrtf(disciminant)) / (2.f * a) */
if (discriminant > 0) {
float t = (-b - sqrtf(discriminant)) / (2.f * a);
#if 0
df_hit_record hit;
hit.at.x = ray_origin_x + t * ray_dx;
hit.at.y = ray_origin_y + t * ray_dy;
hit.at.z = ray_origin_z + t * ray_dz;
hit.ray_t = t;
df_v3 outward_normal = {
hit.at.x - spheres[i].center_x,
hit.at.y - spheres[i].center_y,
hit.at.z - spheres[i].center_z
};
set_face_normal(&hit, (df_v3){ray_dx, ray_dy, ray_dz}, outward_normal);
#endif
if (t > result)
result = t;
}
}
return result;
}
2023-06-20 23:28:33 +02:00
static df_hit plane_test(df_v3 ray_origin,
df_v3 ray_d,
const df_plane *planes,
unsigned int plane_count)
{
df_hit result = {
.ray_t = -1.f,
};
for (unsigned int i = 0; i < plane_count; ++i) {
2023-06-20 23:28:33 +02:00
float dot = (ray_d.x * planes[i].normal_x + ray_d.y * planes[i].normal_y + ray_d.z * planes[i].normal_z);
if (dot > DF_EPSF32 || dot < -DF_EPSF32) {
2023-06-20 23:28:33 +02:00
float delta_x = planes[i].base_x - ray_origin.x;
float delta_y = planes[i].base_y - ray_origin.y;
float delta_z = planes[i].base_z - ray_origin.z;
float num = delta_x * planes[i].normal_x + delta_y * planes[i].normal_y + delta_z * planes[i].normal_z;
float t = num / dot;
if (t > result.ray_t) {
result.ray_t = t;
/* Project point on plane to image coordinate system */
2023-06-20 23:28:33 +02:00
float px = ray_origin.x + t * ray_d.x;
float py = ray_origin.y + t * ray_d.y;
float pz = ray_origin.z + t * ray_d.z;
float img_p3_x = px - planes[i].img_p0_x;
float img_p3_y = py - planes[i].img_p0_y;
float img_p3_z = pz - planes[i].img_p0_z;
2023-06-19 17:29:28 +02:00
float w = planes[i].img_w;
float h = planes[i].img_h;
result.img_u =
img_p3_x * planes[i].img_ax0_x + img_p3_y * planes[i].img_ax0_y + img_p3_z * planes[i].img_ax0_z;
result.img_v =
img_p3_x * planes[i].img_ax1_x + img_p3_y * planes[i].img_ax1_y + img_p3_z * planes[i].img_ax1_z;
result.img_u /= w;
result.img_v /= h;
result.image = planes[i].image;
}
}
/* Line is parallel to the plane, either contained or not. Do we care? */
}
return result;
}
2023-06-20 23:28:33 +02:00
/* Ray packets to support more than one ray (=sample) per pixel */
typedef struct
{
unsigned int samples_per_pixel;
unsigned int ray_count;
df_v3 *ray_origins; /* (0, 0, 0) is the center of the lens */
df_v3 *ray_deltas; /* not necessarily normalized! */
df_v2i *ray_pixels; /* pixel coordinates in the output image */
} df_ray_packet;
DF_API int df_trace_rays(df_trace_rays_settings settings,
const df_sphere *spheres,
unsigned int sphere_count,
const df_plane *planes,
unsigned int plane_count,
uint8_t **result)
{
int image_width = settings.image_width;
int image_height = settings.image_height;
2023-06-20 23:28:33 +02:00
unsigned int samples_per_pixel = (unsigned int)settings.samples_per_pixel;
if (samples_per_pixel == 0)
samples_per_pixel = 1;
df_camera_fn camera = settings.camera;
if (!camera)
return 0;
void *camera_data = settings.camera_data;
2023-07-12 22:57:49 +02:00
float *fpixels = malloc(sizeof(float) * image_width * image_height * 3);
if (!fpixels)
return 0;
2023-07-12 22:57:49 +02:00
memset(fpixels, 0, sizeof(float) * image_width * image_height * 3);
2023-06-20 23:28:33 +02:00
df_ray_packet packet;
packet.samples_per_pixel = samples_per_pixel;
packet.ray_count = image_width * image_height * samples_per_pixel;
void *packet_mem = malloc(packet.ray_count * (2 * sizeof(df_v3) + sizeof(df_v2i)));
if (!packet_mem) {
2023-07-12 22:57:49 +02:00
free(fpixels);
2023-06-20 23:28:33 +02:00
return 0;
}
packet.ray_origins = (df_v3 *)packet_mem;
packet.ray_deltas = packet.ray_origins + packet.ray_count;
packet.ray_pixels = (df_v2i *)(packet.ray_deltas + packet.ray_count);
2023-06-20 23:28:33 +02:00
/* FIXME(Kevin): Replace with dynamic (time() ?) initializer */
pcg32_srandom(1523789, 901842398023);
2023-06-20 23:28:33 +02:00
/* Generate the rays */
unsigned int ray_idx = 0;
for (int y = 0; y < image_height; ++y) {
/* TODO(Kevin): SIMD */
for (int x = 0; x < image_width; ++x) {
float u = (float)x / (float)(image_width - 1);
float v = (float)y / (float)(image_height - 1);
2023-06-20 23:28:33 +02:00
for (unsigned int samplei = 0; samplei < samples_per_pixel; ++samplei) {
packet.ray_pixels[ray_idx] = (df_v2i){x, y};
df_ray ray = camera(u, v, samplei, camera_data);
packet.ray_origins[ray_idx] = ray.origin;
packet.ray_deltas[ray_idx] = ray.dir;
++ray_idx;
}
#if 0
/* Target = Delta because origin is (0, 0, 0) */
2023-06-20 23:28:33 +02:00
df_v3 origin = {0.f, 0.f, 0.f};
df_v3 target = {lower_left_x + u * viewport_width, lower_left_y + v * viewport_height, lower_left_z};
2023-06-20 23:28:33 +02:00
/* Adjust for lens effect */
if (settings.lens_radius > 0.f) {
df_v2 lensp = concentric_sample_disk((df_v2){u, v});
lensp.x *= settings.lens_radius;
lensp.y *= settings.lens_radius;
2023-06-20 23:28:33 +02:00
float ft = -focal_length / target.z;
df_v3 focusp = {target.x * ft, target.y * ft, target.z * ft};
2023-06-20 23:28:33 +02:00
origin.x = lensp.x;
origin.y = lensp.y;
origin.z = 0.f;
target = (df_v3){focusp.x - lensp.x, focusp.y - lensp.y, focusp.z};
}
/* Raycast against all planes */
df_hit plane_hit = plane_test(origin, target, planes, plane_count);
#if 0
float hits[] = {sphere_hit_t, plane_hit.ray_t};
float hit_t = df_max_f32(hits, DF_ARRAY_COUNT(hits));
if (hit_t >= 0) {
df_v3 hit_p = {target_x * hit_t, target_y * hit_t, target_z * hit_t};
hit_p.z -= -1.f;
df_v3 normal = normalize(hit_p);
row[x * 3 + 0] = (uint8_t)((.5f * (normal.x + 1.f)) * 255);
row[x * 3 + 1] = (uint8_t)((.5f * (normal.y + 1.f)) * 255);
row[x * 3 + 2] = (uint8_t)((.5f * (normal.z + 1.f)) * 255);
}
2023-06-20 23:28:33 +02:00
#endif
if (plane_hit.ray_t >= 0) {
float img_u = plane_hit.img_u;
float img_v = plane_hit.img_v;
// Temporary, handle arbitrary scale (esp. resulting from different orientation)
float view_aspect = viewport_width / viewport_height;
if (img_u >= 0 && img_v >= 0 && img_u <= 1.f && img_v <= 1.f) {
int pixelx = (int)floorf(img_u * (_image_table.images[plane_hit.image].w - 1));
int pixely = (int)floorf(img_v * (_image_table.images[plane_hit.image].h - 1));
stbi_uc *pixel = _image_table.images[plane_hit.image].pixels +
4 * (pixely * _image_table.images[plane_hit.image].w + pixelx);
row[x * 3 + 0] = pixel[0];
row[x * 3 + 1] = pixel[1];
row[x * 3 + 2] = pixel[2];
}
}
else {
/* Gradient background color */
2023-06-20 23:28:33 +02:00
float len = sqrtf(target.x * target.x + target.y * target.y + target.z * target.z);
float t = .5f * (target.y / len + 1.f);
float r = (1.f - t) + t * 0.5f;
float g = (1.f - t) + t * 0.7f;
float b = (1.f - t) + t * 1.0f;
row[x * 3 + 0] = (uint8_t)(r * 255);
row[x * 3 + 1] = (uint8_t)(g * 255);
row[x * 3 + 2] = (uint8_t)(b * 255);
}
2023-06-20 23:28:33 +02:00
#endif
}
}
2023-07-12 22:57:49 +02:00
float fsamples = (float)samples_per_pixel;
2023-06-20 23:28:33 +02:00
/* Trace the rays */
for (unsigned int i = 0; i < packet.ray_count; ++i) {
df_v2i pixelp = packet.ray_pixels[i];
2023-07-12 22:57:49 +02:00
float *row = fpixels + pixelp.y * image_width * 3;
2023-06-20 23:28:33 +02:00
df_hit plane_hit = plane_test(packet.ray_origins[i], packet.ray_deltas[i], planes, plane_count);
if (plane_hit.ray_t >= 0) {
float img_u = plane_hit.img_u;
float img_v = plane_hit.img_v;
if (img_u >= 0 && img_v >= 0 && img_u <= 1.f && img_v <= 1.f) {
int pixelx = (int)floorf(img_u * (_image_table.images[plane_hit.image].w - 1));
int pixely = (int)floorf(img_v * (_image_table.images[plane_hit.image].h - 1));
stbi_uc *pixel = _image_table.images[plane_hit.image].pixels +
4 * (pixely * _image_table.images[plane_hit.image].w + pixelx);
2023-07-12 22:57:49 +02:00
row[pixelp.x * 3 + 0] += (float)pixel[0];
row[pixelp.x * 3 + 1] += (float)pixel[1];
row[pixelp.x * 3 + 2] += (float)pixel[2];
2023-06-20 23:28:33 +02:00
}
}
}
2023-07-12 22:57:49 +02:00
/* Map down to 8bit rgb */
uint8_t *pixels = malloc(image_width * image_height * 3);
if (!pixels) {
free(packet_mem);
free(fpixels);
return 0;
}
for (unsigned int i = 0; i < image_width * image_height; ++i) {
float *fp = &fpixels[i * 3];
uint8_t *p = &pixels[i * 3];
p[0] = (uint8_t)(fp[0] / fsamples);
p[1] = (uint8_t)(fp[1] / fsamples);
p[2] = (uint8_t)(fp[2] / fsamples);
}
free(fpixels);
2023-06-20 23:28:33 +02:00
free(packet_mem);
*result = pixels;
return 1;
}