goat3d

view src/goat3d.cc @ 54:dad392c710df

added copyright headers and license files + readme
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 17 Apr 2014 08:50:36 +0300
parents cb5414f406eb
children 99715321ad6d
line source
1 /*
2 goat3d - 3D scene, character, and animation file format library.
3 Copyright (C) 2013-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include <string>
22 #include "goat3d.h"
23 #include "goat3d_impl.h"
24 #include "log.h"
26 #ifndef _MSC_VER
27 #include <alloca.h>
28 #else
29 #include <malloc.h>
30 #endif
32 using namespace g3dimpl;
34 struct goat3d {
35 Scene *scn;
36 unsigned int flags;
37 char *search_path;
38 };
40 struct goat3d_material : public Material {};
41 struct goat3d_mesh : public Mesh {};
42 struct goat3d_light : public Light {};
43 struct goat3d_camera : public Camera {};
44 struct goat3d_node : public Node {};
47 static long read_file(void *buf, size_t bytes, void *uptr);
48 static long write_file(const void *buf, size_t bytes, void *uptr);
49 static long seek_file(long offs, int whence, void *uptr);
51 extern "C" {
53 GOAT3DAPI struct goat3d *goat3d_create(void)
54 {
55 goat3d *goat = new goat3d;
56 goat->flags = 0;
57 goat->search_path = 0;
58 goat->scn = new Scene;
60 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
61 return goat;
62 }
64 GOAT3DAPI void goat3d_free(struct goat3d *g)
65 {
66 delete g->search_path;
67 delete g->scn;
68 delete g;
69 }
71 GOAT3DAPI void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
72 {
73 if(val) {
74 g->flags |= (1 << (int)opt);
75 } else {
76 g->flags &= ~(1 << (int)opt);
77 }
78 }
80 GOAT3DAPI int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
81 {
82 return (g->flags >> (int)opt) & 1;
83 }
85 GOAT3DAPI int goat3d_load(struct goat3d *g, const char *fname)
86 {
87 FILE *fp = fopen(fname, "rb");
88 if(!fp) {
89 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
90 return -1;
91 }
93 /* if the filename contained any directory components, keep the prefix
94 * to use it as a search path for external mesh file loading
95 */
96 g->search_path = new char[strlen(fname) + 1];
97 strcpy(g->search_path, fname);
99 char *slash = strrchr(g->search_path, '/');
100 if(slash) {
101 *slash = 0;
102 } else {
103 if((slash = strrchr(g->search_path, '\\'))) {
104 *slash = 0;
105 } else {
106 delete [] g->search_path;
107 g->search_path = 0;
108 }
109 }
111 int res = goat3d_load_file(g, fp);
112 fclose(fp);
113 return res;
114 }
116 GOAT3DAPI int goat3d_save(const struct goat3d *g, const char *fname)
117 {
118 FILE *fp = fopen(fname, "wb");
119 if(!fp) {
120 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
121 return -1;
122 }
124 int res = goat3d_save_file(g, fp);
125 fclose(fp);
126 return res;
127 }
129 GOAT3DAPI int goat3d_load_file(struct goat3d *g, FILE *fp)
130 {
131 goat3d_io io;
132 io.cls = fp;
133 io.read = read_file;
134 io.write = write_file;
135 io.seek = seek_file;
137 return goat3d_load_io(g, &io);
138 }
140 GOAT3DAPI int goat3d_save_file(const struct goat3d *g, FILE *fp)
141 {
142 goat3d_io io;
143 io.cls = fp;
144 io.read = read_file;
145 io.write = write_file;
146 io.seek = seek_file;
148 return goat3d_save_io(g, &io);
149 }
151 GOAT3DAPI int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
152 {
153 if(!g->scn->load(io)) {
154 if(!g->scn->loadxml(io)) {
155 return -1;
156 }
157 }
158 return 0;
159 }
161 GOAT3DAPI int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
162 {
163 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
164 return g->scn->savexml(io) ? 0 : -1;
165 }
166 return g->scn->save(io) ? 0 : -1;
167 }
169 /* save/load animations */
170 GOAT3DAPI int goat3d_load_anim(struct goat3d *g, const char *fname)
171 {
172 FILE *fp = fopen(fname, "rb");
173 if(!fp) {
174 return -1;
175 }
177 int res = goat3d_load_anim_file(g, fp);
178 fclose(fp);
179 return res;
180 }
182 GOAT3DAPI int goat3d_save_anim(const struct goat3d *g, const struct goat3d_node *root, const char *fname)
183 {
184 FILE *fp = fopen(fname, "wb");
185 if(!fp) {
186 return -1;
187 }
189 int res = goat3d_save_anim_file(g, root, fp);
190 fclose(fp);
191 return res;
192 }
194 GOAT3DAPI int goat3d_load_anim_file(struct goat3d *g, FILE *fp)
195 {
196 goat3d_io io;
197 io.cls = fp;
198 io.read = read_file;
199 io.write = write_file;
200 io.seek = seek_file;
202 return goat3d_load_anim_io(g, &io);
203 }
205 GOAT3DAPI int goat3d_save_anim_file(const struct goat3d *g, const struct goat3d_node *root, FILE *fp)
206 {
207 goat3d_io io;
208 io.cls = fp;
209 io.read = read_file;
210 io.write = write_file;
211 io.seek = seek_file;
213 return goat3d_save_anim_io(g, root, &io);
214 }
216 GOAT3DAPI int goat3d_load_anim_io(struct goat3d *g, struct goat3d_io *io)
217 {
218 if(!g->scn->load_anim(io)) {
219 if(!g->scn->load_anim_xml(io)) {
220 return -1;
221 }
222 }
223 return 0;
224 }
226 GOAT3DAPI int goat3d_save_anim_io(const struct goat3d *g, const struct goat3d_node *root, struct goat3d_io *io)
227 {
228 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
229 return g->scn->save_anim_xml(root, io) ? 0 : -1;
230 }
231 return g->scn->save_anim(root, io) ? 0 : -1;
232 }
235 GOAT3DAPI int goat3d_set_name(struct goat3d *g, const char *name)
236 {
237 g->scn->set_name(name);
238 return 0;
239 }
241 GOAT3DAPI const char *goat3d_get_name(const struct goat3d *g)
242 {
243 return g->scn->get_name();
244 }
246 GOAT3DAPI void goat3d_set_ambient(struct goat3d *g, const float *amb)
247 {
248 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
249 }
251 GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
252 {
253 g->scn->set_ambient(Vector3(ar, ag, ab));
254 }
256 GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g)
257 {
258 return &g->scn->get_ambient().x;
259 }
261 // ---- materials ----
262 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
263 {
264 g->scn->add_material(mtl);
265 }
267 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
268 {
269 return new goat3d_material;
270 }
272 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
273 {
274 delete mtl;
275 }
277 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
278 {
279 mtl->name = std::string(name);
280 }
282 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
283 {
284 return mtl->name.c_str();
285 }
287 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
288 {
289 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
290 }
292 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
293 {
294 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
295 }
297 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
298 {
299 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
300 }
302 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
303 {
304 (*mtl)[attrib].value = Vector4(r, g, b, a);
305 }
307 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
308 {
309 return &(*mtl)[attrib].value.x;
310 }
312 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
313 {
314 (*mtl)[attrib].map = clean_filename(mapname);
315 }
317 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
318 {
319 return (*mtl)[attrib].map.c_str();
320 }
322 // ---- meshes ----
323 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
324 {
325 g->scn->add_mesh(mesh);
326 }
328 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
329 {
330 return g->scn->get_mesh_count();
331 }
333 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
334 {
335 return (goat3d_mesh*)g->scn->get_mesh(idx);
336 }
338 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
339 {
340 return (goat3d_mesh*)g->scn->get_mesh(name);
341 }
343 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
344 {
345 return new goat3d_mesh;
346 }
348 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
349 {
350 delete mesh;
351 }
353 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
354 {
355 mesh->name = std::string(name);
356 }
358 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
359 {
360 return mesh->name.c_str();
361 }
363 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
364 {
365 mesh->material = mtl;
366 }
368 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
369 {
370 return (goat3d_material*)mesh->material;
371 }
373 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
374 {
375 return (int)mesh->vertices.size();
376 }
378 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
379 {
380 return (int)mesh->faces.size();
381 }
383 // VECDATA is in goat3d_impl.h
384 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
385 {
386 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
387 mesh->vertices = VECDATA(Vector3, data, vnum);
388 return;
389 }
391 if(vnum != (int)mesh->vertices.size()) {
392 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
393 return;
394 }
396 switch(attrib) {
397 case GOAT3D_MESH_ATTR_NORMAL:
398 mesh->normals = VECDATA(Vector3, data, vnum);
399 break;
400 case GOAT3D_MESH_ATTR_TANGENT:
401 mesh->tangents = VECDATA(Vector3, data, vnum);
402 break;
403 case GOAT3D_MESH_ATTR_TEXCOORD:
404 mesh->texcoords = VECDATA(Vector2, data, vnum);
405 break;
406 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
407 mesh->skin_weights = VECDATA(Vector4, data, vnum);
408 break;
409 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
410 mesh->skin_matrices = VECDATA(Int4, data, vnum);
411 break;
412 case GOAT3D_MESH_ATTR_COLOR:
413 mesh->colors = VECDATA(Vector4, data, vnum);
414 default:
415 break;
416 }
417 }
419 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
420 float val)
421 {
422 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
423 }
425 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
426 float x, float y, float z)
427 {
428 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
429 }
431 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
432 float x, float y, float z, float w)
433 {
434 switch(attrib) {
435 case GOAT3D_MESH_ATTR_VERTEX:
436 mesh->vertices.push_back(Vector3(x, y, z));
437 break;
438 case GOAT3D_MESH_ATTR_NORMAL:
439 mesh->normals.push_back(Vector3(x, y, z));
440 break;
441 case GOAT3D_MESH_ATTR_TANGENT:
442 mesh->tangents.push_back(Vector3(x, y, z));
443 break;
444 case GOAT3D_MESH_ATTR_TEXCOORD:
445 mesh->texcoords.push_back(Vector2(x, y));
446 break;
447 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
448 mesh->skin_weights.push_back(Vector4(x, y, z, w));
449 break;
450 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
451 mesh->skin_matrices.push_back(Int4(x, y, z, w));
452 break;
453 case GOAT3D_MESH_ATTR_COLOR:
454 mesh->colors.push_back(Vector4(x, y, z, w));
455 default:
456 break;
457 }
458 }
460 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
461 {
462 return goat3d_get_mesh_attrib(mesh, attrib, 0);
463 }
465 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
466 {
467 switch(attrib) {
468 case GOAT3D_MESH_ATTR_VERTEX:
469 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
470 case GOAT3D_MESH_ATTR_NORMAL:
471 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
472 case GOAT3D_MESH_ATTR_TANGENT:
473 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
474 case GOAT3D_MESH_ATTR_TEXCOORD:
475 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
476 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
477 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
478 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
479 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
480 case GOAT3D_MESH_ATTR_COLOR:
481 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
482 default:
483 break;
484 }
485 return 0;
486 }
489 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
490 {
491 mesh->faces = VECDATA(Face, data, num);
492 }
494 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
495 {
496 Face face;
497 face.v[0] = a;
498 face.v[1] = b;
499 face.v[2] = c;
500 mesh->faces.push_back(face);
501 }
503 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
504 {
505 return goat3d_get_mesh_face(mesh, 0);
506 }
508 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
509 {
510 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
511 }
513 // immedate mode state
514 static enum goat3d_im_primitive im_prim;
515 static struct goat3d_mesh *im_mesh;
516 static Vector3 im_norm, im_tang;
517 static Vector2 im_texcoord;
518 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
519 static Int4 im_skinmat;
520 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
523 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
524 {
525 mesh->vertices.clear();
526 mesh->normals.clear();
527 mesh->tangents.clear();
528 mesh->texcoords.clear();
529 mesh->skin_weights.clear();
530 mesh->skin_matrices.clear();
531 mesh->colors.clear();
532 mesh->faces.clear();
534 im_mesh = mesh;
535 memset(im_use, 0, sizeof im_use);
537 im_prim = prim;
538 }
540 GOAT3DAPI void goat3d_end(void)
541 {
542 switch(im_prim) {
543 case GOAT3D_TRIANGLES:
544 {
545 int num_faces = (int)im_mesh->vertices.size() / 3;
546 im_mesh->faces.resize(num_faces);
548 int vidx = 0;
549 for(int i=0; i<num_faces; i++) {
550 im_mesh->faces[i].v[0] = vidx++;
551 im_mesh->faces[i].v[1] = vidx++;
552 im_mesh->faces[i].v[2] = vidx++;
553 }
554 }
555 break;
557 case GOAT3D_QUADS:
558 {
559 int num_quads = (int)im_mesh->vertices.size() / 4;
560 im_mesh->faces.resize(num_quads * 2);
562 int vidx = 0;
563 for(int i=0; i<num_quads; i++) {
564 im_mesh->faces[i * 2].v[0] = vidx;
565 im_mesh->faces[i * 2].v[1] = vidx + 1;
566 im_mesh->faces[i * 2].v[2] = vidx + 2;
568 im_mesh->faces[i * 2 + 1].v[0] = vidx;
569 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
570 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
572 vidx += 4;
573 }
574 }
575 break;
577 default:
578 return;
579 };
580 }
582 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
583 {
584 im_mesh->vertices.push_back(Vector3(x, y, z));
585 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
586 im_mesh->normals.push_back(im_norm);
587 }
588 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
589 im_mesh->tangents.push_back(im_tang);
590 }
591 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
592 im_mesh->texcoords.push_back(im_texcoord);
593 }
594 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
595 im_mesh->skin_weights.push_back(im_skinw);
596 }
597 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
598 im_mesh->skin_matrices.push_back(im_skinmat);
599 }
600 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
601 im_mesh->colors.push_back(im_color);
602 }
603 }
605 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
606 {
607 im_norm = Vector3(x, y, z);
608 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
609 }
611 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
612 {
613 im_tang = Vector3(x, y, z);
614 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
615 }
617 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
618 {
619 im_texcoord = Vector2(x, y);
620 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
621 }
623 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
624 {
625 im_skinw = Vector4(x, y, z, w);
626 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
627 }
629 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
630 {
631 im_skinmat.x = x;
632 im_skinmat.y = y;
633 im_skinmat.z = z;
634 im_skinmat.w = w;
635 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
636 }
638 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
639 {
640 goat3d_color4f(x, y, z, 1.0f);
641 }
643 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
644 {
645 im_color = Vector4(x, y, z, w);
646 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
647 }
649 /* lights */
650 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
651 {
652 g->scn->add_light(lt);
653 }
655 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
656 {
657 return g->scn->get_light_count();
658 }
660 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
661 {
662 return (goat3d_light*)g->scn->get_light(idx);
663 }
665 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
666 {
667 return (goat3d_light*)g->scn->get_light(name);
668 }
671 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
672 {
673 return new goat3d_light;
674 }
676 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
677 {
678 delete lt;
679 }
682 /* cameras */
683 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
684 {
685 g->scn->add_camera(cam);
686 }
688 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
689 {
690 return g->scn->get_camera_count();
691 }
693 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
694 {
695 return (goat3d_camera*)g->scn->get_camera(idx);
696 }
698 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
699 {
700 return (goat3d_camera*)g->scn->get_camera(name);
701 }
703 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
704 {
705 return new goat3d_camera;
706 }
708 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
709 {
710 delete cam;
711 }
715 // node
716 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
717 {
718 g->scn->add_node(node);
719 }
721 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
722 {
723 return g->scn->get_node_count();
724 }
726 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
727 {
728 return (goat3d_node*)g->scn->get_node(idx);
729 }
731 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
732 {
733 return (goat3d_node*)g->scn->get_node(name);
734 }
736 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
737 {
738 return new goat3d_node;
739 }
741 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
742 {
743 node->set_name(name);
744 }
746 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
747 {
748 return node->get_name();
749 }
751 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
752 {
753 node->set_object((Object*)obj);
754 }
756 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
757 {
758 return (void*)node->get_object();
759 }
761 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
762 {
763 const Object *obj = node->get_object();
764 if(dynamic_cast<const Mesh*>(obj)) {
765 return GOAT3D_NODE_MESH;
766 }
767 if(dynamic_cast<const Light*>(obj)) {
768 return GOAT3D_NODE_LIGHT;
769 }
770 if(dynamic_cast<const Camera*>(obj)) {
771 return GOAT3D_NODE_CAMERA;
772 }
774 return GOAT3D_NODE_NULL;
775 }
777 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
778 {
779 node->add_child(node);
780 }
782 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
783 {
784 return node->get_children_count();
785 }
787 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
788 {
789 return (goat3d_node*)node->get_child(idx);
790 }
792 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
793 {
794 return (goat3d_node*)node->get_parent();
795 }
797 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
798 {
799 node->use_animation(idx);
800 }
802 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
803 {
804 node->use_animation(aidx, bidx, t);
805 }
807 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
808 {
809 node->use_animation(name);
810 }
812 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
813 {
814 node->use_animation(aname, bname, t);
815 }
817 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
818 {
819 return node->get_active_animation_index(which);
820 }
822 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
823 {
824 return node->get_active_animation_mix();
825 }
827 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
828 {
829 return node->get_animation_count();
830 }
832 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
833 {
834 root->add_animation();
835 }
837 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
838 {
839 root->set_animation_name(name);
840 }
842 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
843 {
844 return node->get_animation_name();
845 }
847 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
848 {
849 node->set_position(Vector3(x, y, z), tmsec);
850 }
852 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
853 {
854 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
855 }
857 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
858 {
859 node->set_scaling(Vector3(sx, sy, sz), tmsec);
860 }
862 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
863 {
864 node->set_pivot(Vector3(px, py, pz));
865 }
868 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
869 {
870 Vector3 pos = node->get_position(tmsec);
871 *xptr = pos.x;
872 *yptr = pos.y;
873 *zptr = pos.z;
874 }
876 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
877 {
878 Quaternion q = node->get_rotation(tmsec);
879 *xptr = q.v.x;
880 *yptr = q.v.y;
881 *zptr = q.v.z;
882 *wptr = q.s;
883 }
885 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
886 {
887 Vector3 scale = node->get_scaling(tmsec);
888 *xptr = scale.x;
889 *yptr = scale.y;
890 *zptr = scale.z;
891 }
893 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
894 {
895 Vector3 pivot = node->get_pivot();
896 *xptr = pivot.x;
897 *yptr = pivot.y;
898 *zptr = pivot.z;
899 }
902 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
903 {
904 node->get_xform(tmsec, (Matrix4x4*)matrix);
905 }
908 } // extern "C"
911 static long read_file(void *buf, size_t bytes, void *uptr)
912 {
913 return (long)fread(buf, 1, bytes, (FILE*)uptr);
914 }
916 static long write_file(const void *buf, size_t bytes, void *uptr)
917 {
918 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
919 }
921 static long seek_file(long offs, int whence, void *uptr)
922 {
923 if(fseek((FILE*)uptr, offs, whence) == -1) {
924 return -1;
925 }
926 return ftell((FILE*)uptr);
927 }
929 std::string g3dimpl::clean_filename(const char *str)
930 {
931 const char *last_slash = strrchr(str, '/');
932 if(!last_slash) {
933 last_slash = strrchr(str, '\\');
934 }
936 if(last_slash) {
937 str = last_slash + 1;
938 }
940 char *buf = (char*)alloca(strlen(str) + 1);
941 char *dest = buf;
942 while(*str) {
943 char c = *str++;
944 *dest++ = tolower(c);
945 }
946 *dest = 0;
947 return buf;
948 }