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