|
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 |
|
30 r = fabs((int)(u*4.4)%2 + (int)(v*4.4)%2 - 1); |
|
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 { |
|
63 u = point.y; |
|
64 v = point.z; |
|
65 } |
|
66 else |
|
67 { |
|
68 u = point.x; |
|
69 v = point.y; |
|
70 } |
|
71 } |
|
72 else |
|
73 { |
|
74 if (fabs(point.y) > fabs(point.z)) |
|
75 { |
|
76 u = point.x; |
|
77 v = point.z; |
|
78 } |
|
79 else |
|
80 { |
|
81 u = point.x; |
|
82 v = point.y; |
|
83 } |
|
84 } |
|
85 u += 1000; |
|
86 v += 1000; |
|
87 Float r,g,b; |
|
88 texture2D(u,v, r,g,b); |
|
89 return Colour(r, g, b); |
|
90 }; |
|
91 }; |
|
92 |
|
93 class CylinderMapTexture: public Texture |
|
94 { |
|
95 Vector3 centre; |
|
96 public: |
|
97 CylinderMapTexture(Vector3 acentre): centre(acentre) {}; |
|
98 Colour evaluate(Vector3 point) |
|
99 { |
|
100 point = point - centre; |
|
101 |
|
102 Float u,v; |
|
103 v = 1000+point.y; |
|
104 u = M_PI + atan2(point.z, point.x); |
|
105 |
|
106 Float r,g,b; |
|
107 texture2D(u,v, r,g,b); |
|
108 return Colour(r, g, b); |
|
109 }; |
|
110 }; |
|
111 |
|
112 class SphereMapTexture: public Texture |
|
113 { |
|
114 Vector3 centre; |
|
115 public: |
|
116 SphereMapTexture(Vector3 acentre): centre(acentre) {}; |
|
117 Colour evaluate(Vector3 point) |
|
118 { |
|
119 point = point - centre; |
|
120 |
|
121 Float u,v; |
|
122 v = acos(point.y / point.mag()); |
|
123 u = M_PI + atan2(point.z, point.x); |
|
124 |
|
125 Float r,g,b; |
|
126 texture2D(u,v, r,g,b); |
|
127 return Colour(r, g, b); |
|
128 }; |
|
129 }; |
|
130 |
|
131 void update_callback() |
|
132 { |
|
133 if (lx != 0.0) |
|
134 light.pos.x += lx; |
|
135 if (ly != 0.0) |
|
136 light.pos.y += ly; |
|
137 if (lz != 0.0) |
|
138 light.pos.z += lz; |
|
139 if (cf != 0.0) |
|
140 cam.f += cf; |
|
141 } |
|
142 |
|
143 void key_callback(int key, int down) |
|
144 { |
|
145 switch (key) |
|
146 { |
|
147 case SDLK_r: |
|
148 lx = -0.1 * down; |
|
149 break; |
|
150 case SDLK_t: |
|
151 lx = +0.1 * down; |
|
152 break; |
|
153 case SDLK_f: |
|
154 ly = -0.1 * down; |
|
155 break; |
|
156 case SDLK_g: |
|
157 ly = +0.1 * down; |
|
158 break; |
|
159 case SDLK_v: |
|
160 lz = -0.1 * down; |
|
161 break; |
|
162 case SDLK_b: |
|
163 lz = +0.1 * down; |
|
164 break; |
|
165 |
|
166 case SDLK_z: |
|
167 cf = -0.02 * down; |
|
168 break; |
|
169 case SDLK_x: |
|
170 cf = +0.02 * down; |
|
171 break; |
|
172 } |
|
173 } |
|
174 |
|
175 int main(int argc, char **argv) |
|
176 { |
|
177 Raytracer rt; |
|
178 rt.setOversample(0); |
|
179 rt.setSubsample(8); |
|
180 |
|
181 Octree top; |
|
182 rt.setTop(&top); |
|
183 |
|
184 rt.addlight(&light); |
|
185 light.castShadows(false); |
|
186 |
|
187 Material mat0a(Colour(0.7, 0.7, 0.7)); |
|
188 mat0a. setReflectivity(0.0); |
|
189 Box box(Vector3(-12.0, -1.2, -20.0), Vector3(12.0, -1.0, 0.0), &mat0a); |
|
190 rt.addshape(&box); |
|
191 |
|
192 Material mat0b(Colour(0.1, 0.7, 0.8)); |
|
193 mat0b.setReflectivity(0.7); |
|
194 Box box2(Vector3(-12.0, -1.2, -10.0), Vector3(12.0, 10.0, -10.2), &mat0b); |
|
195 rt.addshape(&box2); |
|
196 |
|
197 // spheres |
|
198 Material mat1(Colour(1.0, 1.0, 1.0)); |
|
199 mat1.texture = new PlanarMapTexture(Vector3(4.0, 2.0, -7.0)); |
|
200 Sphere sphere1(Vector3(4.0, 2.0, -7.0), 1.0, &mat1); |
|
201 rt.addshape(&sphere1); |
|
202 |
|
203 Material mat2(Colour(1.0, 1.0, 1.0)); |
|
204 mat2.texture = new CubicMapTexture(Vector3(1.0, 2.0, -7.0)); |
|
205 Sphere sphere2(Vector3(1.0, 2.0, -7.0), 1.0, &mat2); |
|
206 rt.addshape(&sphere2); |
|
207 |
|
208 Material mat3(Colour(1.0, 1.0, 1.0)); |
|
209 mat3.texture = new CylinderMapTexture(Vector3(-2.0, 2.0, -7.0)); |
|
210 Sphere sphere3(Vector3(-2.0, 2.0, -7.0), 1.0, &mat3); |
|
211 rt.addshape(&sphere3); |
|
212 |
|
213 Material mat4(Colour(1.0, 1.0, 1.0)); |
|
214 mat4.texture = new SphereMapTexture(Vector3(-5.0, 2.0, -7.0)); |
|
215 Sphere sphere4(Vector3(-5.0, 2.0, -7.0), 1.0, &mat4); |
|
216 rt.addshape(&sphere4); |
|
217 |
|
218 // cubes |
|
219 Material mat5(Colour(1.0, 1.0, 1.0)); |
|
220 mat5.texture = new PlanarMapTexture(Vector3(4.0, 0.0, -7.0)); |
|
221 Box cube1(Vector3(4.0, 0.0, -7.0)-1.0, Vector3(4.0, 0.0, -7.0)+1.0, &mat5); |
|
222 rt.addshape(&cube1); |
|
223 |
|
224 Material mat6(Colour(1.0, 1.0, 1.0)); |
|
225 mat6.texture = new CubicMapTexture(Vector3(1.0, 0.0, -7.0)); |
|
226 Box cube2(Vector3(1.0, 0.0, -7.0)-1.0, Vector3(1.0, 0.0, -7.0)+1.0, &mat6); |
|
227 rt.addshape(&cube2); |
|
228 |
|
229 Material mat7(Colour(1.0, 1.0, 1.0)); |
|
230 mat7.texture = new CylinderMapTexture(Vector3(-2.0, 0.0, -7.0)); |
|
231 Box cube3(Vector3(-2.0, 0.0, -7.0)-1.0, Vector3(-2.0, 0.0, -7.0)+1.0, &mat7); |
|
232 rt.addshape(&cube3); |
|
233 |
|
234 Material mat8(Colour(1.0, 1.0, 1.0)); |
|
235 mat8.texture = new SphereMapTexture(Vector3(-5.0, 0.0, -7.0)); |
|
236 Box cube4(Vector3(-5.0, 0.0, -7.0)-1.0, Vector3(-5.0, 0.0, -7.0)+1.0, &mat8); |
|
237 rt.addshape(&cube4); |
|
238 |
|
239 mat1.setReflectivity(0); |
|
240 mat2.setReflectivity(0); |
|
241 mat3.setReflectivity(0); |
|
242 mat4.setReflectivity(0); |
|
243 mat5.setReflectivity(0); |
|
244 mat6.setReflectivity(0); |
|
245 mat7.setReflectivity(0); |
|
246 mat8.setReflectivity(0); |
|
247 |
|
248 top.optimize(); |
|
249 |
|
250 cam.setEye(Vector3(-0.530505, 11.0964, 11.2208)); |
|
251 cam.p = Vector3(-4.18144e-08, -0.461779, -0.886995); |
|
252 cam.u = Vector3(-1, 0, 6.3393e-11); |
|
253 cam.v = Vector3(3.19387e-08, 0.886995, -0.461779); |
|
254 rt.setCamera(&cam); |
|
255 |
|
256 w = 1024; |
|
257 h = 600; |
|
258 |
|
259 /* run interactive mode */ |
|
260 loop_sdl(rt, cam, update_callback, key_callback); |
|
261 |
|
262 /* render image */ |
|
263 if (argc == 2 && !strcmp(argv[1], "-r")) |
|
264 { |
|
265 pyrit_verbosity = 2; |
|
266 Float *fdata = (Float *) malloc(w*h*3*sizeof(Float)); |
|
267 rt.setOversample(2); |
|
268 rt.setSubsample(1); |
|
269 rt.render(w, h, fdata); |
|
270 |
|
271 struct image *img; |
|
272 new_image(&img, w, h, 3); |
|
273 |
|
274 Float *fd = fdata; |
|
275 for (char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) { |
|
276 if (*fd > 1.0) |
|
277 *cd = 255; |
|
278 else |
|
279 *cd = (unsigned char)(*fd * 255.0); |
|
280 } |
|
281 free(fdata); |
|
282 save_png("textures.png", img); |
|
283 destroy_image(&img); |
|
284 } |
|
285 } |