author | Radek Brich <radek.brich@devl.cz> |
Fri, 14 Dec 2007 10:34:31 +0100 | |
branch | pyrit |
changeset 38 | 5d043eeb09d9 |
parent 37 | 5f954c0d34fc |
child 39 | 7079dcc3bd74 |
permissions | -rw-r--r-- |
29 | 1 |
#include <SDL.h> |
2 |
||
3 |
#include "raytracer.h" |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
4 |
#include "octree.h" |
29 | 5 |
#include <iostream> |
6 |
#include <fstream> |
|
7 |
#include <iomanip> |
|
8 |
||
38
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
9 |
int w = 640; |
5d043eeb09d9
realtime_dragon demo: now fullsize model + octree
Radek Brich <radek.brich@devl.cz>
parents:
37
diff
changeset
|
10 |
int h = 400; |
29 | 11 |
Float *render_buffer; |
12 |
||
13 |
Raytracer rt; |
|
14 |
Camera cam; |
|
15 |
||
37
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
16 |
Uint32 fp10s_acc = 0; |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
17 |
Uint32 fp10s_acc_samples = 0; |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
18 |
|
29 | 19 |
void load_ply(const char *filename, Material *mat, Float scale) |
20 |
{ |
|
21 |
vector<NormalVertex*> vertices; |
|
22 |
vector<Vector3> normals; |
|
23 |
vector<int> vertex_face_num; |
|
24 |
ifstream f(filename); |
|
25 |
string token = "a"; |
|
26 |
if (!f.is_open()) |
|
27 |
{ |
|
28 |
cout << "File not found: " << filename <<endl; |
|
29 |
exit(1); |
|
30 |
} |
|
31 |
// read header |
|
32 |
int vertex_num, face_num; |
|
33 |
while (token != "end_header") |
|
34 |
{ |
|
35 |
f >> token; |
|
36 |
if (token == "element") |
|
37 |
{ |
|
38 |
f >> token; |
|
39 |
if (token == "vertex") |
|
40 |
f >> vertex_num; |
|
41 |
if (token == "face") |
|
42 |
f >> face_num; |
|
43 |
} |
|
44 |
f.ignore(1000,'\n'); |
|
45 |
} |
|
46 |
||
47 |
// read vertices |
|
48 |
Vector3 P; |
|
49 |
int num = vertex_num; |
|
50 |
while (num--) |
|
51 |
{ |
|
52 |
f >> P.x >> P.y >> P.z; |
|
53 |
P.x = -scale*P.x - 1.0; |
|
54 |
P.y = scale*P.y - 3.0; |
|
55 |
P.z = scale*P.z; |
|
56 |
vertices.push_back(new NormalVertex(P)); |
|
57 |
normals.push_back(Vector3()); |
|
58 |
vertex_face_num.push_back(0); |
|
59 |
f.ignore(1000,'\n'); |
|
60 |
} |
|
61 |
||
62 |
// read faces |
|
63 |
Triangle *face; |
|
64 |
int v1, v2, v3; |
|
65 |
while (face_num--) |
|
66 |
{ |
|
67 |
f >> num; |
|
68 |
if (num != 3) |
|
69 |
{ |
|
70 |
printf("ply error: faces of %d vertices not supported", num); |
|
71 |
continue; |
|
72 |
} |
|
73 |
f >> v1 >> v2 >> v3; |
|
74 |
face = new Triangle(vertices.at(v1), vertices.at(v3), vertices.at(v2), mat); |
|
75 |
rt.addshape(face); |
|
76 |
face->setSmooth(); |
|
77 |
||
78 |
normals.at(v1) += face->getNormal(); |
|
79 |
vertex_face_num.at(v1)++; |
|
80 |
normals.at(v2) += face->getNormal(); |
|
81 |
vertex_face_num.at(v2)++; |
|
82 |
normals.at(v3) += face->getNormal(); |
|
83 |
vertex_face_num.at(v3)++; |
|
84 |
f.ignore(1000,'\n'); |
|
85 |
} |
|
86 |
||
37
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
87 |
for (int i = 0; i < vertex_num; i++) |
29 | 88 |
{ |
89 |
normals.at(i) /= vertex_face_num.at(i); |
|
90 |
normals.at(i).normalize(); |
|
91 |
vertices.at(i)->N = normals.at(i); |
|
92 |
} |
|
93 |
||
94 |
f.close(); |
|
95 |
} |
|
96 |
||
97 |
void update(SDL_Surface *screen) |
|
98 |
{ |
|
37
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
99 |
static Uint32 t = 0; |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
100 |
Uint32 tnow = SDL_GetTicks(); |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
101 |
int fp10s = 10000/(int)(tnow - t); |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
102 |
if (t != 0) |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
103 |
{ |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
104 |
fp10s_acc += fp10s; |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
105 |
++fp10s_acc_samples; |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
106 |
} |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
107 |
t = tnow; |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
108 |
printf("\b\b\b\b\b\b\b\b\b%3d.%1d fps", fp10s/10, fp10s%10); |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
109 |
fflush(stdout); |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
110 |
|
29 | 111 |
rt.render(w, h, render_buffer); |
112 |
||
113 |
if (SDL_MUSTLOCK(screen)) |
|
114 |
if (SDL_LockSurface(screen) < 0) |
|
115 |
return; |
|
116 |
||
117 |
Uint32 *bufp = (Uint32 *)screen->pixels; |
|
118 |
unsigned char c[3]; |
|
119 |
for (Float *fd = render_buffer; fd != render_buffer + w*h*3; fd += 3) |
|
120 |
{ |
|
121 |
for (int i = 0; i < 3; i++) |
|
122 |
{ |
|
123 |
if (fd[i] > 1.0) |
|
124 |
c[i] = 255; |
|
125 |
else |
|
126 |
c[i] = (unsigned char)(fd[i] * 255.0); |
|
127 |
} |
|
128 |
*bufp = SDL_MapRGB(screen->format, c[0], c[1], c[2]); |
|
129 |
bufp++; |
|
130 |
} |
|
131 |
||
132 |
if (SDL_MUSTLOCK(screen)) |
|
133 |
SDL_UnlockSurface(screen); |
|
134 |
||
135 |
if (screen->flags & SDL_DOUBLEBUF) |
|
136 |
SDL_Flip(screen); |
|
137 |
else |
|
138 |
SDL_UpdateRect(screen, 0, 0, w, h); |
|
139 |
} |
|
140 |
||
37
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
141 |
void quit() |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
142 |
{ |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
143 |
Uint32 fp100s_aver = fp10s_acc*10/fp10s_acc_samples; |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
144 |
printf("\naverlage fps: %3d.%2d\n", fp100s_aver/100, fp100s_aver%100); |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
145 |
SDL_Quit(); |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
146 |
} |
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
147 |
|
29 | 148 |
int main() |
149 |
{ |
|
150 |
/* initialize SDL */ |
|
151 |
SDL_Surface *screen; |
|
152 |
||
153 |
if( SDL_Init(SDL_INIT_VIDEO) < 0 ) { |
|
154 |
fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
|
155 |
exit(1); |
|
156 |
} |
|
157 |
||
37
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
158 |
atexit(quit); |
29 | 159 |
|
160 |
screen = SDL_SetVideoMode(w, h, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
|
161 |
if ( screen == NULL ) { |
|
162 |
fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); |
|
163 |
exit(1); |
|
164 |
} |
|
165 |
||
166 |
/* initialize raytracer and prepare scene */ |
|
37
5f954c0d34fc
octree traversal rewritten to avoid recursion
Radek Brich <radek.brich@devl.cz>
parents:
36
diff
changeset
|
167 |
pyrit_verbosity = 0; |
29 | 168 |
render_buffer = (Float *) malloc(w*h*3*sizeof(Float)); |
169 |
||
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
170 |
rt.setThreads(2); |
36
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
171 |
rt.setMaxDepth(0); |
29 | 172 |
|
35
fb170fccb19f
new space partitioning structure: octree
Radek Brich <radek.brich@devl.cz>
parents:
29
diff
changeset
|
173 |
Octree top; |
29 | 174 |
rt.setTop(&top); |
175 |
||
176 |
Light light1(Vector3(-5.0, 2.0, 8.0), Colour(0.9, 0.3, 0.6)); |
|
177 |
light1.castShadows(false); |
|
178 |
rt.addlight(&light1); |
|
179 |
||
180 |
//Light light2(Vector3(-2.0, 10.0, 2.0), Colour(0.4, 0.6, 0.3)); |
|
181 |
//light2.castShadows(false); |
|
182 |
//rt.addlight(&light2); |
|
183 |
||
184 |
Material mat(Colour(0.9, 0.9, 0.9)); |
|
36
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
185 |
load_ply("../models/bunny/bun_zipper.ply", &mat, 29); |
29 | 186 |
|
187 |
rt.setCamera(&cam); |
|
188 |
cam.setEye(Vector3(0,0,10)); |
|
189 |
||
190 |
/* build kd-tree */ |
|
36
b490093b0ac3
new virtual Shape::intersect_bbox
Radek Brich <radek.brich@devl.cz>
parents:
35
diff
changeset
|
191 |
top.setMaxDepth(8); |
29 | 192 |
top.optimize(); |
193 |
||
194 |
/* loop... */ |
|
195 |
SDL_Event event; |
|
196 |
bool quit = false; |
|
197 |
Float roty = 0.0, rotx = 0.0, move = 0.0; |
|
198 |
while (!quit) |
|
199 |
{ |
|
200 |
while (SDL_PollEvent(&event)) |
|
201 |
{ |
|
202 |
switch (event.type) { |
|
203 |
case SDL_VIDEORESIZE: |
|
204 |
w = event.resize.w; |
|
205 |
h = event.resize.h; |
|
206 |
render_buffer = (Float *) realloc(render_buffer, w*h*3*sizeof(Float)); |
|
207 |
screen = SDL_SetVideoMode(w, h, 32, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE); |
|
208 |
break; |
|
209 |
case SDL_KEYDOWN: |
|
210 |
if (event.key.keysym.sym == SDLK_ESCAPE) { |
|
211 |
quit = true; |
|
212 |
break; |
|
213 |
} |
|
214 |
if (event.key.keysym.sym == SDLK_LEFT) { |
|
215 |
roty = -0.01; |
|
216 |
break; |
|
217 |
} |
|
218 |
if (event.key.keysym.sym == SDLK_RIGHT) { |
|
219 |
roty = +0.01; |
|
220 |
break; |
|
221 |
} |
|
222 |
if (event.key.keysym.sym == SDLK_DOWN) { |
|
223 |
rotx = +0.01; |
|
224 |
break; |
|
225 |
} |
|
226 |
if (event.key.keysym.sym == SDLK_UP) { |
|
227 |
rotx = -0.01; |
|
228 |
break; |
|
229 |
} |
|
230 |
if (event.key.keysym.sym == SDLK_w) { |
|
231 |
move = +0.5; |
|
232 |
break; |
|
233 |
} |
|
234 |
if (event.key.keysym.sym == SDLK_s) { |
|
235 |
move = -0.5; |
|
236 |
break; |
|
237 |
} |
|
238 |
break; |
|
239 |
case SDL_KEYUP: |
|
240 |
if (event.key.keysym.sym == SDLK_LEFT || event.key.keysym.sym == SDLK_RIGHT) { |
|
241 |
roty = 0.0; |
|
242 |
break; |
|
243 |
} |
|
244 |
if (event.key.keysym.sym == SDLK_UP || event.key.keysym.sym == SDLK_DOWN) { |
|
245 |
rotx = 0.0; |
|
246 |
break; |
|
247 |
} |
|
248 |
if (event.key.keysym.sym == SDLK_w || event.key.keysym.sym == SDLK_s) { |
|
249 |
move = 0.0; |
|
250 |
break; |
|
251 |
} |
|
252 |
break; |
|
253 |
case SDL_QUIT: |
|
254 |
quit = true; |
|
255 |
} |
|
256 |
} |
|
257 |
cam.rotate(Quaternion(cos(roty),0,sin(roty),0).normalize()); |
|
258 |
cam.rotate(Quaternion(cos(rotx),cam.u[0]*sin(rotx),0,cam.u[2]*sin(rotx)).normalize()); |
|
259 |
cam.u.y = 0; |
|
260 |
cam.u.normalize(); |
|
261 |
cam.move(move,0,0); |
|
262 |
update(screen); |
|
263 |
} |
|
264 |
||
265 |
free(render_buffer); |
|
266 |
} |