goat3d

view src/goat3d.cc @ 33:f43f4849c86a

started writing blender exporter
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Oct 2013 03:07:45 +0300
parents 4deb0b12fe14
children 4058337fbb92
line source
1 #include <string.h>
2 #include <errno.h>
3 #include "goat3d.h"
4 #include "goat3d_impl.h"
5 #include "log.h"
7 struct goat3d {
8 Scene *scn;
9 unsigned int flags;
10 };
12 struct goat3d_material : public Material {};
13 struct goat3d_mesh : public Mesh {};
14 struct goat3d_light : public Light {};
15 struct goat3d_camera : public Camera {};
16 struct goat3d_node : public Node {};
19 static long read_file(void *buf, size_t bytes, void *uptr);
20 static long write_file(const void *buf, size_t bytes, void *uptr);
21 static long seek_file(long offs, int whence, void *uptr);
23 extern "C" {
25 struct goat3d *goat3d_create(void)
26 {
27 goat3d *goat = new goat3d;
28 goat->flags = 0;
29 goat->scn = new Scene;
31 goat3d_setopt(goat, GOAT3D_OPT_SAVEXML, 1);
32 return goat;
33 }
35 void goat3d_free(struct goat3d *g)
36 {
37 delete g->scn;
38 delete g;
39 }
41 void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
42 {
43 if(val) {
44 g->flags |= (1 << (int)opt);
45 } else {
46 g->flags &= ~(1 << (int)opt);
47 }
48 }
50 int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
51 {
52 return (g->flags >> (int)opt) & 1;
53 }
55 int goat3d_load(struct goat3d *g, const char *fname)
56 {
57 FILE *fp = fopen(fname, "rb");
58 if(!fp) {
59 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
60 return -1;
61 }
63 int res = goat3d_load_file(g, fp);
64 fclose(fp);
65 return res;
66 }
68 int goat3d_save(const struct goat3d *g, const char *fname)
69 {
70 FILE *fp = fopen(fname, "wb");
71 if(!fp) {
72 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
73 return -1;
74 }
76 int res = goat3d_save_file(g, fp);
77 fclose(fp);
78 return res;
79 }
81 int goat3d_load_file(struct goat3d *g, FILE *fp)
82 {
83 goat3d_io io;
84 io.cls = fp;
85 io.read = read_file;
86 io.write = write_file;
87 io.seek = seek_file;
89 return goat3d_load_io(g, &io);
90 }
92 int goat3d_save_file(const struct goat3d *g, FILE *fp)
93 {
94 goat3d_io io;
95 io.cls = fp;
96 io.read = read_file;
97 io.write = write_file;
98 io.seek = seek_file;
100 return goat3d_save_io(g, &io);
101 }
103 int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
104 {
105 if(!g->scn->load(io)) {
106 if(g->scn->loadxml(io)) {
107 return -1;
108 }
109 }
110 return 0;
111 }
113 int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
114 {
115 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
116 return g->scn->savexml(io) ? 0 : -1;
117 }
118 return g->scn->save(io) ? 0 : -1;
119 }
121 int goat3d_set_name(struct goat3d *g, const char *name)
122 {
123 g->scn->set_name(name);
124 return 0;
125 }
127 const char *goat3d_get_name(const struct goat3d *g)
128 {
129 return g->scn->get_name();
130 }
132 void goat3d_set_ambient(struct goat3d *g, const float *amb)
133 {
134 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
135 }
137 void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
138 {
139 g->scn->set_ambient(Vector3(ar, ag, ab));
140 }
142 const float *goat3d_get_ambient(const struct goat3d *g)
143 {
144 return &g->scn->get_ambient().x;
145 }
147 // ---- materials ----
148 void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
149 {
150 g->scn->add_material(mtl);
151 }
153 struct goat3d_material *goat3d_create_mtl(void)
154 {
155 return new goat3d_material;
156 }
158 void goat3d_destroy_mtl(struct goat3d_material *mtl)
159 {
160 delete mtl;
161 }
163 void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
164 {
165 mtl->name = std::string(name);
166 }
168 const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
169 {
170 return mtl->name.c_str();
171 }
173 void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
174 {
175 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
176 }
178 void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
179 {
180 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
181 }
183 void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
184 {
185 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
186 }
188 void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
189 {
190 (*mtl)[attrib].value = Vector4(r, g, b, a);
191 }
193 const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
194 {
195 return &(*mtl)[attrib].value.x;
196 }
198 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
199 {
200 (*mtl)[attrib].map = std::string(mapname);
201 }
203 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
204 {
205 return (*mtl)[attrib].map.c_str();
206 }
208 // ---- meshes ----
209 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
210 {
211 g->scn->add_mesh(mesh);
212 }
214 int goat3d_get_mesh_count(struct goat3d *g)
215 {
216 return g->scn->get_mesh_count();
217 }
219 struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
220 {
221 return (goat3d_mesh*)g->scn->get_mesh(idx);
222 }
224 struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
225 {
226 return (goat3d_mesh*)g->scn->get_mesh(name);
227 }
229 struct goat3d_mesh *goat3d_create_mesh(void)
230 {
231 return new goat3d_mesh;
232 }
234 void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
235 {
236 delete mesh;
237 }
239 void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
240 {
241 mesh->name = std::string(name);
242 }
244 const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
245 {
246 return mesh->name.c_str();
247 }
249 void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
250 {
251 mesh->material = mtl;
252 }
254 struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
255 {
256 return (goat3d_material*)mesh->material;
257 }
259 int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
260 {
261 return (int)mesh->vertices.size();
262 }
264 int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
265 {
266 return (int)mesh->faces.size();
267 }
269 // VECDATA is in goat3d_impl.h
270 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
271 {
272 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
273 mesh->vertices = VECDATA(Vector3, data, vnum);
274 return;
275 }
277 if(vnum != (int)mesh->vertices.size()) {
278 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
279 return;
280 }
282 switch(attrib) {
283 case GOAT3D_MESH_ATTR_NORMAL:
284 mesh->normals = VECDATA(Vector3, data, vnum);
285 break;
286 case GOAT3D_MESH_ATTR_TANGENT:
287 mesh->tangents = VECDATA(Vector3, data, vnum);
288 break;
289 case GOAT3D_MESH_ATTR_TEXCOORD:
290 mesh->texcoords = VECDATA(Vector2, data, vnum);
291 break;
292 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
293 mesh->skin_weights = VECDATA(Vector4, data, vnum);
294 break;
295 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
296 mesh->skin_matrices = VECDATA(Int4, data, vnum);
297 break;
298 case GOAT3D_MESH_ATTR_COLOR:
299 mesh->colors = VECDATA(Vector4, data, vnum);
300 default:
301 break;
302 }
303 }
305 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
306 {
307 return goat3d_get_mesh_attrib(mesh, attrib, 0);
308 }
310 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
311 {
312 switch(attrib) {
313 case GOAT3D_MESH_ATTR_VERTEX:
314 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
315 case GOAT3D_MESH_ATTR_NORMAL:
316 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
317 case GOAT3D_MESH_ATTR_TANGENT:
318 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
319 case GOAT3D_MESH_ATTR_TEXCOORD:
320 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
321 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
322 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
323 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
324 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
325 case GOAT3D_MESH_ATTR_COLOR:
326 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
327 default:
328 break;
329 }
330 return 0;
331 }
334 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
335 {
336 mesh->faces = VECDATA(Face, data, num);
337 }
339 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
340 {
341 return goat3d_get_mesh_face(mesh, 0);
342 }
344 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
345 {
346 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
347 }
349 // immedate mode state
350 static enum goat3d_im_primitive im_prim;
351 static struct goat3d_mesh *im_mesh;
352 static Vector3 im_norm, im_tang;
353 static Vector2 im_texcoord;
354 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
355 static Int4 im_skinmat;
356 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
359 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
360 {
361 mesh->vertices.clear();
362 mesh->normals.clear();
363 mesh->tangents.clear();
364 mesh->texcoords.clear();
365 mesh->skin_weights.clear();
366 mesh->skin_matrices.clear();
367 mesh->colors.clear();
368 mesh->faces.clear();
370 im_mesh = mesh;
371 memset(im_use, 0, sizeof im_use);
373 im_prim = prim;
374 }
376 void goat3d_end(void)
377 {
378 switch(im_prim) {
379 case GOAT3D_TRIANGLES:
380 {
381 int num_faces = (int)im_mesh->vertices.size() / 3;
382 im_mesh->faces.resize(num_faces);
384 int vidx = 0;
385 for(int i=0; i<num_faces; i++) {
386 im_mesh->faces[i].v[0] = vidx++;
387 im_mesh->faces[i].v[1] = vidx++;
388 im_mesh->faces[i].v[2] = vidx++;
389 }
390 }
391 break;
393 case GOAT3D_QUADS:
394 {
395 int num_quads = (int)im_mesh->vertices.size() / 4;
396 im_mesh->faces.resize(num_quads * 2);
398 int vidx = 0;
399 for(int i=0; i<num_quads; i++) {
400 im_mesh->faces[i * 2].v[0] = vidx;
401 im_mesh->faces[i * 2].v[1] = vidx + 1;
402 im_mesh->faces[i * 2].v[2] = vidx + 2;
404 im_mesh->faces[i * 2 + 1].v[0] = vidx;
405 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
406 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
408 vidx += 4;
409 }
410 }
411 break;
413 default:
414 return;
415 };
416 }
418 void goat3d_vertex3f(float x, float y, float z)
419 {
420 im_mesh->vertices.push_back(Vector3(x, y, z));
421 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
422 im_mesh->normals.push_back(im_norm);
423 }
424 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
425 im_mesh->tangents.push_back(im_tang);
426 }
427 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
428 im_mesh->texcoords.push_back(im_texcoord);
429 }
430 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
431 im_mesh->skin_weights.push_back(im_skinw);
432 }
433 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
434 im_mesh->skin_matrices.push_back(im_skinmat);
435 }
436 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
437 im_mesh->colors.push_back(im_color);
438 }
439 }
441 void goat3d_normal3f(float x, float y, float z)
442 {
443 im_norm = Vector3(x, y, z);
444 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
445 }
447 void goat3d_tangent3f(float x, float y, float z)
448 {
449 im_tang = Vector3(x, y, z);
450 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
451 }
453 void goat3d_texcoord2f(float x, float y)
454 {
455 im_texcoord = Vector2(x, y);
456 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
457 }
459 void goat3d_skin_weight4f(float x, float y, float z, float w)
460 {
461 im_skinw = Vector4(x, y, z, w);
462 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
463 }
465 void goat3d_skin_matrix4i(int x, int y, int z, int w)
466 {
467 im_skinmat.x = x;
468 im_skinmat.y = y;
469 im_skinmat.z = z;
470 im_skinmat.w = w;
471 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
472 }
474 void goat3d_color3f(float x, float y, float z)
475 {
476 goat3d_color4f(x, y, z, 1.0f);
477 }
479 void goat3d_color4f(float x, float y, float z, float w)
480 {
481 im_color = Vector4(x, y, z, w);
482 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
483 }
485 /* lights */
486 void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
487 {
488 g->scn->add_light(lt);
489 }
491 int goat3d_get_light_count(struct goat3d *g)
492 {
493 return g->scn->get_light_count();
494 }
496 struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
497 {
498 return (goat3d_light*)g->scn->get_light(idx);
499 }
501 struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
502 {
503 return (goat3d_light*)g->scn->get_light(name);
504 }
507 struct goat3d_light *goat3d_create_light(void)
508 {
509 return new goat3d_light;
510 }
512 void goat3d_destroy_light(struct goat3d_light *lt)
513 {
514 delete lt;
515 }
518 /* cameras */
519 void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
520 {
521 g->scn->add_camera(cam);
522 }
524 int goat3d_get_camera_count(struct goat3d *g)
525 {
526 return g->scn->get_camera_count();
527 }
529 struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
530 {
531 return (goat3d_camera*)g->scn->get_camera(idx);
532 }
534 struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
535 {
536 return (goat3d_camera*)g->scn->get_camera(name);
537 }
539 struct goat3d_camera *goat3d_create_camera(void)
540 {
541 return new goat3d_camera;
542 }
544 void goat3d_destroy_camera(struct goat3d_camera *cam)
545 {
546 delete cam;
547 }
551 // node
552 void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
553 {
554 g->scn->add_node(node);
555 }
557 int goat3d_get_node_count(struct goat3d *g)
558 {
559 return g->scn->get_node_count();
560 }
562 struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
563 {
564 return (goat3d_node*)g->scn->get_node(idx);
565 }
567 struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
568 {
569 return (goat3d_node*)g->scn->get_node(name);
570 }
572 struct goat3d_node *goat3d_create_node(void)
573 {
574 return new goat3d_node;
575 }
577 void goat3d_set_node_name(struct goat3d_node *node, const char *name)
578 {
579 node->set_name(name);
580 }
582 const char *goat3d_get_node_name(const struct goat3d_node *node)
583 {
584 return node->get_name();
585 }
587 void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
588 {
589 node->set_object((Object*)obj);
590 }
592 void *goat3d_get_node_object(const struct goat3d_node *node)
593 {
594 return (void*)node->get_object();
595 }
597 enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
598 {
599 const Object *obj = node->get_object();
600 if(dynamic_cast<const Mesh*>(obj)) {
601 return GOAT3D_NODE_MESH;
602 }
603 if(dynamic_cast<const Light*>(obj)) {
604 return GOAT3D_NODE_LIGHT;
605 }
606 if(dynamic_cast<const Camera*>(obj)) {
607 return GOAT3D_NODE_CAMERA;
608 }
610 return GOAT3D_NODE_NULL;
611 }
613 void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
614 {
615 node->add_child(node);
616 }
618 int goat3d_get_node_child_count(const struct goat3d_node *node)
619 {
620 return node->get_children_count();
621 }
623 struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
624 {
625 return (goat3d_node*)node->get_child(idx);
626 }
628 void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
629 {
630 node->set_position(Vector3(x, y, z), tmsec);
631 }
633 void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
634 {
635 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
636 }
638 void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
639 {
640 node->set_scaling(Vector3(sx, sy, sz), tmsec);
641 }
643 void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
644 {
645 node->set_pivot(Vector3(px, py, pz));
646 }
649 void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
650 {
651 Vector3 pos = node->get_position(tmsec);
652 *xptr = pos.x;
653 *yptr = pos.y;
654 *zptr = pos.z;
655 }
657 void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
658 {
659 Quaternion q = node->get_rotation(tmsec);
660 *xptr = q.v.x;
661 *yptr = q.v.y;
662 *zptr = q.v.z;
663 *wptr = q.s;
664 }
666 void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
667 {
668 Vector3 scale = node->get_scaling(tmsec);
669 *xptr = scale.x;
670 *yptr = scale.y;
671 *zptr = scale.z;
672 }
674 void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
675 {
676 Vector3 pivot = node->get_pivot();
677 *xptr = pivot.x;
678 *yptr = pivot.y;
679 *zptr = pivot.z;
680 }
683 void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
684 {
685 node->get_xform(tmsec, (Matrix4x4*)matrix);
686 }
689 } // extern "C"
692 static long read_file(void *buf, size_t bytes, void *uptr)
693 {
694 return (long)fread(buf, 1, bytes, (FILE*)uptr);
695 }
697 static long write_file(const void *buf, size_t bytes, void *uptr)
698 {
699 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
700 }
702 static long seek_file(long offs, int whence, void *uptr)
703 {
704 if(fseek((FILE*)uptr, offs, whence) == -1) {
705 return -1;
706 }
707 return ftell((FILE*)uptr);
708 }