2023-06-19 17:26:08 +02:00
|
|
|
/* Main command line executable. */
|
2023-05-23 13:20:59 +02:00
|
|
|
|
2023-06-19 17:26:08 +02:00
|
|
|
#include <defocus/defocus.h>
|
2023-07-12 22:57:49 +02:00
|
|
|
#include <defocus/df_math.h>
|
2023-05-23 13:20:59 +02:00
|
|
|
|
2023-06-19 17:26:08 +02:00
|
|
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
|
|
|
#include <stb_image_write.h>
|
|
|
|
|
2023-06-20 23:28:33 +02:00
|
|
|
df_plane get_image_filling_plane(int width, int height, df_image_handle image, float focal_length)
|
|
|
|
{
|
|
|
|
float aspect = (float)width / (float)height;
|
2023-06-19 17:26:08 +02:00
|
|
|
|
|
|
|
df_plane plane;
|
|
|
|
plane.base_x = 0.f;
|
|
|
|
plane.base_y = 0.f;
|
2023-06-20 23:28:33 +02:00
|
|
|
plane.base_z = -2 * focal_length;
|
2023-06-19 17:26:08 +02:00
|
|
|
plane.normal_x = 0.f;
|
|
|
|
plane.normal_y = 0.f;
|
|
|
|
plane.normal_z = 1.f;
|
2023-06-20 23:28:33 +02:00
|
|
|
plane.img_p0_x = -aspect / focal_length;
|
|
|
|
plane.img_p0_y = -1.f / focal_length;
|
2023-06-19 17:26:08 +02:00
|
|
|
plane.img_p0_z = plane.base_z;
|
2023-06-20 23:28:33 +02:00
|
|
|
plane.img_w = 2.f * aspect / focal_length;
|
|
|
|
plane.img_h = 2.f / focal_length;
|
2023-06-19 17:26:08 +02:00
|
|
|
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;
|
2023-06-20 23:28:33 +02:00
|
|
|
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;
|
2023-07-12 22:57:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
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;
|
2023-06-19 17:26:08 +02:00
|
|
|
|
|
|
|
uint8_t *image = NULL;
|
2023-07-12 22:57:49 +02:00
|
|
|
|
|
|
|
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));
|
2023-06-19 17:26:08 +02:00
|
|
|
|
2023-07-12 22:57:49 +02:00
|
|
|
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);
|
2023-06-19 17:26:08 +02:00
|
|
|
|
|
|
|
df_trace_rays((df_trace_rays_settings){
|
|
|
|
.image_width = image_width,
|
2023-06-19 17:29:28 +02:00
|
|
|
.image_height = image_height,
|
2023-06-20 23:28:33 +02:00
|
|
|
.samples_per_pixel = 64,
|
2023-07-12 22:57:49 +02:00
|
|
|
.camera = df_pinhole_camera,
|
2023-06-20 23:28:33 +02:00
|
|
|
.camera_data = &camera_data,
|
2023-06-19 17:26:08 +02:00
|
|
|
},
|
|
|
|
spheres, 0,
|
|
|
|
&plane, 1,
|
|
|
|
&image);
|
|
|
|
|
|
|
|
stbi_write_png("raytracer.png", image_width, image_height, 3, image, image_width * 3);
|
2023-05-23 13:20:59 +02:00
|
|
|
|
2023-05-09 13:04:14 +02:00
|
|
|
|
|
|
|
return 0;
|
2023-04-19 13:13:08 +02:00
|
|
|
}
|