Subversion Repositories gelsvn

Rev

Rev 353 | Rev 357 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 353 Rev 355
1
#include "pathtracer.h"
1
#include "pathtracer.h"
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
pathtracer::pathtracer(int w, int h, bool explicit_direct, int subsamples)
6
: tracer(w, h)
6
: width_(w), height_(h), scene_(0)
7
{
7
{
8
    explicit_direct_ = explicit_direct;
8
    explicit_direct_ = explicit_direct;
9
    subsamples_ = subsamples;
9
    subsamples_ = subsamples;
10
}
10
}
11
 
11
 
-
 
12
void pathtracer::set_scene(scene* s)
-
 
13
{
-
 
14
    scene_ = s;
-
 
15
}
-
 
16
 
12
CGLA::Vec3f path_tracer::trace(const ray& r, bool include_emitted)
17
CGLA::Vec3f pathtracer::trace(const ray& r, bool include_emitted)
13
{
18
{
14
    //intersect ray with
19
    //intersect ray with
15
    hit_info hi;
20
    hit_info hi;
16
    bool hit = scene_->intersect(r, hi);
21
    bool hit = scene_->intersect(r, hi);
17
 
22
 
18
    if (!hit)
23
    if (!hit)
19
        return Vec3f(0.f, 0.f, 0.f);
24
        return Vec3f(0.f, 0.f, 0.f);
20
 
25
 
21
    //Vec3f x, y, z = hi.shading_normal;
26
    //Vec3f x, y, z = hi.shading_normal;
22
    //orthogonal(z, x, y);
27
    //orthogonal(z, x, y);
23
    //return (Vec3f(hi.texcoords(0),hi.texcoords(1),0) + Vec3f(0.f)) / 1.f;
28
    //return (Vec3f(hi.texcoords(0),hi.texcoords(1),0) + Vec3f(0.f)) / 1.f;
24
 
29
 
25
    //only include emitted light if requested
30
    //only include emitted light if requested
26
    CGLA::Vec3f Le(0.f);
31
    CGLA::Vec3f Le(0.f);
27
    if (include_emitted)
32
    if (include_emitted)
28
        Le = hi.emitted;
33
        Le = hi.emitted;
29
 
34
 
30
    //compute reflectance for each bsdf component
35
    //compute reflectance for each bsdf component
31
    float rho_diffuse = intensity(hi.diffuse);
36
    float rho_diffuse = intensity(hi.diffuse);
32
    float rho_glossy = intensity(hi.glossy);
37
    float rho_glossy = intensity(hi.glossy);
33
    float rho_reflection = intensity(hi.reflection);
38
    float rho_reflection = intensity(hi.reflection);
34
    float rho_refraction = intensity(hi.refraction);
39
    float rho_refraction = intensity(hi.refraction);
35
    float rho_total = rho_diffuse+rho_glossy+rho_reflection+rho_refraction;
40
    float rho_total = rho_diffuse+rho_glossy+rho_reflection+rho_refraction;
36
    assert(rho_total < 1.f);
41
    assert(rho_total < 1.f);
37
 
42
 
38
    if (rho_total == 0.f)
43
    if (rho_total == 0.f)
39
        return Le;
44
        return Le;
40
 
45
 
41
    //compute direct lighting on hit point
46
    //compute direct lighting on hit point
42
    CGLA::Vec3f Ld(0.f);
47
    CGLA::Vec3f Ld(0.f);
43
    Vec3f wo = -r.direction;
48
    Vec3f wo = -r.direction;
44
    if (explicit_direct_ && rho_diffuse+rho_glossy>0.f)
49
    if (explicit_direct_ && rho_diffuse+rho_glossy>0.f)
45
    {
50
    {
46
        size_t nlums = scene_->luminaires();
51
        size_t nlums = scene_->luminaires();
47
        for (size_t i=0; i<nlums; ++i)
52
        for (size_t i=0; i<nlums; ++i)
48
        {
53
        {
49
            const luminaire* lum = scene_->get_luminaire(i);
54
            const luminaire* lum = scene_->get_luminaire(i);
50
            int samples = lum->samples();
55
            int samples = lum->samples();
51
 
56
 
52
            Vec3f L(0.f);
57
            Vec3f L(0.f);
53
            for (int j=0; j<samples; ++j)
58
            for (int j=0; j<samples; ++j)
54
            {
59
            {
55
                Vec3f Li, wi;
60
                Vec3f Li, wi;
56
                if (lum->sample(r, hi, Li, wi))
61
                if (lum->sample(r, hi, Li, wi))
57
                {
62
                {
58
                    float cost = std::max(dot(hi.shading_normal, wi),0.f);
63
                    float cost = std::max(dot(hi.shading_normal, wi),0.f);
59
                    L += Li * cost * bsdf_evaluate(hi, wi, wo);
64
                    L += Li * cost * bsdf_evaluate(hi, wi, wo);
60
                }
65
                }
61
 
66
 
62
                Ld += L / float(samples);
67
                Ld += L / float(samples);
63
            }
68
            }
64
        }
69
        }
65
    }
70
    }
66
 
71
 
67
    //use russian roulette to terminate path
72
    //use russian roulette to terminate path
68
    float prussian = 1.f;
73
    float prussian = 1.f;
69
    float rr = mt_random();
74
    float rr = mt_random();
70
 
75
 
71
    if (r.depth > 3)
76
    if (r.depth > 3)
72
    {
77
    {
73
        prussian = rho_total;
78
        prussian = rho_total;
74
 
79
 
75
        if (rr >= prussian)
80
        if (rr >= prussian)
76
            return Ld + Le;
81
            return Ld + Le;
77
    }
82
    }
78
 
83
 
79
    //figure out which bXdf to sample
84
    //figure out which bXdf to sample
80
    float pdiffuse = rho_diffuse/rho_total;
85
    float pdiffuse = rho_diffuse/rho_total;
81
    float pglossy = rho_glossy/rho_total;
86
    float pglossy = rho_glossy/rho_total;
82
    float preflection = rho_reflection/rho_total;
87
    float preflection = rho_reflection/rho_total;
83
    float prefraction = rho_refraction/rho_total;
88
    float prefraction = rho_refraction/rho_total;
84
 
89
 
85
    Vec3f wi;
90
    Vec3f wi;
86
    float pwi;
91
    float pwi;
87
    Vec3f fs;
92
    Vec3f fs;
88
    bool sample_emitted = !explicit_direct_;
93
    bool sample_emitted = !explicit_direct_;
89
    if (rr <= pdiffuse)
94
    if (rr <= pdiffuse)
90
    {
95
    {
91
        //sample diffuse
96
        //sample diffuse
92
        float pwi = sample_lambertian(hi, wo, wi);
97
        float pwi = sample_lambertian(hi, wo, wi);
93
        fs = lambertian_brdf(hi, wi, wo) / (pwi * pdiffuse * prussian);
98
        fs = lambertian_brdf(hi, wi, wo) / (pwi * pdiffuse * prussian);
94
    }
99
    }
95
    else if (rr <= pdiffuse+pglossy)
100
    else if (rr <= pdiffuse+pglossy)
96
    {
101
    {
97
        //sample glossy part
102
        //sample glossy part
98
        float pwi = sample_phong(hi, wo, wi);
103
        float pwi = sample_phong(hi, wo, wi);
99
 
104
 
100
        if (dot(hi.shading_normal, wi) <= 0.f)
105
        if (dot(hi.shading_normal, wi) <= 0.f)
101
            return Le + Ld;
106
            return Le + Ld;
102
 
107
 
103
        fs = phong_brdf(hi, wi, wo) / (pwi * pglossy * prussian);
108
        fs = phong_brdf(hi, wi, wo) / (pwi * pglossy * prussian);
104
    }
109
    }
105
    else if (rr <= pdiffuse+pglossy+preflection)
110
    else if (rr <= pdiffuse+pglossy+preflection)
106
    {
111
    {
107
        //sample perfect specular reflection
112
        //sample perfect specular reflection
108
        wi = reflect(hi.shading_normal, wo);
113
        wi = reflect(hi.shading_normal, wo);
109
        pwi = 1.f;
114
        pwi = 1.f;
110
        float cost = dot(hi.shading_normal, wo);
115
        float cost = dot(hi.shading_normal, wo);
111
        fs = hi.reflection / (pwi * preflection * cost);
116
        fs = hi.reflection / (pwi * preflection * cost);
112
        sample_emitted = true;
117
        sample_emitted = true;
113
    }
118
    }
114
    else if (rr <= pdiffuse+pglossy+preflection+prefraction)
119
    else if (rr <= pdiffuse+pglossy+preflection+prefraction)
115
    {
120
    {
116
        //sample perfect specular refraction
121
        //sample perfect specular refraction
117
        bool not_tir = refract(hi.shading_normal, wo, 1.f/hi.ior, wi);
122
        bool not_tir = refract(hi.shading_normal, wo, 1.f/hi.ior, wi);
118
        assert(not_tir);
123
        assert(not_tir);
119
        pwi = 1.f;
124
        pwi = 1.f;
120
        float cost = std::abs(dot(hi.shading_normal, wi));
125
        float cost = std::abs(dot(hi.shading_normal, wi));
121
        fs = hi.refraction / (pwi * prefraction * cost);
126
        fs = hi.refraction / (pwi * prefraction * cost);
122
        sample_emitted = true;
127
        sample_emitted = true;
123
    }
128
    }
124
    else
129
    else
125
        assert(false);
130
        assert(false);
126
 
131
 
127
    //create aux ray
132
    //create aux ray
128
    ray s;
133
    ray s;
129
    s.origin = hi.position + epsilon * wi;
134
    s.origin = hi.position + epsilon * wi;
130
    s.direction = wi;
135
    s.direction = wi;
131
    s.depth = r.depth + 1;
136
    s.depth = r.depth + 1;
132
    s.distance = std::numeric_limits<float>::infinity();
137
    s.distance = std::numeric_limits<float>::infinity();
133
    float cost = std::abs(dot(hi.shading_normal, wi));
138
    float cost = std::abs(dot(hi.shading_normal, wi));
134
    Vec3f Li = cost * fs * trace(s, sample_emitted);
139
    Vec3f Li = cost * fs * trace(s, sample_emitted);
135
 
140
 
136
    //returm sum of emitted + direct + indirect
141
    //returm sum of emitted + direct + indirect
137
    return Le + Ld + Li;
142
    return Le + Ld + Li;
138
}
143
}
139
 
144
 
140
Vec3f path_tracer::compute_pixel(int w, int h)
145
Vec3f pathtracer::compute_pixel(int w, int h)
141
{
146
{
142
    assert(scene_);
147
    assert(scene_);
143
 
148
 
144
    //supersample
149
    //supersample
145
    Vec3f L(0.f);
150
    Vec3f L(0.f);
146
    for (int j=0; j<subsamples_; ++j)
151
    for (int j=0; j<subsamples_; ++j)
147
    {
152
    {
148
        float y  = h + (j + 0.5f) / subsamples_;
153
        float y  = h + (j + 0.5f) / subsamples_;
149
 
154
 
150
        for (int i=0; i<subsamples_; ++i)
155
        for (int i=0; i<subsamples_; ++i)
151
        {
156
        {
152
            float x  = w + (i + 0.5f) / subsamples_;
157
            float x  = w + (i + 0.5f) / subsamples_;
153
 
158
 
154
            //ask camera for initial ray..
159
            //ask camera for initial ray..
155
            Vec2f uv(x/width_, y/height_);
160
            Vec2f uv(x/width_, y/height_);
156
            ray r = scene_->get_camera()->generate(uv);
161
            ray r = scene_->get_camera()->generate(uv);
157
 
162
 
158
            //trace ray
163
            //trace ray
159
            L += trace(r, true);
164
            L += trace(r, true);
160
        }
165
        }
161
    }
166
    }
162
 
167
 
163
    return L / float(subsamples_ * subsamples_);
168
    return L / float(subsamples_ * subsamples_);
164
}
169
}
165
 
170
 
166
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
171
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
167
 
172
 
168
#if 0
173
#if 0
169
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
174
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
170
 
175
 
171
#include <stdlib.h>
176
#include <stdlib.h>
172
#include <GL/glut.h>
177
#include <GL/glut.h>
173
 
178
 
174
#include <CGLA/Vec2i.h>
179
#include <CGLA/Vec2i.h>
175
#include <CGLA/Vec3uc.h>
180
#include <CGLA/Vec3uc.h>
176
 
181
 
177
#include "scene.h"
182
#include "scene.h"
178
#include "camera.h"
183
#include "camera.h"
179
#include "mesh.h"
184
#include "mesh.h"
180
#include "omni.h"
185
#include "omni.h"
181
 
186
 
182
#include "matte.h"
187
#include "matte.h"
183
#include "plastic.h"
188
#include "plastic.h"
184
#include "metal.h"
189
#include "metal.h"
185
#include "glass.h"
190
#include "glass.h"
186
 
191
 
187
#include "path_tracer.h"
192
#include "pathtracer.h"
188
 
193
 
189
using namespace CGLA;
194
using namespace CGLA;
190
 
195
 
191
//global pointer to the active scene
196
//global pointer to the active scene
192
scene* current;
197
scene* current;
193
 
198
 
194
static path_tracer* renderer;
199
static pathtracer* renderer;
195
 
200
 
196
const int width = 64*8;
201
const int width = 64*8;
197
const int height = 64*8;
202
const int height = 64*8;
198
 
203
 
199
static Vec3f* film;
204
static Vec3f* film;
200
static Vec3uc* image;
205
static Vec3uc* image;
201
static Vec2i pixel(0);
206
static Vec2i pixel(0);
202
static bool done = false;
207
static bool done = false;
203
 
208
 
204
static float dgamma = 2.2f;
209
static float dgamma = 2.2f;
205
static float dexposure = 0.f;
210
static float dexposure = 0.f;
206
 
211
 
207
Vec3uc tonemap(const Vec3f& v)
212
Vec3uc tonemap(const Vec3f& v)
208
{
213
{
209
    Vec3f u = v * std::pow(2.f, dexposure);
214
    Vec3f u = v * std::pow(2.f, dexposure);
210
    float r = mt_random() - 0.5f;
215
    float r = mt_random() - 0.5f;
211
 
216
 
212
    Vec3uc I;
217
    Vec3uc I;
213
    for (int i=0; i<3; ++i)
218
    for (int i=0; i<3; ++i)
214
    {
219
    {
215
        float c = std::pow(u[i], 1.f / dgamma);
220
        float c = std::pow(u[i], 1.f / dgamma);
216
        c = 255.f * c + 0.5f + r;
221
        c = 255.f * c + 0.5f + r;
217
        I[i] = clamp(int(c), 0, 255);
222
        I[i] = clamp(int(c), 0, 255);
218
    }
223
    }
219
    return I;
224
    return I;
220
}
225
}
221
 
226
 
222
void display(void)
227
void display(void)
223
{
228
{
224
    glClear(GL_COLOR_BUFFER_BIT);
229
    glClear(GL_COLOR_BUFFER_BIT);
225
 
230
 
226
    for (int j=0; j<height; ++j)
231
    for (int j=0; j<height; ++j)
227
        for (int i=0; i<width; ++i)
232
        for (int i=0; i<width; ++i)
228
            image[i + j*width] = tonemap(film[i + j*width]);
233
            image[i + j*width] = tonemap(film[i + j*width]);
229
 
234
 
230
    glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
235
    glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
231
    glutSwapBuffers();
236
    glutSwapBuffers();
232
 
237
 
233
    GLenum e = glGetError();
238
    GLenum e = glGetError();
234
    if (e != GL_NO_ERROR)
239
    if (e != GL_NO_ERROR)
235
    {
240
    {
236
        printf("OpenGL error: %s\n", gluErrorString(e));
241
        printf("OpenGL error: %s\n", gluErrorString(e));
237
        exit(EXIT_FAILURE);
242
        exit(EXIT_FAILURE);
238
    }
243
    }
239
}
244
}
240
 
245
 
241
void reshape(GLint width, GLint height)
246
void reshape(GLint width, GLint height)
242
{
247
{
243
    glMatrixMode(GL_PROJECTION);
248
    glMatrixMode(GL_PROJECTION);
244
    glLoadIdentity();
249
    glLoadIdentity();
245
    gluOrtho2D(0, width, 0, height);
250
    gluOrtho2D(0, width, 0, height);
246
    glMatrixMode(GL_MODELVIEW);
251
    glMatrixMode(GL_MODELVIEW);
247
    glLoadIdentity();
252
    glLoadIdentity();
248
}
253
}
249
 
254
 
250
void special(int key, int x, int y)
255
void special(int key, int x, int y)
251
{
256
{
252
    switch (key)
257
    switch (key)
253
    {
258
    {
254
    case GLUT_KEY_LEFT:
259
    case GLUT_KEY_LEFT:
255
        dgamma = std::max(dgamma - 0.1f, 0.5f);
260
        dgamma = std::max(dgamma - 0.1f, 0.5f);
256
        break;
261
        break;
257
 
262
 
258
    case GLUT_KEY_RIGHT:
263
    case GLUT_KEY_RIGHT:
259
        dgamma = std::min(dgamma + 0.1f, 5.f);
264
        dgamma = std::min(dgamma + 0.1f, 5.f);
260
        break;
265
        break;
261
 
266
 
262
    case GLUT_KEY_DOWN:
267
    case GLUT_KEY_DOWN:
263
        dexposure = std::max(dexposure - 0.1f, -10.f);
268
        dexposure = std::max(dexposure - 0.1f, -10.f);
264
        break;
269
        break;
265
 
270
 
266
    case GLUT_KEY_UP:
271
    case GLUT_KEY_UP:
267
        dexposure = std::min(dexposure + 0.1f, 10.f);
272
        dexposure = std::min(dexposure + 0.1f, 10.f);
268
        break;
273
        break;
269
    }
274
    }
270
 
275
 
271
    printf("gamma: %.2f, exposure: %.2f\n", dgamma, dexposure);
276
    printf("gamma: %.2f, exposure: %.2f\n", dgamma, dexposure);
272
    glutPostRedisplay();
277
    glutPostRedisplay();
273
}
278
}
274
 
279
 
275
void keyboard(unsigned char key, int x, int y)
280
void keyboard(unsigned char key, int x, int y)
276
{
281
{
277
    switch (key)
282
    switch (key)
278
    {
283
    {
279
    case 27: //ESCAPE key
284
    case 27: //ESCAPE key
280
        exit(0);
285
        exit(0);
281
        break;
286
        break;
282
    }
287
    }
283
}
288
}
284
 
289
 
285
void idle(void)
290
void idle(void)
286
{
291
{
287
    for (int i=0; i<width*8 && !done; ++i)
292
    for (int i=0; i<width*8 && !done; ++i)
288
    {
293
    {
289
        Vec3f L = renderer->compute_pixel(pixel[0], pixel[1]);
294
        Vec3f L = renderer->compute_pixel(pixel[0], pixel[1]);
290
        film[pixel[0] + pixel[1] * width] = L;
295
        film[pixel[0] + pixel[1] * width] = L;
291
 
296
 
292
        //advance
297
        //advance
293
        ++pixel[0];
298
        ++pixel[0];
294
 
299
 
295
        if (pixel[0] == width)
300
        if (pixel[0] == width)
296
        {
301
        {
297
            pixel[0] = 0;
302
            pixel[0] = 0;
298
            ++pixel[1];
303
            ++pixel[1];
299
 
304
 
300
            if (pixel[1] == height)
305
            if (pixel[1] == height)
301
            {
306
            {
302
                done = true;
307
                done = true;
303
                glutIdleFunc(NULL);
308
                glutIdleFunc(NULL);
304
            }
309
            }
305
        }
310
        }
306
    }
311
    }
307
 
312
 
308
    glutPostRedisplay();
313
    glutPostRedisplay();
309
}
314
}
310
 
315
 
311
int main(int argc, char* argv[])
316
int main(int argc, char* argv[])
312
{
317
{
313
    //make the scene
318
    //make the scene
314
    current = new scene;
319
    current = new scene;
315
 
320
 
316
    //setup some materials
321
    //setup some materials
317
    matte dull_white(Vec3f(0.5f, 0.5f, 0.5f));
322
    matte dull_white(Vec3f(0.5f, 0.5f, 0.5f));
318
    matte dull_gray(Vec3f(0.4f, 0.4f, 0.4f));
323
    matte dull_gray(Vec3f(0.4f, 0.4f, 0.4f));
319
    matte dull_red(Vec3f(0.6f, 0.3f, 0.2f));
324
    matte dull_red(Vec3f(0.6f, 0.3f, 0.2f));
320
    matte dull_green(Vec3f(0.3f, 0.6f, 0.2f));
325
    matte dull_green(Vec3f(0.3f, 0.6f, 0.2f));
321
    matte dull_blue(Vec3f(0.2f, 0.2f, 0.6f));
326
    matte dull_blue(Vec3f(0.2f, 0.2f, 0.6f));
322
 
327
 
323
    plastic glossy_white(Vec3f(0.4f, 0.4f, 0.4f), Vec3f(0.5f), 20.f);
328
    plastic glossy_white(Vec3f(0.4f, 0.4f, 0.4f), Vec3f(0.5f), 20.f);
324
    plastic glossy_purple(Vec3f(0.3f, 0.1f, 0.3f), Vec3f(0.65f), 8.f);
329
    plastic glossy_purple(Vec3f(0.3f, 0.1f, 0.3f), Vec3f(0.65f), 8.f);
325
    plastic glossy_yellow(Vec3f(0.3f, 0.3f, 0.0f), Vec3f(0.65f), 32.f);
330
    plastic glossy_yellow(Vec3f(0.3f, 0.3f, 0.0f), Vec3f(0.65f), 32.f);
326
 
331
 
327
    //for exercise 2
332
    //for exercise 2
328
    glass clear(Vec3f(0.99f, 0.99f, 0.99f), 1.5f);
333
    glass clear(Vec3f(0.99f, 0.99f, 0.99f), 1.5f);
329
    metal silver(Vec3f(0.9f, 0.9f, 0.9f), 22.f, 0.177f, 3.638f);
334
    metal silver(Vec3f(0.9f, 0.9f, 0.9f), 22.f, 0.177f, 3.638f);
330
 
335
 
331
    //setup cornell box 1mx1mx1m
336
    //setup cornell box 1mx1mx1m
332
    mesh floor("../../data/cornell_box/floor.obj");
337
    mesh floor("../../data/cornell_box/floor.obj");
333
    floor.set_material(&glossy_white);
338
    floor.set_material(&glossy_white);
334
    current->insert(&floor);
339
    current->insert(&floor);
335
 
340
 
336
    mesh ceiling("../../data/cornell_box/ceiling.obj");
341
    mesh ceiling("../../data/cornell_box/ceiling.obj");
337
    ceiling.set_material(&dull_gray);
342
    ceiling.set_material(&dull_gray);
338
    current->insert(&ceiling);
343
    current->insert(&ceiling);
339
 
344
 
340
    mesh back("../../data/cornell_box/back.obj");
345
    mesh back("../../data/cornell_box/back.obj");
341
    back.set_material(&silver);
346
    back.set_material(&silver);
342
    current->insert(&back);
347
    current->insert(&back);
343
 
348
 
344
    mesh left("../../data/cornell_box/left.obj");
349
    mesh left("../../data/cornell_box/left.obj");
345
    left.set_material(&dull_red);
350
    left.set_material(&dull_red);
346
    current->insert(&left);
351
    current->insert(&left);
347
 
352
 
348
    mesh right("../../data/cornell_box/right.obj");
353
    mesh right("../../data/cornell_box/right.obj");
349
    right.set_material(&dull_blue);
354
    right.set_material(&dull_blue);
350
    current->insert(&right);
355
    current->insert(&right);
351
 
356
 
352
    //add some objects to the box
357
    //add some objects to the box
353
    Mat4x4f tmp;
358
    Mat4x4f tmp;
354
 
359
 
355
    mesh box1("../../data/cornell_box/box1.obj");
360
    mesh box1("../../data/cornell_box/box1.obj");
356
    tmp = rotation_Mat4x4f(YAXIS, -float(M_PI)/8.f);
361
    tmp = rotation_Mat4x4f(YAXIS, -float(M_PI)/8.f);
357
    tmp = translation_Mat4x4f(Vec3f(-0.3f,0.f,-0.05f)) * tmp;
362
    tmp = translation_Mat4x4f(Vec3f(-0.3f,0.f,-0.05f)) * tmp;
358
    box1.set_transform(tmp);
363
    box1.set_transform(tmp);
359
    box1.set_material(&dull_green);
364
    box1.set_material(&dull_green);
360
    current->insert(&box1);
365
    current->insert(&box1);
361
 
366
 
362
    mesh box2("../../data/cornell_box/box2.obj");
367
    mesh box2("../../data/cornell_box/box2.obj");
363
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.f, -0.05f));
368
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.f, -0.05f));
364
    box2.set_transform(tmp);
369
    box2.set_transform(tmp);
365
    box2.set_material(&glossy_purple);
370
    box2.set_material(&glossy_purple);
366
    current->insert(&box2);
371
    current->insert(&box2);
367
 
372
 
368
    mesh teapot("../../data/cornell_box/teapot1.obj");
373
    mesh teapot("../../data/cornell_box/teapot1.obj");
369
    tmp = rotation_Mat4x4f(YAXIS, float(M_PI/4.f));
374
    tmp = rotation_Mat4x4f(YAXIS, float(M_PI/4.f));
370
    tmp = translation_Mat4x4f(Vec3f(-0.25f,0.25f, -0.05)) * tmp;
375
    tmp = translation_Mat4x4f(Vec3f(-0.25f,0.25f, -0.05)) * tmp;
371
    teapot.set_transform(tmp);
376
    teapot.set_transform(tmp);
372
    teapot.set_material(&silver);
377
    teapot.set_material(&silver);
373
    current->insert(&teapot);
378
    current->insert(&teapot);
374
 
379
 
375
    mesh sphere1("../../data/cornell_box/sphere2.obj");
380
    mesh sphere1("../../data/cornell_box/sphere2.obj");
376
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.45f,-0.05f));
381
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.45f,-0.05f));
377
    sphere1.set_transform(tmp);
382
    sphere1.set_transform(tmp);
378
    sphere1.set_material(&clear);
383
    sphere1.set_material(&clear);
379
    current->insert(&sphere1);
384
    current->insert(&sphere1);
380
 
385
 
381
    mesh sphere2("../../data/cornell_box/sphere2.obj");
386
    mesh sphere2("../../data/cornell_box/sphere2.obj");
382
    tmp = translation_Mat4x4f(Vec3f(0.22f,0.15f,0.25f));
387
    tmp = translation_Mat4x4f(Vec3f(0.22f,0.15f,0.25f));
383
    sphere2.set_transform(tmp);
388
    sphere2.set_transform(tmp);
384
    sphere2.set_material(&glossy_yellow);
389
    sphere2.set_material(&glossy_yellow);
385
    //current->insert(&sphere2);
390
    //current->insert(&sphere2);
386
 
391
 
387
    //light sources
392
    //light sources
388
    mesh quad_light("../../data/cornell_box/quad.obj");
393
    mesh quad_light("../../data/cornell_box/quad.obj");
389
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
394
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
390
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
395
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
391
    quad_light.set_transform(tmp);
396
    quad_light.set_transform(tmp);
392
    quad_light.set_exitance(Vec3f(300,300,300));
397
    quad_light.set_exitance(Vec3f(300,300,300));
393
    current->insert(&quad_light);
398
    current->insert(&quad_light);
394
 
399
 
395
    mesh quad_light1("../../data/cornell_box/quad.obj");
400
    mesh quad_light1("../../data/cornell_box/quad.obj");
396
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
401
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
397
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
402
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
398
    quad_light1.set_transform(tmp);
403
    quad_light1.set_transform(tmp);
399
    quad_light1.set_exitance(Vec3f(0,400,0));
404
    quad_light1.set_exitance(Vec3f(0,400,0));
400
//  current->insert(&quad_light1);
405
//  current->insert(&quad_light1);
401
 
406
 
402
    mesh quad_light2("../../data/cornell_box/quad.obj");
407
    mesh quad_light2("../../data/cornell_box/quad.obj");
403
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
408
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
404
    tmp = translation_Mat4x4f(Vec3f(-0.2f,0.99f,0.2f)) * tmp;
409
    tmp = translation_Mat4x4f(Vec3f(-0.2f,0.99f,0.2f)) * tmp;
405
    quad_light2.set_transform(tmp);
410
    quad_light2.set_transform(tmp);
406
    quad_light2.set_exitance(Vec3f(400,0,0));
411
    quad_light2.set_exitance(Vec3f(400,0,0));
407
//  current->insert(&quad_light2);
412
//  current->insert(&quad_light2);
408
 
413
 
409
    mesh quad_light3("../../data/cornell_box/quad.obj");
414
    mesh quad_light3("../../data/cornell_box/quad.obj");
410
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
415
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
411
    tmp = translation_Mat4x4f(Vec3f(0.2f,0.99f,0.2f)) * tmp;
416
    tmp = translation_Mat4x4f(Vec3f(0.2f,0.99f,0.2f)) * tmp;
412
    quad_light3.set_transform(tmp);
417
    quad_light3.set_transform(tmp);
413
    quad_light3.set_exitance(Vec3f(0,0,400));
418
    quad_light3.set_exitance(Vec3f(0,0,400));
414
//  current->insert(&quad_light3);
419
//  current->insert(&quad_light3);
415
 
420
 
416
    omni omni_light(Vec3f(30.f));
421
    omni omni_light(Vec3f(30.f));
417
    omni_light.set_transform(translation_Mat4x4f(Vec3f(0.0f,0.95f,0.0f)));
422
    omni_light.set_transform(translation_Mat4x4f(Vec3f(0.0f,0.95f,0.0f)));
418
    //current->insert(&omni_light);
423
    //current->insert(&omni_light);
419
 
424
 
420
    mesh sphere_light("../../data/cornell_box/small_sphere.obj");
425
    mesh sphere_light("../../data/cornell_box/small_sphere.obj");
421
    sphere_light.set_transform(translation_Mat4x4f(Vec3f(0.f,0.95f,0.f)));
426
    sphere_light.set_transform(translation_Mat4x4f(Vec3f(0.f,0.95f,0.f)));
422
    sphere_light.set_exitance(Vec3f(30.f/(4.f*float(M_PI)*0.01f*0.01f)));
427
    sphere_light.set_exitance(Vec3f(30.f/(4.f*float(M_PI)*0.01f*0.01f)));
423
    //current->insert(&sphere_light);
428
    //current->insert(&sphere_light);
424
 
429
 
425
    //setup camera (eye, center, up, focal length)
430
    //setup camera (eye, center, up, focal length)
426
    camera pentax(
431
    camera pentax(
427
        Vec3f(0.f,0.5f,2.0f),
432
        Vec3f(0.f,0.5f,2.0f),
428
        Vec3f(0.f,0.5f,0.5f),
433
        Vec3f(0.f,0.5f,0.5f),
429
        Vec3f(0,1,0),
434
        Vec3f(0,1,0),
430
        0.035f);
435
        0.035f);
431
 
436
 
432
    current->set_camera(&pentax);
437
    current->set_camera(&pentax);
433
 
438
 
434
    //build acceleration structure
439
    //build acceleration structure
435
    current->initialize(8, 25);
440
    current->initialize(8, 25);
436
 
441
 
437
    //create the renderer
442
    //create the renderer
438
    renderer = new path_tracer(width, height, true, 1);
443
    renderer = new pathtracer(width, height, true, 1);
439
    renderer->set_scene(current);
444
    renderer->set_scene(current);
440
 
445
 
441
    //create the film
446
    //create the film
442
    film = new Vec3f[width * height];
447
    film = new Vec3f[width * height];
443
    std::fill(film, film+width*height, Vec3f(0.3f));
448
    std::fill(film, film+width*height, Vec3f(0.3f));
444
    image = new Vec3uc[width * height];
449
    image = new Vec3uc[width * height];
445
    std::fill(image, image+width*height, Vec3uc(32,32,32));
450
    std::fill(image, image+width*height, Vec3uc(32,32,32));
446
 
451
 
447
    //init glut
452
    //init glut
448
    glutInit(&argc, argv);
453
    glutInit(&argc, argv);
449
    glutInitWindowSize(width, height);
454
    glutInitWindowSize(width, height);
450
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
455
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
451
    glutCreateWindow("Course 02566 - IMM DTU");
456
    glutCreateWindow("Course 02566 - IMM DTU");
452
 
457
 
453
    glutDisplayFunc(display);
458
    glutDisplayFunc(display);
454
    glutReshapeFunc(reshape);
459
    glutReshapeFunc(reshape);
455
    glutKeyboardFunc(keyboard);
460
    glutKeyboardFunc(keyboard);
456
    glutSpecialFunc(special);
461
    glutSpecialFunc(special);
457
 
462
 
458
    glutIdleFunc(idle);
463
    glutIdleFunc(idle);
459
 
464
 
460
    //Turn the flow of control over to GLUT
465
    //Turn the flow of control over to GLUT
461
    glutMainLoop();
466
    glutMainLoop();
462
 
467
 
463
    //clean up
468
    //clean up
464
    delete current;
469
    delete current;
465
    delete renderer;
470
    delete renderer;
466
 
471
 
467
    return EXIT_SUCCESS;
472
    return EXIT_SUCCESS;
468
}
473
}
469
#endif
474
#endif
470
 
475