Subversion Repositories gelsvn

Rev

Details | Last modification | View Log | RSS feed

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