erebus
view liberebus/src/scene.cc @ 22:11cdaa510201
added -brdf arguments in scene construction commands
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 29 May 2014 03:35:04 +0300 |
parents | ea95b62dbc87 |
children | 56d504cc555a |
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++;
128 return str;
129 }
131 bool Scene::load(FILE *fp)
132 {
133 static char buf[256];
135 while(fgets(buf, sizeof buf, fp)) {
136 char *line = strip_wspace(buf);
137 if(!line || !*line || *line == '#') {
138 continue;
139 }
141 std::vector<char*> cmd;
143 char *tok = 0;
144 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
145 cmd.push_back(tok);
146 }
147 cmd.push_back(0);
150 if(!proc_cmd((int)cmd.size() - 1, &cmd[0])) {
151 return false;
152 }
153 }
155 return true;
156 }
158 static int parse_vec(char **argv, float *vec)
159 {
160 int nelem = 0;
161 char *endp;
163 for(int i=0; i<4; i++) {
164 if(!argv[i]) break;
166 vec[nelem] = strtod(argv[i], &endp);
167 if(endp == argv[i]) {
168 break;
169 }
170 nelem++;
171 }
172 return nelem;
173 }
175 static int parse_brdf(char **argv, Reflectance **brdf_ptr)
176 {
177 CompositeRefl *composite = 0;
178 Reflectance *brdf = 0;
179 float weight = 1.0;
180 int i;
182 for(i=0; argv[i]; i++) {
183 if(strcmp(argv[i], "-brdf") == 0) {
184 if(strcmp(argv[++i], "lambert") == 0) {
185 if(composite && brdf) {
186 composite->add_brdf(brdf, weight);
187 }
188 brdf = new LambertRefl;
189 if(!composite) break;
191 } else if(strcmp(argv[i], "mirror") == 0) {
192 if(composite && brdf) {
193 composite->add_brdf(brdf, weight);
194 }
195 brdf = new MirrorRefl;
196 if(!composite) break;
198 } else if(strcmp(argv[i], "composite") == 0) {
199 if(composite) {
200 fprintf(stderr, "can't have nested composite BRDFs\n");
201 return -1;
202 }
203 composite = new CompositeRefl;
205 } else {
206 --i;
207 break;
208 }
210 } else if(strcmp(argv[i], "-brdf-weight") == 0) {
211 if(!composite) {
212 fprintf(stderr, "-brdf-weight is invalid without a composite brdf\n");
213 return -1;
214 }
215 char *endp;
216 weight = strtod(argv[++i], &endp);
217 if(endp == argv[i]) {
218 fprintf(stderr, "-brdf-weight must be followed by a number\n");
219 return -1;
220 }
221 } else {
222 --i;
223 break;
224 }
225 }
227 if(composite && brdf) {
228 composite->add_brdf(brdf, weight);
229 *brdf_ptr = composite;
230 } else {
231 *brdf_ptr = brdf;
232 }
233 return i;
234 }
236 static SceneNode *proc_object(Object *obj, int argc, char **argv)
237 {
238 float vec[4];
239 SceneNode *node = new SceneNode(obj);
240 GeomObject *gobj = (GeomObject*)obj;
242 for(int i=1; i<argc; i++) {
243 if(strcmp(argv[i], "-position") == 0) {
244 int nelem = parse_vec(argv + i + 1, vec);
245 if(nelem < 3) {
246 fprintf(stderr, "-position must be followed by 3 numbers\n");
247 goto err;
248 }
249 node->set_position(Vector3(vec[0], vec[1], vec[2]));
250 i += 3;
252 } else if(strcmp(argv[i], "-rotation") == 0) {
253 int nelem = parse_vec(argv + i + 1, vec);
254 if(nelem < 4) {
255 fprintf(stderr, "-rotation must be followed by axis vector and angle\n");
256 goto err;
257 }
258 node->set_rotation(Quaternion(Vector3(vec[0], vec[1], vec[2]), vec[3]));
259 i += 4;
261 } else if(strcmp(argv[i], "-scaling") == 0) {
262 int nelem = parse_vec(argv + i + 1, vec);
263 Vector3 s;
264 if(nelem == 1) {
265 s.x = s.y = s.z = vec[0];
266 } else if(nelem == 3) {
267 s = Vector3(vec[0], vec[1], vec[2]);
268 } else {
269 fprintf(stderr, "-scaling must be followed by 1 or 3 numbers\n");
270 goto err;
271 }
272 node->set_scaling(s);
273 i += nelem;
275 } else if(strcmp(argv[i], "-diffuse") == 0) {
276 int nelem = parse_vec(argv + i + 1, vec);
277 if(nelem < 3) {
278 fprintf(stderr, "-diffuse must be followed by a color (r g b)\n");
279 goto err;
280 }
281 gobj->mtl.set_attrib("diffuse", Color(vec[0], vec[1], vec[2]));
282 i += 3;
284 } else if(strcmp(argv[i], "-specular") == 0) {
285 int nelem = parse_vec(argv + i + 1, vec);
286 if(nelem < 3) {
287 fprintf(stderr, "-specular must be followed by a color\n");
288 goto err;
289 }
290 gobj->mtl.set_attrib("specular", Color(vec[0], vec[1], vec[2]));
291 i += 3;
293 } else if(strcmp(argv[i], "-emissive") == 0) {
294 int nelem = parse_vec(argv + i + 1, vec);
295 Color emissive;
296 if(nelem == 1) {
297 emissive = Vector3(1, 1, 1);
298 } else if(nelem == 3) {
299 emissive = Vector3(vec[0], vec[1], vec[2]);
300 } else {
301 fprintf(stderr, "-emissive must be followed by an intensity or a color\n");
302 goto err;
303 }
304 i += nelem;
305 gobj->mtl.set_attrib("emissive", emissive);
307 } else if(strcmp(argv[i], "-brdf") == 0) {
308 int nelem = parse_brdf(argv + i, &gobj->brdf);
309 if(nelem == -1) {
310 goto err;
311 }
312 i += nelem;
314 } else {
315 fprintf(stderr, "unrecognized %s option: %s\n", argv[0], argv[i]);
316 goto err;
317 }
318 }
320 return node;
321 err:
322 delete node;
323 return 0;
324 }
326 static Camera *proc_camera(int argc, char **argv)
327 {
328 float vec[4];
329 TargetCamera *cam = new TargetCamera;
331 for(int i=1; i<argc; i++) {
332 if(strcmp(argv[i], "-position") == 0) {
333 int nelem = parse_vec(argv + i + 1, vec);
334 if(nelem < 3) {
335 fprintf(stderr, "-position must be followed by 3 numbers\n");
336 goto err;
337 }
338 cam->set_position(Vector3(vec[0], vec[1], vec[2]));
339 i += 3;
341 } else if(strcmp(argv[i], "-target") == 0) {
342 int nelem = parse_vec(argv + i + 1, vec);
343 if(nelem < 3) {
344 fprintf(stderr, "-target must be followed by 3 numbers\n");
345 goto err;
346 }
347 cam->set_target(Vector3(vec[0], vec[1], vec[2]));
348 i += 3;
350 } else if(strcmp(argv[i], "-fov") == 0) {
351 int nelem = parse_vec(argv + i + 1, vec);
352 if(nelem < 1) {
353 fprintf(stderr, "-fov must be followed by the field of view in degrees\n");
354 goto err;
355 }
356 cam->set_fov(vec[0]);
357 i += 1;
359 } else {
360 fprintf(stderr, "unrecognized camera option: %s\n", argv[i]);
361 goto err;
362 }
363 }
364 return cam;
366 err:
367 delete cam;
368 return 0;
369 }
371 bool Scene::proc_cmd(int argc, char **argv)
372 {
373 Object *obj = 0;
374 SceneNode *node = 0;
376 if(strcmp(argv[0], "sphere") == 0) {
377 obj = new Sphere;
378 node = proc_object(obj, argc, argv);
380 } else if(strcmp(argv[0], "box") == 0) {
381 obj = new Box;
382 node = proc_object(obj, argc, argv);
384 } else if(strcmp(argv[0], "mesh") == 0) {
385 fprintf(stderr, "meshes not implemented yet\n");
386 return false;
388 } else if(strcmp(argv[0], "camera") == 0) {
389 Camera *cam = proc_camera(argc, argv);
390 if(!cam) {
391 return false;
392 }
393 use_camera(cam);
395 } else {
396 fprintf(stderr, "unknown command: %s\n", argv[0]);
397 return false;
398 }
400 if(obj) {
401 if(!node) {
402 return false;
403 }
404 add_object(obj);
405 add_node(node);
406 }
408 return true;
409 }