goat3d

view src/goat3d.cc @ 84:022b13ed975b

minor gui fix
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 12 May 2014 07:37:30 +0300
parents 70b7c41a4f17
children f0277d348c2c
line source
1 /*
2 goat3d - 3D scene, character, and animation file format library.
3 Copyright (C) 2013-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <string.h>
19 #include <errno.h>
20 #include <ctype.h>
21 #include <string>
22 #include "goat3d.h"
23 #include "goat3d_impl.h"
24 #include "log.h"
26 #ifndef _MSC_VER
27 #include <alloca.h>
28 #else
29 #include <malloc.h>
30 #endif
32 using namespace g3dimpl;
34 struct goat3d_material : public Material {};
35 struct goat3d_mesh : public Mesh {};
36 struct goat3d_light : public Light {};
37 struct goat3d_camera : public Camera {};
38 struct goat3d_node : public Node {};
41 static long read_file(void *buf, size_t bytes, void *uptr);
42 static long write_file(const void *buf, size_t bytes, void *uptr);
43 static long seek_file(long offs, int whence, void *uptr);
45 extern "C" {
47 GOAT3DAPI struct goat3d *goat3d_create(void)
48 {
49 goat3d *goat = new goat3d;
50 goat->flags = 0;
51 goat->search_path = 0;
52 goat->scn = new Scene;
53 goat->scn->goat = goat;
55 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
56 return goat;
57 }
59 GOAT3DAPI void goat3d_free(struct goat3d *g)
60 {
61 delete [] g->search_path;
62 delete g->scn;
63 delete g;
64 }
66 GOAT3DAPI void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
67 {
68 if(val) {
69 g->flags |= (1 << (int)opt);
70 } else {
71 g->flags &= ~(1 << (int)opt);
72 }
73 }
75 GOAT3DAPI int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
76 {
77 return (g->flags >> (int)opt) & 1;
78 }
80 GOAT3DAPI int goat3d_load(struct goat3d *g, const char *fname)
81 {
82 FILE *fp = fopen(fname, "rb");
83 if(!fp) {
84 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
85 return -1;
86 }
88 /* if the filename contained any directory components, keep the prefix
89 * to use it as a search path for external mesh file loading
90 */
91 g->search_path = new char[strlen(fname) + 1];
92 strcpy(g->search_path, fname);
94 char *slash = strrchr(g->search_path, '/');
95 if(slash) {
96 *slash = 0;
97 } else {
98 if((slash = strrchr(g->search_path, '\\'))) {
99 *slash = 0;
100 } else {
101 delete [] g->search_path;
102 g->search_path = 0;
103 }
104 }
106 int res = goat3d_load_file(g, fp);
107 fclose(fp);
108 return res;
109 }
111 GOAT3DAPI int goat3d_save(const struct goat3d *g, const char *fname)
112 {
113 FILE *fp = fopen(fname, "wb");
114 if(!fp) {
115 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
116 return -1;
117 }
119 int res = goat3d_save_file(g, fp);
120 fclose(fp);
121 return res;
122 }
124 GOAT3DAPI int goat3d_load_file(struct goat3d *g, FILE *fp)
125 {
126 goat3d_io io;
127 io.cls = fp;
128 io.read = read_file;
129 io.write = write_file;
130 io.seek = seek_file;
132 return goat3d_load_io(g, &io);
133 }
135 GOAT3DAPI int goat3d_save_file(const struct goat3d *g, FILE *fp)
136 {
137 goat3d_io io;
138 io.cls = fp;
139 io.read = read_file;
140 io.write = write_file;
141 io.seek = seek_file;
143 return goat3d_save_io(g, &io);
144 }
146 GOAT3DAPI int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
147 {
148 if(!g->scn->load(io)) {
149 if(!g->scn->loadxml(io)) {
150 return -1;
151 }
152 }
153 return 0;
154 }
156 GOAT3DAPI int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
157 {
158 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
159 return g->scn->savexml(io) ? 0 : -1;
160 }
161 return g->scn->save(io) ? 0 : -1;
162 }
164 /* save/load animations */
165 GOAT3DAPI int goat3d_load_anim(struct goat3d *g, const char *fname)
166 {
167 FILE *fp = fopen(fname, "rb");
168 if(!fp) {
169 return -1;
170 }
172 int res = goat3d_load_anim_file(g, fp);
173 fclose(fp);
174 return res;
175 }
177 GOAT3DAPI int goat3d_save_anim(const struct goat3d *g, const char *fname)
178 {
179 FILE *fp = fopen(fname, "wb");
180 if(!fp) {
181 return -1;
182 }
184 int res = goat3d_save_anim_file(g, fp);
185 fclose(fp);
186 return res;
187 }
189 GOAT3DAPI int goat3d_load_anim_file(struct goat3d *g, FILE *fp)
190 {
191 goat3d_io io;
192 io.cls = fp;
193 io.read = read_file;
194 io.write = write_file;
195 io.seek = seek_file;
197 return goat3d_load_anim_io(g, &io);
198 }
200 GOAT3DAPI int goat3d_save_anim_file(const struct goat3d *g, FILE *fp)
201 {
202 goat3d_io io;
203 io.cls = fp;
204 io.read = read_file;
205 io.write = write_file;
206 io.seek = seek_file;
208 return goat3d_save_anim_io(g, &io);
209 }
211 GOAT3DAPI int goat3d_load_anim_io(struct goat3d *g, struct goat3d_io *io)
212 {
213 if(!g->scn->load_anim(io)) {
214 if(!g->scn->load_anim_xml(io)) {
215 return -1;
216 }
217 }
218 return 0;
219 }
221 GOAT3DAPI int goat3d_save_anim_io(const struct goat3d *g, struct goat3d_io *io)
222 {
223 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
224 return g->scn->save_anim_xml(io) ? 0 : -1;
225 }
226 return g->scn->save_anim(io) ? 0 : -1;
227 }
230 GOAT3DAPI int goat3d_set_name(struct goat3d *g, const char *name)
231 {
232 g->scn->set_name(name);
233 return 0;
234 }
236 GOAT3DAPI const char *goat3d_get_name(const struct goat3d *g)
237 {
238 return g->scn->get_name();
239 }
241 GOAT3DAPI void goat3d_set_ambient(struct goat3d *g, const float *amb)
242 {
243 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
244 }
246 GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
247 {
248 g->scn->set_ambient(Vector3(ar, ag, ab));
249 }
251 GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g)
252 {
253 return &g->scn->get_ambient().x;
254 }
256 GOAT3DAPI int goat3d_get_bounds(const struct goat3d *g, float *bmin, float *bmax)
257 {
258 AABox bbox = g->scn->get_bounds();
259 if(bbox == AABox()) {
260 return -1;
261 }
263 for(int i=0; i<3; i++) {
264 bmin[i] = bbox.bmin[i];
265 bmax[i] = bbox.bmax[i];
266 }
267 return 0;
268 }
270 // ---- materials ----
271 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
272 {
273 g->scn->add_material(mtl);
274 }
276 GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g)
277 {
278 return g->scn->get_material_count();
279 }
281 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx)
282 {
283 return (goat3d_material*)g->scn->get_material(idx);
284 }
286 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name)
287 {
288 return (goat3d_material*)g->scn->get_material(name);
289 }
291 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
292 {
293 return new goat3d_material;
294 }
296 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
297 {
298 delete mtl;
299 }
301 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
302 {
303 mtl->name = std::string(name);
304 }
306 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
307 {
308 return mtl->name.c_str();
309 }
311 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
312 {
313 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
314 }
316 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
317 {
318 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
319 }
321 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
322 {
323 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
324 }
326 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
327 {
328 (*mtl)[attrib].value = Vector4(r, g, b, a);
329 }
331 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
332 {
333 return &(*mtl)[attrib].value.x;
334 }
336 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
337 {
338 (*mtl)[attrib].map = clean_filename(mapname);
339 }
341 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
342 {
343 return (*mtl)[attrib].map.c_str();
344 }
346 // ---- meshes ----
347 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
348 {
349 g->scn->add_mesh(mesh);
350 }
352 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
353 {
354 return g->scn->get_mesh_count();
355 }
357 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
358 {
359 return (goat3d_mesh*)g->scn->get_mesh(idx);
360 }
362 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
363 {
364 return (goat3d_mesh*)g->scn->get_mesh(name);
365 }
367 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
368 {
369 return new goat3d_mesh;
370 }
372 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
373 {
374 delete mesh;
375 }
377 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
378 {
379 mesh->name = std::string(name);
380 }
382 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
383 {
384 return mesh->name.c_str();
385 }
387 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
388 {
389 mesh->material = mtl;
390 }
392 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
393 {
394 return (goat3d_material*)mesh->material;
395 }
397 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
398 {
399 return (int)mesh->vertices.size();
400 }
402 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
403 {
404 return (int)mesh->faces.size();
405 }
407 // VECDATA is in goat3d_impl.h
408 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
409 {
410 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
411 mesh->vertices = VECDATA(Vector3, data, vnum);
412 return;
413 }
415 if(vnum != (int)mesh->vertices.size()) {
416 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
417 return;
418 }
420 switch(attrib) {
421 case GOAT3D_MESH_ATTR_NORMAL:
422 mesh->normals = VECDATA(Vector3, data, vnum);
423 break;
424 case GOAT3D_MESH_ATTR_TANGENT:
425 mesh->tangents = VECDATA(Vector3, data, vnum);
426 break;
427 case GOAT3D_MESH_ATTR_TEXCOORD:
428 mesh->texcoords = VECDATA(Vector2, data, vnum);
429 break;
430 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
431 mesh->skin_weights = VECDATA(Vector4, data, vnum);
432 break;
433 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
434 mesh->skin_matrices = VECDATA(Int4, data, vnum);
435 break;
436 case GOAT3D_MESH_ATTR_COLOR:
437 mesh->colors = VECDATA(Vector4, data, vnum);
438 default:
439 break;
440 }
441 }
443 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
444 float val)
445 {
446 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
447 }
449 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
450 float x, float y)
451 {
452 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, 0, 1);
453 }
455 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
456 float x, float y, float z)
457 {
458 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
459 }
461 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
462 float x, float y, float z, float w)
463 {
464 switch(attrib) {
465 case GOAT3D_MESH_ATTR_VERTEX:
466 mesh->vertices.push_back(Vector3(x, y, z));
467 break;
468 case GOAT3D_MESH_ATTR_NORMAL:
469 mesh->normals.push_back(Vector3(x, y, z));
470 break;
471 case GOAT3D_MESH_ATTR_TANGENT:
472 mesh->tangents.push_back(Vector3(x, y, z));
473 break;
474 case GOAT3D_MESH_ATTR_TEXCOORD:
475 mesh->texcoords.push_back(Vector2(x, y));
476 break;
477 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
478 mesh->skin_weights.push_back(Vector4(x, y, z, w));
479 break;
480 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
481 mesh->skin_matrices.push_back(Int4(x, y, z, w));
482 break;
483 case GOAT3D_MESH_ATTR_COLOR:
484 mesh->colors.push_back(Vector4(x, y, z, w));
485 default:
486 break;
487 }
488 }
490 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
491 {
492 return goat3d_get_mesh_attrib(mesh, attrib, 0);
493 }
495 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
496 {
497 switch(attrib) {
498 case GOAT3D_MESH_ATTR_VERTEX:
499 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
500 case GOAT3D_MESH_ATTR_NORMAL:
501 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
502 case GOAT3D_MESH_ATTR_TANGENT:
503 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
504 case GOAT3D_MESH_ATTR_TEXCOORD:
505 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
506 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
507 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
508 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
509 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
510 case GOAT3D_MESH_ATTR_COLOR:
511 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
512 default:
513 break;
514 }
515 return 0;
516 }
519 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
520 {
521 mesh->faces = VECDATA(Face, data, num);
522 }
524 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
525 {
526 Face face;
527 face.v[0] = a;
528 face.v[1] = b;
529 face.v[2] = c;
530 mesh->faces.push_back(face);
531 }
533 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
534 {
535 return goat3d_get_mesh_face(mesh, 0);
536 }
538 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
539 {
540 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
541 }
543 // immedate mode state
544 static enum goat3d_im_primitive im_prim;
545 static struct goat3d_mesh *im_mesh;
546 static Vector3 im_norm, im_tang;
547 static Vector2 im_texcoord;
548 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
549 static Int4 im_skinmat;
550 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
553 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
554 {
555 mesh->vertices.clear();
556 mesh->normals.clear();
557 mesh->tangents.clear();
558 mesh->texcoords.clear();
559 mesh->skin_weights.clear();
560 mesh->skin_matrices.clear();
561 mesh->colors.clear();
562 mesh->faces.clear();
564 im_mesh = mesh;
565 memset(im_use, 0, sizeof im_use);
567 im_prim = prim;
568 }
570 GOAT3DAPI void goat3d_end(void)
571 {
572 switch(im_prim) {
573 case GOAT3D_TRIANGLES:
574 {
575 int num_faces = (int)im_mesh->vertices.size() / 3;
576 im_mesh->faces.resize(num_faces);
578 int vidx = 0;
579 for(int i=0; i<num_faces; i++) {
580 im_mesh->faces[i].v[0] = vidx++;
581 im_mesh->faces[i].v[1] = vidx++;
582 im_mesh->faces[i].v[2] = vidx++;
583 }
584 }
585 break;
587 case GOAT3D_QUADS:
588 {
589 int num_quads = (int)im_mesh->vertices.size() / 4;
590 im_mesh->faces.resize(num_quads * 2);
592 int vidx = 0;
593 for(int i=0; i<num_quads; i++) {
594 im_mesh->faces[i * 2].v[0] = vidx;
595 im_mesh->faces[i * 2].v[1] = vidx + 1;
596 im_mesh->faces[i * 2].v[2] = vidx + 2;
598 im_mesh->faces[i * 2 + 1].v[0] = vidx;
599 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
600 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
602 vidx += 4;
603 }
604 }
605 break;
607 default:
608 return;
609 };
610 }
612 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
613 {
614 im_mesh->vertices.push_back(Vector3(x, y, z));
615 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
616 im_mesh->normals.push_back(im_norm);
617 }
618 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
619 im_mesh->tangents.push_back(im_tang);
620 }
621 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
622 im_mesh->texcoords.push_back(im_texcoord);
623 }
624 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
625 im_mesh->skin_weights.push_back(im_skinw);
626 }
627 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
628 im_mesh->skin_matrices.push_back(im_skinmat);
629 }
630 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
631 im_mesh->colors.push_back(im_color);
632 }
633 }
635 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
636 {
637 im_norm = Vector3(x, y, z);
638 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
639 }
641 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
642 {
643 im_tang = Vector3(x, y, z);
644 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
645 }
647 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
648 {
649 im_texcoord = Vector2(x, y);
650 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
651 }
653 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
654 {
655 im_skinw = Vector4(x, y, z, w);
656 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
657 }
659 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
660 {
661 im_skinmat.x = x;
662 im_skinmat.y = y;
663 im_skinmat.z = z;
664 im_skinmat.w = w;
665 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
666 }
668 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
669 {
670 goat3d_color4f(x, y, z, 1.0f);
671 }
673 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
674 {
675 im_color = Vector4(x, y, z, w);
676 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
677 }
679 /* lights */
680 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
681 {
682 g->scn->add_light(lt);
683 }
685 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
686 {
687 return g->scn->get_light_count();
688 }
690 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
691 {
692 return (goat3d_light*)g->scn->get_light(idx);
693 }
695 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
696 {
697 return (goat3d_light*)g->scn->get_light(name);
698 }
701 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
702 {
703 return new goat3d_light;
704 }
706 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
707 {
708 delete lt;
709 }
712 /* cameras */
713 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
714 {
715 g->scn->add_camera(cam);
716 }
718 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
719 {
720 return g->scn->get_camera_count();
721 }
723 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
724 {
725 return (goat3d_camera*)g->scn->get_camera(idx);
726 }
728 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
729 {
730 return (goat3d_camera*)g->scn->get_camera(name);
731 }
733 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
734 {
735 return new goat3d_camera;
736 }
738 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
739 {
740 delete cam;
741 }
745 // node
746 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
747 {
748 g->scn->add_node(node);
749 }
751 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
752 {
753 return g->scn->get_node_count();
754 }
756 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
757 {
758 return (goat3d_node*)g->scn->get_node(idx);
759 }
761 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
762 {
763 return (goat3d_node*)g->scn->get_node(name);
764 }
766 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
767 {
768 return new goat3d_node;
769 }
771 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
772 {
773 node->set_name(name);
774 }
776 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
777 {
778 return node->get_name();
779 }
781 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
782 {
783 node->set_object((Object*)obj);
784 }
786 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
787 {
788 return (void*)node->get_object();
789 }
791 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
792 {
793 const Object *obj = node->get_object();
794 if(dynamic_cast<const Mesh*>(obj)) {
795 return GOAT3D_NODE_MESH;
796 }
797 if(dynamic_cast<const Light*>(obj)) {
798 return GOAT3D_NODE_LIGHT;
799 }
800 if(dynamic_cast<const Camera*>(obj)) {
801 return GOAT3D_NODE_CAMERA;
802 }
804 return GOAT3D_NODE_NULL;
805 }
807 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
808 {
809 node->add_child(child);
810 }
812 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
813 {
814 return node->get_children_count();
815 }
817 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
818 {
819 return (goat3d_node*)node->get_child(idx);
820 }
822 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
823 {
824 return (goat3d_node*)node->get_parent();
825 }
827 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
828 {
829 node->use_animation(idx);
830 }
832 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
833 {
834 node->use_animation(aidx, bidx, t);
835 }
837 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
838 {
839 node->use_animation(name);
840 }
842 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
843 {
844 node->use_animation(aname, bname, t);
845 }
847 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
848 {
849 return node->get_active_animation_index(which);
850 }
852 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
853 {
854 return node->get_active_animation_mix();
855 }
857 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
858 {
859 return node->get_animation_count();
860 }
862 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
863 {
864 root->add_animation();
865 }
867 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
868 {
869 root->set_animation_name(name);
870 }
872 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
873 {
874 return node->get_animation_name();
875 }
877 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
878 {
879 node->set_position(Vector3(x, y, z), tmsec);
880 }
882 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
883 {
884 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
885 }
887 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
888 {
889 node->set_scaling(Vector3(sx, sy, sz), tmsec);
890 }
892 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
893 {
894 node->set_pivot(Vector3(px, py, pz));
895 }
898 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
899 {
900 Vector3 pos = node->get_position(tmsec);
901 *xptr = pos.x;
902 *yptr = pos.y;
903 *zptr = pos.z;
904 }
906 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
907 {
908 Quaternion q = node->get_rotation(tmsec);
909 *xptr = q.v.x;
910 *yptr = q.v.y;
911 *zptr = q.v.z;
912 *wptr = q.s;
913 }
915 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
916 {
917 Vector3 scale = node->get_scaling(tmsec);
918 *xptr = scale.x;
919 *yptr = scale.y;
920 *zptr = scale.z;
921 }
923 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
924 {
925 Vector3 pivot = node->get_pivot();
926 *xptr = pivot.x;
927 *yptr = pivot.y;
928 *zptr = pivot.z;
929 }
932 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
933 {
934 node->get_node_xform(tmsec, (Matrix4x4*)matrix);
935 }
938 } // extern "C"
941 static long read_file(void *buf, size_t bytes, void *uptr)
942 {
943 return (long)fread(buf, 1, bytes, (FILE*)uptr);
944 }
946 static long write_file(const void *buf, size_t bytes, void *uptr)
947 {
948 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
949 }
951 static long seek_file(long offs, int whence, void *uptr)
952 {
953 if(fseek((FILE*)uptr, offs, whence) == -1) {
954 return -1;
955 }
956 return ftell((FILE*)uptr);
957 }
959 std::string g3dimpl::clean_filename(const char *str)
960 {
961 const char *last_slash = strrchr(str, '/');
962 if(!last_slash) {
963 last_slash = strrchr(str, '\\');
964 }
966 if(last_slash) {
967 str = last_slash + 1;
968 }
970 char *buf = (char*)alloca(strlen(str) + 1);
971 char *dest = buf;
972 while(*str) {
973 char c = *str++;
974 *dest++ = tolower(c);
975 }
976 *dest = 0;
977 return buf;
978 }