Arty
random.h
1 #ifndef RANDOM_H
2 #define RANDOM_H
3 
4 #include "float3.h"
5 #include "color.h"
6 
7 #include <algorithm>
8 
10 struct LocalCoords {
14  LocalCoords() {}
15  LocalCoords(const float3& n, const float3& t, const float3& bt) : n(n), t(t), bt(bt) {}
16 };
17 
19 inline LocalCoords gen_local_coords(const float3& n) {
20  // From "Building an Orthonormal Basis, Revisited", Duff et al.
21  const float sign = copysignf(1.0f, n.z);
22  const float a = -1.0f / (sign + n.z);
23  const float b = n.x * n.y * a;
24  float3 t (1.0f + sign * n.x* n.x * a, sign * b, -sign * n.x);
25  float3 bt(b, sign + n.y * n.y * a, -n.y);
26  return LocalCoords(n, t, bt);
27 }
28 
30 struct DirSample {
31  float3 dir;
32  float pdf;
33  DirSample() {}
34  DirSample(const float3& d, float p) : dir(d), pdf(p) {}
35 };
36 
38 inline float uniform_sphere_pdf() {
39  return 1.0f / (4.0f * pi);
40 }
41 
43 inline DirSample sample_uniform_sphere(float u, float v) {
44  const float c = 2.0f * v - 1.0f;
45  const float s = std::sqrt(1.0f - c * c);
46  const float phi = 2.0f * pi * u;
47  const float x = s * std::cos(phi);
48  const float y = s * std::sin(phi);
49  const float z = c;
50  return DirSample(float3(x, y, z), uniform_sphere_pdf());
51 }
52 
54 inline float cosine_hemisphere_pdf(float c) {
55  // TODO: "c" is the cosine of the direction.
56  // You should return the corresponding pdf.
57  return 1.0f; // <--- This is probably incorrect
58 }
59 
61 inline DirSample sample_cosine_hemisphere(const LocalCoords& coords, float u, float v) {
62  // TODO: Sample a direction on the hemisphere using a pdf proportional to cos(theta).
63  // The hemisphere is defined by the coordinate system "coords".
64  // "u" and "v" are random numbers between [0, 1].
65  return DirSample(float3(1, 0, 0), 1.0f); // <--- This is probably incorrect
66 }
67 
69 inline float cosine_power_hemisphere_pdf(float c, float k) {
70  // TODO: "c" is the cosine of the direction, and k is the power.
71  // You should return the corresponding pdf.
72  return 1.0f; // <--- This is probably incorrect
73 }
74 
76 inline DirSample sample_cosine_power_hemisphere(const LocalCoords& coords, float k, float u, float v) {
77  // TODO: Sample a direction on the hemisphere using a pdf proportional to cos(theta)^k.
78  // The hemisphere is defined by the coordinate system "coords".
79  // "u" and "v" are random numbers between [0, 1].
80  return DirSample(float3(1, 0, 0), 1.0f); // <--- This is probably incorrect
81 }
82 
86 inline float russian_roulette(const rgb& c, float max = 0.75f) {
87  return std::min(max, dot(c, luminance) * 2.0f);
88 }
89 
90 #endif // RANDOM_H
float3 n
Normal.
Definition: random.h:11
Definition: float3.h:10
Direction sample, from sampling a set of directions.
Definition: random.h:30
float3 t
Tangent.
Definition: random.h:12
Local coordinates for shading.
Definition: random.h:10
float3 bt
Bitangent.
Definition: random.h:13
Definition: color.h:9