Lambertian Reflection

This commit is contained in:
Armin Friedl 2022-07-30 10:25:16 +02:00
parent cf5406dbba
commit 6f206bb898
4 changed files with 72 additions and 58 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
image.ppm image.ppm
debug/
# Created by https://www.toptal.com/developers/gitignore/api/intellij+all,vim,emacs,linux,clion+all # Created by https://www.toptal.com/developers/gitignore/api/intellij+all,vim,emacs,linux,clion+all
# Edit at https://www.toptal.com/developers/gitignore?templates=intellij+all,vim,emacs,linux,clion+all # Edit at https://www.toptal.com/developers/gitignore?templates=intellij+all,vim,emacs,linux,clion+all

View file

@ -12,9 +12,9 @@ void write_color(std::ostream &out, color pixel_color, int samples_per_pixel) {
auto b = pixel_color.z(); auto b = pixel_color.z();
auto scale = 1.0 / samples_per_pixel; auto scale = 1.0 / samples_per_pixel;
r *= scale; r = sqrt(scale * r);
g *= scale; g = sqrt(scale * g);
b *= scale; b = sqrt(scale * b);
out << static_cast<int>(255.999 * clamp(r, 0.0, 0.999)) << ' ' out << static_cast<int>(255.999 * clamp(r, 0.0, 0.999)) << ' '
<< static_cast<int>(255.999 * clamp(g, 0.0, 0.999)) << ' ' << static_cast<int>(255.999 * clamp(g, 0.0, 0.999)) << ' '

View file

@ -5,14 +5,20 @@
#include "color.h" #include "color.h"
#include "hittable_list.h" #include "hittable_list.h"
#include "sphere.h" #include "sphere.h"
#include "vec3.h"
#include <iostream> #include <iostream>
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 (world.hit(r, 0, infinity, rec)) { if (depth <= 0)
return 0.5 * (rec.normal + color(1, 1, 1)); return color(0, 0, 0);
if (world.hit(r, 0.001, infinity, rec)) {
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);
return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0); return (1.0 - t) * color(1.0, 1.0, 1.0) + t * color(0.5, 0.7, 1.0);
@ -25,6 +31,7 @@ int main() {
const int image_width = 400; const int image_width = 400;
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 = 100;
const int max_depth = 50;
// World // World
hittable_list world; hittable_list world;
@ -46,7 +53,7 @@ int main() {
auto v = (j + random_double()) / (image_height - 1); auto v = (j + random_double()) / (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);
} }

View file

@ -1,6 +1,7 @@
#ifndef RTIWW_VEC3_H #ifndef RTIWW_VEC3_H
#define RTIWW_VEC3_H #define RTIWW_VEC3_H
#include "rtweekend.h"
#include <cmath> #include <cmath>
#include <ostream> #include <ostream>
@ -34,18 +35,22 @@ public:
return *this; return *this;
} }
vec3& operator/=(const double t) { vec3 &operator/=(const double t) { return *this *= 1 / t; }
return *this *= 1/t;
}
double length() const { double length() const { return sqrt(length_squared()); }
return sqrt(length_squared());
}
double length_squared() const { double length_squared() const {
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));
}
public: public:
double e[3]; double e[3];
@ -74,18 +79,12 @@ inline vec3 operator*(double t, const vec3 &v) {
return vec3(t * v.e[0], t * v.e[1], t * v.e[2]); return vec3(t * v.e[0], t * v.e[1], t * v.e[2]);
} }
inline vec3 operator*(const vec3 &v, double t) { inline vec3 operator*(const vec3 &v, double t) { return t * v; }
return t*v;
}
inline vec3 operator/(vec3 v, double t) { inline vec3 operator/(vec3 v, double t) { return (1 / t) * v; }
return (1/t) * v;
}
inline double dot(const vec3 &u, const vec3 &v) { inline double dot(const vec3 &u, const vec3 &v) {
return u.e[0] * v.e[0] return u.e[0] * v.e[0] + u.e[1] * v.e[1] + u.e[2] * v.e[2];
+ u.e[1] * v.e[1]
+ u.e[2] * v.e[2];
} }
inline vec3 cross(const vec3 &u, const vec3 &v) { inline vec3 cross(const vec3 &u, const vec3 &v) {
@ -94,8 +93,15 @@ inline vec3 cross(const vec3 &u, const vec3 &v) {
u.e[0] * v.e[1] - u.e[1] * v.e[0]); u.e[0] * v.e[1] - u.e[1] * v.e[0]);
} }
inline vec3 unit_vector(vec3 v) { 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 // RTIWW_VEC3_H #endif // RTIWW_VEC3_H