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
debug/
# 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

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 scale = 1.0 / samples_per_pixel;
r *= scale;
g *= scale;
b *= scale;
r = sqrt(scale * r);
g = sqrt(scale * g);
b = sqrt(scale * b);
out << static_cast<int>(255.999 * clamp(r, 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 "hittable_list.h"
#include "sphere.h"
#include "vec3.h"
#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;
if (world.hit(r, 0, infinity, rec)) {
return 0.5 * (rec.normal + color(1, 1, 1));
if (depth <= 0)
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());
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);
@ -25,6 +31,7 @@ int main() {
const int image_width = 400;
const int image_height = static_cast<int>(image_width / aspect_ratio);
const int samples_per_pixel = 100;
const int max_depth = 50;
// World
hittable_list world;
@ -46,7 +53,7 @@ int main() {
auto v = (j + random_double()) / (image_height - 1);
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);
}

View file

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