toytracer/wtracer.cpp

52 lines
1.6 KiB
C++
Raw Normal View History

2020-06-02 22:06:53 +00:00
#include <iostream>
#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
Color ray_color(const Ray& r, const Hittable& world) {
hit_record rec;
if (world.hit(r, 0, infinity, rec)) {
return 0.5 * (rec.normal + Color(1,1,1));
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;
const int image_width = 1280;
2020-06-03 22:39:14 +00:00
//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-03 22:39:14 +00:00
const int samples_per_pixel = 100;
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;
world.add(std::make_shared<Sphere>(Point3(0, 0, -1), 0.5));
world.add(std::make_shared<Sphere>(Point3(0, -100.5, -1), 100));
2020-06-03 22:39:14 +00:00
Camera cam;
2020-06-02 22:06:53 +00:00
for (int j = image_height - 1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << " " << std::flush;
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) {
auto u = double(i + random_double()) / (image_width - 1);
auto v = double(j + random_double()) / (image_height - 1);
Ray r = cam.get_ray(u, v);
pixel_color += ray_color(r, world);
}
write_color(std::cout, pixel_color, samples_per_pixel);
2020-06-02 22:06:53 +00:00
}
}
std::cerr << "\nDone.\n";
}