Subversion Repositories gelsvn

Rev

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

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