Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
348 awk 1
#include "path_tracer.hpp"
2
 
3
using namespace CGLA;
4
 
5
path_tracer::path_tracer(int w, int h, bool explicit_direct, int subsamples)
6
: tracer(w, h)
7
{
8
    explicit_direct_ = explicit_direct;
9
    subsamples_ = subsamples;
10
}
11
 
12
CGLA::Vec3f path_tracer::trace(const ray& r, bool include_emitted)
13
{
14
    //intersect ray with
15
    hit_info hi;
16
    bool hit = scene_->intersect(r, hi);
17
 
18
    if (!hit)
19
        return Vec3f(0.f, 0.5f, 0.f);
20
 
21
    //Vec3f x, y, z = hi.shading_normal;
22
    //orthogonal(z, x, y);
23
    //return (Vec3f(hi.texcoords(0),hi.texcoords(1),0) + Vec3f(0.f)) / 1.f;
24
 
25
    //only include emitted light if requested
26
    CGLA::Vec3f Le(0.f);
27
    if (include_emitted)
28
        Le = hi.emitted;
29
 
30
    //compute reflectance for each bsdf component
31
    float rho_diffuse = intensity(hi.diffuse);
32
    float rho_glossy = intensity(hi.glossy);
33
    float rho_reflection = intensity(hi.reflection);
34
    float rho_refraction = intensity(hi.refraction);
35
    float rho_total = rho_diffuse+rho_glossy+rho_reflection+rho_refraction;
36
    assert(rho_total < 1.f);
37
 
38
    if (rho_total == 0.f)
39
        return Le;
40
 
41
    //compute direct lighting on hit point
42
    CGLA::Vec3f Ld(0.f);
43
    Vec3f wo = -r.direction;
44
    if (explicit_direct_ && rho_diffuse+rho_glossy>0.f)
45
    {
46
        size_t nlums = scene_->luminaires();
47
        for (size_t i=0; i<nlums; ++i)
48
        {
49
            const luminaire* lum = scene_->get_luminaire(i);
50
            int samples = lum->samples();
51
 
52
            Vec3f L(0.f);
53
            for (int j=0; j<samples; ++j)
54
            {
55
                Vec3f Li, wi;
56
                if (lum->sample(r, hi, Li, wi))
57
                {
58
                    float cost = std::max(dot(hi.shading_normal, wi),0.f);
59
                    L += Li * cost * bsdf_evaluate(hi, wi, wo);
60
                }
61
 
62
                Ld += L / float(samples);
63
            }
64
        }
65
    }
66
 
67
    //use russian roulette to terminate path
68
    float prussian = 1.f;
69
    float rr = mt_random();
70
 
71
    if (r.depth > 3)
72
    {
73
        prussian = rho_total;
74
 
75
        if (rr >= prussian)
76
            return Ld + Le;
77
    }
78
 
79
    //figure out which bXdf to sample
80
    float pdiffuse = rho_diffuse/rho_total;
81
    float pglossy = rho_glossy/rho_total;
82
    float preflection = rho_reflection/rho_total;
83
    float prefraction = rho_refraction/rho_total;
84
 
85
    Vec3f wi;
86
    float pwi;
87
    Vec3f fs;
88
    bool sample_emitted = !explicit_direct_;
89
    if (rr <= pdiffuse)
90
    {
91
        //sample diffuse
92
        float pwi = sample_lambertian(hi, wo, wi);
93
        fs = lambertian_brdf(hi, wi, wo) / (pwi * pdiffuse * prussian);
94
    }
95
    else if (rr <= pdiffuse+pglossy)
96
    {
97
        //sample glossy part
98
        float pwi = sample_phong(hi, wo, wi);
99
 
100
        if (dot(hi.shading_normal, wi) <= 0.f)
101
            return Le + Ld;
102
 
103
        fs = phong_brdf(hi, wi, wo) / (pwi * pglossy * prussian);
104
    }
105
    else if (rr <= pdiffuse+pglossy+preflection)
106
    {
107
        //sample perfect specular reflection
108
        wi = reflect(hi.shading_normal, wo);
109
        pwi = 1.f;
110
        float cost = dot(hi.shading_normal, wo);
111
        fs = hi.reflection / (pwi * preflection * cost);
112
        sample_emitted = true;
113
    }
114
    else if (rr <= pdiffuse+pglossy+preflection+prefraction)
115
    {
116
        //sample perfect specular refraction
117
        bool not_tir = refract(hi.shading_normal, wo, 1.f/hi.ior, wi);
118
        assert(not_tir);
119
        pwi = 1.f;
120
        float cost = std::abs(dot(hi.shading_normal, wi));
121
        fs = hi.refraction / (pwi * prefraction * cost);
122
        sample_emitted = true;
123
    }
124
    else
125
        assert(false);
126
 
127
    //create aux ray
128
    ray s;
129
    s.origin = hi.position + epsilon * wi;
130
    s.direction = wi;
131
    s.depth = r.depth + 1;
132
    s.distance = std::numeric_limits<float>::infinity();
133
    float cost = std::abs(dot(hi.shading_normal, wi));
134
    Vec3f Li = cost * fs * trace(s, sample_emitted);
135
 
136
    //returm sum of emitted + direct + indirect
137
    return Le + Ld + Li;
138
}
139
 
140
Vec3f path_tracer::compute_pixel(int w, int h)
141
{
142
    assert(scene_);
143
 
144
    //supersample
145
    Vec3f L(0.f);
146
    for (int j=0; j<subsamples_; ++j)
147
    {
148
        float y  = h + (j + 0.5f) / subsamples_;
149
 
150
        for (int i=0; i<subsamples_; ++i)
151
        {
152
            float x  = w + (i + 0.5f) / subsamples_;
153
 
154
            //ask camera for initial ray..
155
            Vec2f uv(x/width_, y/height_);
156
            ray r = scene_->get_camera()->generate(uv);
157
 
158
            //trace ray
159
            L += trace(r, true);
160
        }
161
    }
162
 
163
    return L / float(subsamples_ * subsamples_);
164
}
165
 
166
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007