Subversion Repositories gelsvn

Rev

Rev 348 | Rev 355 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
353 awk 1
#include "pathtracer.h"
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.f, 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
 
346 awk 166
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
167
 
353 awk 168
#if 0
169
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
170
 
346 awk 171
#include <stdlib.h>
172
#include <GL/glut.h>
173
 
174
#include <CGLA/Vec2i.h>
175
#include <CGLA/Vec3uc.h>
176
 
353 awk 177
#include "scene.h"
178
#include "camera.h"
179
#include "mesh.h"
180
#include "omni.h"
346 awk 181
 
353 awk 182
#include "matte.h"
183
#include "plastic.h"
184
#include "metal.h"
185
#include "glass.h"
346 awk 186
 
353 awk 187
#include "path_tracer.h"
346 awk 188
 
189
using namespace CGLA;
190
 
191
//global pointer to the active scene
192
scene* current;
193
 
194
static path_tracer* renderer;
195
 
196
const int width = 64*8;
197
const int height = 64*8;
198
 
199
static Vec3f* film;
200
static Vec3uc* image;
201
static Vec2i pixel(0);
202
static bool done = false;
203
 
347 awk 204
static float dgamma = 2.2f;
205
static float dexposure = 0.f;
346 awk 206
 
207
Vec3uc tonemap(const Vec3f& v)
208
{
347 awk 209
    Vec3f u = v * std::pow(2.f, dexposure);
348 awk 210
    float r = mt_random() - 0.5f;
346 awk 211
 
347 awk 212
    Vec3uc I;
213
    for (int i=0; i<3; ++i)
214
    {
215
        float c = std::pow(u[i], 1.f / dgamma);
216
        c = 255.f * c + 0.5f + r;
217
        I[i] = clamp(int(c), 0, 255);
218
    }
219
    return I;
346 awk 220
}
221
 
222
void display(void)
223
{
347 awk 224
    glClear(GL_COLOR_BUFFER_BIT);
346 awk 225
 
347 awk 226
    for (int j=0; j<height; ++j)
227
        for (int i=0; i<width; ++i)
228
            image[i + j*width] = tonemap(film[i + j*width]);
346 awk 229
 
347 awk 230
    glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
231
    glutSwapBuffers();
232
 
233
    GLenum e = glGetError();
234
    if (e != GL_NO_ERROR)
235
    {
236
        printf("OpenGL error: %s\n", gluErrorString(e));
237
        exit(EXIT_FAILURE);
238
    }
346 awk 239
}
240
 
241
void reshape(GLint width, GLint height)
242
{
347 awk 243
    glMatrixMode(GL_PROJECTION);
244
    glLoadIdentity();
245
    gluOrtho2D(0, width, 0, height);
246
    glMatrixMode(GL_MODELVIEW);
247
    glLoadIdentity();
346 awk 248
}
249
 
250
void special(int key, int x, int y)
251
{
347 awk 252
    switch (key)
253
    {
254
    case GLUT_KEY_LEFT:
255
        dgamma = std::max(dgamma - 0.1f, 0.5f);
256
        break;
346 awk 257
 
347 awk 258
    case GLUT_KEY_RIGHT:
259
        dgamma = std::min(dgamma + 0.1f, 5.f);
260
        break;
346 awk 261
 
347 awk 262
    case GLUT_KEY_DOWN:
263
        dexposure = std::max(dexposure - 0.1f, -10.f);
264
        break;
346 awk 265
 
347 awk 266
    case GLUT_KEY_UP:
267
        dexposure = std::min(dexposure + 0.1f, 10.f);
268
        break;
269
    }
346 awk 270
 
347 awk 271
    printf("gamma: %.2f, exposure: %.2f\n", dgamma, dexposure);
272
    glutPostRedisplay();
346 awk 273
}
274
 
275
void keyboard(unsigned char key, int x, int y)
276
{
347 awk 277
    switch (key)
278
    {
279
    case 27: //ESCAPE key
280
        exit(0);
281
        break;
282
    }
346 awk 283
}
284
 
285
void idle(void)
286
{
347 awk 287
    for (int i=0; i<width*8 && !done; ++i)
288
    {
289
        Vec3f L = renderer->compute_pixel(pixel[0], pixel[1]);
290
        film[pixel[0] + pixel[1] * width] = L;
346 awk 291
 
347 awk 292
        //advance
293
        ++pixel[0];
346 awk 294
 
347 awk 295
        if (pixel[0] == width)
296
        {
297
            pixel[0] = 0;
298
            ++pixel[1];
346 awk 299
 
347 awk 300
            if (pixel[1] == height)
301
            {
302
                done = true;
303
                glutIdleFunc(NULL);
304
            }
305
        }
306
    }
346 awk 307
 
347 awk 308
    glutPostRedisplay();
346 awk 309
}
310
 
311
int main(int argc, char* argv[])
312
{
347 awk 313
    //make the scene
314
    current = new scene;
346 awk 315
 
347 awk 316
    //setup some materials
317
    matte dull_white(Vec3f(0.5f, 0.5f, 0.5f));
318
    matte dull_gray(Vec3f(0.4f, 0.4f, 0.4f));
319
    matte dull_red(Vec3f(0.6f, 0.3f, 0.2f));
320
    matte dull_green(Vec3f(0.3f, 0.6f, 0.2f));
321
    matte dull_blue(Vec3f(0.2f, 0.2f, 0.6f));
346 awk 322
 
347 awk 323
    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);
325
    plastic glossy_yellow(Vec3f(0.3f, 0.3f, 0.0f), Vec3f(0.65f), 32.f);
346 awk 326
 
347 awk 327
    //for exercise 2
328
    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);
346 awk 330
 
347 awk 331
    //setup cornell box 1mx1mx1m
332
    mesh floor("../../data/cornell_box/floor.obj");
333
    floor.set_material(&glossy_white);
334
    current->insert(&floor);
346 awk 335
 
347 awk 336
    mesh ceiling("../../data/cornell_box/ceiling.obj");
337
    ceiling.set_material(&dull_gray);
338
    current->insert(&ceiling);
346 awk 339
 
347 awk 340
    mesh back("../../data/cornell_box/back.obj");
341
    back.set_material(&silver);
342
    current->insert(&back);
346 awk 343
 
347 awk 344
    mesh left("../../data/cornell_box/left.obj");
345
    left.set_material(&dull_red);
346
    current->insert(&left);
346 awk 347
 
347 awk 348
    mesh right("../../data/cornell_box/right.obj");
349
    right.set_material(&dull_blue);
350
    current->insert(&right);
346 awk 351
 
347 awk 352
    //add some objects to the box
353
    Mat4x4f tmp;
346 awk 354
 
347 awk 355
    mesh box1("../../data/cornell_box/box1.obj");
356
    tmp = rotation_Mat4x4f(YAXIS, -float(M_PI)/8.f);
357
    tmp = translation_Mat4x4f(Vec3f(-0.3f,0.f,-0.05f)) * tmp;
358
    box1.set_transform(tmp);
359
    box1.set_material(&dull_green);
360
    current->insert(&box1);
346 awk 361
 
347 awk 362
    mesh box2("../../data/cornell_box/box2.obj");
363
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.f, -0.05f));
364
    box2.set_transform(tmp);
365
    box2.set_material(&glossy_purple);
366
    current->insert(&box2);
346 awk 367
 
347 awk 368
    mesh teapot("../../data/cornell_box/teapot1.obj");
369
    tmp = rotation_Mat4x4f(YAXIS, float(M_PI/4.f));
370
    tmp = translation_Mat4x4f(Vec3f(-0.25f,0.25f, -0.05)) * tmp;
371
    teapot.set_transform(tmp);
372
    teapot.set_material(&silver);
373
    current->insert(&teapot);
346 awk 374
 
347 awk 375
    mesh sphere1("../../data/cornell_box/sphere2.obj");
376
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.45f,-0.05f));
377
    sphere1.set_transform(tmp);
378
    sphere1.set_material(&clear);
379
    current->insert(&sphere1);
346 awk 380
 
347 awk 381
    mesh sphere2("../../data/cornell_box/sphere2.obj");
382
    tmp = translation_Mat4x4f(Vec3f(0.22f,0.15f,0.25f));
383
    sphere2.set_transform(tmp);
384
    sphere2.set_material(&glossy_yellow);
385
    //current->insert(&sphere2);
346 awk 386
 
347 awk 387
    //light sources
388
    mesh quad_light("../../data/cornell_box/quad.obj");
389
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
390
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
391
    quad_light.set_transform(tmp);
392
    quad_light.set_exitance(Vec3f(300,300,300));
393
    current->insert(&quad_light);
346 awk 394
 
347 awk 395
    mesh quad_light1("../../data/cornell_box/quad.obj");
396
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
397
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
398
    quad_light1.set_transform(tmp);
399
    quad_light1.set_exitance(Vec3f(0,400,0));
400
//  current->insert(&quad_light1);
346 awk 401
 
347 awk 402
    mesh quad_light2("../../data/cornell_box/quad.obj");
403
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
404
    tmp = translation_Mat4x4f(Vec3f(-0.2f,0.99f,0.2f)) * tmp;
405
    quad_light2.set_transform(tmp);
406
    quad_light2.set_exitance(Vec3f(400,0,0));
407
//  current->insert(&quad_light2);
346 awk 408
 
347 awk 409
    mesh quad_light3("../../data/cornell_box/quad.obj");
410
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
411
    tmp = translation_Mat4x4f(Vec3f(0.2f,0.99f,0.2f)) * tmp;
412
    quad_light3.set_transform(tmp);
413
    quad_light3.set_exitance(Vec3f(0,0,400));
414
//  current->insert(&quad_light3);
346 awk 415
 
347 awk 416
    omni omni_light(Vec3f(30.f));
417
    omni_light.set_transform(translation_Mat4x4f(Vec3f(0.0f,0.95f,0.0f)));
418
    //current->insert(&omni_light);
346 awk 419
 
347 awk 420
    mesh sphere_light("../../data/cornell_box/small_sphere.obj");
421
    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)));
423
    //current->insert(&sphere_light);
346 awk 424
 
347 awk 425
    //setup camera (eye, center, up, focal length)
426
    camera pentax(
427
        Vec3f(0.f,0.5f,2.0f),
428
        Vec3f(0.f,0.5f,0.5f),
429
        Vec3f(0,1,0),
430
        0.035f);
346 awk 431
 
347 awk 432
    current->set_camera(&pentax);
346 awk 433
 
347 awk 434
    //build acceleration structure
435
    current->initialize(8, 25);
346 awk 436
 
347 awk 437
    //create the renderer
348 awk 438
    renderer = new path_tracer(width, height, true, 1);
347 awk 439
    renderer->set_scene(current);
346 awk 440
 
347 awk 441
    //create the film
442
    film = new Vec3f[width * height];
443
    std::fill(film, film+width*height, Vec3f(0.3f));
444
    image = new Vec3uc[width * height];
445
    std::fill(image, image+width*height, Vec3uc(32,32,32));
346 awk 446
 
347 awk 447
    //init glut
448
    glutInit(&argc, argv);
449
    glutInitWindowSize(width, height);
450
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
451
    glutCreateWindow("Course 02566 - IMM DTU");
346 awk 452
 
347 awk 453
    glutDisplayFunc(display);
454
    glutReshapeFunc(reshape);
455
    glutKeyboardFunc(keyboard);
456
    glutSpecialFunc(special);
346 awk 457
 
347 awk 458
    glutIdleFunc(idle);
346 awk 459
 
347 awk 460
    //Turn the flow of control over to GLUT
461
    glutMainLoop();
346 awk 462
 
347 awk 463
    //clean up
464
    delete current;
465
    delete renderer;
466
 
467
    return EXIT_SUCCESS;
468
}
353 awk 469
#endif