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