2020-06-02 22:06:53 +00:00
|
|
|
#include <iostream>
|
2020-06-06 14:31:51 +00:00
|
|
|
#include <memory>
|
2020-06-02 22:06:53 +00:00
|
|
|
|
|
|
|
#include "color.h"
|
|
|
|
#include "vec3.h"
|
2020-06-03 20:39:40 +00:00
|
|
|
#include "ray.h"
|
2020-06-03 22:15:32 +00:00
|
|
|
#include "util.h"
|
2020-06-03 20:39:40 +00:00
|
|
|
|
2020-06-03 22:15:32 +00:00
|
|
|
#include "hittable_list.h"
|
|
|
|
#include "sphere.h"
|
2020-06-03 22:39:14 +00:00
|
|
|
#include "camera.h"
|
2020-06-03 22:15:32 +00:00
|
|
|
|
2020-06-06 12:47:39 +00:00
|
|
|
Color ray_color(const Ray& r, const Hittable& world, int depth) {
|
2020-06-03 22:15:32 +00:00
|
|
|
hit_record rec;
|
2020-06-06 12:47:39 +00:00
|
|
|
|
|
|
|
if (depth <= 0)
|
|
|
|
return Color(0, 0, 0);
|
|
|
|
|
2020-06-06 14:16:31 +00:00
|
|
|
if (world.hit(r, 0.001, infinity, rec)) {
|
2020-06-06 21:21:21 +00:00
|
|
|
Ray scattered;
|
|
|
|
Color attenuation;
|
|
|
|
if (rec.mat_ptr->scatter(r, rec, attenuation, scattered))
|
|
|
|
return attenuation * ray_color(scattered, world, depth - 1);
|
|
|
|
return Color(0, 0, 0);
|
2020-06-03 21:25:47 +00:00
|
|
|
}
|
2020-06-03 20:39:40 +00:00
|
|
|
Vec3 unit_direction = unit_vector(r.direction());
|
2020-06-03 22:15:32 +00:00
|
|
|
auto t = 0.5 * (unit_direction.y() + 1.0);
|
2020-06-03 20:39:40 +00:00
|
|
|
return (1.0 - t) * Color(1.0, 1.0, 1.0) + t * Color(0.5, 0.7, 1.0);
|
|
|
|
}
|
2020-06-02 22:06:53 +00:00
|
|
|
|
|
|
|
int main() {
|
2020-06-03 20:39:40 +00:00
|
|
|
const auto aspect_ratio = 16.0 / 9.0;
|
2020-06-06 12:47:39 +00:00
|
|
|
//const int image_width = 1280;
|
2020-06-06 13:55:24 +00:00
|
|
|
const int image_width = 768;
|
|
|
|
//const int image_width = 384;
|
2020-06-03 20:39:40 +00:00
|
|
|
const int image_height = static_cast<int>(image_width / aspect_ratio);
|
2020-06-06 21:21:21 +00:00
|
|
|
//const int samples_per_pixel = 1000;
|
2020-06-06 14:16:31 +00:00
|
|
|
const int samples_per_pixel = 400;
|
2020-06-06 12:47:39 +00:00
|
|
|
const int max_depth = 50;
|
2020-06-02 22:06:53 +00:00
|
|
|
|
|
|
|
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
|
|
|
|
|
2020-06-03 22:15:32 +00:00
|
|
|
Hittable_list world;
|
2020-06-06 21:21:21 +00:00
|
|
|
|
2020-06-06 22:03:25 +00:00
|
|
|
world.add(std::make_shared<Sphere>(Point3(0, 0, -1), 0.5, std::make_shared<Lambertian>(Color(0.1, 0.2, 0.5))));
|
2020-06-06 21:28:33 +00:00
|
|
|
world.add(std::make_shared<Sphere>(Point3(0, -100.5, -1), 100, std::make_shared<Lambertian>(Color(0.8, 0.8, 0.0))));
|
2020-06-06 21:21:21 +00:00
|
|
|
|
2020-06-06 22:03:25 +00:00
|
|
|
world.add(std::make_shared<Sphere>(Point3(1, 0, -1), 0.5, std::make_shared<Metal>(Color(0.8, 0.6, 0.2), 0.0)));
|
|
|
|
|
|
|
|
|
|
|
|
world.add(std::make_shared<Sphere>(Point3(-1, 0, -1), 0.5, std::make_shared<Dielectric>(1.45)));
|
2020-06-06 22:17:33 +00:00
|
|
|
world.add(std::make_shared<Sphere>(Point3(-1, 0, -1), -0.45, std::make_shared<Dielectric>(1.45)));
|
2020-06-03 22:15:32 +00:00
|
|
|
|
2020-06-06 22:17:33 +00:00
|
|
|
Camera cam(Point3(-2, 2, 1), Point3(0, 0, -1), Vec3(0, 1, 0), 75, aspect_ratio);
|
2020-06-03 22:39:14 +00:00
|
|
|
|
2020-06-06 14:31:51 +00:00
|
|
|
auto image = std::make_unique<Color[]>(image_height*image_width);
|
2020-06-06 13:55:24 +00:00
|
|
|
|
2020-06-02 22:06:53 +00:00
|
|
|
for (int j = image_height - 1; j >= 0; --j) {
|
|
|
|
std::cerr << "\rScanlines remaining: " << j << " " << std::flush;
|
2020-06-06 13:55:24 +00:00
|
|
|
#pragma omp parallel for
|
2020-06-02 22:06:53 +00:00
|
|
|
for (int i = 0; i < image_width; ++i) {
|
2020-06-03 22:39:14 +00:00
|
|
|
Color pixel_color(0, 0, 0);
|
|
|
|
for (int s = 0; s<samples_per_pixel; ++s) {
|
2020-06-06 12:47:39 +00:00
|
|
|
auto u = double(i + random_double(-0.5, 0.5)) / (image_width - 1);
|
|
|
|
auto v = double(j + random_double(-0.5, 0.5)) / (image_height - 1);
|
2020-06-03 22:39:14 +00:00
|
|
|
Ray r = cam.get_ray(u, v);
|
2020-06-06 12:47:39 +00:00
|
|
|
pixel_color += ray_color(r, world, max_depth);
|
2020-06-03 22:39:14 +00:00
|
|
|
}
|
2020-06-06 13:55:24 +00:00
|
|
|
image[i*image_height+j] = pixel_color;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cerr << "\nWriting file.\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);
|
2020-06-02 22:06:53 +00:00
|
|
|
}
|
|
|
|
}
|
2020-06-06 13:55:24 +00:00
|
|
|
std::cerr << "Done.\n";
|
2020-06-02 22:06:53 +00:00
|
|
|
}
|