Subversion Repositories gelsvn

Rev

Rev 348 | Rev 355 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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