goat3d

view src/goat3d.cc @ 31:4058337fbb92

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