toytracer/wtracer.cpp
2020-06-07 03:28:59 +02:00

135 lines
4.7 KiB
C++

#include <iostream>
#include <iomanip>
#include <memory>
#define LODEPNG_NO_COMPILE_DECODER
#include "lodepng.h"
#include "color.h"
#include "vec3.h"
#include "ray.h"
#include "util.h"
#include "hittable_list.h"
#include "sphere.h"
#include "camera.h"
Color ray_color(const Ray& r, const Hittable& world, int depth) {
hit_record rec;
if (depth <= 0)
return Color(0, 0, 0);
if (world.hit(r, 0.001, infinity, rec)) {
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);
}
Vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 1.0);
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<Lambertian>(Color(0.5, 0.5, 0.5));
world.add(std::make_shared<Sphere>(Point3(0, -1000, 0), 1000, ground_material));
for (int a = -sph_i; a<sph_i; a++) {
for (int b = -sph_i; b<sph_i; b++) {
auto choose_mat = random_double();
Point3 center(a + 0.9 * (11/sph_i) * random_double(), 0.2, b + 0.9 * (11/sph_i) * random_double());
if ((center - Point3(4, 0.2, 0)).length() > 0.9) {
std::shared_ptr<Material> sphere_material;
if (choose_mat < 0.8) {
// diffuse
auto albedo = Color::random() * Color::random();
sphere_material = std::make_shared<Lambertian>(albedo);
world.add(std::make_shared<Sphere>(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<Metal>(albedo, fuzz);
world.add(std::make_shared<Sphere>(center, 0.2, sphere_material));
} else {
// glass
sphere_material = std::make_shared<Dielectric>(1.45);
world.add(std::make_shared<Sphere>(center, 0.2, sphere_material));
}
}
}
}
auto material1 = std::make_shared<Dielectric>(1.45);
world.add(std::make_shared<Sphere>(Point3(0, 1, 0), 1.0, material1));
auto material2 = std::make_shared<Lambertian>(Color(0.4, 0.2, 0.1));
world.add(std::make_shared<Sphere>(Point3(-4, 1, 0), 1.0, material2));
auto material3 = std::make_shared<Metal>(Color(0.7, 0.6, 0.5), 0.0);
world.add(std::make_shared<Sphere>(Point3(4, 1, 0), 1.0, material3));
return world;
}
int main() {
const auto aspect_ratio = 16.0 / 9.0;
//const int image_width = 1280;
const int image_width = 768;
//const int image_width = 384;
const int image_height = static_cast<int>(image_width / aspect_ratio);
//const int samples_per_pixel = 1000;
const int samples_per_pixel = 400;
const int max_depth = 50;
auto world = setup_random_scene();
Point3 lookfrom(13, 2, 3);
Point3 lookat(0, 0, 0);
Vec3 vup(0, 1, 0);
auto dist_to_focus = (lookfrom - lookat).length();
auto aperture = 0.1;
Camera cam(lookfrom, lookat, vup, 20, aspect_ratio, aperture, dist_to_focus);
std::vector<Color> image(image_width * image_height);
for (int i = image_height-1; i>=0; --i) {
std::cerr << "\rScanline remaining: " << std::setw(4) << i << std::flush;
#pragma omp parallel for
for (int j = 0; j<image_width; ++j) {
Color pixel_color(0, 0, 0);
for (int s = 0; s<samples_per_pixel; ++s) {
auto u = double(j + random_double(-0.5, 0.5)) / (image_width - 1);
auto v = double(i + random_double(-0.5, 0.5)) / (image_height - 1);
Ray r = cam.get_ray(u, v);
pixel_color += ray_color(r, world, max_depth);
}
image.at(i*image_width+j) = pixel_color;
}
}
std::cerr << "\nAssembling image.\n";
std::vector<unsigned char> img_lode(image_width * image_height * 3);
for (int i = 0; i<image_height; ++i) {
for (int j = 0; j<image_width; ++j) {
write_color_vec(img_lode, i*image_width*3+j*3, image.at((image_height-1-i)*image_width+j), samples_per_pixel);
}
}
std::cerr << "Writing file \"image.png\".\n";
unsigned error = lodepng::encode("image.png", img_lode, image_width, image_height, LCT_RGB);
if (error)
std::cerr << "lodepng encoder error " << error << ": "<< lodepng_error_text(error) << std::endl;
std::cerr << "Done.\n";
}