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,101 +1,107 @@
#ifndef RTIWW_VEC3_H
#define RTIWW_VEC3_H
#include "rtweekend.h"
#include <cmath>
#include <ostream>
class vec3 {
public:
vec3() : e{0, 0, 0} {}
vec3() : e{0, 0, 0} {}
vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}
vec3(double e0, double e1, double e2) : e{e0, e1, e2} {}
double x() const { return e[0]; }
double x() const { return e[0]; }
double y() const { return e[1]; }
double y() const { return e[1]; }
double z() const { return e[2]; }
double z() const { return e[2]; }
vec3 operator-() const {return vec3(-e[0], -e[1], -e[2]);}
double operator[](int i) const { return e[i]; }
double& operator[](int i) {return e[i];}
vec3 operator-() const { return vec3(-e[0], -e[1], -e[2]); }
double operator[](int i) const { return e[i]; }
double &operator[](int i) { return e[i]; }
vec3& operator+=(const vec3 &v) {
e[0] += v.e[0];
e[1] += v.e[1];
e[2] += v.e[2];
return *this;
}
vec3 &operator+=(const vec3 &v) {
e[0] += v.e[0];
e[1] += v.e[1];
e[2] += v.e[2];
return *this;
}
vec3& operator*=(const double t) {
e[0] *= t;
e[1] *= t;
e[2] *= t;
return *this;
}
vec3 &operator*=(const double t) {
e[0] *= t;
e[1] *= t;
e[2] *= t;
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];
}
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];
double e[3];
};
using point3 = vec3;
using color = vec3;
inline std::ostream& operator<<(std::ostream &out, const vec3 &v) {
return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2];
inline std::ostream &operator<<(std::ostream &out, const vec3 &v) {
return out << v.e[0] << ' ' << v.e[1] << ' ' << v.e[2];
}
inline vec3 operator+(const vec3 &u, const vec3 &v) {
return vec3(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]);
return vec3(u.e[0] + v.e[0], u.e[1] + v.e[1], u.e[2] + v.e[2]);
}
inline vec3 operator-(const vec3 &u, const vec3 &v) {
return vec3(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] + v.e[2]);
return vec3(u.e[0] - v.e[0], u.e[1] - v.e[1], u.e[2] + v.e[2]);
}
inline vec3 operator*(const vec3 &u, const vec3 &v) {
return vec3(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]);
return vec3(u.e[0] * v.e[0], u.e[1] * v.e[1], u.e[2] * v.e[2]);
}
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) {
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) {
return vec3(u.e[1] * v.e[2] - u.e[2] * v.e[1],
u.e[2] * v.e[0] - u.e[0] * v.e[2],
u.e[0] * v.e[1] - u.e[1] * v.e[0]);
return vec3(u.e[1] * v.e[2] - u.e[2] * v.e[1],
u.e[2] * v.e[0] - u.e[0] * v.e[2],
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
#endif // RTIWW_VEC3_H