Subversion Repositories gelsvn

Rev

Rev 346 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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