diff --git a/wtracer.cpp b/wtracer.cpp index f793b4c..25e5ef8 100644 --- a/wtracer.cpp +++ b/wtracer.cpp @@ -28,6 +28,53 @@ Color ray_color(const Ray& r, const Hittable& world, int depth) { return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0); } +Hittable_list setup_random_scene() { + Hittable_list world; + + const int sph_i = 3; + + auto ground_material = std::make_shared(Color(0.5, 0.5, 0.5)); + world.add(std::make_shared(Point3(0, -1000, 0), 1000, ground_material)); + for (int a = -sph_i; a 0.9) { + std::shared_ptr sphere_material; + if (choose_mat < 0.8) { + // diffuse + auto albedo = Color::random() * Color::random(); + sphere_material = std::make_shared(albedo); + world.add(std::make_shared(center, 0.2, sphere_material)); + } else if (choose_mat < 0.95) { + // metal + auto albedo = Color::random(0.5, 1); + auto fuzz = random_double(0, 0.5); + + sphere_material = std::make_shared(albedo, fuzz); + world.add(std::make_shared(center, 0.2, sphere_material)); + } else { + // glass + sphere_material = std::make_shared(1.45); + world.add(std::make_shared(center, 0.2, sphere_material)); + } + } + } + } + + auto material1 = std::make_shared(1.45); + world.add(std::make_shared(Point3(0, 1, 0), 1.0, material1)); + + auto material2 = std::make_shared(Color(0.4, 0.2, 0.1)); + world.add(std::make_shared(Point3(-4, 1, 0), 1.0, material2)); + + auto material3 = std::make_shared(Color(0.7, 0.6, 0.5), 0.0); + world.add(std::make_shared(Point3(4, 1, 0), 1.0, material3)); + + return world; +} + int main() { const auto aspect_ratio = 16.0 / 9.0; //const int image_width = 1280; @@ -38,24 +85,13 @@ int main() { const int samples_per_pixel = 400; const int max_depth = 50; - std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; + auto world = setup_random_scene(); - Hittable_list world; - - world.add(std::make_shared(Point3(0, 0, -1), 0.5, std::make_shared(Color(0.1, 0.2, 0.5)))); - world.add(std::make_shared(Point3(0, -100.5, -1), 100, std::make_shared(Color(0.8, 0.8, 0.0)))); - - world.add(std::make_shared(Point3(1, 0, -1), 0.5, std::make_shared(Color(0.8, 0.6, 0.2), 0.0))); - - - world.add(std::make_shared(Point3(-1, 0, -1), 0.5, std::make_shared(1.45))); - world.add(std::make_shared(Point3(-1, 0, -1), -0.45, std::make_shared(1.45))); - - 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 aperture = 0.1; Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus); @@ -76,6 +112,8 @@ int main() { } } std::cerr << "\nWriting file.\n"; + + std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; for (int j = image_height - 1; j >= 0; --j) { for (int i = 0; i < image_width; ++i) { write_color(std::cout, image[i*image_height+j], samples_per_pixel);