Subversion Repositories gelsvn

Rev

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

Rev 347 Rev 348
1
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
1
//02566 framework, Anders Wang Kristensen, awk@imm.dtu.dk, 2007
2
 
2
 
3
#include <stdlib.h>
3
#include <stdlib.h>
4
#include <GL/glut.h>
4
#include <GL/glut.h>
5
 
5
 
6
#include <CGLA/Vec2i.h>
6
#include <CGLA/Vec2i.h>
7
#include <CGLA/Vec3uc.h>
7
#include <CGLA/Vec3uc.h>
8
 
8
 
9
#include "scene.hpp"
9
#include "scene.hpp"
10
#include "camera.hpp"
10
#include "camera.hpp"
11
#include "mesh.hpp"
11
#include "mesh.hpp"
12
#include "omni.hpp"
12
#include "omni.hpp"
13
 
13
 
14
#include "matte.hpp"
14
#include "matte.hpp"
15
#include "plastic.hpp"
15
#include "plastic.hpp"
16
#include "metal.hpp"
16
#include "metal.hpp"
17
#include "glass.hpp"
17
#include "glass.hpp"
18
 
18
 
19
#include "path_tracer.hpp"
19
#include "path_tracer.hpp"
20
 
20
 
21
using namespace CGLA;
21
using namespace CGLA;
22
 
22
 
23
//global pointer to the active scene
23
//global pointer to the active scene
24
scene* current;
24
scene* current;
25
 
25
 
26
static path_tracer* renderer;
26
static path_tracer* renderer;
27
 
27
 
28
const int width = 64*8;
28
const int width = 64*8;
29
const int height = 64*8;
29
const int height = 64*8;
30
 
30
 
31
static Vec3f* film;
31
static Vec3f* film;
32
static Vec3uc* image;
32
static Vec3uc* image;
33
static Vec2i pixel(0);
33
static Vec2i pixel(0);
34
static bool done = false;
34
static bool done = false;
35
 
35
 
36
static float dgamma = 2.2f;
36
static float dgamma = 2.2f;
37
static float dexposure = 0.f;
37
static float dexposure = 0.f;
38
 
38
 
39
Vec3uc tonemap(const Vec3f& v)
39
Vec3uc tonemap(const Vec3f& v)
40
{
40
{
41
    Vec3f u = v * std::pow(2.f, dexposure);
41
    Vec3f u = v * std::pow(2.f, dexposure);
42
    float r = random() - 0.5f;
42
    float r = mt_random() - 0.5f;
43
 
43
 
44
    Vec3uc I;
44
    Vec3uc I;
45
    for (int i=0; i<3; ++i)
45
    for (int i=0; i<3; ++i)
46
    {
46
    {
47
        float c = std::pow(u[i], 1.f / dgamma);
47
        float c = std::pow(u[i], 1.f / dgamma);
48
        c = 255.f * c + 0.5f + r;
48
        c = 255.f * c + 0.5f + r;
49
        I[i] = clamp(int(c), 0, 255);
49
        I[i] = clamp(int(c), 0, 255);
50
    }
50
    }
51
    return I;
51
    return I;
52
}
52
}
53
 
53
 
54
void display(void)
54
void display(void)
55
{
55
{
56
    glClear(GL_COLOR_BUFFER_BIT);
56
    glClear(GL_COLOR_BUFFER_BIT);
57
 
57
 
58
    for (int j=0; j<height; ++j)
58
    for (int j=0; j<height; ++j)
59
        for (int i=0; i<width; ++i)
59
        for (int i=0; i<width; ++i)
60
            image[i + j*width] = tonemap(film[i + j*width]);
60
            image[i + j*width] = tonemap(film[i + j*width]);
61
 
61
 
62
    glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
62
    glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, image);
63
    glutSwapBuffers();
63
    glutSwapBuffers();
64
 
64
 
65
    GLenum e = glGetError();
65
    GLenum e = glGetError();
66
    if (e != GL_NO_ERROR)
66
    if (e != GL_NO_ERROR)
67
    {
67
    {
68
        printf("OpenGL error: %s\n", gluErrorString(e));
68
        printf("OpenGL error: %s\n", gluErrorString(e));
69
        exit(EXIT_FAILURE);
69
        exit(EXIT_FAILURE);
70
    }
70
    }
71
}
71
}
72
 
72
 
73
void reshape(GLint width, GLint height)
73
void reshape(GLint width, GLint height)
74
{
74
{
75
    glMatrixMode(GL_PROJECTION);
75
    glMatrixMode(GL_PROJECTION);
76
    glLoadIdentity();
76
    glLoadIdentity();
77
    gluOrtho2D(0, width, 0, height);
77
    gluOrtho2D(0, width, 0, height);
78
    glMatrixMode(GL_MODELVIEW);
78
    glMatrixMode(GL_MODELVIEW);
79
    glLoadIdentity();
79
    glLoadIdentity();
80
}
80
}
81
 
81
 
82
void special(int key, int x, int y)
82
void special(int key, int x, int y)
83
{
83
{
84
    switch (key)
84
    switch (key)
85
    {
85
    {
86
    case GLUT_KEY_LEFT:
86
    case GLUT_KEY_LEFT:
87
        dgamma = std::max(dgamma - 0.1f, 0.5f);
87
        dgamma = std::max(dgamma - 0.1f, 0.5f);
88
        break;
88
        break;
89
 
89
 
90
    case GLUT_KEY_RIGHT:
90
    case GLUT_KEY_RIGHT:
91
        dgamma = std::min(dgamma + 0.1f, 5.f);
91
        dgamma = std::min(dgamma + 0.1f, 5.f);
92
        break;
92
        break;
93
 
93
 
94
    case GLUT_KEY_DOWN:
94
    case GLUT_KEY_DOWN:
95
        dexposure = std::max(dexposure - 0.1f, -10.f);
95
        dexposure = std::max(dexposure - 0.1f, -10.f);
96
        break;
96
        break;
97
 
97
 
98
    case GLUT_KEY_UP:
98
    case GLUT_KEY_UP:
99
        dexposure = std::min(dexposure + 0.1f, 10.f);
99
        dexposure = std::min(dexposure + 0.1f, 10.f);
100
        break;
100
        break;
101
    }
101
    }
102
 
102
 
103
    printf("gamma: %.2f, exposure: %.2f\n", dgamma, dexposure);
103
    printf("gamma: %.2f, exposure: %.2f\n", dgamma, dexposure);
104
    glutPostRedisplay();
104
    glutPostRedisplay();
105
}
105
}
106
 
106
 
107
void keyboard(unsigned char key, int x, int y)
107
void keyboard(unsigned char key, int x, int y)
108
{
108
{
109
    switch (key)
109
    switch (key)
110
    {
110
    {
111
    case 27: //ESCAPE key
111
    case 27: //ESCAPE key
112
        exit(0);
112
        exit(0);
113
        break;
113
        break;
114
    }
114
    }
115
}
115
}
116
 
116
 
117
void idle(void)
117
void idle(void)
118
{
118
{
119
    for (int i=0; i<width*8 && !done; ++i)
119
    for (int i=0; i<width*8 && !done; ++i)
120
    {
120
    {
121
        Vec3f L = renderer->compute_pixel(pixel[0], pixel[1]);
121
        Vec3f L = renderer->compute_pixel(pixel[0], pixel[1]);
122
        film[pixel[0] + pixel[1] * width] = L;
122
        film[pixel[0] + pixel[1] * width] = L;
123
 
123
 
124
        //advance
124
        //advance
125
        ++pixel[0];
125
        ++pixel[0];
126
 
126
 
127
        if (pixel[0] == width)
127
        if (pixel[0] == width)
128
        {
128
        {
129
            pixel[0] = 0;
129
            pixel[0] = 0;
130
            ++pixel[1];
130
            ++pixel[1];
131
 
131
 
132
            if (pixel[1] == height)
132
            if (pixel[1] == height)
133
            {
133
            {
134
                done = true;
134
                done = true;
135
                glutIdleFunc(NULL);
135
                glutIdleFunc(NULL);
136
            }
136
            }
137
        }
137
        }
138
    }
138
    }
139
 
139
 
140
    glutPostRedisplay();
140
    glutPostRedisplay();
141
}
141
}
142
 
142
 
143
int main(int argc, char* argv[])
143
int main(int argc, char* argv[])
144
{
144
{
145
    //make the scene
145
    //make the scene
146
    current = new scene;
146
    current = new scene;
147
 
147
 
148
    //setup some materials
148
    //setup some materials
149
    matte dull_white(Vec3f(0.5f, 0.5f, 0.5f));
149
    matte dull_white(Vec3f(0.5f, 0.5f, 0.5f));
150
    matte dull_gray(Vec3f(0.4f, 0.4f, 0.4f));
150
    matte dull_gray(Vec3f(0.4f, 0.4f, 0.4f));
151
    matte dull_red(Vec3f(0.6f, 0.3f, 0.2f));
151
    matte dull_red(Vec3f(0.6f, 0.3f, 0.2f));
152
    matte dull_green(Vec3f(0.3f, 0.6f, 0.2f));
152
    matte dull_green(Vec3f(0.3f, 0.6f, 0.2f));
153
    matte dull_blue(Vec3f(0.2f, 0.2f, 0.6f));
153
    matte dull_blue(Vec3f(0.2f, 0.2f, 0.6f));
154
 
154
 
155
    plastic glossy_white(Vec3f(0.4f, 0.4f, 0.4f), Vec3f(0.5f), 20.f);
155
    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);
156
    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);
157
    plastic glossy_yellow(Vec3f(0.3f, 0.3f, 0.0f), Vec3f(0.65f), 32.f);
158
 
158
 
159
    //for exercise 2
159
    //for exercise 2
160
    glass clear(Vec3f(0.99f, 0.99f, 0.99f), 1.5f);
160
    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);
161
    metal silver(Vec3f(0.9f, 0.9f, 0.9f), 22.f, 0.177f, 3.638f);
162
 
162
 
163
    //setup cornell box 1mx1mx1m
163
    //setup cornell box 1mx1mx1m
164
    mesh floor("../../data/cornell_box/floor.obj");
164
    mesh floor("../../data/cornell_box/floor.obj");
165
    floor.set_material(&glossy_white);
165
    floor.set_material(&glossy_white);
166
    current->insert(&floor);
166
    current->insert(&floor);
167
 
167
 
168
    mesh ceiling("../../data/cornell_box/ceiling.obj");
168
    mesh ceiling("../../data/cornell_box/ceiling.obj");
169
    ceiling.set_material(&dull_gray);
169
    ceiling.set_material(&dull_gray);
170
    current->insert(&ceiling);
170
    current->insert(&ceiling);
171
 
171
 
172
    mesh back("../../data/cornell_box/back.obj");
172
    mesh back("../../data/cornell_box/back.obj");
173
    back.set_material(&silver);
173
    back.set_material(&silver);
174
    current->insert(&back);
174
    current->insert(&back);
175
 
175
 
176
    mesh left("../../data/cornell_box/left.obj");
176
    mesh left("../../data/cornell_box/left.obj");
177
    left.set_material(&dull_red);
177
    left.set_material(&dull_red);
178
    current->insert(&left);
178
    current->insert(&left);
179
 
179
 
180
    mesh right("../../data/cornell_box/right.obj");
180
    mesh right("../../data/cornell_box/right.obj");
181
    right.set_material(&dull_blue);
181
    right.set_material(&dull_blue);
182
    current->insert(&right);
182
    current->insert(&right);
183
 
183
 
184
    //add some objects to the box
184
    //add some objects to the box
185
    Mat4x4f tmp;
185
    Mat4x4f tmp;
186
 
186
 
187
    mesh box1("../../data/cornell_box/box1.obj");
187
    mesh box1("../../data/cornell_box/box1.obj");
188
    tmp = rotation_Mat4x4f(YAXIS, -float(M_PI)/8.f);
188
    tmp = rotation_Mat4x4f(YAXIS, -float(M_PI)/8.f);
189
    tmp = translation_Mat4x4f(Vec3f(-0.3f,0.f,-0.05f)) * tmp;
189
    tmp = translation_Mat4x4f(Vec3f(-0.3f,0.f,-0.05f)) * tmp;
190
    box1.set_transform(tmp);
190
    box1.set_transform(tmp);
191
    box1.set_material(&dull_green);
191
    box1.set_material(&dull_green);
192
    current->insert(&box1);
192
    current->insert(&box1);
193
 
193
 
194
    mesh box2("../../data/cornell_box/box2.obj");
194
    mesh box2("../../data/cornell_box/box2.obj");
195
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.f, -0.05f));
195
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.f, -0.05f));
196
    box2.set_transform(tmp);
196
    box2.set_transform(tmp);
197
    box2.set_material(&glossy_purple);
197
    box2.set_material(&glossy_purple);
198
    current->insert(&box2);
198
    current->insert(&box2);
199
 
199
 
200
    mesh teapot("../../data/cornell_box/teapot1.obj");
200
    mesh teapot("../../data/cornell_box/teapot1.obj");
201
    tmp = rotation_Mat4x4f(YAXIS, float(M_PI/4.f));
201
    tmp = rotation_Mat4x4f(YAXIS, float(M_PI/4.f));
202
    tmp = translation_Mat4x4f(Vec3f(-0.25f,0.25f, -0.05)) * tmp;
202
    tmp = translation_Mat4x4f(Vec3f(-0.25f,0.25f, -0.05)) * tmp;
203
    teapot.set_transform(tmp);
203
    teapot.set_transform(tmp);
204
    teapot.set_material(&silver);
204
    teapot.set_material(&silver);
205
    current->insert(&teapot);
205
    current->insert(&teapot);
206
 
206
 
207
    mesh sphere1("../../data/cornell_box/sphere2.obj");
207
    mesh sphere1("../../data/cornell_box/sphere2.obj");
208
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.45f,-0.05f));
208
    tmp = translation_Mat4x4f(Vec3f(0.25f,0.45f,-0.05f));
209
    sphere1.set_transform(tmp);
209
    sphere1.set_transform(tmp);
210
    sphere1.set_material(&clear);
210
    sphere1.set_material(&clear);
211
    current->insert(&sphere1);
211
    current->insert(&sphere1);
212
 
212
 
213
    mesh sphere2("../../data/cornell_box/sphere2.obj");
213
    mesh sphere2("../../data/cornell_box/sphere2.obj");
214
    tmp = translation_Mat4x4f(Vec3f(0.22f,0.15f,0.25f));
214
    tmp = translation_Mat4x4f(Vec3f(0.22f,0.15f,0.25f));
215
    sphere2.set_transform(tmp);
215
    sphere2.set_transform(tmp);
216
    sphere2.set_material(&glossy_yellow);
216
    sphere2.set_material(&glossy_yellow);
217
    //current->insert(&sphere2);
217
    //current->insert(&sphere2);
218
 
218
 
219
    //light sources
219
    //light sources
220
    mesh quad_light("../../data/cornell_box/quad.obj");
220
    mesh quad_light("../../data/cornell_box/quad.obj");
221
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
221
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
222
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
222
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
223
    quad_light.set_transform(tmp);
223
    quad_light.set_transform(tmp);
224
    quad_light.set_exitance(Vec3f(300,300,300));
224
    quad_light.set_exitance(Vec3f(300,300,300));
225
    current->insert(&quad_light);
225
    current->insert(&quad_light);
226
 
226
 
227
    mesh quad_light1("../../data/cornell_box/quad.obj");
227
    mesh quad_light1("../../data/cornell_box/quad.obj");
228
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
228
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
229
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
229
    tmp = translation_Mat4x4f(Vec3f(0.f,0.99f,0.2f)) * tmp;
230
    quad_light1.set_transform(tmp);
230
    quad_light1.set_transform(tmp);
231
    quad_light1.set_exitance(Vec3f(0,400,0));
231
    quad_light1.set_exitance(Vec3f(0,400,0));
232
//  current->insert(&quad_light1);
232
//  current->insert(&quad_light1);
233
 
233
 
234
    mesh quad_light2("../../data/cornell_box/quad.obj");
234
    mesh quad_light2("../../data/cornell_box/quad.obj");
235
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
235
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
236
    tmp = translation_Mat4x4f(Vec3f(-0.2f,0.99f,0.2f)) * tmp;
236
    tmp = translation_Mat4x4f(Vec3f(-0.2f,0.99f,0.2f)) * tmp;
237
    quad_light2.set_transform(tmp);
237
    quad_light2.set_transform(tmp);
238
    quad_light2.set_exitance(Vec3f(400,0,0));
238
    quad_light2.set_exitance(Vec3f(400,0,0));
239
//  current->insert(&quad_light2);
239
//  current->insert(&quad_light2);
240
 
240
 
241
    mesh quad_light3("../../data/cornell_box/quad.obj");
241
    mesh quad_light3("../../data/cornell_box/quad.obj");
242
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
242
    tmp = rotation_Mat4x4f(XAXIS, float(M_PI));
243
    tmp = translation_Mat4x4f(Vec3f(0.2f,0.99f,0.2f)) * tmp;
243
    tmp = translation_Mat4x4f(Vec3f(0.2f,0.99f,0.2f)) * tmp;
244
    quad_light3.set_transform(tmp);
244
    quad_light3.set_transform(tmp);
245
    quad_light3.set_exitance(Vec3f(0,0,400));
245
    quad_light3.set_exitance(Vec3f(0,0,400));
246
//  current->insert(&quad_light3);
246
//  current->insert(&quad_light3);
247
 
247
 
248
    omni omni_light(Vec3f(30.f));
248
    omni omni_light(Vec3f(30.f));
249
    omni_light.set_transform(translation_Mat4x4f(Vec3f(0.0f,0.95f,0.0f)));
249
    omni_light.set_transform(translation_Mat4x4f(Vec3f(0.0f,0.95f,0.0f)));
250
    //current->insert(&omni_light);
250
    //current->insert(&omni_light);
251
 
251
 
252
    mesh sphere_light("../../data/cornell_box/small_sphere.obj");
252
    mesh sphere_light("../../data/cornell_box/small_sphere.obj");
253
    sphere_light.set_transform(translation_Mat4x4f(Vec3f(0.f,0.95f,0.f)));
253
    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)));
254
    sphere_light.set_exitance(Vec3f(30.f/(4.f*float(M_PI)*0.01f*0.01f)));
255
    //current->insert(&sphere_light);
255
    //current->insert(&sphere_light);
256
 
256
 
257
    //setup camera (eye, center, up, focal length)
257
    //setup camera (eye, center, up, focal length)
258
    camera pentax(
258
    camera pentax(
259
        Vec3f(0.f,0.5f,2.0f),
259
        Vec3f(0.f,0.5f,2.0f),
260
        Vec3f(0.f,0.5f,0.5f),
260
        Vec3f(0.f,0.5f,0.5f),
261
        Vec3f(0,1,0),
261
        Vec3f(0,1,0),
262
        0.035f);
262
        0.035f);
263
 
263
 
264
    current->set_camera(&pentax);
264
    current->set_camera(&pentax);
265
 
265
 
266
    //build acceleration structure
266
    //build acceleration structure
267
    current->initialize(8, 25);
267
    current->initialize(8, 25);
268
 
268
 
269
    //create the renderer
269
    //create the renderer
270
    renderer = new path_tracer(width, height, true, 4);
270
    renderer = new path_tracer(width, height, true, 1);
271
    renderer->set_scene(current);
271
    renderer->set_scene(current);
272
 
272
 
273
    //create the film
273
    //create the film
274
    film = new Vec3f[width * height];
274
    film = new Vec3f[width * height];
275
    std::fill(film, film+width*height, Vec3f(0.3f));
275
    std::fill(film, film+width*height, Vec3f(0.3f));
276
    image = new Vec3uc[width * height];
276
    image = new Vec3uc[width * height];
277
    std::fill(image, image+width*height, Vec3uc(32,32,32));
277
    std::fill(image, image+width*height, Vec3uc(32,32,32));
278
 
278
 
279
    //init glut
279
    //init glut
280
    glutInit(&argc, argv);
280
    glutInit(&argc, argv);
281
    glutInitWindowSize(width, height);
281
    glutInitWindowSize(width, height);
282
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
282
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
283
    glutCreateWindow("Course 02566 - IMM DTU");
283
    glutCreateWindow("Course 02566 - IMM DTU");
284
 
284
 
285
    glutDisplayFunc(display);
285
    glutDisplayFunc(display);
286
    glutReshapeFunc(reshape);
286
    glutReshapeFunc(reshape);
287
    glutKeyboardFunc(keyboard);
287
    glutKeyboardFunc(keyboard);
288
    glutSpecialFunc(special);
288
    glutSpecialFunc(special);
289
 
289
 
290
    glutIdleFunc(idle);
290
    glutIdleFunc(idle);
291
 
291
 
292
    //Turn the flow of control over to GLUT
292
    //Turn the flow of control over to GLUT
293
    glutMainLoop();
293
    glutMainLoop();
294
 
294
 
295
    //clean up
295
    //clean up
296
    delete current;
296
    delete current;
297
    delete renderer;
297
    delete renderer;
298
 
298
 
299
    return EXIT_SUCCESS;
299
    return EXIT_SUCCESS;
300
}
300
}
301
 
301