erebus

view liberebus/src/scene.cc @ 20:ea95b62dbc87

removed debugging messages while parsing scene
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 27 May 2014 07:49:09 +0300
parents 6204e4d3f445
children 11cdaa510201
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 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 SceneNode *proc_object(Object *obj, int argc, char **argv)
176 {
177 float vec[4];
178 SceneNode *node = new SceneNode(obj);
179 GeomObject *gobj = (GeomObject*)obj;
181 for(int i=1; i<argc; i++) {
182 if(strcmp(argv[i], "-position") == 0) {
183 int nelem = parse_vec(argv + i + 1, vec);
184 if(nelem < 3) {
185 fprintf(stderr, "-position must be followed by 3 numbers\n");
186 goto err;
187 }
188 node->set_position(Vector3(vec[0], vec[1], vec[2]));
189 i += 3;
191 } else if(strcmp(argv[i], "-rotation") == 0) {
192 int nelem = parse_vec(argv + i + 1, vec);
193 if(nelem < 4) {
194 fprintf(stderr, "-rotation must be followed by axis vector and angle\n");
195 goto err;
196 }
197 node->set_rotation(Quaternion(Vector3(vec[0], vec[1], vec[2]), vec[3]));
198 i += 4;
200 } else if(strcmp(argv[i], "-scaling") == 0) {
201 int nelem = parse_vec(argv + i + 1, vec);
202 Vector3 s;
203 if(nelem == 1) {
204 s.x = s.y = s.z = vec[0];
205 } else if(nelem == 3) {
206 s = Vector3(vec[0], vec[1], vec[2]);
207 } else {
208 fprintf(stderr, "-scaling must be followed by 1 or 3 numbers\n");
209 goto err;
210 }
211 node->set_scaling(s);
212 i += nelem;
214 } else if(strcmp(argv[i], "-diffuse") == 0) {
215 int nelem = parse_vec(argv + i + 1, vec);
216 if(nelem < 3) {
217 fprintf(stderr, "-diffuse must be followed by a color (r g b)\n");
218 goto err;
219 }
220 gobj->mtl.set_attrib("diffuse", Color(vec[0], vec[1], vec[2]));
221 i += 3;
223 } else if(strcmp(argv[i], "-specular") == 0) {
224 int nelem = parse_vec(argv + i + 1, vec);
225 if(nelem < 3) {
226 fprintf(stderr, "-specular must be followed by a color\n");
227 goto err;
228 }
229 gobj->mtl.set_attrib("specular", Color(vec[0], vec[1], vec[2]));
230 i += 3;
232 } else if(strcmp(argv[i], "-emissive") == 0) {
233 int nelem = parse_vec(argv + i + 1, vec);
234 Color emissive;
235 if(nelem == 1) {
236 emissive = Vector3(1, 1, 1);
237 } else if(nelem == 3) {
238 emissive = Vector3(vec[0], vec[1], vec[2]);
239 } else {
240 fprintf(stderr, "-emissive must be followed by an intensity or a color\n");
241 goto err;
242 }
243 i += nelem;
244 gobj->mtl.set_attrib("emissive", emissive);
246 } else {
247 fprintf(stderr, "unrecognized %s option: %s\n", argv[0], argv[i]);
248 goto err;
249 }
250 }
252 return node;
253 err:
254 delete node;
255 return 0;
256 }
258 static Camera *proc_camera(int argc, char **argv)
259 {
260 float vec[4];
261 TargetCamera *cam = new TargetCamera;
263 for(int i=1; i<argc; i++) {
264 if(strcmp(argv[i], "-position") == 0) {
265 int nelem = parse_vec(argv + i + 1, vec);
266 if(nelem < 3) {
267 fprintf(stderr, "-position must be followed by 3 numbers\n");
268 goto err;
269 }
270 cam->set_position(Vector3(vec[0], vec[1], vec[2]));
271 i += 3;
273 } else if(strcmp(argv[i], "-target") == 0) {
274 int nelem = parse_vec(argv + i + 1, vec);
275 if(nelem < 3) {
276 fprintf(stderr, "-target must be followed by 3 numbers\n");
277 goto err;
278 }
279 cam->set_target(Vector3(vec[0], vec[1], vec[2]));
280 i += 3;
282 } else if(strcmp(argv[i], "-fov") == 0) {
283 int nelem = parse_vec(argv + i + 1, vec);
284 if(nelem < 1) {
285 fprintf(stderr, "-fov must be followed by the field of view in degrees\n");
286 goto err;
287 }
288 cam->set_fov(vec[0]);
289 i += 1;
291 } else {
292 fprintf(stderr, "unrecognized camera option: %s\n", argv[i]);
293 goto err;
294 }
295 }
296 return cam;
298 err:
299 delete cam;
300 return 0;
301 }
303 bool Scene::proc_cmd(int argc, char **argv)
304 {
305 Object *obj = 0;
306 SceneNode *node = 0;
308 if(strcmp(argv[0], "sphere") == 0) {
309 obj = new Sphere;
310 node = proc_object(obj, argc, argv);
312 } else if(strcmp(argv[0], "box") == 0) {
313 obj = new Box;
314 node = proc_object(obj, argc, argv);
316 } else if(strcmp(argv[0], "mesh") == 0) {
317 fprintf(stderr, "meshes not implemented yet\n");
318 return false;
320 } else if(strcmp(argv[0], "camera") == 0) {
321 Camera *cam = proc_camera(argc, argv);
322 if(!cam) {
323 return false;
324 }
325 use_camera(cam);
327 } else {
328 fprintf(stderr, "unknown command: %s\n", argv[0]);
329 return false;
330 }
332 if(obj) {
333 if(!node) {
334 return false;
335 }
336 add_object(obj);
337 add_node(node);
338 }
340 return true;
341 }