rev |
line source |
nuclear@19
|
1 #include <stdio.h>
|
nuclear@19
|
2 #include <stdlib.h>
|
nuclear@19
|
3 #include <string.h>
|
nuclear@4
|
4 #include "scene.h"
|
nuclear@19
|
5 #include "snode.h"
|
nuclear@19
|
6 #include "object.h"
|
nuclear@19
|
7 #include "geomobj.h"
|
nuclear@4
|
8
|
nuclear@8
|
9 // default enviromental parameters
|
nuclear@8
|
10 Environment::Environment()
|
nuclear@19
|
11 : bgcolor(0.01, 0.01, 0.01), ambient(0.01, 0.01, 0.01)
|
nuclear@8
|
12 {
|
nuclear@8
|
13 }
|
nuclear@8
|
14
|
nuclear@4
|
15 Scene::Scene()
|
nuclear@4
|
16 {
|
nuclear@4
|
17 active_cam = 0;
|
nuclear@4
|
18 root = new SceneNode;
|
nuclear@4
|
19 }
|
nuclear@4
|
20
|
nuclear@4
|
21 Scene::~Scene()
|
nuclear@4
|
22 {
|
nuclear@4
|
23 for(auto obj : objects) {
|
nuclear@4
|
24 delete obj;
|
nuclear@4
|
25 }
|
nuclear@4
|
26 for(auto node : nodes) {
|
nuclear@4
|
27 delete node;
|
nuclear@4
|
28 }
|
nuclear@4
|
29 delete root;
|
nuclear@4
|
30 }
|
nuclear@4
|
31
|
nuclear@8
|
32 void Scene::set_env(const Environment &env)
|
nuclear@8
|
33 {
|
nuclear@8
|
34 this->env = env;
|
nuclear@8
|
35 }
|
nuclear@8
|
36
|
nuclear@8
|
37 Environment &Scene::get_env()
|
nuclear@8
|
38 {
|
nuclear@8
|
39 return env;
|
nuclear@8
|
40 }
|
nuclear@8
|
41
|
nuclear@8
|
42 const Environment &Scene::get_env() const
|
nuclear@8
|
43 {
|
nuclear@8
|
44 return env;
|
nuclear@8
|
45 }
|
nuclear@8
|
46
|
nuclear@8
|
47 Color Scene::get_env_color() const
|
nuclear@8
|
48 {
|
nuclear@8
|
49 return env.bgcolor;
|
nuclear@8
|
50 }
|
nuclear@8
|
51
|
nuclear@8
|
52 Color Scene::get_env_color(const Ray &ray) const
|
nuclear@8
|
53 {
|
nuclear@8
|
54 // TODO
|
nuclear@8
|
55 return get_env_color();
|
nuclear@8
|
56 }
|
nuclear@8
|
57
|
nuclear@4
|
58 void Scene::add_object(Object *obj)
|
nuclear@4
|
59 {
|
nuclear@4
|
60 objects.push_back(obj);
|
nuclear@4
|
61 }
|
nuclear@4
|
62
|
nuclear@4
|
63 int Scene::get_object_count() const
|
nuclear@4
|
64 {
|
nuclear@4
|
65 return (int)objects.size();
|
nuclear@4
|
66 }
|
nuclear@4
|
67
|
nuclear@4
|
68 Object *Scene::get_object(int idx) const
|
nuclear@4
|
69 {
|
nuclear@4
|
70 return objects[idx];
|
nuclear@4
|
71 }
|
nuclear@4
|
72
|
nuclear@4
|
73 void Scene::add_node(SceneNode *node)
|
nuclear@4
|
74 {
|
nuclear@4
|
75 nodes.push_back(node);
|
nuclear@4
|
76
|
nuclear@4
|
77 if(!node->get_parent()) {
|
nuclear@4
|
78 root->add_child(node);
|
nuclear@4
|
79 }
|
nuclear@4
|
80 }
|
nuclear@4
|
81
|
nuclear@4
|
82 int Scene::get_node_count() const
|
nuclear@4
|
83 {
|
nuclear@4
|
84 return (int)nodes.size();
|
nuclear@4
|
85 }
|
nuclear@4
|
86
|
nuclear@4
|
87 SceneNode *Scene::get_node(int idx) const
|
nuclear@4
|
88 {
|
nuclear@4
|
89 return nodes[idx];
|
nuclear@4
|
90 }
|
nuclear@4
|
91
|
nuclear@4
|
92 void Scene::use_camera(Camera *cam)
|
nuclear@4
|
93 {
|
nuclear@4
|
94 active_cam = cam;
|
nuclear@4
|
95 }
|
nuclear@4
|
96
|
nuclear@4
|
97 Camera *Scene::get_active_camera() const
|
nuclear@4
|
98 {
|
nuclear@4
|
99 return active_cam;
|
nuclear@4
|
100 }
|
nuclear@4
|
101
|
nuclear@4
|
102 void Scene::update(long msec)
|
nuclear@4
|
103 {
|
nuclear@4
|
104 root->update(msec);
|
nuclear@4
|
105 }
|
nuclear@4
|
106
|
nuclear@4
|
107 bool Scene::intersect(const Ray &ray, RayHit *hit) const
|
nuclear@4
|
108 {
|
nuclear@4
|
109 return root->intersect(ray, hit);
|
nuclear@4
|
110 }
|
nuclear@4
|
111
|
nuclear@4
|
112 bool Scene::load(const char *fname)
|
nuclear@4
|
113 {
|
nuclear@19
|
114 FILE *fp = fopen(fname, "r");
|
nuclear@19
|
115 if(!fp) {
|
nuclear@19
|
116 fprintf(stderr, "failed to open scene file: %s\n", fname);
|
nuclear@19
|
117 return false;
|
nuclear@19
|
118 }
|
nuclear@19
|
119
|
nuclear@19
|
120 bool res = load(fp);
|
nuclear@19
|
121 fclose(fp);
|
nuclear@19
|
122 return res;
|
nuclear@4
|
123 }
|
nuclear@19
|
124
|
nuclear@19
|
125 static char *strip_wspace(char *str)
|
nuclear@19
|
126 {
|
nuclear@19
|
127 while(*str && isspace(*str)) str++;
|
nuclear@23
|
128
|
nuclear@23
|
129 char *comment = strchr(str, '#');
|
nuclear@23
|
130 if(comment) *comment = 0;
|
nuclear@23
|
131
|
nuclear@23
|
132 if(str) {
|
nuclear@23
|
133 char *end = str + strlen(str) - 1;
|
nuclear@23
|
134 while(end > str && isspace(*end)) end--;
|
nuclear@23
|
135 end[1] = 0;
|
nuclear@23
|
136 }
|
nuclear@23
|
137
|
nuclear@19
|
138 return str;
|
nuclear@19
|
139 }
|
nuclear@19
|
140
|
nuclear@19
|
141 bool Scene::load(FILE *fp)
|
nuclear@19
|
142 {
|
nuclear@28
|
143 static char buf[4096];
|
nuclear@28
|
144 int offset = 0;
|
nuclear@19
|
145
|
nuclear@28
|
146 while(fgets(buf + offset, sizeof buf - offset, fp)) {
|
nuclear@28
|
147 // handle line continuations
|
nuclear@28
|
148 int len = strlen(buf);
|
nuclear@28
|
149 if(strcmp(buf + len - 2, "\\\n") == 0 || strcmp(buf + len - 2, "\\\r") == 0) {
|
nuclear@28
|
150 offset = len - 2;
|
nuclear@28
|
151 continue;
|
nuclear@28
|
152 }
|
nuclear@28
|
153 if(strcmp(buf + len - 3, "\\\r\n") == 0) {
|
nuclear@28
|
154 offset = len - 3;
|
nuclear@28
|
155 continue;
|
nuclear@28
|
156 }
|
nuclear@28
|
157 offset = 0;
|
nuclear@28
|
158
|
nuclear@19
|
159 char *line = strip_wspace(buf);
|
nuclear@19
|
160 if(!line || !*line || *line == '#') {
|
nuclear@19
|
161 continue;
|
nuclear@19
|
162 }
|
nuclear@19
|
163
|
nuclear@19
|
164 std::vector<char*> cmd;
|
nuclear@19
|
165
|
nuclear@19
|
166 char *tok = 0;
|
nuclear@19
|
167 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
|
nuclear@19
|
168 cmd.push_back(tok);
|
nuclear@19
|
169 }
|
nuclear@19
|
170 cmd.push_back(0);
|
nuclear@19
|
171
|
nuclear@19
|
172
|
nuclear@19
|
173 if(!proc_cmd((int)cmd.size() - 1, &cmd[0])) {
|
nuclear@19
|
174 return false;
|
nuclear@19
|
175 }
|
nuclear@19
|
176 }
|
nuclear@19
|
177
|
nuclear@19
|
178 return true;
|
nuclear@19
|
179 }
|
nuclear@19
|
180
|
nuclear@19
|
181 static int parse_vec(char **argv, float *vec)
|
nuclear@19
|
182 {
|
nuclear@19
|
183 int nelem = 0;
|
nuclear@19
|
184 char *endp;
|
nuclear@19
|
185
|
nuclear@19
|
186 for(int i=0; i<4; i++) {
|
nuclear@19
|
187 if(!argv[i]) break;
|
nuclear@19
|
188
|
nuclear@19
|
189 vec[nelem] = strtod(argv[i], &endp);
|
nuclear@19
|
190 if(endp == argv[i]) {
|
nuclear@19
|
191 break;
|
nuclear@19
|
192 }
|
nuclear@19
|
193 nelem++;
|
nuclear@19
|
194 }
|
nuclear@19
|
195 return nelem;
|
nuclear@19
|
196 }
|
nuclear@19
|
197
|
nuclear@22
|
198 static int parse_brdf(char **argv, Reflectance **brdf_ptr)
|
nuclear@22
|
199 {
|
nuclear@22
|
200 CompositeRefl *composite = 0;
|
nuclear@22
|
201 Reflectance *brdf = 0;
|
nuclear@22
|
202 float weight = 1.0;
|
nuclear@22
|
203 int i;
|
nuclear@22
|
204
|
nuclear@22
|
205 for(i=0; argv[i]; i++) {
|
nuclear@22
|
206 if(strcmp(argv[i], "-brdf") == 0) {
|
nuclear@22
|
207 if(strcmp(argv[++i], "lambert") == 0) {
|
nuclear@22
|
208 if(composite && brdf) {
|
nuclear@22
|
209 composite->add_brdf(brdf, weight);
|
nuclear@22
|
210 }
|
nuclear@22
|
211 brdf = new LambertRefl;
|
nuclear@22
|
212 if(!composite) break;
|
nuclear@22
|
213
|
nuclear@28
|
214 } else if(strcmp(argv[i], "phong") == 0) {
|
nuclear@28
|
215 if(composite && brdf) {
|
nuclear@28
|
216 composite->add_brdf(brdf, weight);
|
nuclear@28
|
217 }
|
nuclear@28
|
218 brdf = new PhongRefl;
|
nuclear@28
|
219 if(!composite) break;
|
nuclear@28
|
220
|
nuclear@22
|
221 } else if(strcmp(argv[i], "mirror") == 0) {
|
nuclear@22
|
222 if(composite && brdf) {
|
nuclear@22
|
223 composite->add_brdf(brdf, weight);
|
nuclear@22
|
224 }
|
nuclear@22
|
225 brdf = new MirrorRefl;
|
nuclear@22
|
226 if(!composite) break;
|
nuclear@22
|
227
|
nuclear@22
|
228 } else if(strcmp(argv[i], "composite") == 0) {
|
nuclear@22
|
229 if(composite) {
|
nuclear@22
|
230 fprintf(stderr, "can't have nested composite BRDFs\n");
|
nuclear@22
|
231 return -1;
|
nuclear@22
|
232 }
|
nuclear@22
|
233 composite = new CompositeRefl;
|
nuclear@22
|
234
|
nuclear@22
|
235 } else {
|
nuclear@22
|
236 --i;
|
nuclear@22
|
237 break;
|
nuclear@22
|
238 }
|
nuclear@22
|
239
|
nuclear@22
|
240 } else if(strcmp(argv[i], "-brdf-weight") == 0) {
|
nuclear@22
|
241 if(!composite) {
|
nuclear@22
|
242 fprintf(stderr, "-brdf-weight is invalid without a composite brdf\n");
|
nuclear@22
|
243 return -1;
|
nuclear@22
|
244 }
|
nuclear@22
|
245 char *endp;
|
nuclear@22
|
246 weight = strtod(argv[++i], &endp);
|
nuclear@22
|
247 if(endp == argv[i]) {
|
nuclear@22
|
248 fprintf(stderr, "-brdf-weight must be followed by a number\n");
|
nuclear@22
|
249 return -1;
|
nuclear@22
|
250 }
|
nuclear@22
|
251 } else {
|
nuclear@22
|
252 --i;
|
nuclear@22
|
253 break;
|
nuclear@22
|
254 }
|
nuclear@22
|
255 }
|
nuclear@22
|
256
|
nuclear@22
|
257 if(composite && brdf) {
|
nuclear@22
|
258 composite->add_brdf(brdf, weight);
|
nuclear@22
|
259 *brdf_ptr = composite;
|
nuclear@22
|
260 } else {
|
nuclear@22
|
261 *brdf_ptr = brdf;
|
nuclear@22
|
262 }
|
nuclear@22
|
263 return i;
|
nuclear@22
|
264 }
|
nuclear@22
|
265
|
nuclear@19
|
266 static SceneNode *proc_object(Object *obj, int argc, char **argv)
|
nuclear@19
|
267 {
|
nuclear@19
|
268 float vec[4];
|
nuclear@19
|
269 SceneNode *node = new SceneNode(obj);
|
nuclear@19
|
270 GeomObject *gobj = (GeomObject*)obj;
|
nuclear@19
|
271
|
nuclear@19
|
272 for(int i=1; i<argc; i++) {
|
nuclear@19
|
273 if(strcmp(argv[i], "-position") == 0) {
|
nuclear@19
|
274 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
275 if(nelem < 3) {
|
nuclear@19
|
276 fprintf(stderr, "-position must be followed by 3 numbers\n");
|
nuclear@19
|
277 goto err;
|
nuclear@19
|
278 }
|
nuclear@19
|
279 node->set_position(Vector3(vec[0], vec[1], vec[2]));
|
nuclear@19
|
280 i += 3;
|
nuclear@19
|
281
|
nuclear@19
|
282 } else if(strcmp(argv[i], "-rotation") == 0) {
|
nuclear@19
|
283 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
284 if(nelem < 4) {
|
nuclear@19
|
285 fprintf(stderr, "-rotation must be followed by axis vector and angle\n");
|
nuclear@19
|
286 goto err;
|
nuclear@19
|
287 }
|
nuclear@19
|
288 node->set_rotation(Quaternion(Vector3(vec[0], vec[1], vec[2]), vec[3]));
|
nuclear@19
|
289 i += 4;
|
nuclear@19
|
290
|
nuclear@19
|
291 } else if(strcmp(argv[i], "-scaling") == 0) {
|
nuclear@19
|
292 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
293 Vector3 s;
|
nuclear@19
|
294 if(nelem == 1) {
|
nuclear@19
|
295 s.x = s.y = s.z = vec[0];
|
nuclear@19
|
296 } else if(nelem == 3) {
|
nuclear@19
|
297 s = Vector3(vec[0], vec[1], vec[2]);
|
nuclear@19
|
298 } else {
|
nuclear@19
|
299 fprintf(stderr, "-scaling must be followed by 1 or 3 numbers\n");
|
nuclear@19
|
300 goto err;
|
nuclear@19
|
301 }
|
nuclear@19
|
302 node->set_scaling(s);
|
nuclear@19
|
303 i += nelem;
|
nuclear@19
|
304
|
nuclear@19
|
305 } else if(strcmp(argv[i], "-diffuse") == 0) {
|
nuclear@19
|
306 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
307 if(nelem < 3) {
|
nuclear@19
|
308 fprintf(stderr, "-diffuse must be followed by a color (r g b)\n");
|
nuclear@19
|
309 goto err;
|
nuclear@19
|
310 }
|
nuclear@19
|
311 gobj->mtl.set_attrib("diffuse", Color(vec[0], vec[1], vec[2]));
|
nuclear@19
|
312 i += 3;
|
nuclear@19
|
313
|
nuclear@19
|
314 } else if(strcmp(argv[i], "-specular") == 0) {
|
nuclear@19
|
315 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
316 if(nelem < 3) {
|
nuclear@19
|
317 fprintf(stderr, "-specular must be followed by a color\n");
|
nuclear@19
|
318 goto err;
|
nuclear@19
|
319 }
|
nuclear@19
|
320 gobj->mtl.set_attrib("specular", Color(vec[0], vec[1], vec[2]));
|
nuclear@19
|
321 i += 3;
|
nuclear@19
|
322
|
nuclear@19
|
323 } else if(strcmp(argv[i], "-emissive") == 0) {
|
nuclear@19
|
324 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
325 Color emissive;
|
nuclear@19
|
326 if(nelem == 1) {
|
nuclear@19
|
327 emissive = Vector3(1, 1, 1);
|
nuclear@19
|
328 } else if(nelem == 3) {
|
nuclear@19
|
329 emissive = Vector3(vec[0], vec[1], vec[2]);
|
nuclear@19
|
330 } else {
|
nuclear@19
|
331 fprintf(stderr, "-emissive must be followed by an intensity or a color\n");
|
nuclear@19
|
332 goto err;
|
nuclear@19
|
333 }
|
nuclear@19
|
334 i += nelem;
|
nuclear@19
|
335 gobj->mtl.set_attrib("emissive", emissive);
|
nuclear@19
|
336
|
nuclear@22
|
337 } else if(strcmp(argv[i], "-brdf") == 0) {
|
nuclear@22
|
338 int nelem = parse_brdf(argv + i, &gobj->brdf);
|
nuclear@22
|
339 if(nelem == -1) {
|
nuclear@22
|
340 goto err;
|
nuclear@22
|
341 }
|
nuclear@22
|
342 i += nelem;
|
nuclear@22
|
343
|
nuclear@19
|
344 } else {
|
nuclear@19
|
345 fprintf(stderr, "unrecognized %s option: %s\n", argv[0], argv[i]);
|
nuclear@19
|
346 goto err;
|
nuclear@19
|
347 }
|
nuclear@19
|
348 }
|
nuclear@19
|
349
|
nuclear@19
|
350 return node;
|
nuclear@19
|
351 err:
|
nuclear@19
|
352 delete node;
|
nuclear@19
|
353 return 0;
|
nuclear@19
|
354 }
|
nuclear@19
|
355
|
nuclear@19
|
356 static Camera *proc_camera(int argc, char **argv)
|
nuclear@19
|
357 {
|
nuclear@19
|
358 float vec[4];
|
nuclear@19
|
359 TargetCamera *cam = new TargetCamera;
|
nuclear@19
|
360
|
nuclear@19
|
361 for(int i=1; i<argc; i++) {
|
nuclear@19
|
362 if(strcmp(argv[i], "-position") == 0) {
|
nuclear@19
|
363 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
364 if(nelem < 3) {
|
nuclear@19
|
365 fprintf(stderr, "-position must be followed by 3 numbers\n");
|
nuclear@19
|
366 goto err;
|
nuclear@19
|
367 }
|
nuclear@19
|
368 cam->set_position(Vector3(vec[0], vec[1], vec[2]));
|
nuclear@19
|
369 i += 3;
|
nuclear@19
|
370
|
nuclear@19
|
371 } else if(strcmp(argv[i], "-target") == 0) {
|
nuclear@19
|
372 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
373 if(nelem < 3) {
|
nuclear@19
|
374 fprintf(stderr, "-target must be followed by 3 numbers\n");
|
nuclear@19
|
375 goto err;
|
nuclear@19
|
376 }
|
nuclear@19
|
377 cam->set_target(Vector3(vec[0], vec[1], vec[2]));
|
nuclear@19
|
378 i += 3;
|
nuclear@19
|
379
|
nuclear@19
|
380 } else if(strcmp(argv[i], "-fov") == 0) {
|
nuclear@19
|
381 int nelem = parse_vec(argv + i + 1, vec);
|
nuclear@19
|
382 if(nelem < 1) {
|
nuclear@19
|
383 fprintf(stderr, "-fov must be followed by the field of view in degrees\n");
|
nuclear@19
|
384 goto err;
|
nuclear@19
|
385 }
|
nuclear@19
|
386 cam->set_fov(vec[0]);
|
nuclear@19
|
387 i += 1;
|
nuclear@19
|
388
|
nuclear@19
|
389 } else {
|
nuclear@19
|
390 fprintf(stderr, "unrecognized camera option: %s\n", argv[i]);
|
nuclear@19
|
391 goto err;
|
nuclear@19
|
392 }
|
nuclear@19
|
393 }
|
nuclear@19
|
394 return cam;
|
nuclear@19
|
395
|
nuclear@19
|
396 err:
|
nuclear@19
|
397 delete cam;
|
nuclear@19
|
398 return 0;
|
nuclear@19
|
399 }
|
nuclear@19
|
400
|
nuclear@19
|
401 bool Scene::proc_cmd(int argc, char **argv)
|
nuclear@19
|
402 {
|
nuclear@19
|
403 Object *obj = 0;
|
nuclear@19
|
404 SceneNode *node = 0;
|
nuclear@19
|
405
|
nuclear@19
|
406 if(strcmp(argv[0], "sphere") == 0) {
|
nuclear@19
|
407 obj = new Sphere;
|
nuclear@19
|
408 node = proc_object(obj, argc, argv);
|
nuclear@19
|
409
|
nuclear@19
|
410 } else if(strcmp(argv[0], "box") == 0) {
|
nuclear@19
|
411 obj = new Box;
|
nuclear@19
|
412 node = proc_object(obj, argc, argv);
|
nuclear@19
|
413
|
nuclear@19
|
414 } else if(strcmp(argv[0], "mesh") == 0) {
|
nuclear@19
|
415 fprintf(stderr, "meshes not implemented yet\n");
|
nuclear@19
|
416 return false;
|
nuclear@19
|
417
|
nuclear@19
|
418 } else if(strcmp(argv[0], "camera") == 0) {
|
nuclear@19
|
419 Camera *cam = proc_camera(argc, argv);
|
nuclear@19
|
420 if(!cam) {
|
nuclear@19
|
421 return false;
|
nuclear@19
|
422 }
|
nuclear@19
|
423 use_camera(cam);
|
nuclear@19
|
424
|
nuclear@19
|
425 } else {
|
nuclear@19
|
426 fprintf(stderr, "unknown command: %s\n", argv[0]);
|
nuclear@19
|
427 return false;
|
nuclear@19
|
428 }
|
nuclear@19
|
429
|
nuclear@19
|
430 if(obj) {
|
nuclear@19
|
431 if(!node) {
|
nuclear@19
|
432 return false;
|
nuclear@19
|
433 }
|
nuclear@19
|
434 add_object(obj);
|
nuclear@19
|
435 add_node(node);
|
nuclear@19
|
436 }
|
nuclear@19
|
437
|
nuclear@19
|
438 return true;
|
nuclear@19
|
439 }
|