samplers.cpp 2.09 KB
Newer Older
TheNumbat's avatar
TheNumbat committed
1
2

#include "../rays/samplers.h"
TheNumbat's avatar
TheNumbat committed
3
4
#include "../util/rand.h"
#include "debug.h"
TheNumbat's avatar
TheNumbat committed
5
6
7

namespace Samplers {

TheNumbat's avatar
TheNumbat committed
8
Vec2 Rect::Uniform::sample(float &pdf) const {
TheNumbat's avatar
TheNumbat committed
9
10
11
12
13
14
15
16
17

    // TODO (PathTracer): Task 1
    // Generate a uniformly random point on a rectangle of size size.x * size.y
    // Tip: RNG::unit()

    pdf = 1.0f; // the PDF should integrate to 1 over the whole rectangle
    return Vec2();
}

TheNumbat's avatar
TheNumbat committed
18
Vec3 Hemisphere::Cosine::sample(float &pdf) const {
TheNumbat's avatar
TheNumbat committed
19
20
21
22
23
24

    // TODO (PathTracer): Task 6
    // You may implement this, but don't have to.
    return Vec3();
}

TheNumbat's avatar
TheNumbat committed
25
Vec3 Sphere::Uniform::sample(float &pdf) const {
TheNumbat's avatar
TheNumbat committed
26
27

    // TODO (PathTracer): Task 7
TheNumbat's avatar
TheNumbat committed
28
    // Generate a uniformly random point on the unit sphere (or equivalently, direction)
TheNumbat's avatar
TheNumbat committed
29
30
31
32
33
34
    // Tip: start with Hemisphere::Uniform

    pdf = 1.0f; // what was the PDF at the chosen direction?
    return Vec3();
}

TheNumbat's avatar
TheNumbat committed
35
Sphere::Image::Image(const HDR_Image &image) {
TheNumbat's avatar
TheNumbat committed
36
37
38

    // TODO (PathTracer): Task 7
    // Set up importance sampling for a spherical environment map image.
TheNumbat's avatar
TheNumbat committed
39

TheNumbat's avatar
TheNumbat committed
40
41
42
    // You may make use of the pdf, cdf, and total members, or create your own
    // representation.

TheNumbat's avatar
TheNumbat committed
43
44
45
    const auto [_w, _h] = image.dimension();
    w = _w;
    h = _h;
TheNumbat's avatar
TheNumbat committed
46
47
}

TheNumbat's avatar
TheNumbat committed
48
Vec3 Sphere::Image::sample(float &out_pdf) const {
TheNumbat's avatar
TheNumbat committed
49
50
51
52
53
54
55
56
57

    // TODO (PathTracer): Task 7
    // Use your importance sampling data structure to generate a sample direction.
    // Tip: std::upper_bound can easily binary search your CDF

    out_pdf = 1.0f; // what was the PDF (again, PMF here) of your chosen sample?
    return Vec3();
}

TheNumbat's avatar
TheNumbat committed
58
Vec3 Point::sample(float &pmf) const {
TheNumbat's avatar
TheNumbat committed
59
60
61
62
63

    pmf = 1.0f;
    return point;
}

TheNumbat's avatar
TheNumbat committed
64
65
Vec3 Two_Points::sample(float &pmf) const {
    if (RNG::unit() < prob) {
TheNumbat's avatar
TheNumbat committed
66
67
68
69
70
71
72
        pmf = prob;
        return p1;
    }
    pmf = 1.0f - prob;
    return p2;
}

TheNumbat's avatar
TheNumbat committed
73
Vec3 Hemisphere::Uniform::sample(float &pdf) const {
TheNumbat's avatar
TheNumbat committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

    float Xi1 = RNG::unit();
    float Xi2 = RNG::unit();

    float theta = std::acos(Xi1);
    float phi = 2.0f * PI_F * Xi2;

    float xs = std::sin(theta) * std::cos(phi);
    float ys = std::cos(theta);
    float zs = std::sin(theta) * std::sin(phi);

    pdf = 1.0f / (2.0f * PI_F);
    return Vec3(xs, ys, zs);
}

TheNumbat's avatar
TheNumbat committed
89
} // namespace Samplers