Subversion Repositories gelsvn

Rev

Rev 632 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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