goat3d

view src/goat3d.cc @ 32:d24f63e8031e

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 30 Sep 2013 05:17:29 +0300
parents 4058337fbb92
children a5c5cec3cb88
line source
1 #include <string.h>
2 #include <errno.h>
3 #include <ctype.h>
4 #include <string>
5 #include "goat3d.h"
6 #include "goat3d_impl.h"
7 #include "log.h"
9 #ifndef _MSC_VER
10 #include <alloca.h>
11 #else
12 #include <malloc.h>
13 #endif
15 struct goat3d {
16 Scene *scn;
17 unsigned int flags;
18 char *search_path;
19 };
21 struct goat3d_material : public Material {};
22 struct goat3d_mesh : public Mesh {};
23 struct goat3d_light : public Light {};
24 struct goat3d_camera : public Camera {};
25 struct goat3d_node : public Node {};
28 static long read_file(void *buf, size_t bytes, void *uptr);
29 static long write_file(const void *buf, size_t bytes, void *uptr);
30 static long seek_file(long offs, int whence, void *uptr);
32 static std::string clean_filename(const char *str);
34 extern "C" {
36 struct goat3d *goat3d_create(void)
37 {
38 goat3d *goat = new goat3d;
39 goat->flags = 0;
40 goat->search_path = 0;
41 goat->scn = new Scene;
43 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
44 return goat;
45 }
47 void goat3d_free(struct goat3d *g)
48 {
49 delete g->search_path;
50 delete g->scn;
51 delete g;
52 }
54 void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
55 {
56 if(val) {
57 g->flags |= (1 << (int)opt);
58 } else {
59 g->flags &= ~(1 << (int)opt);
60 }
61 }
63 int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
64 {
65 return (g->flags >> (int)opt) & 1;
66 }
68 int goat3d_load(struct goat3d *g, const char *fname)
69 {
70 FILE *fp = fopen(fname, "rb");
71 if(!fp) {
72 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
73 return -1;
74 }
76 /* if the filename contained any directory components, keep the prefix
77 * to use it as a search path for external mesh file loading
78 */
79 g->search_path = new char[strlen(fname) + 1];
80 strcpy(g->search_path, fname);
82 char *slash = strrchr(g->search_path, '/');
83 if(slash) {
84 *slash = 0;
85 } else {
86 if((slash = strrchr(g->search_path, '\\'))) {
87 *slash = 0;
88 } else {
89 delete [] g->search_path;
90 g->search_path = 0;
91 }
92 }
94 int res = goat3d_load_file(g, fp);
95 fclose(fp);
96 return res;
97 }
99 int goat3d_save(const struct goat3d *g, const char *fname)
100 {
101 FILE *fp = fopen(fname, "wb");
102 if(!fp) {
103 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
104 return -1;
105 }
107 int res = goat3d_save_file(g, fp);
108 fclose(fp);
109 return res;
110 }
112 int goat3d_load_file(struct goat3d *g, FILE *fp)
113 {
114 goat3d_io io;
115 io.cls = fp;
116 io.read = read_file;
117 io.write = write_file;
118 io.seek = seek_file;
120 return goat3d_load_io(g, &io);
121 }
123 int goat3d_save_file(const struct goat3d *g, FILE *fp)
124 {
125 goat3d_io io;
126 io.cls = fp;
127 io.read = read_file;
128 io.write = write_file;
129 io.seek = seek_file;
131 return goat3d_save_io(g, &io);
132 }
134 int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
135 {
136 if(!g->scn->load(io)) {
137 if(g->scn->loadxml(io)) {
138 return -1;
139 }
140 }
141 return 0;
142 }
144 int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
145 {
146 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
147 return g->scn->savexml(io) ? 0 : -1;
148 }
149 return g->scn->save(io) ? 0 : -1;
150 }
152 int goat3d_set_name(struct goat3d *g, const char *name)
153 {
154 g->scn->set_name(name);
155 return 0;
156 }
158 const char *goat3d_get_name(const struct goat3d *g)
159 {
160 return g->scn->get_name();
161 }
163 void goat3d_set_ambient(struct goat3d *g, const float *amb)
164 {
165 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
166 }
168 void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
169 {
170 g->scn->set_ambient(Vector3(ar, ag, ab));
171 }
173 const float *goat3d_get_ambient(const struct goat3d *g)
174 {
175 return &g->scn->get_ambient().x;
176 }
178 // ---- materials ----
179 void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
180 {
181 g->scn->add_material(mtl);
182 }
184 struct goat3d_material *goat3d_create_mtl(void)
185 {
186 return new goat3d_material;
187 }
189 void goat3d_destroy_mtl(struct goat3d_material *mtl)
190 {
191 delete mtl;
192 }
194 void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
195 {
196 mtl->name = std::string(name);
197 }
199 const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
200 {
201 return mtl->name.c_str();
202 }
204 void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
205 {
206 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
207 }
209 void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
210 {
211 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
212 }
214 void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
215 {
216 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
217 }
219 void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
220 {
221 (*mtl)[attrib].value = Vector4(r, g, b, a);
222 }
224 const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
225 {
226 return &(*mtl)[attrib].value.x;
227 }
229 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
230 {
231 (*mtl)[attrib].map = clean_filename(mapname);
232 }
234 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
235 {
236 return (*mtl)[attrib].map.c_str();
237 }
239 // ---- meshes ----
240 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
241 {
242 g->scn->add_mesh(mesh);
243 }
245 int goat3d_get_mesh_count(struct goat3d *g)
246 {
247 return g->scn->get_mesh_count();
248 }
250 struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
251 {
252 return (goat3d_mesh*)g->scn->get_mesh(idx);
253 }
255 struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
256 {
257 return (goat3d_mesh*)g->scn->get_mesh(name);
258 }
260 struct goat3d_mesh *goat3d_create_mesh(void)
261 {
262 return new goat3d_mesh;
263 }
265 void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
266 {
267 delete mesh;
268 }
270 void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
271 {
272 mesh->name = std::string(name);
273 }
275 const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
276 {
277 return mesh->name.c_str();
278 }
280 void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
281 {
282 mesh->material = mtl;
283 }
285 struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
286 {
287 return (goat3d_material*)mesh->material;
288 }
290 int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
291 {
292 return (int)mesh->vertices.size();
293 }
295 int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
296 {
297 return (int)mesh->faces.size();
298 }
300 // VECDATA is in goat3d_impl.h
301 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
302 {
303 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
304 mesh->vertices = VECDATA(Vector3, data, vnum);
305 return;
306 }
308 if(vnum != (int)mesh->vertices.size()) {
309 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
310 return;
311 }
313 switch(attrib) {
314 case GOAT3D_MESH_ATTR_NORMAL:
315 mesh->normals = VECDATA(Vector3, data, vnum);
316 break;
317 case GOAT3D_MESH_ATTR_TANGENT:
318 mesh->tangents = VECDATA(Vector3, data, vnum);
319 break;
320 case GOAT3D_MESH_ATTR_TEXCOORD:
321 mesh->texcoords = VECDATA(Vector2, data, vnum);
322 break;
323 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
324 mesh->skin_weights = VECDATA(Vector4, data, vnum);
325 break;
326 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
327 mesh->skin_matrices = VECDATA(Int4, data, vnum);
328 break;
329 case GOAT3D_MESH_ATTR_COLOR:
330 mesh->colors = VECDATA(Vector4, data, vnum);
331 default:
332 break;
333 }
334 }
336 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
337 {
338 return goat3d_get_mesh_attrib(mesh, attrib, 0);
339 }
341 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
342 {
343 switch(attrib) {
344 case GOAT3D_MESH_ATTR_VERTEX:
345 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
346 case GOAT3D_MESH_ATTR_NORMAL:
347 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
348 case GOAT3D_MESH_ATTR_TANGENT:
349 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
350 case GOAT3D_MESH_ATTR_TEXCOORD:
351 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
352 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
353 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
354 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
355 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
356 case GOAT3D_MESH_ATTR_COLOR:
357 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
358 default:
359 break;
360 }
361 return 0;
362 }
365 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
366 {
367 mesh->faces = VECDATA(Face, data, num);
368 }
370 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
371 {
372 return goat3d_get_mesh_face(mesh, 0);
373 }
375 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
376 {
377 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
378 }
380 // immedate mode state
381 static enum goat3d_im_primitive im_prim;
382 static struct goat3d_mesh *im_mesh;
383 static Vector3 im_norm, im_tang;
384 static Vector2 im_texcoord;
385 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
386 static Int4 im_skinmat;
387 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
390 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
391 {
392 mesh->vertices.clear();
393 mesh->normals.clear();
394 mesh->tangents.clear();
395 mesh->texcoords.clear();
396 mesh->skin_weights.clear();
397 mesh->skin_matrices.clear();
398 mesh->colors.clear();
399 mesh->faces.clear();
401 im_mesh = mesh;
402 memset(im_use, 0, sizeof im_use);
404 im_prim = prim;
405 }
407 void goat3d_end(void)
408 {
409 switch(im_prim) {
410 case GOAT3D_TRIANGLES:
411 {
412 int num_faces = (int)im_mesh->vertices.size() / 3;
413 im_mesh->faces.resize(num_faces);
415 int vidx = 0;
416 for(int i=0; i<num_faces; i++) {
417 im_mesh->faces[i].v[0] = vidx++;
418 im_mesh->faces[i].v[1] = vidx++;
419 im_mesh->faces[i].v[2] = vidx++;
420 }
421 }
422 break;
424 case GOAT3D_QUADS:
425 {
426 int num_quads = (int)im_mesh->vertices.size() / 4;
427 im_mesh->faces.resize(num_quads * 2);
429 int vidx = 0;
430 for(int i=0; i<num_quads; i++) {
431 im_mesh->faces[i * 2].v[0] = vidx;
432 im_mesh->faces[i * 2].v[1] = vidx + 1;
433 im_mesh->faces[i * 2].v[2] = vidx + 2;
435 im_mesh->faces[i * 2 + 1].v[0] = vidx;
436 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
437 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
439 vidx += 4;
440 }
441 }
442 break;
444 default:
445 return;
446 };
447 }
449 void goat3d_vertex3f(float x, float y, float z)
450 {
451 im_mesh->vertices.push_back(Vector3(x, y, z));
452 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
453 im_mesh->normals.push_back(im_norm);
454 }
455 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
456 im_mesh->tangents.push_back(im_tang);
457 }
458 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
459 im_mesh->texcoords.push_back(im_texcoord);
460 }
461 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
462 im_mesh->skin_weights.push_back(im_skinw);
463 }
464 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
465 im_mesh->skin_matrices.push_back(im_skinmat);
466 }
467 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
468 im_mesh->colors.push_back(im_color);
469 }
470 }
472 void goat3d_normal3f(float x, float y, float z)
473 {
474 im_norm = Vector3(x, y, z);
475 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
476 }
478 void goat3d_tangent3f(float x, float y, float z)
479 {
480 im_tang = Vector3(x, y, z);
481 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
482 }
484 void goat3d_texcoord2f(float x, float y)
485 {
486 im_texcoord = Vector2(x, y);
487 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
488 }
490 void goat3d_skin_weight4f(float x, float y, float z, float w)
491 {
492 im_skinw = Vector4(x, y, z, w);
493 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
494 }
496 void goat3d_skin_matrix4i(int x, int y, int z, int w)
497 {
498 im_skinmat.x = x;
499 im_skinmat.y = y;
500 im_skinmat.z = z;
501 im_skinmat.w = w;
502 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
503 }
505 void goat3d_color3f(float x, float y, float z)
506 {
507 goat3d_color4f(x, y, z, 1.0f);
508 }
510 void goat3d_color4f(float x, float y, float z, float w)
511 {
512 im_color = Vector4(x, y, z, w);
513 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
514 }
516 /* lights */
517 void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
518 {
519 g->scn->add_light(lt);
520 }
522 int goat3d_get_light_count(struct goat3d *g)
523 {
524 return g->scn->get_light_count();
525 }
527 struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
528 {
529 return (goat3d_light*)g->scn->get_light(idx);
530 }
532 struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
533 {
534 return (goat3d_light*)g->scn->get_light(name);
535 }
538 struct goat3d_light *goat3d_create_light(void)
539 {
540 return new goat3d_light;
541 }
543 void goat3d_destroy_light(struct goat3d_light *lt)
544 {
545 delete lt;
546 }
549 /* cameras */
550 void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
551 {
552 g->scn->add_camera(cam);
553 }
555 int goat3d_get_camera_count(struct goat3d *g)
556 {
557 return g->scn->get_camera_count();
558 }
560 struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
561 {
562 return (goat3d_camera*)g->scn->get_camera(idx);
563 }
565 struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
566 {
567 return (goat3d_camera*)g->scn->get_camera(name);
568 }
570 struct goat3d_camera *goat3d_create_camera(void)
571 {
572 return new goat3d_camera;
573 }
575 void goat3d_destroy_camera(struct goat3d_camera *cam)
576 {
577 delete cam;
578 }
582 // node
583 void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
584 {
585 g->scn->add_node(node);
586 }
588 int goat3d_get_node_count(struct goat3d *g)
589 {
590 return g->scn->get_node_count();
591 }
593 struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
594 {
595 return (goat3d_node*)g->scn->get_node(idx);
596 }
598 struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
599 {
600 return (goat3d_node*)g->scn->get_node(name);
601 }
603 struct goat3d_node *goat3d_create_node(void)
604 {
605 return new goat3d_node;
606 }
608 void goat3d_set_node_name(struct goat3d_node *node, const char *name)
609 {
610 node->set_name(name);
611 }
613 const char *goat3d_get_node_name(const struct goat3d_node *node)
614 {
615 return node->get_name();
616 }
618 void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
619 {
620 node->set_object((Object*)obj);
621 }
623 void *goat3d_get_node_object(const struct goat3d_node *node)
624 {
625 return (void*)node->get_object();
626 }
628 enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
629 {
630 const Object *obj = node->get_object();
631 if(dynamic_cast<const Mesh*>(obj)) {
632 return GOAT3D_NODE_MESH;
633 }
634 if(dynamic_cast<const Light*>(obj)) {
635 return GOAT3D_NODE_LIGHT;
636 }
637 if(dynamic_cast<const Camera*>(obj)) {
638 return GOAT3D_NODE_CAMERA;
639 }
641 return GOAT3D_NODE_NULL;
642 }
644 void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
645 {
646 node->add_child(node);
647 }
649 int goat3d_get_node_child_count(const struct goat3d_node *node)
650 {
651 return node->get_children_count();
652 }
654 struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
655 {
656 return (goat3d_node*)node->get_child(idx);
657 }
659 void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
660 {
661 node->set_position(Vector3(x, y, z), tmsec);
662 }
664 void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
665 {
666 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
667 }
669 void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
670 {
671 node->set_scaling(Vector3(sx, sy, sz), tmsec);
672 }
674 void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
675 {
676 node->set_pivot(Vector3(px, py, pz));
677 }
680 void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
681 {
682 Vector3 pos = node->get_position(tmsec);
683 *xptr = pos.x;
684 *yptr = pos.y;
685 *zptr = pos.z;
686 }
688 void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
689 {
690 Quaternion q = node->get_rotation(tmsec);
691 *xptr = q.v.x;
692 *yptr = q.v.y;
693 *zptr = q.v.z;
694 *wptr = q.s;
695 }
697 void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
698 {
699 Vector3 scale = node->get_scaling(tmsec);
700 *xptr = scale.x;
701 *yptr = scale.y;
702 *zptr = scale.z;
703 }
705 void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
706 {
707 Vector3 pivot = node->get_pivot();
708 *xptr = pivot.x;
709 *yptr = pivot.y;
710 *zptr = pivot.z;
711 }
714 void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
715 {
716 node->get_xform(tmsec, (Matrix4x4*)matrix);
717 }
720 } // extern "C"
723 static long read_file(void *buf, size_t bytes, void *uptr)
724 {
725 return (long)fread(buf, 1, bytes, (FILE*)uptr);
726 }
728 static long write_file(const void *buf, size_t bytes, void *uptr)
729 {
730 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
731 }
733 static long seek_file(long offs, int whence, void *uptr)
734 {
735 if(fseek((FILE*)uptr, offs, whence) == -1) {
736 return -1;
737 }
738 return ftell((FILE*)uptr);
739 }
741 static std::string clean_filename(const char *str)
742 {
743 const char *last_slash = strrchr(str, '/');
744 if(!last_slash) {
745 last_slash = strrchr(str, '\\');
746 }
748 if(last_slash) {
749 str = last_slash + 1;
750 }
752 char *buf = (char*)alloca(strlen(str) + 1);
753 char *dest = buf;
754 while(*str) {
755 char c = *str++;
756 *dest++ = tolower(c);
757 }
758 return buf;
759 }