Subversion Repositories gelsvn

Rev

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