erebus

view liberebus/src/scene.cc @ 27:0ced900e15a7

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