goat3d

view src/goat3d.cc @ 27:4deb0b12fe14

wtf... corrupted heap?
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 29 Sep 2013 08:20:19 +0300
parents 1c601bf07b86
children 3d669155709d
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;
30 return goat;
31 }
33 void goat3d_free(struct goat3d *g)
34 {
35 delete g->scn;
36 delete g;
37 }
39 void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
40 {
41 if(val) {
42 g->flags |= (1 << (int)opt);
43 } else {
44 g->flags &= ~(1 << (int)opt);
45 }
46 }
48 int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
49 {
50 return (g->flags >> (int)opt) & 1;
51 }
53 int goat3d_load(struct goat3d *g, const char *fname)
54 {
55 FILE *fp = fopen(fname, "rb");
56 if(!fp) {
57 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
58 return -1;
59 }
61 int res = goat3d_load_file(g, fp);
62 fclose(fp);
63 return res;
64 }
66 int goat3d_save(const struct goat3d *g, const char *fname)
67 {
68 FILE *fp = fopen(fname, "wb");
69 if(!fp) {
70 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
71 return -1;
72 }
74 int res = goat3d_save_file(g, fp);
75 fclose(fp);
76 return res;
77 }
79 int goat3d_load_file(struct goat3d *g, FILE *fp)
80 {
81 goat3d_io io;
82 io.cls = fp;
83 io.read = read_file;
84 io.write = write_file;
85 io.seek = seek_file;
87 return goat3d_load_io(g, &io);
88 }
90 int goat3d_save_file(const struct goat3d *g, FILE *fp)
91 {
92 goat3d_io io;
93 io.cls = fp;
94 io.read = read_file;
95 io.write = write_file;
96 io.seek = seek_file;
98 return goat3d_save_io(g, &io);
99 }
101 int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
102 {
103 if(!g->scn->load(io)) {
104 if(g->scn->loadxml(io)) {
105 return -1;
106 }
107 }
108 return 0;
109 }
111 int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
112 {
113 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
114 return g->scn->savexml(io) ? 0 : -1;
115 }
116 return g->scn->save(io) ? 0 : -1;
117 }
119 int goat3d_set_name(struct goat3d *g, const char *name)
120 {
121 g->scn->set_name(name);
122 return 0;
123 }
125 const char *goat3d_get_name(const struct goat3d *g)
126 {
127 return g->scn->get_name();
128 }
130 void goat3d_set_ambient(struct goat3d *g, const float *amb)
131 {
132 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
133 }
135 void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
136 {
137 g->scn->set_ambient(Vector3(ar, ag, ab));
138 }
140 const float *goat3d_get_ambient(const struct goat3d *g)
141 {
142 return &g->scn->get_ambient().x;
143 }
145 // ---- materials ----
146 void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
147 {
148 g->scn->add_material(mtl);
149 }
151 struct goat3d_material *goat3d_create_mtl(void)
152 {
153 return new goat3d_material;
154 }
156 void goat3d_destroy_mtl(struct goat3d_material *mtl)
157 {
158 delete mtl;
159 }
161 void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
162 {
163 mtl->name = std::string(name);
164 }
166 const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
167 {
168 return mtl->name.c_str();
169 }
171 void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
172 {
173 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
174 }
176 void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
177 {
178 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
179 }
181 void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
182 {
183 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
184 }
186 void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
187 {
188 (*mtl)[attrib].value = Vector4(r, g, b, a);
189 }
191 const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
192 {
193 return &(*mtl)[attrib].value.x;
194 }
196 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
197 {
198 (*mtl)[attrib].map = std::string(mapname);
199 }
201 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
202 {
203 return (*mtl)[attrib].map.c_str();
204 }
206 // ---- meshes ----
207 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
208 {
209 g->scn->add_mesh(mesh);
210 }
212 int goat3d_get_mesh_count(struct goat3d *g)
213 {
214 return g->scn->get_mesh_count();
215 }
217 struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
218 {
219 return (goat3d_mesh*)g->scn->get_mesh(idx);
220 }
222 struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
223 {
224 return (goat3d_mesh*)g->scn->get_mesh(name);
225 }
227 struct goat3d_mesh *goat3d_create_mesh(void)
228 {
229 return new goat3d_mesh;
230 }
232 void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
233 {
234 delete mesh;
235 }
237 void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
238 {
239 mesh->name = std::string(name);
240 }
242 const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
243 {
244 return mesh->name.c_str();
245 }
247 void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
248 {
249 mesh->material = mtl;
250 }
252 struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
253 {
254 return (goat3d_material*)mesh->material;
255 }
257 int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
258 {
259 return (int)mesh->vertices.size();
260 }
262 int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
263 {
264 return (int)mesh->faces.size();
265 }
267 // VECDATA is in goat3d_impl.h
268 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
269 {
270 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
271 mesh->vertices = VECDATA(Vector3, data, vnum);
272 return;
273 }
275 if(vnum != (int)mesh->vertices.size()) {
276 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
277 return;
278 }
280 switch(attrib) {
281 case GOAT3D_MESH_ATTR_NORMAL:
282 mesh->normals = VECDATA(Vector3, data, vnum);
283 break;
284 case GOAT3D_MESH_ATTR_TANGENT:
285 mesh->tangents = VECDATA(Vector3, data, vnum);
286 break;
287 case GOAT3D_MESH_ATTR_TEXCOORD:
288 mesh->texcoords = VECDATA(Vector2, data, vnum);
289 break;
290 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
291 mesh->skin_weights = VECDATA(Vector4, data, vnum);
292 break;
293 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
294 mesh->skin_matrices = VECDATA(Int4, data, vnum);
295 break;
296 case GOAT3D_MESH_ATTR_COLOR:
297 mesh->colors = VECDATA(Vector4, data, vnum);
298 default:
299 break;
300 }
301 }
303 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
304 {
305 return goat3d_get_mesh_attrib(mesh, attrib, 0);
306 }
308 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
309 {
310 switch(attrib) {
311 case GOAT3D_MESH_ATTR_VERTEX:
312 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
313 case GOAT3D_MESH_ATTR_NORMAL:
314 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
315 case GOAT3D_MESH_ATTR_TANGENT:
316 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
317 case GOAT3D_MESH_ATTR_TEXCOORD:
318 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
319 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
320 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
321 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
322 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
323 case GOAT3D_MESH_ATTR_COLOR:
324 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
325 default:
326 break;
327 }
328 return 0;
329 }
332 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
333 {
334 mesh->faces = VECDATA(Face, data, num);
335 }
337 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
338 {
339 return goat3d_get_mesh_face(mesh, 0);
340 }
342 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
343 {
344 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
345 }
347 // immedate mode state
348 static enum goat3d_im_primitive im_prim;
349 static struct goat3d_mesh *im_mesh;
350 static Vector3 im_norm, im_tang;
351 static Vector2 im_texcoord;
352 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
353 static Int4 im_skinmat;
354 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
357 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
358 {
359 mesh->vertices.clear();
360 mesh->normals.clear();
361 mesh->tangents.clear();
362 mesh->texcoords.clear();
363 mesh->skin_weights.clear();
364 mesh->skin_matrices.clear();
365 mesh->colors.clear();
366 mesh->faces.clear();
368 im_mesh = mesh;
369 memset(im_use, 0, sizeof im_use);
371 im_prim = prim;
372 }
374 void goat3d_end(void)
375 {
376 switch(im_prim) {
377 case GOAT3D_TRIANGLES:
378 {
379 int num_faces = (int)im_mesh->vertices.size() / 3;
380 im_mesh->faces.resize(num_faces);
382 int vidx = 0;
383 for(int i=0; i<num_faces; i++) {
384 im_mesh->faces[i].v[0] = vidx++;
385 im_mesh->faces[i].v[1] = vidx++;
386 im_mesh->faces[i].v[2] = vidx++;
387 }
388 }
389 break;
391 case GOAT3D_QUADS:
392 {
393 int num_quads = (int)im_mesh->vertices.size() / 4;
394 im_mesh->faces.resize(num_quads * 2);
396 int vidx = 0;
397 for(int i=0; i<num_quads; i++) {
398 im_mesh->faces[i * 2].v[0] = vidx;
399 im_mesh->faces[i * 2].v[1] = vidx + 1;
400 im_mesh->faces[i * 2].v[2] = vidx + 2;
402 im_mesh->faces[i * 2 + 1].v[0] = vidx;
403 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
404 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
406 vidx += 4;
407 }
408 }
409 break;
411 default:
412 return;
413 };
414 }
416 void goat3d_vertex3f(float x, float y, float z)
417 {
418 im_mesh->vertices.push_back(Vector3(x, y, z));
419 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
420 im_mesh->normals.push_back(im_norm);
421 }
422 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
423 im_mesh->tangents.push_back(im_tang);
424 }
425 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
426 im_mesh->texcoords.push_back(im_texcoord);
427 }
428 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
429 im_mesh->skin_weights.push_back(im_skinw);
430 }
431 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
432 im_mesh->skin_matrices.push_back(im_skinmat);
433 }
434 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
435 im_mesh->colors.push_back(im_color);
436 }
437 }
439 void goat3d_normal3f(float x, float y, float z)
440 {
441 im_norm = Vector3(x, y, z);
442 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
443 }
445 void goat3d_tangent3f(float x, float y, float z)
446 {
447 im_tang = Vector3(x, y, z);
448 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
449 }
451 void goat3d_texcoord2f(float x, float y)
452 {
453 im_texcoord = Vector2(x, y);
454 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
455 }
457 void goat3d_skin_weight4f(float x, float y, float z, float w)
458 {
459 im_skinw = Vector4(x, y, z, w);
460 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
461 }
463 void goat3d_skin_matrix4i(int x, int y, int z, int w)
464 {
465 im_skinmat.x = x;
466 im_skinmat.y = y;
467 im_skinmat.z = z;
468 im_skinmat.w = w;
469 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
470 }
472 void goat3d_color3f(float x, float y, float z)
473 {
474 goat3d_color4f(x, y, z, 1.0f);
475 }
477 void goat3d_color4f(float x, float y, float z, float w)
478 {
479 im_color = Vector4(x, y, z, w);
480 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
481 }
483 /* lights */
484 void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
485 {
486 g->scn->add_light(lt);
487 }
489 int goat3d_get_light_count(struct goat3d *g)
490 {
491 return g->scn->get_light_count();
492 }
494 struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
495 {
496 return (goat3d_light*)g->scn->get_light(idx);
497 }
499 struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
500 {
501 return (goat3d_light*)g->scn->get_light(name);
502 }
505 struct goat3d_light *goat3d_create_light(void)
506 {
507 return new goat3d_light;
508 }
510 void goat3d_destroy_light(struct goat3d_light *lt)
511 {
512 delete lt;
513 }
516 /* cameras */
517 void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
518 {
519 g->scn->add_camera(cam);
520 }
522 int goat3d_get_camera_count(struct goat3d *g)
523 {
524 return g->scn->get_camera_count();
525 }
527 struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
528 {
529 return (goat3d_camera*)g->scn->get_camera(idx);
530 }
532 struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
533 {
534 return (goat3d_camera*)g->scn->get_camera(name);
535 }
537 struct goat3d_camera *goat3d_create_camera(void)
538 {
539 return new goat3d_camera;
540 }
542 void goat3d_destroy_camera(struct goat3d_camera *cam)
543 {
544 delete cam;
545 }
549 // node
550 void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
551 {
552 g->scn->add_node(node);
553 }
555 int goat3d_get_node_count(struct goat3d *g)
556 {
557 return g->scn->get_node_count();
558 }
560 struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
561 {
562 return (goat3d_node*)g->scn->get_node(idx);
563 }
565 struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
566 {
567 return (goat3d_node*)g->scn->get_node(name);
568 }
570 struct goat3d_node *goat3d_create_node(void)
571 {
572 return new goat3d_node;
573 }
575 void goat3d_set_node_name(struct goat3d_node *node, const char *name)
576 {
577 node->set_name(name);
578 }
580 const char *goat3d_get_node_name(const struct goat3d_node *node)
581 {
582 return node->get_name();
583 }
585 void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
586 {
587 node->set_object((Object*)obj);
588 }
590 void *goat3d_get_node_object(const struct goat3d_node *node)
591 {
592 return (void*)node->get_object();
593 }
595 enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
596 {
597 const Object *obj = node->get_object();
598 if(dynamic_cast<const Mesh*>(obj)) {
599 return GOAT3D_NODE_MESH;
600 }
601 if(dynamic_cast<const Light*>(obj)) {
602 return GOAT3D_NODE_LIGHT;
603 }
604 if(dynamic_cast<const Camera*>(obj)) {
605 return GOAT3D_NODE_CAMERA;
606 }
608 return GOAT3D_NODE_NULL;
609 }
611 void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
612 {
613 node->add_child(node);
614 }
616 int goat3d_get_node_child_count(const struct goat3d_node *node)
617 {
618 return node->get_children_count();
619 }
621 struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
622 {
623 return (goat3d_node*)node->get_child(idx);
624 }
626 void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
627 {
628 node->set_position(Vector3(x, y, z), tmsec);
629 }
631 void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
632 {
633 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
634 }
636 void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
637 {
638 node->set_scaling(Vector3(sx, sy, sz), tmsec);
639 }
641 void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
642 {
643 node->set_pivot(Vector3(px, py, pz));
644 }
647 void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
648 {
649 Vector3 pos = node->get_position(tmsec);
650 *xptr = pos.x;
651 *yptr = pos.y;
652 *zptr = pos.z;
653 }
655 void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
656 {
657 Quaternion q = node->get_rotation(tmsec);
658 *xptr = q.v.x;
659 *yptr = q.v.y;
660 *zptr = q.v.z;
661 *wptr = q.s;
662 }
664 void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
665 {
666 Vector3 scale = node->get_scaling(tmsec);
667 *xptr = scale.x;
668 *yptr = scale.y;
669 *zptr = scale.z;
670 }
672 void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
673 {
674 Vector3 pivot = node->get_pivot();
675 *xptr = pivot.x;
676 *yptr = pivot.y;
677 *zptr = pivot.z;
678 }
681 void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
682 {
683 node->get_xform(tmsec, (Matrix4x4*)matrix);
684 }
687 } // extern "C"
690 static long read_file(void *buf, size_t bytes, void *uptr)
691 {
692 return (long)fread(buf, 1, bytes, (FILE*)uptr);
693 }
695 static long write_file(const void *buf, size_t bytes, void *uptr)
696 {
697 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
698 }
700 static long seek_file(long offs, int whence, void *uptr)
701 {
702 if(fseek((FILE*)uptr, offs, whence) == -1) {
703 return -1;
704 }
705 return ftell((FILE*)uptr);
706 }