goat3d

view src/goat3d.cc @ 100:fae6dc0fd462

[goatview] disable animation controls on close_scene
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 21 May 2014 05:09:33 +0300
parents 7941e89798e5
children
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_material : public Material {};
35 struct goat3d_mesh : public Mesh {};
36 struct goat3d_light : public Light {};
37 struct goat3d_camera : public Camera {};
38 struct goat3d_node : public Node {};
41 static long read_file(void *buf, size_t bytes, void *uptr);
42 static long write_file(const void *buf, size_t bytes, void *uptr);
43 static long seek_file(long offs, int whence, void *uptr);
45 extern "C" {
47 GOAT3DAPI struct goat3d *goat3d_create(void)
48 {
49 goat3d *goat = new goat3d;
50 goat->flags = 0;
51 goat->search_path = 0;
52 goat->scn = new Scene;
53 goat->scn->goat = goat;
55 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
56 return goat;
57 }
59 GOAT3DAPI void goat3d_free(struct goat3d *g)
60 {
61 delete [] g->search_path;
62 delete g->scn;
63 delete g;
64 }
66 GOAT3DAPI void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
67 {
68 if(val) {
69 g->flags |= (1 << (int)opt);
70 } else {
71 g->flags &= ~(1 << (int)opt);
72 }
73 }
75 GOAT3DAPI int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
76 {
77 return (g->flags >> (int)opt) & 1;
78 }
80 GOAT3DAPI int goat3d_load(struct goat3d *g, const char *fname)
81 {
82 FILE *fp = fopen(fname, "rb");
83 if(!fp) {
84 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
85 return -1;
86 }
88 /* if the filename contained any directory components, keep the prefix
89 * to use it as a search path for external mesh file loading
90 */
91 g->search_path = new char[strlen(fname) + 1];
92 strcpy(g->search_path, fname);
94 char *slash = strrchr(g->search_path, '/');
95 if(slash) {
96 *slash = 0;
97 } else {
98 if((slash = strrchr(g->search_path, '\\'))) {
99 *slash = 0;
100 } else {
101 delete [] g->search_path;
102 g->search_path = 0;
103 }
104 }
106 int res = goat3d_load_file(g, fp);
107 fclose(fp);
108 return res;
109 }
111 GOAT3DAPI int goat3d_save(const struct goat3d *g, const char *fname)
112 {
113 FILE *fp = fopen(fname, "wb");
114 if(!fp) {
115 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
116 return -1;
117 }
119 int res = goat3d_save_file(g, fp);
120 fclose(fp);
121 return res;
122 }
124 GOAT3DAPI int goat3d_load_file(struct goat3d *g, FILE *fp)
125 {
126 goat3d_io io;
127 io.cls = fp;
128 io.read = read_file;
129 io.write = write_file;
130 io.seek = seek_file;
132 return goat3d_load_io(g, &io);
133 }
135 GOAT3DAPI int goat3d_save_file(const struct goat3d *g, FILE *fp)
136 {
137 goat3d_io io;
138 io.cls = fp;
139 io.read = read_file;
140 io.write = write_file;
141 io.seek = seek_file;
143 return goat3d_save_io(g, &io);
144 }
146 GOAT3DAPI int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
147 {
148 if(!g->scn->load(io)) {
149 if(!g->scn->loadxml(io)) {
150 return -1;
151 }
152 }
153 return 0;
154 }
156 GOAT3DAPI int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
157 {
158 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
159 return g->scn->savexml(io) ? 0 : -1;
160 }
161 return g->scn->save(io) ? 0 : -1;
162 }
164 /* save/load animations */
165 GOAT3DAPI int goat3d_load_anim(struct goat3d *g, const char *fname)
166 {
167 FILE *fp = fopen(fname, "rb");
168 if(!fp) {
169 return -1;
170 }
172 int res = goat3d_load_anim_file(g, fp);
173 fclose(fp);
174 return res;
175 }
177 GOAT3DAPI int goat3d_save_anim(const struct goat3d *g, const char *fname)
178 {
179 FILE *fp = fopen(fname, "wb");
180 if(!fp) {
181 return -1;
182 }
184 int res = goat3d_save_anim_file(g, fp);
185 fclose(fp);
186 return res;
187 }
189 GOAT3DAPI int goat3d_load_anim_file(struct goat3d *g, FILE *fp)
190 {
191 goat3d_io io;
192 io.cls = fp;
193 io.read = read_file;
194 io.write = write_file;
195 io.seek = seek_file;
197 return goat3d_load_anim_io(g, &io);
198 }
200 GOAT3DAPI int goat3d_save_anim_file(const struct goat3d *g, FILE *fp)
201 {
202 goat3d_io io;
203 io.cls = fp;
204 io.read = read_file;
205 io.write = write_file;
206 io.seek = seek_file;
208 return goat3d_save_anim_io(g, &io);
209 }
211 GOAT3DAPI int goat3d_load_anim_io(struct goat3d *g, struct goat3d_io *io)
212 {
213 if(!g->scn->load_anim(io)) {
214 if(!g->scn->load_anim_xml(io)) {
215 return -1;
216 }
217 }
218 return 0;
219 }
221 GOAT3DAPI int goat3d_save_anim_io(const struct goat3d *g, struct goat3d_io *io)
222 {
223 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
224 return g->scn->save_anim_xml(io) ? 0 : -1;
225 }
226 return g->scn->save_anim(io) ? 0 : -1;
227 }
230 GOAT3DAPI int goat3d_set_name(struct goat3d *g, const char *name)
231 {
232 g->scn->set_name(name);
233 return 0;
234 }
236 GOAT3DAPI const char *goat3d_get_name(const struct goat3d *g)
237 {
238 return g->scn->get_name();
239 }
241 GOAT3DAPI void goat3d_set_ambient(struct goat3d *g, const float *amb)
242 {
243 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
244 }
246 GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
247 {
248 g->scn->set_ambient(Vector3(ar, ag, ab));
249 }
251 GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g)
252 {
253 return &g->scn->get_ambient().x;
254 }
256 GOAT3DAPI int goat3d_get_bounds(const struct goat3d *g, float *bmin, float *bmax)
257 {
258 AABox bbox = g->scn->get_bounds();
259 if(bbox == AABox()) {
260 return -1;
261 }
263 for(int i=0; i<3; i++) {
264 bmin[i] = bbox.bmin[i];
265 bmax[i] = bbox.bmax[i];
266 }
267 return 0;
268 }
270 // ---- materials ----
271 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
272 {
273 g->scn->add_material(mtl);
274 }
276 GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g)
277 {
278 return g->scn->get_material_count();
279 }
281 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx)
282 {
283 return (goat3d_material*)g->scn->get_material(idx);
284 }
286 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name)
287 {
288 return (goat3d_material*)g->scn->get_material(name);
289 }
291 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
292 {
293 return new goat3d_material;
294 }
296 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
297 {
298 delete mtl;
299 }
301 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
302 {
303 mtl->name = std::string(name);
304 }
306 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
307 {
308 return mtl->name.c_str();
309 }
311 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
312 {
313 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
314 }
316 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
317 {
318 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
319 }
321 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
322 {
323 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
324 }
326 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
327 {
328 (*mtl)[attrib].value = Vector4(r, g, b, a);
329 }
331 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
332 {
333 return &(*mtl)[attrib].value.x;
334 }
336 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
337 {
338 (*mtl)[attrib].map = clean_filename(mapname);
339 }
341 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
342 {
343 return (*mtl)[attrib].map.c_str();
344 }
346 // ---- meshes ----
347 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
348 {
349 g->scn->add_mesh(mesh);
350 }
352 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
353 {
354 return g->scn->get_mesh_count();
355 }
357 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
358 {
359 return (goat3d_mesh*)g->scn->get_mesh(idx);
360 }
362 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
363 {
364 return (goat3d_mesh*)g->scn->get_mesh(name);
365 }
367 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
368 {
369 return new goat3d_mesh;
370 }
372 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
373 {
374 delete mesh;
375 }
377 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
378 {
379 mesh->name = std::string(name);
380 }
382 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
383 {
384 return mesh->name.c_str();
385 }
387 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
388 {
389 mesh->material = mtl;
390 }
392 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
393 {
394 return (goat3d_material*)mesh->material;
395 }
397 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
398 {
399 return (int)mesh->vertices.size();
400 }
402 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
403 {
404 return (int)mesh->faces.size();
405 }
407 // VECDATA is in goat3d_impl.h
408 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
409 {
410 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
411 mesh->vertices = VECDATA(Vector3, data, vnum);
412 return;
413 }
415 if(vnum != (int)mesh->vertices.size()) {
416 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
417 return;
418 }
420 switch(attrib) {
421 case GOAT3D_MESH_ATTR_NORMAL:
422 mesh->normals = VECDATA(Vector3, data, vnum);
423 break;
424 case GOAT3D_MESH_ATTR_TANGENT:
425 mesh->tangents = VECDATA(Vector3, data, vnum);
426 break;
427 case GOAT3D_MESH_ATTR_TEXCOORD:
428 mesh->texcoords = VECDATA(Vector2, data, vnum);
429 break;
430 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
431 mesh->skin_weights = VECDATA(Vector4, data, vnum);
432 break;
433 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
434 mesh->skin_matrices = VECDATA(Int4, data, vnum);
435 break;
436 case GOAT3D_MESH_ATTR_COLOR:
437 mesh->colors = VECDATA(Vector4, data, vnum);
438 default:
439 break;
440 }
441 }
443 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
444 float val)
445 {
446 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
447 }
449 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
450 float x, float y)
451 {
452 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, 0, 1);
453 }
455 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
456 float x, float y, float z)
457 {
458 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
459 }
461 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
462 float x, float y, float z, float w)
463 {
464 switch(attrib) {
465 case GOAT3D_MESH_ATTR_VERTEX:
466 mesh->vertices.push_back(Vector3(x, y, z));
467 break;
468 case GOAT3D_MESH_ATTR_NORMAL:
469 mesh->normals.push_back(Vector3(x, y, z));
470 break;
471 case GOAT3D_MESH_ATTR_TANGENT:
472 mesh->tangents.push_back(Vector3(x, y, z));
473 break;
474 case GOAT3D_MESH_ATTR_TEXCOORD:
475 mesh->texcoords.push_back(Vector2(x, y));
476 break;
477 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
478 mesh->skin_weights.push_back(Vector4(x, y, z, w));
479 break;
480 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
481 mesh->skin_matrices.push_back(Int4(x, y, z, w));
482 break;
483 case GOAT3D_MESH_ATTR_COLOR:
484 mesh->colors.push_back(Vector4(x, y, z, w));
485 default:
486 break;
487 }
488 }
490 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
491 {
492 return goat3d_get_mesh_attrib(mesh, attrib, 0);
493 }
495 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
496 {
497 switch(attrib) {
498 case GOAT3D_MESH_ATTR_VERTEX:
499 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
500 case GOAT3D_MESH_ATTR_NORMAL:
501 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
502 case GOAT3D_MESH_ATTR_TANGENT:
503 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
504 case GOAT3D_MESH_ATTR_TEXCOORD:
505 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
506 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
507 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
508 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
509 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
510 case GOAT3D_MESH_ATTR_COLOR:
511 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
512 default:
513 break;
514 }
515 return 0;
516 }
519 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
520 {
521 mesh->faces = VECDATA(Face, data, num);
522 }
524 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
525 {
526 Face face;
527 face.v[0] = a;
528 face.v[1] = b;
529 face.v[2] = c;
530 mesh->faces.push_back(face);
531 }
533 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
534 {
535 return goat3d_get_mesh_face(mesh, 0);
536 }
538 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
539 {
540 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
541 }
543 // immedate mode state
544 static enum goat3d_im_primitive im_prim;
545 static struct goat3d_mesh *im_mesh;
546 static Vector3 im_norm, im_tang;
547 static Vector2 im_texcoord;
548 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
549 static Int4 im_skinmat;
550 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
553 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
554 {
555 mesh->vertices.clear();
556 mesh->normals.clear();
557 mesh->tangents.clear();
558 mesh->texcoords.clear();
559 mesh->skin_weights.clear();
560 mesh->skin_matrices.clear();
561 mesh->colors.clear();
562 mesh->faces.clear();
564 im_mesh = mesh;
565 memset(im_use, 0, sizeof im_use);
567 im_prim = prim;
568 }
570 GOAT3DAPI void goat3d_end(void)
571 {
572 switch(im_prim) {
573 case GOAT3D_TRIANGLES:
574 {
575 int num_faces = (int)im_mesh->vertices.size() / 3;
576 im_mesh->faces.resize(num_faces);
578 int vidx = 0;
579 for(int i=0; i<num_faces; i++) {
580 im_mesh->faces[i].v[0] = vidx++;
581 im_mesh->faces[i].v[1] = vidx++;
582 im_mesh->faces[i].v[2] = vidx++;
583 }
584 }
585 break;
587 case GOAT3D_QUADS:
588 {
589 int num_quads = (int)im_mesh->vertices.size() / 4;
590 im_mesh->faces.resize(num_quads * 2);
592 int vidx = 0;
593 for(int i=0; i<num_quads; i++) {
594 im_mesh->faces[i * 2].v[0] = vidx;
595 im_mesh->faces[i * 2].v[1] = vidx + 1;
596 im_mesh->faces[i * 2].v[2] = vidx + 2;
598 im_mesh->faces[i * 2 + 1].v[0] = vidx;
599 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
600 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
602 vidx += 4;
603 }
604 }
605 break;
607 default:
608 return;
609 };
610 }
612 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
613 {
614 im_mesh->vertices.push_back(Vector3(x, y, z));
615 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
616 im_mesh->normals.push_back(im_norm);
617 }
618 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
619 im_mesh->tangents.push_back(im_tang);
620 }
621 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
622 im_mesh->texcoords.push_back(im_texcoord);
623 }
624 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
625 im_mesh->skin_weights.push_back(im_skinw);
626 }
627 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
628 im_mesh->skin_matrices.push_back(im_skinmat);
629 }
630 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
631 im_mesh->colors.push_back(im_color);
632 }
633 }
635 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
636 {
637 im_norm = Vector3(x, y, z);
638 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
639 }
641 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
642 {
643 im_tang = Vector3(x, y, z);
644 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
645 }
647 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
648 {
649 im_texcoord = Vector2(x, y);
650 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
651 }
653 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
654 {
655 im_skinw = Vector4(x, y, z, w);
656 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
657 }
659 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
660 {
661 im_skinmat.x = x;
662 im_skinmat.y = y;
663 im_skinmat.z = z;
664 im_skinmat.w = w;
665 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
666 }
668 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
669 {
670 goat3d_color4f(x, y, z, 1.0f);
671 }
673 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
674 {
675 im_color = Vector4(x, y, z, w);
676 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
677 }
680 GOAT3DAPI void goat3d_get_mesh_bounds(const struct goat3d_mesh *mesh, float *bmin, float *bmax)
681 {
682 AABox box = mesh->get_bounds(Matrix4x4::identity);
684 for(int i=0; i<3; i++) {
685 bmin[i] = box.bmin[i];
686 bmax[i] = box.bmax[i];
687 }
688 }
690 /* lights */
691 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
692 {
693 g->scn->add_light(lt);
694 }
696 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
697 {
698 return g->scn->get_light_count();
699 }
701 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
702 {
703 return (goat3d_light*)g->scn->get_light(idx);
704 }
706 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
707 {
708 return (goat3d_light*)g->scn->get_light(name);
709 }
712 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
713 {
714 return new goat3d_light;
715 }
717 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
718 {
719 delete lt;
720 }
723 /* cameras */
724 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
725 {
726 g->scn->add_camera(cam);
727 }
729 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
730 {
731 return g->scn->get_camera_count();
732 }
734 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
735 {
736 return (goat3d_camera*)g->scn->get_camera(idx);
737 }
739 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
740 {
741 return (goat3d_camera*)g->scn->get_camera(name);
742 }
744 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
745 {
746 return new goat3d_camera;
747 }
749 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
750 {
751 delete cam;
752 }
756 // node
757 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
758 {
759 g->scn->add_node(node);
760 }
762 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
763 {
764 return g->scn->get_node_count();
765 }
767 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
768 {
769 return (goat3d_node*)g->scn->get_node(idx);
770 }
772 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
773 {
774 return (goat3d_node*)g->scn->get_node(name);
775 }
777 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
778 {
779 return new goat3d_node;
780 }
782 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
783 {
784 node->set_name(name);
785 }
787 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
788 {
789 return node->get_name();
790 }
792 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
793 {
794 node->set_object((Object*)obj);
795 }
797 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
798 {
799 return (void*)node->get_object();
800 }
802 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
803 {
804 const Object *obj = node->get_object();
805 if(dynamic_cast<const Mesh*>(obj)) {
806 return GOAT3D_NODE_MESH;
807 }
808 if(dynamic_cast<const Light*>(obj)) {
809 return GOAT3D_NODE_LIGHT;
810 }
811 if(dynamic_cast<const Camera*>(obj)) {
812 return GOAT3D_NODE_CAMERA;
813 }
815 return GOAT3D_NODE_NULL;
816 }
818 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
819 {
820 node->add_child(child);
821 }
823 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
824 {
825 return node->get_children_count();
826 }
828 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
829 {
830 return (goat3d_node*)node->get_child(idx);
831 }
833 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
834 {
835 return (goat3d_node*)node->get_parent();
836 }
838 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
839 {
840 node->use_animation(idx);
841 }
843 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
844 {
845 node->use_animation(aidx, bidx, t);
846 }
848 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
849 {
850 node->use_animation(name);
851 }
853 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
854 {
855 node->use_animation(aname, bname, t);
856 }
858 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
859 {
860 return node->get_active_animation_index(which);
861 }
863 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
864 {
865 return node->get_active_animation_mix();
866 }
868 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
869 {
870 return node->get_animation_count();
871 }
873 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
874 {
875 root->add_animation();
876 }
878 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
879 {
880 root->set_animation_name(name);
881 }
883 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
884 {
885 return node->get_animation_name();
886 }
888 GOAT3DAPI long goat3d_get_anim_timeline(struct goat3d_node *root, long *tstart, long *tend)
889 {
890 if(root->get_timeline_bounds(tstart, tend)) {
891 return *tend - *tstart;
892 }
893 return -1;
894 }
896 GOAT3DAPI int goat3d_get_node_position_key_count(struct goat3d_node *node)
897 {
898 return node->get_position_key_count();
899 }
901 GOAT3DAPI int goat3d_get_node_rotation_key_count(struct goat3d_node *node)
902 {
903 return node->get_rotation_key_count();
904 }
906 GOAT3DAPI int goat3d_get_node_scaling_key_count(struct goat3d_node *node)
907 {
908 return node->get_scaling_key_count();
909 }
911 GOAT3DAPI long goat3d_get_node_position_key(struct goat3d_node *node, int idx, float *xptr, float *yptr, float *zptr)
912 {
913 Vector3 pos = node->get_position_key_value(idx);
914 long tm = node->get_position_key_time(idx);
916 if(xptr) *xptr = pos.x;
917 if(yptr) *yptr = pos.y;
918 if(zptr) *zptr = pos.z;
919 return tm;
920 }
922 GOAT3DAPI long goat3d_get_node_rotation_key(struct goat3d_node *node, int idx, float *xptr, float *yptr, float *zptr, float *wptr)
923 {
924 Quaternion rot = node->get_rotation_key_value(idx);
925 long tm = node->get_rotation_key_time(idx);
927 if(xptr) *xptr = rot.v.x;
928 if(yptr) *yptr = rot.v.y;
929 if(zptr) *zptr = rot.v.z;
930 if(wptr) *wptr = rot.s;
931 return tm;
932 }
934 GOAT3DAPI long goat3d_get_node_scaling_key(struct goat3d_node *node, int idx, float *xptr, float *yptr, float *zptr)
935 {
936 Vector3 scale = node->get_scaling_key_value(idx);
937 long tm = node->get_scaling_key_time(idx);
939 if(xptr) *xptr = scale.x;
940 if(yptr) *yptr = scale.y;
941 if(zptr) *zptr = scale.z;
942 return tm;
943 }
945 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
946 {
947 node->set_position(Vector3(x, y, z), tmsec);
948 }
950 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
951 {
952 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
953 }
955 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
956 {
957 node->set_scaling(Vector3(sx, sy, sz), tmsec);
958 }
960 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
961 {
962 node->set_pivot(Vector3(px, py, pz));
963 }
966 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
967 {
968 Vector3 pos = node->get_node_position(tmsec);
969 *xptr = pos.x;
970 *yptr = pos.y;
971 *zptr = pos.z;
972 }
974 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
975 {
976 Quaternion q = node->get_node_rotation(tmsec);
977 *xptr = q.v.x;
978 *yptr = q.v.y;
979 *zptr = q.v.z;
980 *wptr = q.s;
981 }
983 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
984 {
985 Vector3 scale = node->get_node_scaling(tmsec);
986 *xptr = scale.x;
987 *yptr = scale.y;
988 *zptr = scale.z;
989 }
991 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
992 {
993 Vector3 pivot = node->get_pivot();
994 *xptr = pivot.x;
995 *yptr = pivot.y;
996 *zptr = pivot.z;
997 }
1000 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
1002 node->get_node_xform(tmsec, (Matrix4x4*)matrix);
1005 GOAT3DAPI void goat3d_get_node_bounds(const struct goat3d_node *node, float *bmin, float *bmax)
1007 AABox box = node->get_bounds();
1009 for(int i=0; i<3; i++) {
1010 bmin[i] = box.bmin[i];
1011 bmax[i] = box.bmax[i];
1015 } // extern "C"
1018 static long read_file(void *buf, size_t bytes, void *uptr)
1020 return (long)fread(buf, 1, bytes, (FILE*)uptr);
1023 static long write_file(const void *buf, size_t bytes, void *uptr)
1025 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
1028 static long seek_file(long offs, int whence, void *uptr)
1030 if(fseek((FILE*)uptr, offs, whence) == -1) {
1031 return -1;
1033 return ftell((FILE*)uptr);
1036 std::string g3dimpl::clean_filename(const char *str)
1038 const char *last_slash = strrchr(str, '/');
1039 if(!last_slash) {
1040 last_slash = strrchr(str, '\\');
1043 if(last_slash) {
1044 str = last_slash + 1;
1047 char *buf = (char*)alloca(strlen(str) + 1);
1048 char *dest = buf;
1049 while(*str) {
1050 char c = *str++;
1051 *dest++ = tolower(c);
1053 *dest = 0;
1054 return buf;