goat3d
view src/goat3d.cc @ 67:8970ca3d55e0
merged
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 19 Apr 2014 08:01:37 +0300 |
parents | 3751aabbc5b3 99715321ad6d |
children | 0bb33d04f279 |
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 char *fname)
183 {
184 FILE *fp = fopen(fname, "wb");
185 if(!fp) {
186 return -1;
187 }
189 int res = goat3d_save_anim_file(g, 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, 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, &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, struct goat3d_io *io)
227 {
228 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
229 return g->scn->save_anim_xml(io) ? 0 : -1;
230 }
231 return g->scn->save_anim(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 int goat3d_get_mtl_count(struct goat3d *g)
268 {
269 return g->scn->get_material_count();
270 }
272 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx)
273 {
274 return (goat3d_material*)g->scn->get_material(idx);
275 }
277 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name)
278 {
279 return (goat3d_material*)g->scn->get_material(name);
280 }
282 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
283 {
284 return new goat3d_material;
285 }
287 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
288 {
289 delete mtl;
290 }
292 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
293 {
294 mtl->name = std::string(name);
295 }
297 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
298 {
299 return mtl->name.c_str();
300 }
302 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
303 {
304 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
305 }
307 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
308 {
309 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
310 }
312 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
313 {
314 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
315 }
317 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
318 {
319 (*mtl)[attrib].value = Vector4(r, g, b, a);
320 }
322 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
323 {
324 return &(*mtl)[attrib].value.x;
325 }
327 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
328 {
329 (*mtl)[attrib].map = clean_filename(mapname);
330 }
332 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
333 {
334 return (*mtl)[attrib].map.c_str();
335 }
337 // ---- meshes ----
338 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
339 {
340 g->scn->add_mesh(mesh);
341 }
343 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
344 {
345 return g->scn->get_mesh_count();
346 }
348 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
349 {
350 return (goat3d_mesh*)g->scn->get_mesh(idx);
351 }
353 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
354 {
355 return (goat3d_mesh*)g->scn->get_mesh(name);
356 }
358 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
359 {
360 return new goat3d_mesh;
361 }
363 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
364 {
365 delete mesh;
366 }
368 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
369 {
370 mesh->name = std::string(name);
371 }
373 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
374 {
375 return mesh->name.c_str();
376 }
378 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
379 {
380 mesh->material = mtl;
381 }
383 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
384 {
385 return (goat3d_material*)mesh->material;
386 }
388 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
389 {
390 return (int)mesh->vertices.size();
391 }
393 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
394 {
395 return (int)mesh->faces.size();
396 }
398 // VECDATA is in goat3d_impl.h
399 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
400 {
401 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
402 mesh->vertices = VECDATA(Vector3, data, vnum);
403 return;
404 }
406 if(vnum != (int)mesh->vertices.size()) {
407 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
408 return;
409 }
411 switch(attrib) {
412 case GOAT3D_MESH_ATTR_NORMAL:
413 mesh->normals = VECDATA(Vector3, data, vnum);
414 break;
415 case GOAT3D_MESH_ATTR_TANGENT:
416 mesh->tangents = VECDATA(Vector3, data, vnum);
417 break;
418 case GOAT3D_MESH_ATTR_TEXCOORD:
419 mesh->texcoords = VECDATA(Vector2, data, vnum);
420 break;
421 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
422 mesh->skin_weights = VECDATA(Vector4, data, vnum);
423 break;
424 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
425 mesh->skin_matrices = VECDATA(Int4, data, vnum);
426 break;
427 case GOAT3D_MESH_ATTR_COLOR:
428 mesh->colors = VECDATA(Vector4, data, vnum);
429 default:
430 break;
431 }
432 }
434 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
435 float val)
436 {
437 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
438 }
440 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
441 float x, float y)
442 {
443 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, 0, 1);
444 }
446 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
447 float x, float y, float z)
448 {
449 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
450 }
452 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
453 float x, float y, float z, float w)
454 {
455 switch(attrib) {
456 case GOAT3D_MESH_ATTR_VERTEX:
457 mesh->vertices.push_back(Vector3(x, y, z));
458 break;
459 case GOAT3D_MESH_ATTR_NORMAL:
460 mesh->normals.push_back(Vector3(x, y, z));
461 break;
462 case GOAT3D_MESH_ATTR_TANGENT:
463 mesh->tangents.push_back(Vector3(x, y, z));
464 break;
465 case GOAT3D_MESH_ATTR_TEXCOORD:
466 mesh->texcoords.push_back(Vector2(x, y));
467 break;
468 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
469 mesh->skin_weights.push_back(Vector4(x, y, z, w));
470 break;
471 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
472 mesh->skin_matrices.push_back(Int4(x, y, z, w));
473 break;
474 case GOAT3D_MESH_ATTR_COLOR:
475 mesh->colors.push_back(Vector4(x, y, z, w));
476 default:
477 break;
478 }
479 }
481 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
482 {
483 return goat3d_get_mesh_attrib(mesh, attrib, 0);
484 }
486 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
487 {
488 switch(attrib) {
489 case GOAT3D_MESH_ATTR_VERTEX:
490 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
491 case GOAT3D_MESH_ATTR_NORMAL:
492 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
493 case GOAT3D_MESH_ATTR_TANGENT:
494 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
495 case GOAT3D_MESH_ATTR_TEXCOORD:
496 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
497 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
498 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
499 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
500 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
501 case GOAT3D_MESH_ATTR_COLOR:
502 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
503 default:
504 break;
505 }
506 return 0;
507 }
510 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
511 {
512 mesh->faces = VECDATA(Face, data, num);
513 }
515 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
516 {
517 Face face;
518 face.v[0] = a;
519 face.v[1] = b;
520 face.v[2] = c;
521 mesh->faces.push_back(face);
522 }
524 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
525 {
526 return goat3d_get_mesh_face(mesh, 0);
527 }
529 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
530 {
531 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
532 }
534 // immedate mode state
535 static enum goat3d_im_primitive im_prim;
536 static struct goat3d_mesh *im_mesh;
537 static Vector3 im_norm, im_tang;
538 static Vector2 im_texcoord;
539 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
540 static Int4 im_skinmat;
541 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
544 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
545 {
546 mesh->vertices.clear();
547 mesh->normals.clear();
548 mesh->tangents.clear();
549 mesh->texcoords.clear();
550 mesh->skin_weights.clear();
551 mesh->skin_matrices.clear();
552 mesh->colors.clear();
553 mesh->faces.clear();
555 im_mesh = mesh;
556 memset(im_use, 0, sizeof im_use);
558 im_prim = prim;
559 }
561 GOAT3DAPI void goat3d_end(void)
562 {
563 switch(im_prim) {
564 case GOAT3D_TRIANGLES:
565 {
566 int num_faces = (int)im_mesh->vertices.size() / 3;
567 im_mesh->faces.resize(num_faces);
569 int vidx = 0;
570 for(int i=0; i<num_faces; i++) {
571 im_mesh->faces[i].v[0] = vidx++;
572 im_mesh->faces[i].v[1] = vidx++;
573 im_mesh->faces[i].v[2] = vidx++;
574 }
575 }
576 break;
578 case GOAT3D_QUADS:
579 {
580 int num_quads = (int)im_mesh->vertices.size() / 4;
581 im_mesh->faces.resize(num_quads * 2);
583 int vidx = 0;
584 for(int i=0; i<num_quads; i++) {
585 im_mesh->faces[i * 2].v[0] = vidx;
586 im_mesh->faces[i * 2].v[1] = vidx + 1;
587 im_mesh->faces[i * 2].v[2] = vidx + 2;
589 im_mesh->faces[i * 2 + 1].v[0] = vidx;
590 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
591 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
593 vidx += 4;
594 }
595 }
596 break;
598 default:
599 return;
600 };
601 }
603 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
604 {
605 im_mesh->vertices.push_back(Vector3(x, y, z));
606 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
607 im_mesh->normals.push_back(im_norm);
608 }
609 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
610 im_mesh->tangents.push_back(im_tang);
611 }
612 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
613 im_mesh->texcoords.push_back(im_texcoord);
614 }
615 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
616 im_mesh->skin_weights.push_back(im_skinw);
617 }
618 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
619 im_mesh->skin_matrices.push_back(im_skinmat);
620 }
621 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
622 im_mesh->colors.push_back(im_color);
623 }
624 }
626 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
627 {
628 im_norm = Vector3(x, y, z);
629 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
630 }
632 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
633 {
634 im_tang = Vector3(x, y, z);
635 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
636 }
638 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
639 {
640 im_texcoord = Vector2(x, y);
641 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
642 }
644 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
645 {
646 im_skinw = Vector4(x, y, z, w);
647 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
648 }
650 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
651 {
652 im_skinmat.x = x;
653 im_skinmat.y = y;
654 im_skinmat.z = z;
655 im_skinmat.w = w;
656 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
657 }
659 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
660 {
661 goat3d_color4f(x, y, z, 1.0f);
662 }
664 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
665 {
666 im_color = Vector4(x, y, z, w);
667 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
668 }
670 /* lights */
671 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
672 {
673 g->scn->add_light(lt);
674 }
676 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
677 {
678 return g->scn->get_light_count();
679 }
681 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
682 {
683 return (goat3d_light*)g->scn->get_light(idx);
684 }
686 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
687 {
688 return (goat3d_light*)g->scn->get_light(name);
689 }
692 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
693 {
694 return new goat3d_light;
695 }
697 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
698 {
699 delete lt;
700 }
703 /* cameras */
704 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
705 {
706 g->scn->add_camera(cam);
707 }
709 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
710 {
711 return g->scn->get_camera_count();
712 }
714 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
715 {
716 return (goat3d_camera*)g->scn->get_camera(idx);
717 }
719 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
720 {
721 return (goat3d_camera*)g->scn->get_camera(name);
722 }
724 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
725 {
726 return new goat3d_camera;
727 }
729 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
730 {
731 delete cam;
732 }
736 // node
737 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
738 {
739 g->scn->add_node(node);
740 }
742 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
743 {
744 return g->scn->get_node_count();
745 }
747 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
748 {
749 return (goat3d_node*)g->scn->get_node(idx);
750 }
752 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
753 {
754 return (goat3d_node*)g->scn->get_node(name);
755 }
757 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
758 {
759 return new goat3d_node;
760 }
762 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
763 {
764 node->set_name(name);
765 }
767 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
768 {
769 return node->get_name();
770 }
772 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
773 {
774 node->set_object((Object*)obj);
775 }
777 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
778 {
779 return (void*)node->get_object();
780 }
782 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
783 {
784 const Object *obj = node->get_object();
785 if(dynamic_cast<const Mesh*>(obj)) {
786 return GOAT3D_NODE_MESH;
787 }
788 if(dynamic_cast<const Light*>(obj)) {
789 return GOAT3D_NODE_LIGHT;
790 }
791 if(dynamic_cast<const Camera*>(obj)) {
792 return GOAT3D_NODE_CAMERA;
793 }
795 return GOAT3D_NODE_NULL;
796 }
798 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
799 {
800 node->add_child(child);
801 }
803 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
804 {
805 return node->get_children_count();
806 }
808 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
809 {
810 return (goat3d_node*)node->get_child(idx);
811 }
813 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
814 {
815 return (goat3d_node*)node->get_parent();
816 }
818 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
819 {
820 node->use_animation(idx);
821 }
823 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
824 {
825 node->use_animation(aidx, bidx, t);
826 }
828 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
829 {
830 node->use_animation(name);
831 }
833 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
834 {
835 node->use_animation(aname, bname, t);
836 }
838 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
839 {
840 return node->get_active_animation_index(which);
841 }
843 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
844 {
845 return node->get_active_animation_mix();
846 }
848 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
849 {
850 return node->get_animation_count();
851 }
853 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
854 {
855 root->add_animation();
856 }
858 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
859 {
860 root->set_animation_name(name);
861 }
863 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
864 {
865 return node->get_animation_name();
866 }
868 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
869 {
870 node->set_position(Vector3(x, y, z), tmsec);
871 }
873 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
874 {
875 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
876 }
878 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
879 {
880 node->set_scaling(Vector3(sx, sy, sz), tmsec);
881 }
883 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
884 {
885 node->set_pivot(Vector3(px, py, pz));
886 }
889 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
890 {
891 Vector3 pos = node->get_position(tmsec);
892 *xptr = pos.x;
893 *yptr = pos.y;
894 *zptr = pos.z;
895 }
897 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
898 {
899 Quaternion q = node->get_rotation(tmsec);
900 *xptr = q.v.x;
901 *yptr = q.v.y;
902 *zptr = q.v.z;
903 *wptr = q.s;
904 }
906 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
907 {
908 Vector3 scale = node->get_scaling(tmsec);
909 *xptr = scale.x;
910 *yptr = scale.y;
911 *zptr = scale.z;
912 }
914 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
915 {
916 Vector3 pivot = node->get_pivot();
917 *xptr = pivot.x;
918 *yptr = pivot.y;
919 *zptr = pivot.z;
920 }
923 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
924 {
925 node->get_xform(tmsec, (Matrix4x4*)matrix);
926 }
929 } // extern "C"
932 static long read_file(void *buf, size_t bytes, void *uptr)
933 {
934 return (long)fread(buf, 1, bytes, (FILE*)uptr);
935 }
937 static long write_file(const void *buf, size_t bytes, void *uptr)
938 {
939 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
940 }
942 static long seek_file(long offs, int whence, void *uptr)
943 {
944 if(fseek((FILE*)uptr, offs, whence) == -1) {
945 return -1;
946 }
947 return ftell((FILE*)uptr);
948 }
950 std::string g3dimpl::clean_filename(const char *str)
951 {
952 const char *last_slash = strrchr(str, '/');
953 if(!last_slash) {
954 last_slash = strrchr(str, '\\');
955 }
957 if(last_slash) {
958 str = last_slash + 1;
959 }
961 char *buf = (char*)alloca(strlen(str) + 1);
962 char *dest = buf;
963 while(*str) {
964 char c = *str++;
965 *dest++ = tolower(c);
966 }
967 *dest = 0;
968 return buf;
969 }