author | Radek Brich <radek.brich@devl.cz> |
Sun, 27 Apr 2008 09:44:49 +0200 | |
branch | pyrit |
changeset 84 | 6f7fe14782c2 |
parent 81 | 9dbb9c8c115b |
child 88 | f7edb3b90816 |
permissions | -rw-r--r-- |
42 | 1 |
#include "raytracer.h" |
2 |
#include "octree.h" |
|
3 |
||
4 |
#include "image.h" |
|
5 |
#include "common_sdl.h" |
|
6 |
||
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
7 |
Camera cam(Vector3(0.,6.,6.), Vector3(0.,2.,-7.), Vector3(0.,0.,-1.)); |
42 | 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 |
||
81
9dbb9c8c115b
add 2D pixmap texture class
Radek Brich <radek.brich@devl.cz>
parents:
79
diff
changeset
|
12 |
void update_callback(Float*) |
42 | 13 |
{ |
14 |
if (lx != 0.0) |
|
15 |
light.pos.x += lx; |
|
16 |
if (ly != 0.0) |
|
17 |
light.pos.y += ly; |
|
18 |
if (lz != 0.0) |
|
19 |
light.pos.z += lz; |
|
20 |
if (cf != 0.0) |
|
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
21 |
cam.F += cf; |
42 | 22 |
} |
23 |
||
24 |
void key_callback(int key, int down) |
|
25 |
{ |
|
26 |
switch (key) |
|
27 |
{ |
|
28 |
case SDLK_r: |
|
29 |
lx = -0.1 * down; |
|
30 |
break; |
|
31 |
case SDLK_t: |
|
32 |
lx = +0.1 * down; |
|
33 |
break; |
|
34 |
case SDLK_f: |
|
35 |
ly = -0.1 * down; |
|
36 |
break; |
|
37 |
case SDLK_g: |
|
38 |
ly = +0.1 * down; |
|
39 |
break; |
|
40 |
case SDLK_v: |
|
41 |
lz = -0.1 * down; |
|
42 |
break; |
|
43 |
case SDLK_b: |
|
44 |
lz = +0.1 * down; |
|
45 |
break; |
|
46 |
||
47 |
case SDLK_z: |
|
48 |
cf = -0.02 * down; |
|
49 |
break; |
|
50 |
case SDLK_x: |
|
51 |
cf = +0.02 * down; |
|
52 |
break; |
|
53 |
} |
|
54 |
} |
|
55 |
||
56 |
int main(int argc, char **argv) |
|
57 |
{ |
|
58 |
Raytracer rt; |
|
59 |
||
60 |
Octree top; |
|
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
61 |
rt.setCamera(&cam); |
42 | 62 |
rt.setTop(&top); |
63 |
||
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
64 |
rt.addLight(&light); |
42 | 65 |
light.castShadows(false); |
66 |
||
67 |
Material mat0a(Colour(0.7, 0.7, 0.7)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
68 |
mat0a.setReflectivity(0.0); |
42 | 69 |
Box box(Vector3(-12.0, -1.2, -20.0), Vector3(12.0, -1.0, 0.0), &mat0a); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
70 |
rt.addShape(&box); |
42 | 71 |
|
72 |
Material mat0b(Colour(0.1, 0.7, 0.8)); |
|
73 |
mat0b.setReflectivity(0.7); |
|
74 |
Box box2(Vector3(-12.0, -1.2, -10.0), Vector3(12.0, 10.0, -10.2), &mat0b); |
|
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
75 |
rt.addShape(&box2); |
42 | 76 |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
77 |
Float bounds[] = {0.3, 0.6, 1.1}; |
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
78 |
Colour colours[] = {Colour(0,0,0), Colour(1,1,1), Colour(0,0,0)}; |
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
79 |
BoundColourMap cmap(bounds, colours); |
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
80 |
|
42 | 81 |
// spheres |
82 |
Material mat1(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
83 |
mat1.texture = new CheckersTexture(new PlanarMap(Vector3(-4.5, 2.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
84 |
Sphere sphere1(Vector3(-4.5, 2.0, -7.0), 1.0, &mat1); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
85 |
rt.addShape(&sphere1); |
42 | 86 |
|
87 |
Material mat2(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
88 |
mat2.texture = new CheckersTexture(new CubicMap(Vector3(-1.5, 2.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
89 |
Sphere sphere2(Vector3(-1.5, 2.0, -7.0), 1.0, &mat2); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
90 |
rt.addShape(&sphere2); |
42 | 91 |
|
92 |
Material mat3(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
93 |
mat3.texture = new CheckersTexture(new CylinderMap(Vector3(1.5, 2.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
94 |
Sphere sphere3(Vector3(1.5, 2.0, -7.0), 1.0, &mat3); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
95 |
rt.addShape(&sphere3); |
42 | 96 |
|
97 |
Material mat4(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
98 |
mat4.texture = new CheckersTexture(new SphereMap(Vector3(4.5, 2.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
99 |
Sphere sphere4(Vector3(4.5, 2.0, -7.0), 1.0, &mat4); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
100 |
rt.addShape(&sphere4); |
42 | 101 |
|
102 |
// cubes |
|
103 |
Material mat5(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
104 |
mat5.texture = new CheckersTexture(new PlanarMap(Vector3(-4.5, 0.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
105 |
Box cube1(Vector3(-4.5, 0.0, -7.0)-1.0, Vector3(-4.5, 0.0, -7.0)+1.0, &mat5); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
106 |
rt.addShape(&cube1); |
42 | 107 |
|
108 |
Material mat6(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
109 |
mat6.texture = new CheckersTexture(new CubicMap(Vector3(-1.5, 0.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
110 |
Box cube2(Vector3(-1.5, 0.0, -7.0)-1.0, Vector3(-1.5, 0.0, -7.0)+1.0, &mat6); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
111 |
rt.addShape(&cube2); |
42 | 112 |
|
113 |
Material mat7(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
114 |
mat7.texture = new CheckersTexture(new CylinderMap(Vector3(1.5, 0.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
115 |
Box cube3(Vector3(1.5, 0.0, -7.0)-1.0, Vector3(1.5, 0.0, -7.0)+1.0, &mat7); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
116 |
rt.addShape(&cube3); |
42 | 117 |
|
118 |
Material mat8(Colour(1.0, 1.0, 1.0)); |
|
79
062b1c4143f7
material and texture classes moved to material.(cc,h)
Radek Brich <radek.brich@devl.cz>
parents:
75
diff
changeset
|
119 |
mat8.texture = new CheckersTexture(new SphereMap(Vector3(4.5, 0.0, -7.0), 0.48), &cmap); |
72
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
120 |
Box cube4(Vector3(4.5, 0.0, -7.0)-1.0, Vector3(4.5, 0.0, -7.0)+1.0, &mat8); |
7c3f38dff082
kd-tree building - check all axes for best split, add additional shape-bbox check
Radek Brich <radek.brich@devl.cz>
parents:
60
diff
changeset
|
121 |
rt.addShape(&cube4); |
42 | 122 |
|
123 |
mat1.setReflectivity(0); |
|
124 |
mat2.setReflectivity(0); |
|
125 |
mat3.setReflectivity(0); |
|
126 |
mat4.setReflectivity(0); |
|
127 |
mat5.setReflectivity(0); |
|
128 |
mat6.setReflectivity(0); |
|
129 |
mat7.setReflectivity(0); |
|
130 |
mat8.setReflectivity(0); |
|
131 |
||
132 |
top.optimize(); |
|
133 |
||
134 |
w = 1024; |
|
135 |
h = 600; |
|
136 |
||
137 |
/* run interactive mode */ |
|
138 |
loop_sdl(rt, cam, update_callback, key_callback); |
|
139 |
||
140 |
/* render image */ |
|
141 |
if (argc == 2 && !strcmp(argv[1], "-r")) |
|
142 |
{ |
|
143 |
pyrit_verbosity = 2; |
|
144 |
Float *fdata = (Float *) malloc(w*h*3*sizeof(Float)); |
|
75
20dee9819b17
unify capitalization of method names in C++ and Python
Radek Brich <radek.brich@devl.cz>
parents:
72
diff
changeset
|
145 |
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
|
146 |
DefaultSampler sampler(fdata, w, h); |
48
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
147 |
sampler.setOversample(2); |
a4913301c626
begin moving subsampling and oversampling to Sampler
Radek Brich <radek.brich@devl.cz>
parents:
47
diff
changeset
|
148 |
sampler.setSubsample(1); |
47
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
43
diff
changeset
|
149 |
rt.setSampler(&sampler); |
320d5d466864
move Sampler classes to sampler.cc
Radek Brich <radek.brich@devl.cz>
parents:
43
diff
changeset
|
150 |
rt.render(); |
42 | 151 |
|
152 |
struct image *img; |
|
153 |
new_image(&img, w, h, 3); |
|
154 |
||
155 |
Float *fd = fdata; |
|
60
a23b5089b9c3
moving to SCons build system
Radek Brich <radek.brich@devl.cz>
parents:
48
diff
changeset
|
156 |
for (unsigned char *cd = img->data; cd != img->data + w*h*3; cd++, fd++) { |
42 | 157 |
if (*fd > 1.0) |
158 |
*cd = 255; |
|
159 |
else |
|
160 |
*cd = (unsigned char)(*fd * 255.0); |
|
161 |
} |
|
162 |
free(fdata); |
|
163 |
save_png("textures.png", img); |
|
164 |
destroy_image(&img); |
|
165 |
} |
|
166 |
} |