Subversion Repositories gelsvn

Rev

Rev 346 | Rev 348 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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