goat3d
view src/goat3d.cc @ 70:0bb33d04f279
fixed missing assignment to the scene goat pointer in goat3d_create
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 20 Apr 2014 13:36:28 +0300 |
parents | 8970ca3d55e0 |
children | 36e39632db75 |
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;
59 goat->scn->goat = goat;
61 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
62 return goat;
63 }
65 GOAT3DAPI void goat3d_free(struct goat3d *g)
66 {
67 delete g->search_path;
68 delete g->scn;
69 delete g;
70 }
72 GOAT3DAPI void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
73 {
74 if(val) {
75 g->flags |= (1 << (int)opt);
76 } else {
77 g->flags &= ~(1 << (int)opt);
78 }
79 }
81 GOAT3DAPI int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
82 {
83 return (g->flags >> (int)opt) & 1;
84 }
86 GOAT3DAPI int goat3d_load(struct goat3d *g, const char *fname)
87 {
88 FILE *fp = fopen(fname, "rb");
89 if(!fp) {
90 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
91 return -1;
92 }
94 /* if the filename contained any directory components, keep the prefix
95 * to use it as a search path for external mesh file loading
96 */
97 g->search_path = new char[strlen(fname) + 1];
98 strcpy(g->search_path, fname);
100 char *slash = strrchr(g->search_path, '/');
101 if(slash) {
102 *slash = 0;
103 } else {
104 if((slash = strrchr(g->search_path, '\\'))) {
105 *slash = 0;
106 } else {
107 delete [] g->search_path;
108 g->search_path = 0;
109 }
110 }
112 int res = goat3d_load_file(g, fp);
113 fclose(fp);
114 return res;
115 }
117 GOAT3DAPI int goat3d_save(const struct goat3d *g, const char *fname)
118 {
119 FILE *fp = fopen(fname, "wb");
120 if(!fp) {
121 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
122 return -1;
123 }
125 int res = goat3d_save_file(g, fp);
126 fclose(fp);
127 return res;
128 }
130 GOAT3DAPI int goat3d_load_file(struct goat3d *g, FILE *fp)
131 {
132 goat3d_io io;
133 io.cls = fp;
134 io.read = read_file;
135 io.write = write_file;
136 io.seek = seek_file;
138 return goat3d_load_io(g, &io);
139 }
141 GOAT3DAPI int goat3d_save_file(const struct goat3d *g, FILE *fp)
142 {
143 goat3d_io io;
144 io.cls = fp;
145 io.read = read_file;
146 io.write = write_file;
147 io.seek = seek_file;
149 return goat3d_save_io(g, &io);
150 }
152 GOAT3DAPI int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
153 {
154 if(!g->scn->load(io)) {
155 if(!g->scn->loadxml(io)) {
156 return -1;
157 }
158 }
159 return 0;
160 }
162 GOAT3DAPI int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
163 {
164 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
165 return g->scn->savexml(io) ? 0 : -1;
166 }
167 return g->scn->save(io) ? 0 : -1;
168 }
170 /* save/load animations */
171 GOAT3DAPI int goat3d_load_anim(struct goat3d *g, const char *fname)
172 {
173 FILE *fp = fopen(fname, "rb");
174 if(!fp) {
175 return -1;
176 }
178 int res = goat3d_load_anim_file(g, fp);
179 fclose(fp);
180 return res;
181 }
183 GOAT3DAPI int goat3d_save_anim(const struct goat3d *g, const char *fname)
184 {
185 FILE *fp = fopen(fname, "wb");
186 if(!fp) {
187 return -1;
188 }
190 int res = goat3d_save_anim_file(g, fp);
191 fclose(fp);
192 return res;
193 }
195 GOAT3DAPI int goat3d_load_anim_file(struct goat3d *g, FILE *fp)
196 {
197 goat3d_io io;
198 io.cls = fp;
199 io.read = read_file;
200 io.write = write_file;
201 io.seek = seek_file;
203 return goat3d_load_anim_io(g, &io);
204 }
206 GOAT3DAPI int goat3d_save_anim_file(const struct goat3d *g, FILE *fp)
207 {
208 goat3d_io io;
209 io.cls = fp;
210 io.read = read_file;
211 io.write = write_file;
212 io.seek = seek_file;
214 return goat3d_save_anim_io(g, &io);
215 }
217 GOAT3DAPI int goat3d_load_anim_io(struct goat3d *g, struct goat3d_io *io)
218 {
219 if(!g->scn->load_anim(io)) {
220 if(!g->scn->load_anim_xml(io)) {
221 return -1;
222 }
223 }
224 return 0;
225 }
227 GOAT3DAPI int goat3d_save_anim_io(const struct goat3d *g, struct goat3d_io *io)
228 {
229 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
230 return g->scn->save_anim_xml(io) ? 0 : -1;
231 }
232 return g->scn->save_anim(io) ? 0 : -1;
233 }
236 GOAT3DAPI int goat3d_set_name(struct goat3d *g, const char *name)
237 {
238 g->scn->set_name(name);
239 return 0;
240 }
242 GOAT3DAPI const char *goat3d_get_name(const struct goat3d *g)
243 {
244 return g->scn->get_name();
245 }
247 GOAT3DAPI void goat3d_set_ambient(struct goat3d *g, const float *amb)
248 {
249 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
250 }
252 GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
253 {
254 g->scn->set_ambient(Vector3(ar, ag, ab));
255 }
257 GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g)
258 {
259 return &g->scn->get_ambient().x;
260 }
262 // ---- materials ----
263 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
264 {
265 g->scn->add_material(mtl);
266 }
268 GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g)
269 {
270 return g->scn->get_material_count();
271 }
273 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx)
274 {
275 return (goat3d_material*)g->scn->get_material(idx);
276 }
278 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name)
279 {
280 return (goat3d_material*)g->scn->get_material(name);
281 }
283 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
284 {
285 return new goat3d_material;
286 }
288 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
289 {
290 delete mtl;
291 }
293 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
294 {
295 mtl->name = std::string(name);
296 }
298 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
299 {
300 return mtl->name.c_str();
301 }
303 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
304 {
305 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
306 }
308 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
309 {
310 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
311 }
313 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
314 {
315 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
316 }
318 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
319 {
320 (*mtl)[attrib].value = Vector4(r, g, b, a);
321 }
323 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
324 {
325 return &(*mtl)[attrib].value.x;
326 }
328 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
329 {
330 (*mtl)[attrib].map = clean_filename(mapname);
331 }
333 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
334 {
335 return (*mtl)[attrib].map.c_str();
336 }
338 // ---- meshes ----
339 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
340 {
341 g->scn->add_mesh(mesh);
342 }
344 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
345 {
346 return g->scn->get_mesh_count();
347 }
349 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
350 {
351 return (goat3d_mesh*)g->scn->get_mesh(idx);
352 }
354 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
355 {
356 return (goat3d_mesh*)g->scn->get_mesh(name);
357 }
359 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
360 {
361 return new goat3d_mesh;
362 }
364 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
365 {
366 delete mesh;
367 }
369 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
370 {
371 mesh->name = std::string(name);
372 }
374 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
375 {
376 return mesh->name.c_str();
377 }
379 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
380 {
381 mesh->material = mtl;
382 }
384 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
385 {
386 return (goat3d_material*)mesh->material;
387 }
389 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
390 {
391 return (int)mesh->vertices.size();
392 }
394 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
395 {
396 return (int)mesh->faces.size();
397 }
399 // VECDATA is in goat3d_impl.h
400 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
401 {
402 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
403 mesh->vertices = VECDATA(Vector3, data, vnum);
404 return;
405 }
407 if(vnum != (int)mesh->vertices.size()) {
408 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
409 return;
410 }
412 switch(attrib) {
413 case GOAT3D_MESH_ATTR_NORMAL:
414 mesh->normals = VECDATA(Vector3, data, vnum);
415 break;
416 case GOAT3D_MESH_ATTR_TANGENT:
417 mesh->tangents = VECDATA(Vector3, data, vnum);
418 break;
419 case GOAT3D_MESH_ATTR_TEXCOORD:
420 mesh->texcoords = VECDATA(Vector2, data, vnum);
421 break;
422 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
423 mesh->skin_weights = VECDATA(Vector4, data, vnum);
424 break;
425 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
426 mesh->skin_matrices = VECDATA(Int4, data, vnum);
427 break;
428 case GOAT3D_MESH_ATTR_COLOR:
429 mesh->colors = VECDATA(Vector4, data, vnum);
430 default:
431 break;
432 }
433 }
435 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
436 float val)
437 {
438 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
439 }
441 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
442 float x, float y)
443 {
444 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, 0, 1);
445 }
447 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
448 float x, float y, float z)
449 {
450 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
451 }
453 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
454 float x, float y, float z, float w)
455 {
456 switch(attrib) {
457 case GOAT3D_MESH_ATTR_VERTEX:
458 mesh->vertices.push_back(Vector3(x, y, z));
459 break;
460 case GOAT3D_MESH_ATTR_NORMAL:
461 mesh->normals.push_back(Vector3(x, y, z));
462 break;
463 case GOAT3D_MESH_ATTR_TANGENT:
464 mesh->tangents.push_back(Vector3(x, y, z));
465 break;
466 case GOAT3D_MESH_ATTR_TEXCOORD:
467 mesh->texcoords.push_back(Vector2(x, y));
468 break;
469 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
470 mesh->skin_weights.push_back(Vector4(x, y, z, w));
471 break;
472 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
473 mesh->skin_matrices.push_back(Int4(x, y, z, w));
474 break;
475 case GOAT3D_MESH_ATTR_COLOR:
476 mesh->colors.push_back(Vector4(x, y, z, w));
477 default:
478 break;
479 }
480 }
482 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
483 {
484 return goat3d_get_mesh_attrib(mesh, attrib, 0);
485 }
487 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
488 {
489 switch(attrib) {
490 case GOAT3D_MESH_ATTR_VERTEX:
491 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
492 case GOAT3D_MESH_ATTR_NORMAL:
493 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
494 case GOAT3D_MESH_ATTR_TANGENT:
495 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
496 case GOAT3D_MESH_ATTR_TEXCOORD:
497 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
498 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
499 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
500 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
501 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
502 case GOAT3D_MESH_ATTR_COLOR:
503 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
504 default:
505 break;
506 }
507 return 0;
508 }
511 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
512 {
513 mesh->faces = VECDATA(Face, data, num);
514 }
516 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
517 {
518 Face face;
519 face.v[0] = a;
520 face.v[1] = b;
521 face.v[2] = c;
522 mesh->faces.push_back(face);
523 }
525 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
526 {
527 return goat3d_get_mesh_face(mesh, 0);
528 }
530 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
531 {
532 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
533 }
535 // immedate mode state
536 static enum goat3d_im_primitive im_prim;
537 static struct goat3d_mesh *im_mesh;
538 static Vector3 im_norm, im_tang;
539 static Vector2 im_texcoord;
540 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
541 static Int4 im_skinmat;
542 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
545 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
546 {
547 mesh->vertices.clear();
548 mesh->normals.clear();
549 mesh->tangents.clear();
550 mesh->texcoords.clear();
551 mesh->skin_weights.clear();
552 mesh->skin_matrices.clear();
553 mesh->colors.clear();
554 mesh->faces.clear();
556 im_mesh = mesh;
557 memset(im_use, 0, sizeof im_use);
559 im_prim = prim;
560 }
562 GOAT3DAPI void goat3d_end(void)
563 {
564 switch(im_prim) {
565 case GOAT3D_TRIANGLES:
566 {
567 int num_faces = (int)im_mesh->vertices.size() / 3;
568 im_mesh->faces.resize(num_faces);
570 int vidx = 0;
571 for(int i=0; i<num_faces; i++) {
572 im_mesh->faces[i].v[0] = vidx++;
573 im_mesh->faces[i].v[1] = vidx++;
574 im_mesh->faces[i].v[2] = vidx++;
575 }
576 }
577 break;
579 case GOAT3D_QUADS:
580 {
581 int num_quads = (int)im_mesh->vertices.size() / 4;
582 im_mesh->faces.resize(num_quads * 2);
584 int vidx = 0;
585 for(int i=0; i<num_quads; i++) {
586 im_mesh->faces[i * 2].v[0] = vidx;
587 im_mesh->faces[i * 2].v[1] = vidx + 1;
588 im_mesh->faces[i * 2].v[2] = vidx + 2;
590 im_mesh->faces[i * 2 + 1].v[0] = vidx;
591 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
592 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
594 vidx += 4;
595 }
596 }
597 break;
599 default:
600 return;
601 };
602 }
604 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
605 {
606 im_mesh->vertices.push_back(Vector3(x, y, z));
607 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
608 im_mesh->normals.push_back(im_norm);
609 }
610 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
611 im_mesh->tangents.push_back(im_tang);
612 }
613 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
614 im_mesh->texcoords.push_back(im_texcoord);
615 }
616 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
617 im_mesh->skin_weights.push_back(im_skinw);
618 }
619 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
620 im_mesh->skin_matrices.push_back(im_skinmat);
621 }
622 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
623 im_mesh->colors.push_back(im_color);
624 }
625 }
627 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
628 {
629 im_norm = Vector3(x, y, z);
630 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
631 }
633 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
634 {
635 im_tang = Vector3(x, y, z);
636 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
637 }
639 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
640 {
641 im_texcoord = Vector2(x, y);
642 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
643 }
645 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
646 {
647 im_skinw = Vector4(x, y, z, w);
648 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
649 }
651 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
652 {
653 im_skinmat.x = x;
654 im_skinmat.y = y;
655 im_skinmat.z = z;
656 im_skinmat.w = w;
657 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
658 }
660 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
661 {
662 goat3d_color4f(x, y, z, 1.0f);
663 }
665 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
666 {
667 im_color = Vector4(x, y, z, w);
668 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
669 }
671 /* lights */
672 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
673 {
674 g->scn->add_light(lt);
675 }
677 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
678 {
679 return g->scn->get_light_count();
680 }
682 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
683 {
684 return (goat3d_light*)g->scn->get_light(idx);
685 }
687 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
688 {
689 return (goat3d_light*)g->scn->get_light(name);
690 }
693 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
694 {
695 return new goat3d_light;
696 }
698 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
699 {
700 delete lt;
701 }
704 /* cameras */
705 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
706 {
707 g->scn->add_camera(cam);
708 }
710 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
711 {
712 return g->scn->get_camera_count();
713 }
715 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
716 {
717 return (goat3d_camera*)g->scn->get_camera(idx);
718 }
720 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
721 {
722 return (goat3d_camera*)g->scn->get_camera(name);
723 }
725 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
726 {
727 return new goat3d_camera;
728 }
730 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
731 {
732 delete cam;
733 }
737 // node
738 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
739 {
740 g->scn->add_node(node);
741 }
743 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
744 {
745 return g->scn->get_node_count();
746 }
748 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
749 {
750 return (goat3d_node*)g->scn->get_node(idx);
751 }
753 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
754 {
755 return (goat3d_node*)g->scn->get_node(name);
756 }
758 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
759 {
760 return new goat3d_node;
761 }
763 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
764 {
765 node->set_name(name);
766 }
768 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
769 {
770 return node->get_name();
771 }
773 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
774 {
775 node->set_object((Object*)obj);
776 }
778 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
779 {
780 return (void*)node->get_object();
781 }
783 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
784 {
785 const Object *obj = node->get_object();
786 if(dynamic_cast<const Mesh*>(obj)) {
787 return GOAT3D_NODE_MESH;
788 }
789 if(dynamic_cast<const Light*>(obj)) {
790 return GOAT3D_NODE_LIGHT;
791 }
792 if(dynamic_cast<const Camera*>(obj)) {
793 return GOAT3D_NODE_CAMERA;
794 }
796 return GOAT3D_NODE_NULL;
797 }
799 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
800 {
801 node->add_child(child);
802 }
804 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
805 {
806 return node->get_children_count();
807 }
809 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
810 {
811 return (goat3d_node*)node->get_child(idx);
812 }
814 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
815 {
816 return (goat3d_node*)node->get_parent();
817 }
819 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
820 {
821 node->use_animation(idx);
822 }
824 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
825 {
826 node->use_animation(aidx, bidx, t);
827 }
829 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
830 {
831 node->use_animation(name);
832 }
834 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
835 {
836 node->use_animation(aname, bname, t);
837 }
839 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
840 {
841 return node->get_active_animation_index(which);
842 }
844 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
845 {
846 return node->get_active_animation_mix();
847 }
849 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
850 {
851 return node->get_animation_count();
852 }
854 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
855 {
856 root->add_animation();
857 }
859 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
860 {
861 root->set_animation_name(name);
862 }
864 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
865 {
866 return node->get_animation_name();
867 }
869 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
870 {
871 node->set_position(Vector3(x, y, z), tmsec);
872 }
874 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
875 {
876 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
877 }
879 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
880 {
881 node->set_scaling(Vector3(sx, sy, sz), tmsec);
882 }
884 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
885 {
886 node->set_pivot(Vector3(px, py, pz));
887 }
890 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
891 {
892 Vector3 pos = node->get_position(tmsec);
893 *xptr = pos.x;
894 *yptr = pos.y;
895 *zptr = pos.z;
896 }
898 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
899 {
900 Quaternion q = node->get_rotation(tmsec);
901 *xptr = q.v.x;
902 *yptr = q.v.y;
903 *zptr = q.v.z;
904 *wptr = q.s;
905 }
907 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
908 {
909 Vector3 scale = node->get_scaling(tmsec);
910 *xptr = scale.x;
911 *yptr = scale.y;
912 *zptr = scale.z;
913 }
915 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
916 {
917 Vector3 pivot = node->get_pivot();
918 *xptr = pivot.x;
919 *yptr = pivot.y;
920 *zptr = pivot.z;
921 }
924 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
925 {
926 node->get_xform(tmsec, (Matrix4x4*)matrix);
927 }
930 } // extern "C"
933 static long read_file(void *buf, size_t bytes, void *uptr)
934 {
935 return (long)fread(buf, 1, bytes, (FILE*)uptr);
936 }
938 static long write_file(const void *buf, size_t bytes, void *uptr)
939 {
940 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
941 }
943 static long seek_file(long offs, int whence, void *uptr)
944 {
945 if(fseek((FILE*)uptr, offs, whence) == -1) {
946 return -1;
947 }
948 return ftell((FILE*)uptr);
949 }
951 std::string g3dimpl::clean_filename(const char *str)
952 {
953 const char *last_slash = strrchr(str, '/');
954 if(!last_slash) {
955 last_slash = strrchr(str, '\\');
956 }
958 if(last_slash) {
959 str = last_slash + 1;
960 }
962 char *buf = (char*)alloca(strlen(str) + 1);
963 char *dest = buf;
964 while(*str) {
965 char c = *str++;
966 *dest++ = tolower(c);
967 }
968 *dest = 0;
969 return buf;
970 }