goat3d
view src/goat3d.cc @ 55:af1310ed212b
not done yet
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 19 Jan 2014 14:56:44 +0200 |
parents | cb5414f406eb |
children | 76d0f55f9d5f |
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 using namespace g3dimpl;
17 struct goat3d {
18 Scene *scn;
19 unsigned int flags;
20 char *search_path;
21 };
23 struct goat3d_material : public Material {};
24 struct goat3d_mesh : public Mesh {};
25 struct goat3d_light : public Light {};
26 struct goat3d_camera : public Camera {};
27 struct goat3d_node : public Node {};
30 static long read_file(void *buf, size_t bytes, void *uptr);
31 static long write_file(const void *buf, size_t bytes, void *uptr);
32 static long seek_file(long offs, int whence, void *uptr);
34 extern "C" {
36 GOAT3DAPI 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 GOAT3DAPI void goat3d_free(struct goat3d *g)
48 {
49 delete g->search_path;
50 delete g->scn;
51 delete g;
52 }
54 GOAT3DAPI 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 GOAT3DAPI int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
64 {
65 return (g->flags >> (int)opt) & 1;
66 }
68 GOAT3DAPI 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 GOAT3DAPI 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 GOAT3DAPI 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 GOAT3DAPI 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 GOAT3DAPI 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 GOAT3DAPI 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 /* save/load animations */
153 GOAT3DAPI int goat3d_load_anim(struct goat3d *g, const char *fname)
154 {
155 FILE *fp = fopen(fname, "rb");
156 if(!fp) {
157 return -1;
158 }
160 int res = goat3d_load_anim_file(g, fp);
161 fclose(fp);
162 return res;
163 }
165 GOAT3DAPI int goat3d_save_anim(const struct goat3d *g, const char *fname)
166 {
167 FILE *fp = fopen(fname, "wb");
168 if(!fp) {
169 return -1;
170 }
172 int res = goat3d_save_anim_file(g, fp);
173 fclose(fp);
174 return res;
175 }
177 GOAT3DAPI int goat3d_load_anim_file(struct goat3d *g, FILE *fp)
178 {
179 goat3d_io io;
180 io.cls = fp;
181 io.read = read_file;
182 io.write = write_file;
183 io.seek = seek_file;
185 return goat3d_load_anim_io(g, &io);
186 }
188 GOAT3DAPI int goat3d_save_anim_file(const struct goat3d *g, FILE *fp)
189 {
190 goat3d_io io;
191 io.cls = fp;
192 io.read = read_file;
193 io.write = write_file;
194 io.seek = seek_file;
196 return goat3d_save_anim_io(g, &io);
197 }
199 GOAT3DAPI int goat3d_load_anim_io(struct goat3d *g, struct goat3d_io *io)
200 {
201 if(!g->scn->load_anim(io)) {
202 if(!g->scn->load_anim_xml(io)) {
203 return -1;
204 }
205 }
206 return 0;
207 }
209 GOAT3DAPI int goat3d_save_anim_io(const struct goat3d *g, struct goat3d_io *io)
210 {
211 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
212 return g->scn->save_anim_xml(io) ? 0 : -1;
213 }
214 return g->scn->save_anim(io) ? 0 : -1;
215 }
218 GOAT3DAPI int goat3d_set_name(struct goat3d *g, const char *name)
219 {
220 g->scn->set_name(name);
221 return 0;
222 }
224 GOAT3DAPI const char *goat3d_get_name(const struct goat3d *g)
225 {
226 return g->scn->get_name();
227 }
229 GOAT3DAPI void goat3d_set_ambient(struct goat3d *g, const float *amb)
230 {
231 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
232 }
234 GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
235 {
236 g->scn->set_ambient(Vector3(ar, ag, ab));
237 }
239 GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g)
240 {
241 return &g->scn->get_ambient().x;
242 }
244 // ---- materials ----
245 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
246 {
247 g->scn->add_material(mtl);
248 }
250 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
251 {
252 return new goat3d_material;
253 }
255 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
256 {
257 delete mtl;
258 }
260 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
261 {
262 mtl->name = std::string(name);
263 }
265 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
266 {
267 return mtl->name.c_str();
268 }
270 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
271 {
272 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
273 }
275 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
276 {
277 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
278 }
280 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
281 {
282 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
283 }
285 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
286 {
287 (*mtl)[attrib].value = Vector4(r, g, b, a);
288 }
290 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
291 {
292 return &(*mtl)[attrib].value.x;
293 }
295 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
296 {
297 (*mtl)[attrib].map = clean_filename(mapname);
298 }
300 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
301 {
302 return (*mtl)[attrib].map.c_str();
303 }
305 // ---- meshes ----
306 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
307 {
308 g->scn->add_mesh(mesh);
309 }
311 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
312 {
313 return g->scn->get_mesh_count();
314 }
316 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
317 {
318 return (goat3d_mesh*)g->scn->get_mesh(idx);
319 }
321 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
322 {
323 return (goat3d_mesh*)g->scn->get_mesh(name);
324 }
326 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
327 {
328 return new goat3d_mesh;
329 }
331 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
332 {
333 delete mesh;
334 }
336 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
337 {
338 mesh->name = std::string(name);
339 }
341 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
342 {
343 return mesh->name.c_str();
344 }
346 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
347 {
348 mesh->material = mtl;
349 }
351 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
352 {
353 return (goat3d_material*)mesh->material;
354 }
356 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
357 {
358 return (int)mesh->vertices.size();
359 }
361 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
362 {
363 return (int)mesh->faces.size();
364 }
366 // VECDATA is in goat3d_impl.h
367 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
368 {
369 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
370 mesh->vertices = VECDATA(Vector3, data, vnum);
371 return;
372 }
374 if(vnum != (int)mesh->vertices.size()) {
375 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
376 return;
377 }
379 switch(attrib) {
380 case GOAT3D_MESH_ATTR_NORMAL:
381 mesh->normals = VECDATA(Vector3, data, vnum);
382 break;
383 case GOAT3D_MESH_ATTR_TANGENT:
384 mesh->tangents = VECDATA(Vector3, data, vnum);
385 break;
386 case GOAT3D_MESH_ATTR_TEXCOORD:
387 mesh->texcoords = VECDATA(Vector2, data, vnum);
388 break;
389 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
390 mesh->skin_weights = VECDATA(Vector4, data, vnum);
391 break;
392 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
393 mesh->skin_matrices = VECDATA(Int4, data, vnum);
394 break;
395 case GOAT3D_MESH_ATTR_COLOR:
396 mesh->colors = VECDATA(Vector4, data, vnum);
397 default:
398 break;
399 }
400 }
402 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
403 float val)
404 {
405 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
406 }
408 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
409 float x, float y, float z)
410 {
411 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
412 }
414 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
415 float x, float y, float z, float w)
416 {
417 switch(attrib) {
418 case GOAT3D_MESH_ATTR_VERTEX:
419 mesh->vertices.push_back(Vector3(x, y, z));
420 break;
421 case GOAT3D_MESH_ATTR_NORMAL:
422 mesh->normals.push_back(Vector3(x, y, z));
423 break;
424 case GOAT3D_MESH_ATTR_TANGENT:
425 mesh->tangents.push_back(Vector3(x, y, z));
426 break;
427 case GOAT3D_MESH_ATTR_TEXCOORD:
428 mesh->texcoords.push_back(Vector2(x, y));
429 break;
430 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
431 mesh->skin_weights.push_back(Vector4(x, y, z, w));
432 break;
433 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
434 mesh->skin_matrices.push_back(Int4(x, y, z, w));
435 break;
436 case GOAT3D_MESH_ATTR_COLOR:
437 mesh->colors.push_back(Vector4(x, y, z, w));
438 default:
439 break;
440 }
441 }
443 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
444 {
445 return goat3d_get_mesh_attrib(mesh, attrib, 0);
446 }
448 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
449 {
450 switch(attrib) {
451 case GOAT3D_MESH_ATTR_VERTEX:
452 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
453 case GOAT3D_MESH_ATTR_NORMAL:
454 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
455 case GOAT3D_MESH_ATTR_TANGENT:
456 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
457 case GOAT3D_MESH_ATTR_TEXCOORD:
458 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
459 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
460 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
461 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
462 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
463 case GOAT3D_MESH_ATTR_COLOR:
464 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
465 default:
466 break;
467 }
468 return 0;
469 }
472 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
473 {
474 mesh->faces = VECDATA(Face, data, num);
475 }
477 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
478 {
479 Face face;
480 face.v[0] = a;
481 face.v[1] = b;
482 face.v[2] = c;
483 mesh->faces.push_back(face);
484 }
486 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
487 {
488 return goat3d_get_mesh_face(mesh, 0);
489 }
491 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
492 {
493 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
494 }
496 // immedate mode state
497 static enum goat3d_im_primitive im_prim;
498 static struct goat3d_mesh *im_mesh;
499 static Vector3 im_norm, im_tang;
500 static Vector2 im_texcoord;
501 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
502 static Int4 im_skinmat;
503 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
506 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
507 {
508 mesh->vertices.clear();
509 mesh->normals.clear();
510 mesh->tangents.clear();
511 mesh->texcoords.clear();
512 mesh->skin_weights.clear();
513 mesh->skin_matrices.clear();
514 mesh->colors.clear();
515 mesh->faces.clear();
517 im_mesh = mesh;
518 memset(im_use, 0, sizeof im_use);
520 im_prim = prim;
521 }
523 GOAT3DAPI void goat3d_end(void)
524 {
525 switch(im_prim) {
526 case GOAT3D_TRIANGLES:
527 {
528 int num_faces = (int)im_mesh->vertices.size() / 3;
529 im_mesh->faces.resize(num_faces);
531 int vidx = 0;
532 for(int i=0; i<num_faces; i++) {
533 im_mesh->faces[i].v[0] = vidx++;
534 im_mesh->faces[i].v[1] = vidx++;
535 im_mesh->faces[i].v[2] = vidx++;
536 }
537 }
538 break;
540 case GOAT3D_QUADS:
541 {
542 int num_quads = (int)im_mesh->vertices.size() / 4;
543 im_mesh->faces.resize(num_quads * 2);
545 int vidx = 0;
546 for(int i=0; i<num_quads; i++) {
547 im_mesh->faces[i * 2].v[0] = vidx;
548 im_mesh->faces[i * 2].v[1] = vidx + 1;
549 im_mesh->faces[i * 2].v[2] = vidx + 2;
551 im_mesh->faces[i * 2 + 1].v[0] = vidx;
552 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
553 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
555 vidx += 4;
556 }
557 }
558 break;
560 default:
561 return;
562 };
563 }
565 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
566 {
567 im_mesh->vertices.push_back(Vector3(x, y, z));
568 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
569 im_mesh->normals.push_back(im_norm);
570 }
571 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
572 im_mesh->tangents.push_back(im_tang);
573 }
574 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
575 im_mesh->texcoords.push_back(im_texcoord);
576 }
577 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
578 im_mesh->skin_weights.push_back(im_skinw);
579 }
580 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
581 im_mesh->skin_matrices.push_back(im_skinmat);
582 }
583 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
584 im_mesh->colors.push_back(im_color);
585 }
586 }
588 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
589 {
590 im_norm = Vector3(x, y, z);
591 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
592 }
594 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
595 {
596 im_tang = Vector3(x, y, z);
597 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
598 }
600 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
601 {
602 im_texcoord = Vector2(x, y);
603 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
604 }
606 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
607 {
608 im_skinw = Vector4(x, y, z, w);
609 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
610 }
612 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
613 {
614 im_skinmat.x = x;
615 im_skinmat.y = y;
616 im_skinmat.z = z;
617 im_skinmat.w = w;
618 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
619 }
621 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
622 {
623 goat3d_color4f(x, y, z, 1.0f);
624 }
626 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
627 {
628 im_color = Vector4(x, y, z, w);
629 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
630 }
632 /* lights */
633 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
634 {
635 g->scn->add_light(lt);
636 }
638 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
639 {
640 return g->scn->get_light_count();
641 }
643 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
644 {
645 return (goat3d_light*)g->scn->get_light(idx);
646 }
648 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
649 {
650 return (goat3d_light*)g->scn->get_light(name);
651 }
654 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
655 {
656 return new goat3d_light;
657 }
659 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
660 {
661 delete lt;
662 }
665 /* cameras */
666 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
667 {
668 g->scn->add_camera(cam);
669 }
671 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
672 {
673 return g->scn->get_camera_count();
674 }
676 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
677 {
678 return (goat3d_camera*)g->scn->get_camera(idx);
679 }
681 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
682 {
683 return (goat3d_camera*)g->scn->get_camera(name);
684 }
686 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
687 {
688 return new goat3d_camera;
689 }
691 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
692 {
693 delete cam;
694 }
698 // node
699 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
700 {
701 g->scn->add_node(node);
702 }
704 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
705 {
706 return g->scn->get_node_count();
707 }
709 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
710 {
711 return (goat3d_node*)g->scn->get_node(idx);
712 }
714 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
715 {
716 return (goat3d_node*)g->scn->get_node(name);
717 }
719 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
720 {
721 return new goat3d_node;
722 }
724 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
725 {
726 node->set_name(name);
727 }
729 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
730 {
731 return node->get_name();
732 }
734 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
735 {
736 node->set_object((Object*)obj);
737 }
739 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
740 {
741 return (void*)node->get_object();
742 }
744 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
745 {
746 const Object *obj = node->get_object();
747 if(dynamic_cast<const Mesh*>(obj)) {
748 return GOAT3D_NODE_MESH;
749 }
750 if(dynamic_cast<const Light*>(obj)) {
751 return GOAT3D_NODE_LIGHT;
752 }
753 if(dynamic_cast<const Camera*>(obj)) {
754 return GOAT3D_NODE_CAMERA;
755 }
757 return GOAT3D_NODE_NULL;
758 }
760 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
761 {
762 node->add_child(node);
763 }
765 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
766 {
767 return node->get_children_count();
768 }
770 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
771 {
772 return (goat3d_node*)node->get_child(idx);
773 }
775 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
776 {
777 return (goat3d_node*)node->get_parent();
778 }
780 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
781 {
782 node->use_animation(idx);
783 }
785 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
786 {
787 node->use_animation(aidx, bidx, t);
788 }
790 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
791 {
792 node->use_animation(name);
793 }
795 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
796 {
797 node->use_animation(aname, bname, t);
798 }
800 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
801 {
802 return node->get_active_animation_index(which);
803 }
805 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
806 {
807 return node->get_active_animation_mix();
808 }
810 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
811 {
812 return node->get_animation_count();
813 }
815 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
816 {
817 root->add_animation();
818 }
820 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
821 {
822 root->set_animation_name(name);
823 }
825 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
826 {
827 return node->get_animation_name();
828 }
830 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
831 {
832 node->set_position(Vector3(x, y, z), tmsec);
833 }
835 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
836 {
837 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
838 }
840 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
841 {
842 node->set_scaling(Vector3(sx, sy, sz), tmsec);
843 }
845 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
846 {
847 node->set_pivot(Vector3(px, py, pz));
848 }
851 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
852 {
853 Vector3 pos = node->get_position(tmsec);
854 *xptr = pos.x;
855 *yptr = pos.y;
856 *zptr = pos.z;
857 }
859 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
860 {
861 Quaternion q = node->get_rotation(tmsec);
862 *xptr = q.v.x;
863 *yptr = q.v.y;
864 *zptr = q.v.z;
865 *wptr = q.s;
866 }
868 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
869 {
870 Vector3 scale = node->get_scaling(tmsec);
871 *xptr = scale.x;
872 *yptr = scale.y;
873 *zptr = scale.z;
874 }
876 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
877 {
878 Vector3 pivot = node->get_pivot();
879 *xptr = pivot.x;
880 *yptr = pivot.y;
881 *zptr = pivot.z;
882 }
885 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
886 {
887 node->get_xform(tmsec, (Matrix4x4*)matrix);
888 }
891 } // extern "C"
894 static long read_file(void *buf, size_t bytes, void *uptr)
895 {
896 return (long)fread(buf, 1, bytes, (FILE*)uptr);
897 }
899 static long write_file(const void *buf, size_t bytes, void *uptr)
900 {
901 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
902 }
904 static long seek_file(long offs, int whence, void *uptr)
905 {
906 if(fseek((FILE*)uptr, offs, whence) == -1) {
907 return -1;
908 }
909 return ftell((FILE*)uptr);
910 }
912 std::string g3dimpl::clean_filename(const char *str)
913 {
914 const char *last_slash = strrchr(str, '/');
915 if(!last_slash) {
916 last_slash = strrchr(str, '\\');
917 }
919 if(last_slash) {
920 str = last_slash + 1;
921 }
923 char *buf = (char*)alloca(strlen(str) + 1);
924 char *dest = buf;
925 while(*str) {
926 char c = *str++;
927 *dest++ = tolower(c);
928 }
929 *dest = 0;
930 return buf;
931 }