Subversion Repositories gelsvn

Rev

Go to most recent revision | Details | 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
 
36
static float gamma = 2.2f;
37
static float exposure = 0.f;
38
 
39
Vec3uc tonemap(const Vec3f& v)
40
{
41
	Vec3f u = v * std::pow(2.f, exposure);
42
	float r = random() - 0.5f;
43
 
44
	Vec3uc I;
45
	for (int i=0; i<3; ++i)
46
	{
47
		float c = std::pow(u[i], 1.f / gamma);
48
		c = 255.f * c + 0.5f + r;
49
		I[i] = clamp(int(c), 0, 255);
50
	}
51
	return I;
52
}
53
 
54
void display(void)
55
{
56
	glClear(GL_COLOR_BUFFER_BIT);
57
 
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]);
61
 
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
	}
71
}
72
 
73
void reshape(GLint width, GLint height)
74
{
75
	glMatrixMode(GL_PROJECTION);
76
	glLoadIdentity();
77
	gluOrtho2D(0, width, 0, height);
78
	glMatrixMode(GL_MODELVIEW);
79
	glLoadIdentity();
80
}
81
 
82
void special(int key, int x, int y)
83
{
84
	switch (key)
85
	{
86
	case GLUT_KEY_LEFT:
87
		gamma = std::max(gamma - 0.1f, 0.5f);
88
		break;
89
 
90
	case GLUT_KEY_RIGHT:
91
		gamma = std::min(gamma + 0.1f, 5.f);
92
		break;
93
 
94
	case GLUT_KEY_DOWN:
95
		exposure = std::max(exposure - 0.1f, -10.f);
96
		break;
97
 
98
	case GLUT_KEY_UP:
99
		exposure = std::min(exposure + 0.1f, 10.f);
100
		break;
101
	}
102
 
103
	printf("Gamma: %.2f, exposure: %.2f\n", gamma, exposure);
104
	glutPostRedisplay();
105
}
106
 
107
void keyboard(unsigned char key, int x, int y)
108
{
109
	switch (key)
110
	{
111
	case 27: //ESCAPE key
112
		exit(0);
113
		break;
114
	}
115
}
116
 
117
void idle(void)
118
{
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;
123
 
124
		//advance
125
		++pixel[0];
126
 
127
		if (pixel[0] == width)
128
		{
129
			pixel[0] = 0;
130
			++pixel[1];
131
 
132
			if (pixel[1] == height)
133
			{
134
				done = true;
135
				glutIdleFunc(NULL);
136
			}
137
		}
138
	}
139
 
140
 	glutPostRedisplay();
141
}
142
 
143
int main(int argc, char* argv[])
144
{
145
	//make the scene
146
	current = new scene;
147
 
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));
154
 
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);
158
 
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);
162
 
163
	//setup cornell box 1mx1mx1m
164
	mesh floor("../../data/cornell_box/floor.obj");
165
	floor.set_material(&glossy_white);
166
	current->insert(&floor);
167
 
168
	mesh ceiling("../../data/cornell_box/ceiling.obj");
169
	ceiling.set_material(&dull_gray);
170
	current->insert(&ceiling);
171
 
172
	mesh back("../../data/cornell_box/back.obj");
173
	back.set_material(&silver);
174
	current->insert(&back);
175
 
176
	mesh left("../../data/cornell_box/left.obj");
177
	left.set_material(&dull_red);
178
	current->insert(&left);
179
 
180
	mesh right("../../data/cornell_box/right.obj");
181
	right.set_material(&dull_blue);
182
	current->insert(&right);
183
 
184
	//add some objects to the box
185
	Mat4x4f tmp;
186
 
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);
193
 
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);
199
 
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);
206
 
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);
212
 
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);
218
 
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);
226
 
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);
233
 
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);
240
 
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);
247
 
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);
251
 
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);
256
 
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);
263
 
264
	current->set_camera(&pentax);
265
 
266
	//build acceleration structure
267
	current->initialize(8, 25);
268
 
269
	//create the renderer
270
	renderer = new path_tracer(width, height, true, 4);
271
	renderer->set_scene(current);
272
 
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));
278
 
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");
284
 
285
	glutDisplayFunc(display);
286
	glutReshapeFunc(reshape);
287
	glutKeyboardFunc(keyboard);
288
	glutSpecialFunc(special);
289
 
290
	glutIdleFunc(idle);
291
 
292
	//Turn the flow of control over to GLUT
293
	glutMainLoop();
294
 
295
	//clean up
296
	delete current;
297
	delete renderer;
298
 
299
	return EXIT_SUCCESS;
300
}