defocus-modules/bin/defocus.c
2023-07-12 22:57:49 +02:00

100 lines
2.7 KiB
C

/* Main command line executable. */
#include <defocus/defocus.h>
#include <defocus/df_math.h>
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image_write.h>
df_plane get_image_filling_plane(int width, int height, df_image_handle image, float focal_length)
{
float aspect = (float)width / (float)height;
df_plane plane;
plane.base_x = 0.f;
plane.base_y = 0.f;
plane.base_z = -2 * focal_length;
plane.normal_x = 0.f;
plane.normal_y = 0.f;
plane.normal_z = 1.f;
plane.img_p0_x = -aspect / focal_length;
plane.img_p0_y = -1.f / focal_length;
plane.img_p0_z = plane.base_z;
plane.img_w = 2.f * aspect / focal_length;
plane.img_h = 2.f / focal_length;
plane.img_ax0_x = 1.f;
plane.img_ax0_y = 0.f;
plane.img_ax0_z = 0.f;
plane.img_ax1_x = 0.f;
plane.img_ax1_y = 1.f;
plane.img_ax1_z = 0.f;
plane.image = image;
return plane;
}
int main(int argc, char **argv) {
df_sphere spheres[2];
spheres[0].center_x = 0.f;
spheres[0].center_y = 0.f;
spheres[0].center_z = -1.f;
spheres[0].radius = .5f;
spheres[1].center_x = 0.25f;
spheres[1].center_y = 0.25f;
spheres[1].center_z = -1.5f;
spheres[1].radius = .75f;
int image_width = 0;
int image_height = 0;
float focal_length = 1.f;
df_image_handle input_image = df_load_image("../test_image.png", &image_width, &image_height);
float aspect = (float)image_width / (float)image_height;
df_plane plane;
plane.base_x = 0.f;
plane.base_y = 0.f;
plane.base_z = -2;
plane.normal_x = 0.f;
plane.normal_y = 0.f;
plane.normal_z = 1.f;
plane.img_p0_x = -aspect;
plane.img_p0_y = -1.f;
plane.img_p0_z = plane.base_z;
plane.img_w = 2.f * aspect;
plane.img_h = 2.f;
plane.img_ax0_x = 1.f;
plane.img_ax0_y = 0.f;
plane.img_ax0_z = 0.f;
plane.img_ax1_x = 0.f;
plane.img_ax1_y = 1.f;
plane.img_ax1_z = 0.f;
plane.image = input_image;
uint8_t *image = NULL;
df_v3 look_from = {1, 1.5, 5};
df_v3 look_at = {0, 0, -2};
df_v3 up = {0, 1, 0};
float dist_to_focus = df_len_v3(df_sub_v3(look_at, look_from));
df_thin_lense_camera_data camera_data = df_create_thin_lense_camera_data(look_from, look_at, up, 45.f, aspect, 2.0, dist_to_focus);
df_trace_rays((df_trace_rays_settings){
.image_width = image_width,
.image_height = image_height,
.samples_per_pixel = 64,
.camera = df_pinhole_camera,
.camera_data = &camera_data,
},
spheres, 0,
&plane, 1,
&image);
stbi_write_png("raytracer.png", image_width, image_height, 3, image, image_width * 3);
return 0;
}