goat3d

view src/goat3d.cc @ 26:1c601bf07b86

added the node API glue
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Sep 2013 20:36:55 +0300
parents d0260d80ae09
children 4deb0b12fe14
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->scn = new Scene;
29 return goat;
30 }
32 void goat3d_free(struct goat3d *g)
33 {
34 delete g->scn;
35 delete g;
36 }
38 void goat3d_setopt(struct goat3d *g, enum goat3d_option opt, int val)
39 {
40 if(val) {
41 g->flags |= (1 << (int)opt);
42 } else {
43 g->flags &= ~(1 << (int)opt);
44 }
45 }
47 int goat3d_getopt(const struct goat3d *g, enum goat3d_option opt)
48 {
49 return (g->flags >> (int)opt) & 1;
50 }
52 int goat3d_load(struct goat3d *g, const char *fname)
53 {
54 FILE *fp = fopen(fname, "rb");
55 if(!fp) {
56 logmsg(LOG_ERROR, "failed to open file \"%s\" for reading: %s\n", fname, strerror(errno));
57 return -1;
58 }
60 int res = goat3d_load_file(g, fp);
61 fclose(fp);
62 return res;
63 }
65 int goat3d_save(const struct goat3d *g, const char *fname)
66 {
67 FILE *fp = fopen(fname, "wb");
68 if(!fp) {
69 logmsg(LOG_ERROR, "failed to open file \"%s\" for writing: %s\n", fname, strerror(errno));
70 return -1;
71 }
73 int res = goat3d_save_file(g, fp);
74 fclose(fp);
75 return res;
76 }
78 int goat3d_load_file(struct goat3d *g, FILE *fp)
79 {
80 goat3d_io io;
81 io.cls = fp;
82 io.read = read_file;
83 io.write = write_file;
84 io.seek = seek_file;
86 return goat3d_load_io(g, &io);
87 }
89 int goat3d_save_file(const struct goat3d *g, FILE *fp)
90 {
91 goat3d_io io;
92 io.cls = fp;
93 io.read = read_file;
94 io.write = write_file;
95 io.seek = seek_file;
97 return goat3d_save_io(g, &io);
98 }
100 int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
101 {
102 if(!g->scn->load(io)) {
103 if(g->scn->loadxml(io)) {
104 return -1;
105 }
106 }
107 return 0;
108 }
110 int goat3d_save_io(const struct goat3d *g, struct goat3d_io *io)
111 {
112 if(goat3d_getopt(g, GOAT3D_OPT_SAVEXML)) {
113 return g->scn->savexml(io) ? 0 : -1;
114 }
115 return g->scn->save(io) ? 0 : -1;
116 }
118 int goat3d_set_name(struct goat3d *g, const char *name)
119 {
120 g->scn->set_name(name);
121 return 0;
122 }
124 const char *goat3d_get_name(const struct goat3d *g)
125 {
126 return g->scn->get_name();
127 }
129 void goat3d_set_ambient(struct goat3d *g, const float *amb)
130 {
131 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
132 }
134 void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
135 {
136 g->scn->set_ambient(Vector3(ar, ag, ab));
137 }
139 const float *goat3d_get_ambient(const struct goat3d *g)
140 {
141 return &g->scn->get_ambient().x;
142 }
144 // ---- materials ----
145 struct goat3d_material *goat3d_create_mtl(void)
146 {
147 return new goat3d_material;
148 }
150 void goat3d_destroy_mtl(struct goat3d_material *mtl)
151 {
152 delete mtl;
153 }
155 void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
156 {
157 mtl->name = std::string(name);
158 }
160 const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
161 {
162 return mtl->name.c_str();
163 }
165 void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
166 {
167 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
168 }
170 void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
171 {
172 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
173 }
175 void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
176 {
177 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
178 }
180 void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
181 {
182 (*mtl)[attrib].value = Vector4(r, g, b, a);
183 }
185 const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
186 {
187 return &(*mtl)[attrib].value.x;
188 }
190 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
191 {
192 (*mtl)[attrib].map = std::string(mapname);
193 }
195 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
196 {
197 return (*mtl)[attrib].map.c_str();
198 }
200 void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
201 {
202 g->scn->add_material(mtl);
203 }
205 // ---- meshes ----
206 struct goat3d_mesh *goat3d_create_mesh(void)
207 {
208 return new goat3d_mesh;
209 }
211 void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
212 {
213 delete mesh;
214 }
216 void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
217 {
218 mesh->name = std::string(name);
219 }
221 const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
222 {
223 return mesh->name.c_str();
224 }
226 void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
227 {
228 mesh->material = mtl;
229 }
231 struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
232 {
233 return (goat3d_material*)mesh->material;
234 }
236 int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
237 {
238 return (int)mesh->vertices.size();
239 }
241 int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
242 {
243 return (int)mesh->faces.size();
244 }
246 // VECDATA is in goat3d_impl.h
247 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
248 {
249 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
250 mesh->vertices = VECDATA(Vector3, data, vnum);
251 return;
252 }
254 if(vnum != (int)mesh->vertices.size()) {
255 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
256 return;
257 }
259 switch(attrib) {
260 case GOAT3D_MESH_ATTR_NORMAL:
261 mesh->normals = VECDATA(Vector3, data, vnum);
262 break;
263 case GOAT3D_MESH_ATTR_TANGENT:
264 mesh->tangents = VECDATA(Vector3, data, vnum);
265 break;
266 case GOAT3D_MESH_ATTR_TEXCOORD:
267 mesh->texcoords = VECDATA(Vector2, data, vnum);
268 break;
269 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
270 mesh->skin_weights = VECDATA(Vector4, data, vnum);
271 break;
272 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
273 mesh->skin_matrices = VECDATA(Int4, data, vnum);
274 break;
275 case GOAT3D_MESH_ATTR_COLOR:
276 mesh->colors = VECDATA(Vector4, data, vnum);
277 default:
278 break;
279 }
280 }
282 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
283 {
284 return goat3d_get_mesh_attrib(mesh, attrib, 0);
285 }
287 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
288 {
289 switch(attrib) {
290 case GOAT3D_MESH_ATTR_VERTEX:
291 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
292 case GOAT3D_MESH_ATTR_NORMAL:
293 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
294 case GOAT3D_MESH_ATTR_TANGENT:
295 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
296 case GOAT3D_MESH_ATTR_TEXCOORD:
297 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
298 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
299 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
300 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
301 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
302 case GOAT3D_MESH_ATTR_COLOR:
303 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
304 default:
305 break;
306 }
307 return 0;
308 }
311 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
312 {
313 mesh->faces = VECDATA(Face, data, num);
314 }
316 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
317 {
318 return goat3d_get_mesh_face(mesh, 0);
319 }
321 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
322 {
323 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
324 }
326 // immedate mode state
327 static enum goat3d_im_primitive im_prim;
328 static struct goat3d_mesh *im_mesh;
329 static Vector3 im_norm, im_tang;
330 static Vector2 im_texcoord;
331 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
332 static Int4 im_skinmat;
333 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
336 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
337 {
338 mesh->vertices.clear();
339 mesh->normals.clear();
340 mesh->tangents.clear();
341 mesh->texcoords.clear();
342 mesh->skin_weights.clear();
343 mesh->skin_matrices.clear();
344 mesh->colors.clear();
345 mesh->faces.clear();
347 im_mesh = mesh;
348 memset(im_use, 0, sizeof im_use);
350 im_prim = prim;
351 }
353 void goat3d_end(void)
354 {
355 switch(im_prim) {
356 case GOAT3D_TRIANGLES:
357 {
358 int num_faces = (int)im_mesh->vertices.size() / 3;
359 im_mesh->faces.resize(num_faces);
361 int vidx = 0;
362 for(int i=0; i<num_faces; i++) {
363 im_mesh->faces[i].v[0] = vidx++;
364 im_mesh->faces[i].v[1] = vidx++;
365 im_mesh->faces[i].v[2] = vidx++;
366 }
367 }
368 break;
370 case GOAT3D_QUADS:
371 {
372 int num_quads = (int)im_mesh->vertices.size() / 4;
373 im_mesh->faces.resize(num_quads * 2);
375 int vidx = 0;
376 for(int i=0; i<num_quads; i++) {
377 im_mesh->faces[i * 2].v[0] = vidx;
378 im_mesh->faces[i * 2].v[1] = vidx + 1;
379 im_mesh->faces[i * 2].v[2] = vidx + 2;
381 im_mesh->faces[i * 2 + 1].v[0] = vidx;
382 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
383 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
385 vidx += 4;
386 }
387 }
388 break;
390 default:
391 return;
392 };
393 }
395 void goat3d_vertex3f(float x, float y, float z)
396 {
397 im_mesh->vertices.push_back(Vector3(x, y, z));
398 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
399 im_mesh->normals.push_back(im_norm);
400 }
401 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
402 im_mesh->tangents.push_back(im_tang);
403 }
404 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
405 im_mesh->texcoords.push_back(im_texcoord);
406 }
407 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
408 im_mesh->skin_weights.push_back(im_skinw);
409 }
410 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
411 im_mesh->skin_matrices.push_back(im_skinmat);
412 }
413 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
414 im_mesh->colors.push_back(im_color);
415 }
416 }
418 void goat3d_normal3f(float x, float y, float z)
419 {
420 im_norm = Vector3(x, y, z);
421 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
422 }
424 void goat3d_tangent3f(float x, float y, float z)
425 {
426 im_tang = Vector3(x, y, z);
427 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
428 }
430 void goat3d_texcoord2f(float x, float y)
431 {
432 im_texcoord = Vector2(x, y);
433 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
434 }
436 void goat3d_skin_weight4f(float x, float y, float z, float w)
437 {
438 im_skinw = Vector4(x, y, z, w);
439 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
440 }
442 void goat3d_skin_matrix4i(int x, int y, int z, int w)
443 {
444 im_skinmat.x = x;
445 im_skinmat.y = y;
446 im_skinmat.z = z;
447 im_skinmat.w = w;
448 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
449 }
451 void goat3d_color3f(float x, float y, float z)
452 {
453 goat3d_color4f(x, y, z, 1.0f);
454 }
456 void goat3d_color4f(float x, float y, float z, float w)
457 {
458 im_color = Vector4(x, y, z, w);
459 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
460 }
462 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
463 {
464 g->scn->add_mesh(mesh);
465 }
467 int goat3d_get_mesh_count(struct goat3d *g)
468 {
469 return g->scn->get_mesh_count();
470 }
472 struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
473 {
474 return (goat3d_mesh*)g->scn->get_mesh(idx);
475 }
478 // node
480 struct goat3d_node *goat3d_create_node(void)
481 {
482 return new goat3d_node;
483 }
485 void goat3d_set_node_name(struct goat3d_node *node, const char *name)
486 {
487 node->set_name(name);
488 }
490 const char *goat3d_get_node_name(const struct goat3d_node *node)
491 {
492 return node->get_name();
493 }
495 void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
496 {
497 node->set_object((Object*)obj);
498 }
500 void *goat3d_get_node_object(const struct goat3d_node *node)
501 {
502 return (void*)node->get_object();
503 }
505 enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
506 {
507 const Object *obj = node->get_object();
508 if(dynamic_cast<const Mesh*>(obj)) {
509 return GOAT3D_NODE_MESH;
510 }
511 if(dynamic_cast<const Light*>(obj)) {
512 return GOAT3D_NODE_LIGHT;
513 }
514 if(dynamic_cast<const Camera*>(obj)) {
515 return GOAT3D_NODE_CAMERA;
516 }
518 return GOAT3D_NODE_NULL;
519 }
521 void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
522 {
523 node->add_child(node);
524 }
526 int goat3d_get_node_child_count(const struct goat3d_node *node)
527 {
528 return node->get_children_count();
529 }
531 struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
532 {
533 return (goat3d_node*)node->get_child(idx);
534 }
536 void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
537 {
538 node->set_position(Vector3(x, y, z), tmsec);
539 }
541 void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
542 {
543 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
544 }
546 void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
547 {
548 node->set_scaling(Vector3(sx, sy, sz), tmsec);
549 }
551 void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
552 {
553 node->set_pivot(Vector3(px, py, pz));
554 }
557 void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
558 {
559 Vector3 pos = node->get_position(tmsec);
560 *xptr = pos.x;
561 *yptr = pos.y;
562 *zptr = pos.z;
563 }
565 void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
566 {
567 Quaternion q = node->get_rotation(tmsec);
568 *xptr = q.v.x;
569 *yptr = q.v.y;
570 *zptr = q.v.z;
571 *wptr = q.s;
572 }
574 void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
575 {
576 Vector3 scale = node->get_scaling(tmsec);
577 *xptr = scale.x;
578 *yptr = scale.y;
579 *zptr = scale.z;
580 }
582 void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
583 {
584 Vector3 pivot = node->get_pivot();
585 *xptr = pivot.x;
586 *yptr = pivot.y;
587 *zptr = pivot.z;
588 }
591 void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
592 {
593 node->get_xform(tmsec, (Matrix4x4*)matrix);
594 }
597 } // extern "C"
600 static long read_file(void *buf, size_t bytes, void *uptr)
601 {
602 return (long)fread(buf, 1, bytes, (FILE*)uptr);
603 }
605 static long write_file(const void *buf, size_t bytes, void *uptr)
606 {
607 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
608 }
610 static long seek_file(long offs, int whence, void *uptr)
611 {
612 if(fseek((FILE*)uptr, offs, whence) == -1) {
613 return -1;
614 }
615 return ftell((FILE*)uptr);
616 }