author | Radek Brich <radek.brich@devl.cz> |
Wed, 26 Mar 2008 17:03:38 +0100 | |
branch | pyrit |
changeset 48 | a4913301c626 |
parent 47 | 320d5d466864 |
child 60 | a23b5089b9c3 |
permissions | -rw-r--r-- |
42 | 1 |
#include "raytracer.h" |
2 |
#include "octree.h" |
|
3 |
||
4 |
#include "image.h" |
|
5 |
#include "common_sdl.h" |
|
6 |
||
7 |
Camera cam; |
|
8 |
Light light(Vector3(-2.0, 10.0, -2.0), Colour(0.9, 0.9, 0.9)); |
|
9 |
||
10 |
Float lx, ly, lz, cf; |
|
11 |
||
12 |
||
13 |
/*class CloudTexture: public Texture |
|
14 |
{ |
|
15 |
Vector3 centre; |
|
16 |
public: |
|
17 |
Colour evaluate(Vector3 point) |
|
18 |
{ |
|
19 |
Float sum = 0.0; |
|
20 |
for (int i = 1; i < 5; i++) |
|
21 |
sum += fabsf(perlin(point.x*i, point.y*i, point.z*i))/i; |
|
22 |
Float value = sinf(point.x + sum)/2 + 0.5; |
|
23 |
return Colour(value, value*0.5, value*0.5); |
|
24 |
}; |
|
25 |
};*/ |
|
26 |
||
27 |
void texture2D(Float u, Float v, Float &r, Float &g, Float &b) |
|
28 |
{ |
|
29 |
// checkers |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
30 |
r = fabs((int)(u*4.2)%2 + (int)(v*4.2)%2 - 1); |
42 | 31 |
g=r; b=r; |
32 |
} |
|
33 |
||
34 |
class PlanarMapTexture: public Texture |
|
35 |
{ |
|
36 |
Vector3 centre; |
|
37 |
public: |
|
38 |
PlanarMapTexture(Vector3 acentre): centre(acentre) {}; |
|
39 |
Colour evaluate(Vector3 point) |
|
40 |
{ |
|
41 |
point = point - centre; |
|
42 |
Float u = 1000+point.x; |
|
43 |
Float v = 1000+point.y; |
|
44 |
Float r,g,b; |
|
45 |
texture2D(u,v, r,g,b); |
|
46 |
return Colour(r, g, b); |
|
47 |
}; |
|
48 |
}; |
|
49 |
||
50 |
class CubicMapTexture: public Texture |
|
51 |
{ |
|
52 |
Vector3 centre; |
|
53 |
public: |
|
54 |
CubicMapTexture(Vector3 acentre): centre(acentre) {}; |
|
55 |
Colour evaluate(Vector3 point) |
|
56 |
{ |
|
57 |
point = point - centre; |
|
58 |
Float u,v; |
|
59 |
if (fabs(point.x) > fabs(point.y)) |
|
60 |
{ |
|
61 |
if (fabs(point.x) > fabs(point.z)) |
|
62 |
{ |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
63 |
if (point.x < 0) |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
64 |
u = -point.y; |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
65 |
else |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
66 |
u = point.y; |
42 | 67 |
v = point.z; |
68 |
} |
|
69 |
else |
|
70 |
{ |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
71 |
if (point.z < 0) |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
72 |
u = -point.x; |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
73 |
else |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
74 |
u = point.x; |
42 | 75 |
v = point.y; |
76 |
} |
|
77 |
} |
|
78 |
else |
|
79 |
{ |
|
80 |
if (fabs(point.y) > fabs(point.z)) |
|
81 |
{ |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
82 |
if (point.y < 0) |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
83 |
u = -point.x; |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
84 |
else |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
85 |
u = point.x; |
42 | 86 |
v = point.z; |
87 |
} |
|
88 |
else |
|
89 |
{ |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
90 |
if (point.z < 0) |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
91 |
u = -point.x; |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
92 |
else |
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
93 |
u = point.x; |
42 | 94 |
v = point.y; |
95 |
} |
|
96 |
} |
|
97 |
u += 1000; |
|
98 |
v += 1000; |
|
99 |
Float r,g,b; |
|
100 |
texture2D(u,v, r,g,b); |
|
101 |
return Colour(r, g, b); |
|
102 |
}; |
|
103 |
}; |
|
104 |
||
105 |
class CylinderMapTexture: public Texture |
|
106 |
{ |
|
107 |
Vector3 centre; |
|
108 |
public: |
|
109 |
CylinderMapTexture(Vector3 acentre): centre(acentre) {}; |
|
110 |
Colour evaluate(Vector3 point) |
|
111 |
{ |
|
112 |
point = point - centre; |
|
113 |
||
114 |
Float u,v; |
|
115 |
v = 1000+point.y; |
|
116 |
u = M_PI + atan2(point.z, point.x); |
|
117 |
||
118 |
Float r,g,b; |
|
119 |
texture2D(u,v, r,g,b); |
|
120 |
return Colour(r, g, b); |
|
121 |
}; |
|
122 |
}; |
|
123 |
||
124 |
class SphereMapTexture: public Texture |
|
125 |
{ |
|
126 |
Vector3 centre; |
|
127 |
public: |
|
128 |
SphereMapTexture(Vector3 acentre): centre(acentre) {}; |
|
129 |
Colour evaluate(Vector3 point) |
|
130 |
{ |
|
131 |
point = point - centre; |
|
132 |
||
133 |
Float u,v; |
|
134 |
v = acos(point.y / point.mag()); |
|
135 |
u = M_PI + atan2(point.z, point.x); |
|
136 |
||
137 |
Float r,g,b; |
|
138 |
texture2D(u,v, r,g,b); |
|
139 |
return Colour(r, g, b); |
|
140 |
}; |
|
141 |
}; |
|
142 |
||
143 |
void update_callback() |
|
144 |
{ |
|
145 |
if (lx != 0.0) |
|
146 |
light.pos.x += lx; |
|
147 |
if (ly != 0.0) |
|
148 |
light.pos.y += ly; |
|
149 |
if (lz != 0.0) |
|
150 |
light.pos.z += lz; |
|
151 |
if (cf != 0.0) |
|
152 |
cam.f += cf; |
|
153 |
} |
|
154 |
||
155 |
void key_callback(int key, int down) |
|
156 |
{ |
|
157 |
switch (key) |
|
158 |
{ |
|
159 |
case SDLK_r: |
|
160 |
lx = -0.1 * down; |
|
161 |
break; |
|
162 |
case SDLK_t: |
|
163 |
lx = +0.1 * down; |
|
164 |
break; |
|
165 |
case SDLK_f: |
|
166 |
ly = -0.1 * down; |
|
167 |
break; |
|
168 |
case SDLK_g: |
|
169 |
ly = +0.1 * down; |
|
170 |
break; |
|
171 |
case SDLK_v: |
|
172 |
lz = -0.1 * down; |
|
173 |
break; |
|
174 |
case SDLK_b: |
|
175 |
lz = +0.1 * down; |
|
176 |
break; |
|
177 |
||
178 |
case SDLK_z: |
|
179 |
cf = -0.02 * down; |
|
180 |
break; |
|
181 |
case SDLK_x: |
|
182 |
cf = +0.02 * down; |
|
183 |
break; |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
int main(int argc, char **argv) |
|
188 |
{ |
|
189 |
Raytracer rt; |
|
190 |
||
191 |
Octree top; |
|
192 |
rt.setTop(&top); |
|
193 |
||
194 |
rt.addlight(&light); |
|
195 |
light.castShadows(false); |
|
196 |
||
197 |
Material mat0a(Colour(0.7, 0.7, 0.7)); |
|
198 |
mat0a. setReflectivity(0.0); |
|
199 |
Box box(Vector3(-12.0, -1.2, -20.0), Vector3(12.0, -1.0, 0.0), &mat0a); |
|
200 |
rt.addshape(&box); |
|
201 |
||
202 |
Material mat0b(Colour(0.1, 0.7, 0.8)); |
|
203 |
mat0b.setReflectivity(0.7); |
|
204 |
Box box2(Vector3(-12.0, -1.2, -10.0), Vector3(12.0, 10.0, -10.2), &mat0b); |
|
205 |
rt.addshape(&box2); |
|
206 |
||
207 |
// spheres |
|
208 |
Material mat1(Colour(1.0, 1.0, 1.0)); |
|
209 |
mat1.texture = new PlanarMapTexture(Vector3(4.0, 2.0, -7.0)); |
|
210 |
Sphere sphere1(Vector3(4.0, 2.0, -7.0), 1.0, &mat1); |
|
211 |
rt.addshape(&sphere1); |
|
212 |
||
213 |
Material mat2(Colour(1.0, 1.0, 1.0)); |
|
214 |
mat2.texture = new CubicMapTexture(Vector3(1.0, 2.0, -7.0)); |
|
215 |
Sphere sphere2(Vector3(1.0, 2.0, -7.0), 1.0, &mat2); |
|
216 |
rt.addshape(&sphere2); |
|
217 |
||
218 |
Material mat3(Colour(1.0, 1.0, 1.0)); |
|
219 |
mat3.texture = new CylinderMapTexture(Vector3(-2.0, 2.0, -7.0)); |
|
220 |
Sphere sphere3(Vector3(-2.0, 2.0, -7.0), 1.0, &mat3); |
|
221 |
rt.addshape(&sphere3); |
|
222 |
||
223 |
Material mat4(Colour(1.0, 1.0, 1.0)); |
|
224 |
mat4.texture = new SphereMapTexture(Vector3(-5.0, 2.0, -7.0)); |
|
225 |
Sphere sphere4(Vector3(-5.0, 2.0, -7.0), 1.0, &mat4); |
|
226 |
rt.addshape(&sphere4); |
|
227 |
||
228 |
// cubes |
|
229 |
Material mat5(Colour(1.0, 1.0, 1.0)); |
|
230 |
mat5.texture = new PlanarMapTexture(Vector3(4.0, 0.0, -7.0)); |
|
231 |
Box cube1(Vector3(4.0, 0.0, -7.0)-1.0, Vector3(4.0, 0.0, -7.0)+1.0, &mat5); |
|
232 |
rt.addshape(&cube1); |
|
233 |
||
234 |
Material mat6(Colour(1.0, 1.0, 1.0)); |
|
235 |
mat6.texture = new CubicMapTexture(Vector3(1.0, 0.0, -7.0)); |
|
236 |
Box cube2(Vector3(1.0, 0.0, -7.0)-1.0, Vector3(1.0, 0.0, -7.0)+1.0, &mat6); |
|
237 |
rt.addshape(&cube2); |
|
238 |
||
239 |
Material mat7(Colour(1.0, 1.0, 1.0)); |
|
240 |
mat7.texture = new CylinderMapTexture(Vector3(-2.0, 0.0, -7.0)); |
|
241 |
Box cube3(Vector3(-2.0, 0.0, -7.0)-1.0, Vector3(-2.0, 0.0, -7.0)+1.0, &mat7); |
|
242 |
rt.addshape(&cube3); |
|
243 |
||
244 |
Material mat8(Colour(1.0, 1.0, 1.0)); |
|
245 |
mat8.texture = new SphereMapTexture(Vector3(-5.0, 0.0, -7.0)); |
|
246 |
Box cube4(Vector3(-5.0, 0.0, -7.0)-1.0, Vector3(-5.0, 0.0, -7.0)+1.0, &mat8); |
|
247 |
rt.addshape(&cube4); |
|
248 |
||
249 |
mat1.setReflectivity(0); |
|
250 |
mat2.setReflectivity(0); |
|
251 |
mat3.setReflectivity(0); |
|
252 |
mat4.setReflectivity(0); |
|
253 |
mat5.setReflectivity(0); |
|
254 |
mat6.setReflectivity(0); |
|
255 |
mat7.setReflectivity(0); |
|
256 |
mat8.setReflectivity(0); |
|
257 |
||
258 |
top.optimize(); |
|
259 |
||
260 |
cam.setEye(Vector3(-0.530505, 11.0964, 11.2208)); |
|
261 |
cam.p = Vector3(-4.18144e-08, -0.461779, -0.886995); |
|
262 |
cam.u = Vector3(-1, 0, 6.3393e-11); |
|
263 |
cam.v = Vector3(3.19387e-08, 0.886995, -0.461779); |
|
264 |
rt.setCamera(&cam); |
|
265 |
||
266 |
w = 1024; |
|
267 |
h = 600; |
|
268 |
||
269 |
/* run interactive mode */ |
|
270 |
loop_sdl(rt, cam, update_callback, key_callback); |
|
271 |
||
272 |
/* render image */ |
|
273 |
if (argc == 2 && !strcmp(argv[1], "-r")) |
|
274 |
{ |
|
275 |
pyrit_verbosity = 2; |
|
276 |
Float *fdata = (Float *) malloc(w*h*3*sizeof(Float)); |
|
43
0b8b968b42d1
memory optimization for octree
Radek Brich <radek.brich@devl.cz>
parents:
42
diff
changeset
|
277 |
rt.ambientocclusion(300, 5.0, 0.5); |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
43
diff
changeset
|
278 |
DefaultSampler sampler(fdata, w, h); |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
279 |
sampler.setOversample(2); |
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
280 |
sampler.setSubsample(1); |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
43
diff
changeset
|
281 |
rt.setSampler(&sampler); |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
43
diff
changeset
|
282 |
rt.render(); |
42 | 283 |
|
284 |
struct image *img; |
|
285 |
new_image(&img, w, h, 3); |
|
286 |
||
287 |
Float *fd = fdata; |
|
288 |
for (char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) { |
|
289 |
if (*fd > 1.0) |
|
290 |
*cd = 255; |
|
291 |
else |
|
292 |
*cd = (unsigned char)(*fd * 255.0); |
|
293 |
} |
|
294 |
free(fdata); |
|
295 |
save_png("textures.png", img); |
|
296 |
destroy_image(&img); |
|
297 |
} |
|
298 |
} |