goat3d

view src/goat3d.cc @ 79:a42f6cd4e2fa

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 08 May 2014 19:30:49 +0300
parents ab66cdabf6f2
children 70b7c41a4f17
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 void goat3d_get_bounds(const struct goat3d *g, float *bmin, float *bmax)
257 {
258 AABox bbox = g->scn->get_bounds();
259 for(int i=0; i<3; i++) {
260 bmin[i] = bbox.bmin[i];
261 bmax[i] = bbox.bmax[i];
262 }
263 }
265 // ---- materials ----
266 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
267 {
268 g->scn->add_material(mtl);
269 }
271 GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g)
272 {
273 return g->scn->get_material_count();
274 }
276 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx)
277 {
278 return (goat3d_material*)g->scn->get_material(idx);
279 }
281 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name)
282 {
283 return (goat3d_material*)g->scn->get_material(name);
284 }
286 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
287 {
288 return new goat3d_material;
289 }
291 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
292 {
293 delete mtl;
294 }
296 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
297 {
298 mtl->name = std::string(name);
299 }
301 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
302 {
303 return mtl->name.c_str();
304 }
306 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
307 {
308 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
309 }
311 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
312 {
313 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
314 }
316 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
317 {
318 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
319 }
321 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
322 {
323 (*mtl)[attrib].value = Vector4(r, g, b, a);
324 }
326 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
327 {
328 return &(*mtl)[attrib].value.x;
329 }
331 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
332 {
333 (*mtl)[attrib].map = clean_filename(mapname);
334 }
336 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
337 {
338 return (*mtl)[attrib].map.c_str();
339 }
341 // ---- meshes ----
342 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
343 {
344 g->scn->add_mesh(mesh);
345 }
347 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
348 {
349 return g->scn->get_mesh_count();
350 }
352 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
353 {
354 return (goat3d_mesh*)g->scn->get_mesh(idx);
355 }
357 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
358 {
359 return (goat3d_mesh*)g->scn->get_mesh(name);
360 }
362 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
363 {
364 return new goat3d_mesh;
365 }
367 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
368 {
369 delete mesh;
370 }
372 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
373 {
374 mesh->name = std::string(name);
375 }
377 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
378 {
379 return mesh->name.c_str();
380 }
382 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
383 {
384 mesh->material = mtl;
385 }
387 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
388 {
389 return (goat3d_material*)mesh->material;
390 }
392 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
393 {
394 return (int)mesh->vertices.size();
395 }
397 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
398 {
399 return (int)mesh->faces.size();
400 }
402 // VECDATA is in goat3d_impl.h
403 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
404 {
405 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
406 mesh->vertices = VECDATA(Vector3, data, vnum);
407 return;
408 }
410 if(vnum != (int)mesh->vertices.size()) {
411 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
412 return;
413 }
415 switch(attrib) {
416 case GOAT3D_MESH_ATTR_NORMAL:
417 mesh->normals = VECDATA(Vector3, data, vnum);
418 break;
419 case GOAT3D_MESH_ATTR_TANGENT:
420 mesh->tangents = VECDATA(Vector3, data, vnum);
421 break;
422 case GOAT3D_MESH_ATTR_TEXCOORD:
423 mesh->texcoords = VECDATA(Vector2, data, vnum);
424 break;
425 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
426 mesh->skin_weights = VECDATA(Vector4, data, vnum);
427 break;
428 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
429 mesh->skin_matrices = VECDATA(Int4, data, vnum);
430 break;
431 case GOAT3D_MESH_ATTR_COLOR:
432 mesh->colors = VECDATA(Vector4, data, vnum);
433 default:
434 break;
435 }
436 }
438 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
439 float val)
440 {
441 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
442 }
444 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
445 float x, float y)
446 {
447 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, 0, 1);
448 }
450 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
451 float x, float y, float z)
452 {
453 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
454 }
456 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
457 float x, float y, float z, float w)
458 {
459 switch(attrib) {
460 case GOAT3D_MESH_ATTR_VERTEX:
461 mesh->vertices.push_back(Vector3(x, y, z));
462 break;
463 case GOAT3D_MESH_ATTR_NORMAL:
464 mesh->normals.push_back(Vector3(x, y, z));
465 break;
466 case GOAT3D_MESH_ATTR_TANGENT:
467 mesh->tangents.push_back(Vector3(x, y, z));
468 break;
469 case GOAT3D_MESH_ATTR_TEXCOORD:
470 mesh->texcoords.push_back(Vector2(x, y));
471 break;
472 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
473 mesh->skin_weights.push_back(Vector4(x, y, z, w));
474 break;
475 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
476 mesh->skin_matrices.push_back(Int4(x, y, z, w));
477 break;
478 case GOAT3D_MESH_ATTR_COLOR:
479 mesh->colors.push_back(Vector4(x, y, z, w));
480 default:
481 break;
482 }
483 }
485 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
486 {
487 return goat3d_get_mesh_attrib(mesh, attrib, 0);
488 }
490 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
491 {
492 switch(attrib) {
493 case GOAT3D_MESH_ATTR_VERTEX:
494 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
495 case GOAT3D_MESH_ATTR_NORMAL:
496 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
497 case GOAT3D_MESH_ATTR_TANGENT:
498 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
499 case GOAT3D_MESH_ATTR_TEXCOORD:
500 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
501 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
502 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
503 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
504 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
505 case GOAT3D_MESH_ATTR_COLOR:
506 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
507 default:
508 break;
509 }
510 return 0;
511 }
514 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
515 {
516 mesh->faces = VECDATA(Face, data, num);
517 }
519 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
520 {
521 Face face;
522 face.v[0] = a;
523 face.v[1] = b;
524 face.v[2] = c;
525 mesh->faces.push_back(face);
526 }
528 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
529 {
530 return goat3d_get_mesh_face(mesh, 0);
531 }
533 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
534 {
535 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
536 }
538 // immedate mode state
539 static enum goat3d_im_primitive im_prim;
540 static struct goat3d_mesh *im_mesh;
541 static Vector3 im_norm, im_tang;
542 static Vector2 im_texcoord;
543 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
544 static Int4 im_skinmat;
545 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
548 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
549 {
550 mesh->vertices.clear();
551 mesh->normals.clear();
552 mesh->tangents.clear();
553 mesh->texcoords.clear();
554 mesh->skin_weights.clear();
555 mesh->skin_matrices.clear();
556 mesh->colors.clear();
557 mesh->faces.clear();
559 im_mesh = mesh;
560 memset(im_use, 0, sizeof im_use);
562 im_prim = prim;
563 }
565 GOAT3DAPI void goat3d_end(void)
566 {
567 switch(im_prim) {
568 case GOAT3D_TRIANGLES:
569 {
570 int num_faces = (int)im_mesh->vertices.size() / 3;
571 im_mesh->faces.resize(num_faces);
573 int vidx = 0;
574 for(int i=0; i<num_faces; i++) {
575 im_mesh->faces[i].v[0] = vidx++;
576 im_mesh->faces[i].v[1] = vidx++;
577 im_mesh->faces[i].v[2] = vidx++;
578 }
579 }
580 break;
582 case GOAT3D_QUADS:
583 {
584 int num_quads = (int)im_mesh->vertices.size() / 4;
585 im_mesh->faces.resize(num_quads * 2);
587 int vidx = 0;
588 for(int i=0; i<num_quads; i++) {
589 im_mesh->faces[i * 2].v[0] = vidx;
590 im_mesh->faces[i * 2].v[1] = vidx + 1;
591 im_mesh->faces[i * 2].v[2] = vidx + 2;
593 im_mesh->faces[i * 2 + 1].v[0] = vidx;
594 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
595 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
597 vidx += 4;
598 }
599 }
600 break;
602 default:
603 return;
604 };
605 }
607 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
608 {
609 im_mesh->vertices.push_back(Vector3(x, y, z));
610 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
611 im_mesh->normals.push_back(im_norm);
612 }
613 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
614 im_mesh->tangents.push_back(im_tang);
615 }
616 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
617 im_mesh->texcoords.push_back(im_texcoord);
618 }
619 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
620 im_mesh->skin_weights.push_back(im_skinw);
621 }
622 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
623 im_mesh->skin_matrices.push_back(im_skinmat);
624 }
625 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
626 im_mesh->colors.push_back(im_color);
627 }
628 }
630 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
631 {
632 im_norm = Vector3(x, y, z);
633 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
634 }
636 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
637 {
638 im_tang = Vector3(x, y, z);
639 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
640 }
642 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
643 {
644 im_texcoord = Vector2(x, y);
645 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
646 }
648 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
649 {
650 im_skinw = Vector4(x, y, z, w);
651 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
652 }
654 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
655 {
656 im_skinmat.x = x;
657 im_skinmat.y = y;
658 im_skinmat.z = z;
659 im_skinmat.w = w;
660 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
661 }
663 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
664 {
665 goat3d_color4f(x, y, z, 1.0f);
666 }
668 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
669 {
670 im_color = Vector4(x, y, z, w);
671 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
672 }
674 /* lights */
675 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
676 {
677 g->scn->add_light(lt);
678 }
680 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
681 {
682 return g->scn->get_light_count();
683 }
685 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
686 {
687 return (goat3d_light*)g->scn->get_light(idx);
688 }
690 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
691 {
692 return (goat3d_light*)g->scn->get_light(name);
693 }
696 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
697 {
698 return new goat3d_light;
699 }
701 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
702 {
703 delete lt;
704 }
707 /* cameras */
708 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
709 {
710 g->scn->add_camera(cam);
711 }
713 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
714 {
715 return g->scn->get_camera_count();
716 }
718 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
719 {
720 return (goat3d_camera*)g->scn->get_camera(idx);
721 }
723 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
724 {
725 return (goat3d_camera*)g->scn->get_camera(name);
726 }
728 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
729 {
730 return new goat3d_camera;
731 }
733 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
734 {
735 delete cam;
736 }
740 // node
741 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
742 {
743 g->scn->add_node(node);
744 }
746 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
747 {
748 return g->scn->get_node_count();
749 }
751 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
752 {
753 return (goat3d_node*)g->scn->get_node(idx);
754 }
756 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
757 {
758 return (goat3d_node*)g->scn->get_node(name);
759 }
761 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
762 {
763 return new goat3d_node;
764 }
766 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
767 {
768 node->set_name(name);
769 }
771 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
772 {
773 return node->get_name();
774 }
776 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
777 {
778 node->set_object((Object*)obj);
779 }
781 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
782 {
783 return (void*)node->get_object();
784 }
786 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
787 {
788 const Object *obj = node->get_object();
789 if(dynamic_cast<const Mesh*>(obj)) {
790 return GOAT3D_NODE_MESH;
791 }
792 if(dynamic_cast<const Light*>(obj)) {
793 return GOAT3D_NODE_LIGHT;
794 }
795 if(dynamic_cast<const Camera*>(obj)) {
796 return GOAT3D_NODE_CAMERA;
797 }
799 return GOAT3D_NODE_NULL;
800 }
802 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
803 {
804 node->add_child(child);
805 }
807 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
808 {
809 return node->get_children_count();
810 }
812 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
813 {
814 return (goat3d_node*)node->get_child(idx);
815 }
817 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
818 {
819 return (goat3d_node*)node->get_parent();
820 }
822 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
823 {
824 node->use_animation(idx);
825 }
827 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
828 {
829 node->use_animation(aidx, bidx, t);
830 }
832 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
833 {
834 node->use_animation(name);
835 }
837 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
838 {
839 node->use_animation(aname, bname, t);
840 }
842 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
843 {
844 return node->get_active_animation_index(which);
845 }
847 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
848 {
849 return node->get_active_animation_mix();
850 }
852 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
853 {
854 return node->get_animation_count();
855 }
857 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
858 {
859 root->add_animation();
860 }
862 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
863 {
864 root->set_animation_name(name);
865 }
867 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
868 {
869 return node->get_animation_name();
870 }
872 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
873 {
874 node->set_position(Vector3(x, y, z), tmsec);
875 }
877 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
878 {
879 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
880 }
882 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
883 {
884 node->set_scaling(Vector3(sx, sy, sz), tmsec);
885 }
887 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
888 {
889 node->set_pivot(Vector3(px, py, pz));
890 }
893 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
894 {
895 Vector3 pos = node->get_position(tmsec);
896 *xptr = pos.x;
897 *yptr = pos.y;
898 *zptr = pos.z;
899 }
901 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
902 {
903 Quaternion q = node->get_rotation(tmsec);
904 *xptr = q.v.x;
905 *yptr = q.v.y;
906 *zptr = q.v.z;
907 *wptr = q.s;
908 }
910 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
911 {
912 Vector3 scale = node->get_scaling(tmsec);
913 *xptr = scale.x;
914 *yptr = scale.y;
915 *zptr = scale.z;
916 }
918 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
919 {
920 Vector3 pivot = node->get_pivot();
921 *xptr = pivot.x;
922 *yptr = pivot.y;
923 *zptr = pivot.z;
924 }
927 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
928 {
929 node->get_xform(tmsec, (Matrix4x4*)matrix);
930 }
933 } // extern "C"
936 static long read_file(void *buf, size_t bytes, void *uptr)
937 {
938 return (long)fread(buf, 1, bytes, (FILE*)uptr);
939 }
941 static long write_file(const void *buf, size_t bytes, void *uptr)
942 {
943 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
944 }
946 static long seek_file(long offs, int whence, void *uptr)
947 {
948 if(fseek((FILE*)uptr, offs, whence) == -1) {
949 return -1;
950 }
951 return ftell((FILE*)uptr);
952 }
954 std::string g3dimpl::clean_filename(const char *str)
955 {
956 const char *last_slash = strrchr(str, '/');
957 if(!last_slash) {
958 last_slash = strrchr(str, '\\');
959 }
961 if(last_slash) {
962 str = last_slash + 1;
963 }
965 char *buf = (char*)alloca(strlen(str) + 1);
966 char *dest = buf;
967 while(*str) {
968 char c = *str++;
969 *dest++ = tolower(c);
970 }
971 *dest = 0;
972 return buf;
973 }