Implemented diffuse reflection.

This commit is contained in:
Faerbit 2020-06-06 14:47:39 +02:00
parent be4e32755a
commit 494b48c560
2 changed files with 32 additions and 8 deletions

18
vec3.h
View File

@ -4,6 +4,8 @@
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include "util.h"
class Vec3 { class Vec3 {
public: public:
Vec3() : e{0, 0, 0} {} Vec3() : e{0, 0, 0} {}
@ -38,6 +40,14 @@ class Vec3 {
return e[0] * e[0] + e[1] * e[1] + e[2] * e[2]; return e[0] * e[0] + e[1] * e[1] + e[2] * e[2];
} }
inline static Vec3 random() {
return Vec3(random_double(), random_double(), random_double());
}
inline static Vec3 random(double min, double max) {
return Vec3(random_double(min, max), random_double(min, max), random_double(min, max));
}
private: private:
double e[3]; double e[3];
@ -102,4 +112,12 @@ inline Vec3 unit_vector(Vec3 v) {
return v / v.length(); return v / v.length();
} }
Vec3 random_in_unit_sphere() {
while (true) {
auto p = Vec3::random(-1, 1);
if (p.length_squared() >= 1) continue;
return p;
}
}
#endif // VEC3_H #endif // VEC3_H

View File

@ -9,10 +9,15 @@
#include "sphere.h" #include "sphere.h"
#include "camera.h" #include "camera.h"
Color ray_color(const Ray& r, const Hittable& world) { Color ray_color(const Ray& r, const Hittable& world, int depth) {
hit_record rec; hit_record rec;
if (depth <= 0)
return Color(0, 0, 0);
if (world.hit(r, 0, infinity, rec)) { if (world.hit(r, 0, infinity, rec)) {
return 0.5 * (rec.normal + Color(1,1,1)); Point3 target = rec.p + rec.normal + random_in_unit_sphere();
return 0.5 * ray_color(Ray(rec.p, target - rec.p), world, depth-1);
} }
Vec3 unit_direction = unit_vector(r.direction()); Vec3 unit_direction = unit_vector(r.direction());
auto t = 0.5 * (unit_direction.y() + 1.0); auto t = 0.5 * (unit_direction.y() + 1.0);
@ -21,10 +26,11 @@ Color ray_color(const Ray& r, const Hittable& world) {
int main() { int main() {
const auto aspect_ratio = 16.0 / 9.0; const auto aspect_ratio = 16.0 / 9.0;
const int image_width = 1280; //const int image_width = 1280;
//const int image_width = 384; const int image_width = 384;
const int image_height = static_cast<int>(image_width / aspect_ratio); const int image_height = static_cast<int>(image_width / aspect_ratio);
const int samples_per_pixel = 100; const int samples_per_pixel = 250;
const int max_depth = 50;
std::cout << "P3\n" << image_width << " " << image_height << "\n255\n"; std::cout << "P3\n" << image_width << " " << image_height << "\n255\n";
@ -39,10 +45,10 @@ int main() {
for (int i = 0; i < image_width; ++i) { for (int i = 0; i < image_width; ++i) {
Color pixel_color(0, 0, 0); Color pixel_color(0, 0, 0);
for (int s = 0; s<samples_per_pixel; ++s) { for (int s = 0; s<samples_per_pixel; ++s) {
auto u = double(i + random_double()) / (image_width - 1); auto u = double(i + random_double(-0.5, 0.5)) / (image_width - 1);
auto v = double(j + random_double()) / (image_height - 1); auto v = double(j + random_double(-0.5, 0.5)) / (image_height - 1);
Ray r = cam.get_ray(u, v); Ray r = cam.get_ray(u, v);
pixel_color += ray_color(r, world); pixel_color += ray_color(r, world, max_depth);
} }
write_color(std::cout, pixel_color, samples_per_pixel); write_color(std::cout, pixel_color, samples_per_pixel);
} }