goat3d
view src/goat3d.cc @ 58:d317eb4f83da
- made everything compile properly on windows again
- removed libanim/libvmath, we'll use them as external dependencies
- added new maxgoat_stub 3dsmax plugin project. Gets loaded as a max plugin and
loads the actual maxgoat (and later maxgoat_anim) exporters on demand, to
allow reloading the actual exporters without having to restart 3dsmax (which
takes AGES).
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 25 Mar 2014 03:19:55 +0200 |
parents | af1310ed212b |
children | 99715321ad6d 3751aabbc5b3 |
line source
1 #include <string.h>
2 #include <errno.h>
3 #include <ctype.h>
4 #include <string>
5 #include "goat3d.h"
6 #include "goat3d_impl.h"
7 #include "log.h"
9 #ifndef _MSC_VER
10 #include <alloca.h>
11 #else
12 #include <malloc.h>
13 #endif
15 using namespace g3dimpl;
17 struct goat3d {
18 Scene *scn;
19 unsigned int flags;
20 char *search_path;
21 };
23 struct goat3d_material : public Material {};
24 struct goat3d_mesh : public Mesh {};
25 struct goat3d_light : public Light {};
26 struct goat3d_camera : public Camera {};
27 struct goat3d_node : public Node {};
30 static long read_file(void *buf, size_t bytes, void *uptr);
31 static long write_file(const void *buf, size_t bytes, void *uptr);
32 static long seek_file(long offs, int whence, void *uptr);
34 extern "C" {
36 GOAT3DAPI struct goat3d *goat3d_create(void)
37 {
38 goat3d *goat = new goat3d;
39 goat->flags = 0;
40 goat->search_path = 0;
41 goat->scn = new Scene;
43 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
44 return goat;
45 }
47 GOAT3DAPI void goat3d_free(struct goat3d *g)
48 {
49 delete g->search_path;
50 delete g->scn;
51 delete g;
52 }
54 GOAT3DAPI void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
55 {
56 if(val) {
57 g->flags |= (1 << (int)opt);
58 } else {
59 g->flags &= ~(1 << (int)opt);
60 }
61 }
63 GOAT3DAPI int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
64 {
65 return (g->flags >> (int)opt) & 1;
66 }
68 GOAT3DAPI int goat3d_load(struct goat3d *g, const char *fname)
69 {
70 FILE *fp = fopen(fname, "rb");
71 if(!fp) {
72 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
73 return -1;
74 }
76 /* if the filename contained any directory components, keep the prefix
77 * to use it as a search path for external mesh file loading
78 */
79 g->search_path = new char[strlen(fname) + 1];
80 strcpy(g->search_path, fname);
82 char *slash = strrchr(g->search_path, '/');
83 if(slash) {
84 *slash = 0;
85 } else {
86 if((slash = strrchr(g->search_path, '\\'))) {
87 *slash = 0;
88 } else {
89 delete [] g->search_path;
90 g->search_path = 0;
91 }
92 }
94 int res = goat3d_load_file(g, fp);
95 fclose(fp);
96 return res;
97 }
99 GOAT3DAPI int goat3d_save(const struct goat3d *g, const char *fname)
100 {
101 FILE *fp = fopen(fname, "wb");
102 if(!fp) {
103 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
104 return -1;
105 }
107 int res = goat3d_save_file(g, fp);
108 fclose(fp);
109 return res;
110 }
112 GOAT3DAPI int goat3d_load_file(struct goat3d *g, FILE *fp)
113 {
114 goat3d_io io;
115 io.cls = fp;
116 io.read = read_file;
117 io.write = write_file;
118 io.seek = seek_file;
120 return goat3d_load_io(g, &io);
121 }
123 GOAT3DAPI int goat3d_save_file(const struct goat3d *g, FILE *fp)
124 {
125 goat3d_io io;
126 io.cls = fp;
127 io.read = read_file;
128 io.write = write_file;
129 io.seek = seek_file;
131 return goat3d_save_io(g, &io);
132 }
134 GOAT3DAPI int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
135 {
136 if(!g->scn->load(io)) {
137 if(!g->scn->loadxml(io)) {
138 return -1;
139 }
140 }
141 return 0;
142 }
144 GOAT3DAPI int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
145 {
146 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
147 return g->scn->savexml(io) ? 0 : -1;
148 }
149 return g->scn->save(io) ? 0 : -1;
150 }
152 /* save/load animations */
153 GOAT3DAPI int goat3d_load_anim(struct goat3d *g, const char *fname)
154 {
155 FILE *fp = fopen(fname, "rb");
156 if(!fp) {
157 return -1;
158 }
160 int res = goat3d_load_anim_file(g, fp);
161 fclose(fp);
162 return res;
163 }
165 GOAT3DAPI int goat3d_save_anim(const struct goat3d *g, const char *fname)
166 {
167 FILE *fp = fopen(fname, "wb");
168 if(!fp) {
169 return -1;
170 }
172 int res = goat3d_save_anim_file(g, fp);
173 fclose(fp);
174 return res;
175 }
177 GOAT3DAPI int goat3d_load_anim_file(struct goat3d *g, FILE *fp)
178 {
179 goat3d_io io;
180 io.cls = fp;
181 io.read = read_file;
182 io.write = write_file;
183 io.seek = seek_file;
185 return goat3d_load_anim_io(g, &io);
186 }
188 GOAT3DAPI int goat3d_save_anim_file(const struct goat3d *g, FILE *fp)
189 {
190 goat3d_io io;
191 io.cls = fp;
192 io.read = read_file;
193 io.write = write_file;
194 io.seek = seek_file;
196 return goat3d_save_anim_io(g, &io);
197 }
199 GOAT3DAPI int goat3d_load_anim_io(struct goat3d *g, struct goat3d_io *io)
200 {
201 if(!g->scn->load_anim(io)) {
202 if(!g->scn->load_anim_xml(io)) {
203 return -1;
204 }
205 }
206 return 0;
207 }
209 GOAT3DAPI int goat3d_save_anim_io(const struct goat3d *g, struct goat3d_io *io)
210 {
211 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
212 return g->scn->save_anim_xml(io) ? 0 : -1;
213 }
214 return g->scn->save_anim(io) ? 0 : -1;
215 }
218 GOAT3DAPI int goat3d_set_name(struct goat3d *g, const char *name)
219 {
220 g->scn->set_name(name);
221 return 0;
222 }
224 GOAT3DAPI const char *goat3d_get_name(const struct goat3d *g)
225 {
226 return g->scn->get_name();
227 }
229 GOAT3DAPI void goat3d_set_ambient(struct goat3d *g, const float *amb)
230 {
231 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
232 }
234 GOAT3DAPI void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
235 {
236 g->scn->set_ambient(Vector3(ar, ag, ab));
237 }
239 GOAT3DAPI const float *goat3d_get_ambient(const struct goat3d *g)
240 {
241 return &g->scn->get_ambient().x;
242 }
244 // ---- materials ----
245 GOAT3DAPI void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
246 {
247 g->scn->add_material(mtl);
248 }
250 GOAT3DAPI int goat3d_get_mtl_count(struct goat3d *g)
251 {
252 return g->scn->get_material_count();
253 }
255 GOAT3DAPI struct goat3d_material *goat3d_get_mtl(struct goat3d *g, int idx)
256 {
257 return (goat3d_material*)g->scn->get_material(idx);
258 }
260 GOAT3DAPI struct goat3d_material *goat3d_get_mtl_by_name(struct goat3d *g, const char *name)
261 {
262 return (goat3d_material*)g->scn->get_material(name);
263 }
265 GOAT3DAPI struct goat3d_material *goat3d_create_mtl(void)
266 {
267 return new goat3d_material;
268 }
270 GOAT3DAPI void goat3d_destroy_mtl(struct goat3d_material *mtl)
271 {
272 delete mtl;
273 }
275 GOAT3DAPI void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
276 {
277 mtl->name = std::string(name);
278 }
280 GOAT3DAPI const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
281 {
282 return mtl->name.c_str();
283 }
285 GOAT3DAPI void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
286 {
287 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
288 }
290 GOAT3DAPI void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
291 {
292 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
293 }
295 GOAT3DAPI void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
296 {
297 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
298 }
300 GOAT3DAPI void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
301 {
302 (*mtl)[attrib].value = Vector4(r, g, b, a);
303 }
305 GOAT3DAPI const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
306 {
307 return &(*mtl)[attrib].value.x;
308 }
310 GOAT3DAPI void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
311 {
312 (*mtl)[attrib].map = clean_filename(mapname);
313 }
315 GOAT3DAPI const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
316 {
317 return (*mtl)[attrib].map.c_str();
318 }
320 // ---- meshes ----
321 GOAT3DAPI void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
322 {
323 g->scn->add_mesh(mesh);
324 }
326 GOAT3DAPI int goat3d_get_mesh_count(struct goat3d *g)
327 {
328 return g->scn->get_mesh_count();
329 }
331 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
332 {
333 return (goat3d_mesh*)g->scn->get_mesh(idx);
334 }
336 GOAT3DAPI struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
337 {
338 return (goat3d_mesh*)g->scn->get_mesh(name);
339 }
341 GOAT3DAPI struct goat3d_mesh *goat3d_create_mesh(void)
342 {
343 return new goat3d_mesh;
344 }
346 GOAT3DAPI void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
347 {
348 delete mesh;
349 }
351 GOAT3DAPI void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
352 {
353 mesh->name = std::string(name);
354 }
356 GOAT3DAPI const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
357 {
358 return mesh->name.c_str();
359 }
361 GOAT3DAPI void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
362 {
363 mesh->material = mtl;
364 }
366 GOAT3DAPI struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
367 {
368 return (goat3d_material*)mesh->material;
369 }
371 GOAT3DAPI int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
372 {
373 return (int)mesh->vertices.size();
374 }
376 GOAT3DAPI int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
377 {
378 return (int)mesh->faces.size();
379 }
381 // VECDATA is in goat3d_impl.h
382 GOAT3DAPI void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
383 {
384 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
385 mesh->vertices = VECDATA(Vector3, data, vnum);
386 return;
387 }
389 if(vnum != (int)mesh->vertices.size()) {
390 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
391 return;
392 }
394 switch(attrib) {
395 case GOAT3D_MESH_ATTR_NORMAL:
396 mesh->normals = VECDATA(Vector3, data, vnum);
397 break;
398 case GOAT3D_MESH_ATTR_TANGENT:
399 mesh->tangents = VECDATA(Vector3, data, vnum);
400 break;
401 case GOAT3D_MESH_ATTR_TEXCOORD:
402 mesh->texcoords = VECDATA(Vector2, data, vnum);
403 break;
404 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
405 mesh->skin_weights = VECDATA(Vector4, data, vnum);
406 break;
407 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
408 mesh->skin_matrices = VECDATA(Int4, data, vnum);
409 break;
410 case GOAT3D_MESH_ATTR_COLOR:
411 mesh->colors = VECDATA(Vector4, data, vnum);
412 default:
413 break;
414 }
415 }
417 GOAT3DAPI void goat3d_add_mesh_attrib1f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
418 float val)
419 {
420 goat3d_add_mesh_attrib4f(mesh, attrib, val, 0, 0, 1);
421 }
423 GOAT3DAPI void goat3d_add_mesh_attrib2f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
424 float x, float y)
425 {
426 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, 0, 1);
427 }
429 GOAT3DAPI void goat3d_add_mesh_attrib3f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
430 float x, float y, float z)
431 {
432 goat3d_add_mesh_attrib4f(mesh, attrib, x, y, z, 1);
433 }
435 GOAT3DAPI void goat3d_add_mesh_attrib4f(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib,
436 float x, float y, float z, float w)
437 {
438 switch(attrib) {
439 case GOAT3D_MESH_ATTR_VERTEX:
440 mesh->vertices.push_back(Vector3(x, y, z));
441 break;
442 case GOAT3D_MESH_ATTR_NORMAL:
443 mesh->normals.push_back(Vector3(x, y, z));
444 break;
445 case GOAT3D_MESH_ATTR_TANGENT:
446 mesh->tangents.push_back(Vector3(x, y, z));
447 break;
448 case GOAT3D_MESH_ATTR_TEXCOORD:
449 mesh->texcoords.push_back(Vector2(x, y));
450 break;
451 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
452 mesh->skin_weights.push_back(Vector4(x, y, z, w));
453 break;
454 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
455 mesh->skin_matrices.push_back(Int4(x, y, z, w));
456 break;
457 case GOAT3D_MESH_ATTR_COLOR:
458 mesh->colors.push_back(Vector4(x, y, z, w));
459 default:
460 break;
461 }
462 }
464 GOAT3DAPI void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
465 {
466 return goat3d_get_mesh_attrib(mesh, attrib, 0);
467 }
469 GOAT3DAPI void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
470 {
471 switch(attrib) {
472 case GOAT3D_MESH_ATTR_VERTEX:
473 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
474 case GOAT3D_MESH_ATTR_NORMAL:
475 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
476 case GOAT3D_MESH_ATTR_TANGENT:
477 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
478 case GOAT3D_MESH_ATTR_TEXCOORD:
479 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
480 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
481 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
482 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
483 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
484 case GOAT3D_MESH_ATTR_COLOR:
485 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
486 default:
487 break;
488 }
489 return 0;
490 }
493 GOAT3DAPI void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
494 {
495 mesh->faces = VECDATA(Face, data, num);
496 }
498 GOAT3DAPI void goat3d_add_mesh_face(struct goat3d_mesh *mesh, int a, int b, int c)
499 {
500 Face face;
501 face.v[0] = a;
502 face.v[1] = b;
503 face.v[2] = c;
504 mesh->faces.push_back(face);
505 }
507 GOAT3DAPI int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
508 {
509 return goat3d_get_mesh_face(mesh, 0);
510 }
512 GOAT3DAPI int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
513 {
514 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
515 }
517 // immedate mode state
518 static enum goat3d_im_primitive im_prim;
519 static struct goat3d_mesh *im_mesh;
520 static Vector3 im_norm, im_tang;
521 static Vector2 im_texcoord;
522 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
523 static Int4 im_skinmat;
524 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
527 GOAT3DAPI void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
528 {
529 mesh->vertices.clear();
530 mesh->normals.clear();
531 mesh->tangents.clear();
532 mesh->texcoords.clear();
533 mesh->skin_weights.clear();
534 mesh->skin_matrices.clear();
535 mesh->colors.clear();
536 mesh->faces.clear();
538 im_mesh = mesh;
539 memset(im_use, 0, sizeof im_use);
541 im_prim = prim;
542 }
544 GOAT3DAPI void goat3d_end(void)
545 {
546 switch(im_prim) {
547 case GOAT3D_TRIANGLES:
548 {
549 int num_faces = (int)im_mesh->vertices.size() / 3;
550 im_mesh->faces.resize(num_faces);
552 int vidx = 0;
553 for(int i=0; i<num_faces; i++) {
554 im_mesh->faces[i].v[0] = vidx++;
555 im_mesh->faces[i].v[1] = vidx++;
556 im_mesh->faces[i].v[2] = vidx++;
557 }
558 }
559 break;
561 case GOAT3D_QUADS:
562 {
563 int num_quads = (int)im_mesh->vertices.size() / 4;
564 im_mesh->faces.resize(num_quads * 2);
566 int vidx = 0;
567 for(int i=0; i<num_quads; i++) {
568 im_mesh->faces[i * 2].v[0] = vidx;
569 im_mesh->faces[i * 2].v[1] = vidx + 1;
570 im_mesh->faces[i * 2].v[2] = vidx + 2;
572 im_mesh->faces[i * 2 + 1].v[0] = vidx;
573 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
574 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
576 vidx += 4;
577 }
578 }
579 break;
581 default:
582 return;
583 };
584 }
586 GOAT3DAPI void goat3d_vertex3f(float x, float y, float z)
587 {
588 im_mesh->vertices.push_back(Vector3(x, y, z));
589 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
590 im_mesh->normals.push_back(im_norm);
591 }
592 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
593 im_mesh->tangents.push_back(im_tang);
594 }
595 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
596 im_mesh->texcoords.push_back(im_texcoord);
597 }
598 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
599 im_mesh->skin_weights.push_back(im_skinw);
600 }
601 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
602 im_mesh->skin_matrices.push_back(im_skinmat);
603 }
604 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
605 im_mesh->colors.push_back(im_color);
606 }
607 }
609 GOAT3DAPI void goat3d_normal3f(float x, float y, float z)
610 {
611 im_norm = Vector3(x, y, z);
612 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
613 }
615 GOAT3DAPI void goat3d_tangent3f(float x, float y, float z)
616 {
617 im_tang = Vector3(x, y, z);
618 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
619 }
621 GOAT3DAPI void goat3d_texcoord2f(float x, float y)
622 {
623 im_texcoord = Vector2(x, y);
624 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
625 }
627 GOAT3DAPI void goat3d_skin_weight4f(float x, float y, float z, float w)
628 {
629 im_skinw = Vector4(x, y, z, w);
630 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
631 }
633 GOAT3DAPI void goat3d_skin_matrix4i(int x, int y, int z, int w)
634 {
635 im_skinmat.x = x;
636 im_skinmat.y = y;
637 im_skinmat.z = z;
638 im_skinmat.w = w;
639 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
640 }
642 GOAT3DAPI void goat3d_color3f(float x, float y, float z)
643 {
644 goat3d_color4f(x, y, z, 1.0f);
645 }
647 GOAT3DAPI void goat3d_color4f(float x, float y, float z, float w)
648 {
649 im_color = Vector4(x, y, z, w);
650 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
651 }
653 /* lights */
654 GOAT3DAPI void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
655 {
656 g->scn->add_light(lt);
657 }
659 GOAT3DAPI int goat3d_get_light_count(struct goat3d *g)
660 {
661 return g->scn->get_light_count();
662 }
664 GOAT3DAPI struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
665 {
666 return (goat3d_light*)g->scn->get_light(idx);
667 }
669 GOAT3DAPI struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
670 {
671 return (goat3d_light*)g->scn->get_light(name);
672 }
675 GOAT3DAPI struct goat3d_light *goat3d_create_light(void)
676 {
677 return new goat3d_light;
678 }
680 GOAT3DAPI void goat3d_destroy_light(struct goat3d_light *lt)
681 {
682 delete lt;
683 }
686 /* cameras */
687 GOAT3DAPI void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
688 {
689 g->scn->add_camera(cam);
690 }
692 GOAT3DAPI int goat3d_get_camera_count(struct goat3d *g)
693 {
694 return g->scn->get_camera_count();
695 }
697 GOAT3DAPI struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
698 {
699 return (goat3d_camera*)g->scn->get_camera(idx);
700 }
702 GOAT3DAPI struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
703 {
704 return (goat3d_camera*)g->scn->get_camera(name);
705 }
707 GOAT3DAPI struct goat3d_camera *goat3d_create_camera(void)
708 {
709 return new goat3d_camera;
710 }
712 GOAT3DAPI void goat3d_destroy_camera(struct goat3d_camera *cam)
713 {
714 delete cam;
715 }
719 // node
720 GOAT3DAPI void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
721 {
722 g->scn->add_node(node);
723 }
725 GOAT3DAPI int goat3d_get_node_count(struct goat3d *g)
726 {
727 return g->scn->get_node_count();
728 }
730 GOAT3DAPI struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
731 {
732 return (goat3d_node*)g->scn->get_node(idx);
733 }
735 GOAT3DAPI struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
736 {
737 return (goat3d_node*)g->scn->get_node(name);
738 }
740 GOAT3DAPI struct goat3d_node *goat3d_create_node(void)
741 {
742 return new goat3d_node;
743 }
745 GOAT3DAPI void goat3d_set_node_name(struct goat3d_node *node, const char *name)
746 {
747 node->set_name(name);
748 }
750 GOAT3DAPI const char *goat3d_get_node_name(const struct goat3d_node *node)
751 {
752 return node->get_name();
753 }
755 GOAT3DAPI void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
756 {
757 node->set_object((Object*)obj);
758 }
760 GOAT3DAPI void *goat3d_get_node_object(const struct goat3d_node *node)
761 {
762 return (void*)node->get_object();
763 }
765 GOAT3DAPI enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
766 {
767 const Object *obj = node->get_object();
768 if(dynamic_cast<const Mesh*>(obj)) {
769 return GOAT3D_NODE_MESH;
770 }
771 if(dynamic_cast<const Light*>(obj)) {
772 return GOAT3D_NODE_LIGHT;
773 }
774 if(dynamic_cast<const Camera*>(obj)) {
775 return GOAT3D_NODE_CAMERA;
776 }
778 return GOAT3D_NODE_NULL;
779 }
781 GOAT3DAPI void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
782 {
783 node->add_child(node);
784 }
786 GOAT3DAPI int goat3d_get_node_child_count(const struct goat3d_node *node)
787 {
788 return node->get_children_count();
789 }
791 GOAT3DAPI struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
792 {
793 return (goat3d_node*)node->get_child(idx);
794 }
796 GOAT3DAPI struct goat3d_node *goat3d_get_node_parent(const struct goat3d_node *node)
797 {
798 return (goat3d_node*)node->get_parent();
799 }
801 GOAT3DAPI void goat3d_use_anim(struct goat3d_node *node, int idx)
802 {
803 node->use_animation(idx);
804 }
806 GOAT3DAPI void goat3d_use_anims(struct goat3d_node *node, int aidx, int bidx, float t)
807 {
808 node->use_animation(aidx, bidx, t);
809 }
811 GOAT3DAPI void goat3d_use_anim_by_name(struct goat3d_node *node, const char *name)
812 {
813 node->use_animation(name);
814 }
816 GOAT3DAPI void goat3d_use_anims_by_name(struct goat3d_node *node, const char *aname, const char *bname, float t)
817 {
818 node->use_animation(aname, bname, t);
819 }
821 GOAT3DAPI int goat3d_get_active_anim(struct goat3d_node *node, int which)
822 {
823 return node->get_active_animation_index(which);
824 }
826 GOAT3DAPI float goat3d_get_active_anim_mix(struct goat3d_node *node)
827 {
828 return node->get_active_animation_mix();
829 }
831 GOAT3DAPI int goat3d_get_anim_count(struct goat3d_node *node)
832 {
833 return node->get_animation_count();
834 }
836 GOAT3DAPI void goat3d_add_anim(struct goat3d_node *root)
837 {
838 root->add_animation();
839 }
841 GOAT3DAPI void goat3d_set_anim_name(struct goat3d_node *root, const char *name)
842 {
843 root->set_animation_name(name);
844 }
846 GOAT3DAPI const char *goat3d_get_anim_name(struct goat3d_node *node)
847 {
848 return node->get_animation_name();
849 }
851 GOAT3DAPI void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
852 {
853 node->set_position(Vector3(x, y, z), tmsec);
854 }
856 GOAT3DAPI void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
857 {
858 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
859 }
861 GOAT3DAPI void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
862 {
863 node->set_scaling(Vector3(sx, sy, sz), tmsec);
864 }
866 GOAT3DAPI void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
867 {
868 node->set_pivot(Vector3(px, py, pz));
869 }
872 GOAT3DAPI void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
873 {
874 Vector3 pos = node->get_position(tmsec);
875 *xptr = pos.x;
876 *yptr = pos.y;
877 *zptr = pos.z;
878 }
880 GOAT3DAPI void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
881 {
882 Quaternion q = node->get_rotation(tmsec);
883 *xptr = q.v.x;
884 *yptr = q.v.y;
885 *zptr = q.v.z;
886 *wptr = q.s;
887 }
889 GOAT3DAPI void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
890 {
891 Vector3 scale = node->get_scaling(tmsec);
892 *xptr = scale.x;
893 *yptr = scale.y;
894 *zptr = scale.z;
895 }
897 GOAT3DAPI void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
898 {
899 Vector3 pivot = node->get_pivot();
900 *xptr = pivot.x;
901 *yptr = pivot.y;
902 *zptr = pivot.z;
903 }
906 GOAT3DAPI void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
907 {
908 node->get_xform(tmsec, (Matrix4x4*)matrix);
909 }
912 } // extern "C"
915 static long read_file(void *buf, size_t bytes, void *uptr)
916 {
917 return (long)fread(buf, 1, bytes, (FILE*)uptr);
918 }
920 static long write_file(const void *buf, size_t bytes, void *uptr)
921 {
922 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
923 }
925 static long seek_file(long offs, int whence, void *uptr)
926 {
927 if(fseek((FILE*)uptr, offs, whence) == -1) {
928 return -1;
929 }
930 return ftell((FILE*)uptr);
931 }
933 std::string g3dimpl::clean_filename(const char *str)
934 {
935 const char *last_slash = strrchr(str, '/');
936 if(!last_slash) {
937 last_slash = strrchr(str, '\\');
938 }
940 if(last_slash) {
941 str = last_slash + 1;
942 }
944 char *buf = (char*)alloca(strlen(str) + 1);
945 char *dest = buf;
946 while(*str) {
947 char c = *str++;
948 *dest++ = tolower(c);
949 }
950 *dest = 0;
951 return buf;
952 }