Skip to content

Commit

Permalink
🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
alysssssa committed Apr 30, 2023
1 parent 4e004aa commit 6e387fc
Showing 1 changed file with 55 additions and 19 deletions.
74 changes: 55 additions & 19 deletions raytracer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,35 +38,71 @@ colour ray_colour(const ray& r, const hittable& world, int depth) {
return (1.0-t)*colour(1.0, 1.0, 1.0) + t*colour(0.5, 0.7, 1.0);
}

hittable_list random_scene() {
hittable_list world;

auto ground_material = make_shared<lambertian>(colour(0.5, 0.5, 0.5));
world.add(make_shared<sphere>(point3(0,-1000,0), 1000, ground_material));

for (int a = -11; a < 11; a++)
for (int b = -11; b < 11; b++) {
auto choose_mat = random_double();
point3 center(a + 0.9*random_double(), 0.2, b + 0.9*random_double());

// don't let spheres intersect with each other
if ((center - point3(4, 0.2, 0)).length() > 0.9) {
shared_ptr<material> sphere_material;

// diffuse
if (choose_mat < 0.8) {
auto albedo = colour::random() * colour::random();
sphere_material = make_shared<lambertian>(albedo);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
// metal
} else if (choose_mat < 0.95) {
auto albedo = colour::random(0.5, 1);
auto fuzz = random_double(0, 0.5);
sphere_material = make_shared<metal>(albedo, fuzz);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
// glass
} else {
sphere_material = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(center, 0.2, sphere_material));
}
}
}

// three big spheres
auto material1 = make_shared<dielectric>(1.5);
world.add(make_shared<sphere>(point3(0,1,0), 1.0, material1));

auto material2 = make_shared<lambertian>(colour(0.4, 0.2, 0.1));
world.add(make_shared<sphere>(point3(-4,1,0), 1.0, material2));

auto material3 = make_shared<metal>(colour(0.7, 0.6, 0.5), 0.0);
world.add(make_shared<sphere>(point3(4,1,0), 1.0, material3));

return world;
}

int main() {

// write output image in ppm
const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 400;
const auto aspect_ratio = 3.0 / 2.0;
const int image_width = 1200;
const int image_height = static_cast<int>(image_width / aspect_ratio);
const int samples_per_pixel = 100;
const int samples_per_pixel = 1;
const int max_depth = 50;

// world
hittable_list world;

auto material_ground = make_shared<lambertian>(colour(0.8, 0.8, 0.0));
auto material_center = make_shared<lambertian>(colour(0.1, 0.2, 0.5));
auto material_left = make_shared<dielectric>(1.5);
auto material_right = make_shared<metal>(colour(0.8, 0.6, 0.2), 0.0);

world.add(make_shared<sphere>(point3(0.0, -100.5, -1.0), 100.0, material_ground));
world.add(make_shared<sphere>(point3(0.0, 0.0, -1.0), 0.5, material_center));
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), 0.5, material_left));
world.add(make_shared<sphere>(point3(-1.0, 0.0, -1.0), -0.45, material_left));
world.add(make_shared<sphere>(point3(1.0, 0.0, -1.0), 0.5, material_right));
auto world = random_scene();

// camera with depth of field
point3 lookfrom(3,3,2);
point3 lookat(0,0,-1);
point3 lookfrom(13,2,3);
point3 lookat(0,0,0);
vec3 vup(0,1,0);
auto dist_to_focus = (lookfrom-lookat).length();
auto aperture = 2.0;
auto dist_to_focus = 10.0;
auto aperture = 0.1;

camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);

Expand Down

0 comments on commit 6e387fc

Please sign in to comment.