Skip to content

Commit

Permalink
change to vec3()
Browse files Browse the repository at this point in the history
  • Loading branch information
gau-nernst committed Nov 20, 2023
1 parent dc74dbd commit b8b264c
Showing 1 changed file with 58 additions and 65 deletions.
123 changes: 58 additions & 65 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ void scene_metal_and_lambertian(World *world, Camera *camera) {

mat = material(Lambertian_new(texture(Vec3_new(0.8, 0.8, 0.0))));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, -100.5, -1}, 100, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, -100.5, -1), 100, mat)));

mat = material(Lambertian_new(texture(Vec3_new(0.7, 0.3, 0.3))));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, 0, -1}, 0.5, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, 0, -1), 0.5, mat)));

mat = material(Metal_new(texture(Vec3_new(0.8, 0.8, 0.8)), 0.3));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){-1, 0, -1}, 0.5, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(-1, 0, -1), 0.5, mat)));

mat = material(Metal_new(texture(Vec3_new(0.8, 0.6, 0.2)), 1.0));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){1, 0, -1}, 0.5, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(1, 0, -1), 0.5, mat)));

camera->vfov = 90.0f;
camera->background = (Vec3){0.7, 0.8, 1};
camera->background = vec3(0.7, 0.8, 1);
camera->look_from = VEC3_ZERO;
camera->look_to = (Vec3){0, 0, -1};
camera->look_to = vec3(0, 0, -1);
}

void scene_book1_final(World *world, Camera *camera) {
Expand All @@ -38,34 +38,30 @@ void scene_book1_final(World *world, Camera *camera) {

mat = material(Lambertian_new(texture(Vec3_new(0.5, 0.5, 0.5))));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, -1000, -1}, 1000, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, -1000, -1), 1000, mat)));

mat = material(Dielectric_new(texture(Vec3_new(1, 1, 1)), 1.5));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, 1, 0}, 1, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, 1, 0), 1, mat)));

mat = material(Lambertian_new(texture(Vec3_new(0.4, 0.2, 0.1))));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){-4, 1, 0}, 1, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(-4, 1, 0), 1, mat)));

mat = material(Metal_new(texture(Vec3_new(0.7, 0.6, 0.5)), 0));
MaterialList_append(&world->materials, mat);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){4, 1, 0}, 1, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(4, 1, 0), 1, mat)));

PCG32State rng;
pcg32_seed(&rng, 19, 29);

Vec3 ref_point = {4, 0.2, 0};
Vec3 ref_point = vec3(4, 0.2, 0);
float radius = 0.2f;

for (int a = -11; a < 11; a++)
for (int b = -11; b < 11; b++) {
float choose_material = pcg32_f32(&rng);
Vec3 center = {
(float)a + 0.9f * pcg32_f32(&rng),
radius,
(float)b + 0.9f * pcg32_f32(&rng),
};
Vec3 center = vec3((float)a + 0.9f * pcg32_f32(&rng), radius, (float)b + 0.9f * pcg32_f32(&rng));

if (vec3_length(vec3_sub(center, ref_point)) > 0.9f) {
Vec3 *color_p = my_malloc(sizeof(Vec3));
Expand All @@ -77,7 +73,7 @@ void scene_book1_final(World *world, Camera *camera) {
*color_p = vec3_rand_between(&rng, 0.5f, 1);
mat = material(Metal_new(texture(color_p), pcg32_f32(&rng) * 0.5f));
} else {
*color_p = (Vec3){1, 1, 1};
*color_p = vec3(1, 1, 1);
mat = material(Dielectric_new(texture(color_p), 1.5f));
}

Expand All @@ -92,8 +88,8 @@ void scene_book1_final(World *world, Camera *camera) {
HittableList_append(&world->objects, hittable(bvh));

camera->vfov = 20.0f;
camera->background = (Vec3){0.7, 0.8, 1};
camera->look_from = (Vec3){13, 2, 3};
camera->background = vec3(0.7, 0.8, 1);
camera->look_from = vec3(13, 2, 3);
camera->look_to = VEC3_ZERO;
camera->dof_angle = 0.6f;
}
Expand All @@ -105,12 +101,12 @@ void scene_checker(World *world, Camera *camera) {
Material mat = material(Lambertian_new(texture(checker_p)));
MaterialList_append(&world->materials, mat);

HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, -10, 0}, 10, mat)));
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, 10, 0}, 10, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, -10, 0), 10, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, 10, 0), 10, mat)));

camera->vfov = 20.0f;
camera->background = (Vec3){0.7, 0.8, 1};
camera->look_from = (Vec3){13, 2, 3};
camera->background = vec3(0.7, 0.8, 1);
camera->look_from = vec3(13, 2, 3);
camera->look_to = VEC3_ZERO;
}

Expand All @@ -122,8 +118,8 @@ void scene_earth(World *world, Camera *camera) {
HittableList_append(&world->objects, hittable(Sphere_new(VEC3_ZERO, 2, mat)));

camera->vfov = 20.0f;
camera->background = (Vec3){0.7, 0.8, 1};
camera->look_from = (Vec3){13, 2, 3};
camera->background = vec3(0.7, 0.8, 1);
camera->look_from = vec3(13, 2, 3);
camera->look_to = VEC3_ZERO;
}

Expand All @@ -136,12 +132,12 @@ void scene_perlin(World *world, Camera *camera) {
Material mat = material(Lambertian_new(texture(Perlin_new(4.0f, 7, &rng))));
MaterialList_append(&world->materials, mat);

HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, -1000, 0}, 1000, mat)));
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, 2, 0}, 2, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, -1000, 0), 1000, mat)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, 2, 0), 2, mat)));

camera->vfov = 20.0f;
camera->background = (Vec3){0.7, 0.8, 1};
camera->look_from = (Vec3){13, 2, 3};
camera->background = vec3(0.7, 0.8, 1);
camera->look_from = vec3(13, 2, 3);
camera->look_to = VEC3_ZERO;
camera->dof_angle = 0.0f;
camera->focal_length = 10.0f;
Expand All @@ -159,15 +155,15 @@ void scene_simple_light(World *world, Camera *camera) {
MaterialList_append(&world->materials, perlin);
MaterialList_append(&world->materials, light);

HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, -1000, 0}, 1000, perlin)));
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, 2, 0}, 2, perlin)));
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, 7, 0}, 2, light)));
HittableList_append(&world->objects, hittable(Quad_new((Vec3){3, 1, -2}, (Vec3){2, 0, 0}, (Vec3){0, 2, 0}, light)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, -1000, 0), 1000, perlin)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, 2, 0), 2, perlin)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, 7, 0), 2, light)));
HittableList_append(&world->objects, hittable(Quad_new(vec3(3, 1, -2), vec3(2, 0, 0), vec3(0, 2, 0), light)));

camera->vfov = 20.0f;
camera->background = VEC3_ZERO;
camera->look_from = (Vec3){26, 3, 6};
camera->look_to = (Vec3){0, 2, 0};
camera->look_from = vec3(26, 3, 6);
camera->look_to = vec3(0, 2, 0);
}

void scene_cornell_box(World *world, Camera *camera) {
Expand All @@ -183,33 +179,30 @@ void scene_cornell_box(World *world, Camera *camera) {
MaterialList_append(&world->materials, green);
MaterialList_append(&world->materials, light);

HittableList_append(&world->objects, hittable(Quad_new(vec3(555, 0, 0), vec3(0, 555, 0), vec3(0, 0, 555), green)));
HittableList_append(&world->objects, hittable(Quad_new(vec3(0, 0, 0), vec3(0, 555, 0), vec3(0, 0, 555), red)));
HittableList_append(&world->objects,
hittable(Quad_new((Vec3){555, 0, 0}, (Vec3){0, 555, 0}, (Vec3){0, 0, 555}, green)));
HittableList_append(&world->objects, hittable(Quad_new(vec3(0, 0, 0), (Vec3){0, 555, 0}, (Vec3){0, 0, 555}, red)));
hittable(Quad_new(vec3(343, 554, 332), vec3(-130, 0, 0), vec3(0, 0, -105), light)));
HittableList_append(&world->objects, hittable(Quad_new(vec3(0, 0, 0), vec3(555, 0, 0), vec3(0, 0, 555), white)));
HittableList_append(&world->objects,
hittable(Quad_new((Vec3){343, 554, 332}, (Vec3){-130, 0, 0}, (Vec3){0, 0, -105}, light)));
HittableList_append(&world->objects,
hittable(Quad_new((Vec3){0, 0, 0}, (Vec3){555, 0, 0}, (Vec3){0, 0, 555}, white)));
HittableList_append(&world->objects,
hittable(Quad_new((Vec3){555, 555, 555}, (Vec3){-555, 0, 0}, (Vec3){0, 0, -555}, white)));
HittableList_append(&world->objects,
hittable(Quad_new((Vec3){0, 0, 555}, (Vec3){555, 0, 0}, (Vec3){0, 555, 0}, white)));
hittable(Quad_new(vec3(555, 555, 555), vec3(-555, 0, 0), vec3(0, 0, -555), white)));
HittableList_append(&world->objects, hittable(Quad_new(vec3(0, 0, 555), vec3(555, 0, 0), vec3(0, 555, 0), white)));

Hittable box1 = hittable(Box_new((Vec3){0, 0, 0}, (Vec3){165, 330, 165}, white));
Hittable box1 = hittable(Box_new(vec3(0, 0, 0), vec3(165, 330, 165), white));
box1 = hittable(RotateY_new(box1, 15));
box1 = hittable(Translate_new(box1, (Vec3){265, 0, 295}));
box1 = hittable(Translate_new(box1, vec3(265, 0, 295)));
HittableList_append(&world->objects, box1);

Hittable box2 = hittable(Box_new((Vec3){0, 0, 0}, (Vec3){165, 165, 165}, white));
Hittable box2 = hittable(Box_new(vec3(0, 0, 0), vec3(165, 165, 165), white));
box2 = hittable(RotateY_new(box2, -18));
box2 = hittable(Translate_new(box2, (Vec3){130, 0, 65}));
box2 = hittable(Translate_new(box2, vec3(130, 0, 65)));
HittableList_append(&world->objects, box2);

camera->aspect_ratio = 1.0f;
camera->background = (Vec3){0, 0, 0};
camera->background = vec3(0, 0, 0);
camera->vfov = 40.0f;
camera->look_from = (Vec3){278, 278, -800};
camera->look_to = (Vec3){278, 278, 0};
camera->look_from = vec3(278, 278, -800);
camera->look_to = vec3(278, 278, 0);
}

void scene_book2_final(World *world, Camera *camera, bool enable_bvh) {
Expand All @@ -226,8 +219,8 @@ void scene_book2_final(World *world, Camera *camera, bool enable_bvh) {
for (int i = 0; i < boxes_per_side; i++)
for (int j = 0; j < boxes_per_side; j++) {
float w = 100.0f;
Vec3 p0 = {-1000.0f + i * w, 0.0f, -1000.0f + j * w};
Vec3 p1 = {-1000.0f + (i + 1) * w, pcg32_f32_between(&rng, 1, 101), -1000.0f + (j + 1) * w};
Vec3 p0 = vec3(-1000.0f + i * w, 0.0f, -1000.0f + j * w);
Vec3 p1 = vec3(-1000.0f + (i + 1) * w, pcg32_f32_between(&rng, 1, 101), -1000.0f + (j + 1) * w);
HittableList_append(boxes1_list, hittable(Box_new(p0, p1, ground)));
}

Expand All @@ -243,38 +236,38 @@ void scene_book2_final(World *world, Camera *camera, bool enable_bvh) {
Material light = material(DiffuseLight_new(texture(Vec3_new(7, 7, 7))));
MaterialList_append(&world->materials, light);
HittableList_append(&world->objects,
hittable(Quad_new((Vec3){123, 554, 147}, (Vec3){300, 0, 0}, (Vec3){0, 0, 265}, light)));
hittable(Quad_new(vec3(123, 554, 147), vec3(300, 0, 0), vec3(0, 0, 265), light)));

Vec3 center1 = {400, 400, 200};
Vec3 center1 = vec3(400, 400, 200);
// Vec3 center2 = {430, 400, 200}; // TODO: moving sphere
Material sphere_mat = material(Lambertian_new(texture(Vec3_new(0.7, 0.3, 0.1))));
MaterialList_append(&world->materials, sphere_mat);
HittableList_append(&world->objects, hittable(Sphere_new(center1, 50, sphere_mat)));

Material glass = material(Dielectric_new(texture(Vec3_new(1, 1, 1)), 1.5));
MaterialList_append(&world->materials, glass);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){260, 150, 45}, 50, glass)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(260, 150, 45), 50, glass)));

Material metal = material(Metal_new(texture(Vec3_new(0.8, 0.8, 0.9)), 1.0));
MaterialList_append(&world->materials, metal);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){0, 150, 145}, 50, metal)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(0, 150, 145), 50, metal)));

// subsurface material
Hittable boundary = hittable(Sphere_new((Vec3){360, 150, 145}, 70, glass));
Hittable boundary = hittable(Sphere_new(vec3(360, 150, 145), 70, glass));
HittableList_append(&world->objects, boundary);
HittableList_append(&world->objects, hittable(ConstantMedium_new(boundary, 0.2, texture(Vec3_new(0.2, 0.4, 0.9)))));

// mist
boundary = hittable(Sphere_new((Vec3){0, 0, 0}, 5000, glass));
boundary = hittable(Sphere_new(vec3(0, 0, 0), 5000, glass));
HittableList_append(&world->objects, hittable(ConstantMedium_new(boundary, 0.0001, texture(Vec3_new(1, 1, 1)))));

Material earth = material(Lambertian_new(texture(Image_new("earthmap.jpg"))));
MaterialList_append(&world->materials, earth);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){400, 200, 400}, 100, earth)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(400, 200, 400), 100, earth)));

Material perlin = material(Lambertian_new(texture(Perlin_new(0.1, 7, &rng))));
MaterialList_append(&world->materials, perlin);
HittableList_append(&world->objects, hittable(Sphere_new((Vec3){220, 280, 300}, 80, perlin)));
HittableList_append(&world->objects, hittable(Sphere_new(vec3(220, 280, 300), 80, perlin)));

Material white = material(Lambertian_new(texture(Vec3_new(0.73, 0.73, 0.73))));
MaterialList_append(&world->materials, white);
Expand All @@ -294,14 +287,14 @@ void scene_book2_final(World *world, Camera *camera, bool enable_bvh) {
boxes2 = hittable(boxes2_list);

boxes2 = hittable(RotateY_new(boxes2, 15.0f));
boxes2 = hittable(Translate_new(boxes2, (Vec3){-100, 270, 395}));
boxes2 = hittable(Translate_new(boxes2, vec3(-100, 270, 395)));
HittableList_append(&world->objects, boxes2);

camera->aspect_ratio = 1.0f;
camera->background = VEC3_ZERO;
camera->vfov = 40.0f;
camera->look_from = (Vec3){478, 278, -600};
camera->look_to = (Vec3){278, 278, 0};
camera->look_from = vec3(478, 278, -600);
camera->look_to = vec3(278, 278, 0);
}

int main(int argc, char *argv[]) {
Expand All @@ -313,7 +306,7 @@ int main(int argc, char *argv[]) {
camera.img_width = 400;
camera.samples_per_pixel = 100;
camera.max_depth = 10;
camera.vup = (Vec3){0, 1, 0};
camera.vup = vec3(0, 1, 0);
camera.dof_angle = 0.0f;
camera.focal_length = 10.0f;

Expand Down

0 comments on commit b8b264c

Please sign in to comment.