Subversion Repositories gelsvn

Rev

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

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