goat3d

annotate src/goat3d.cc @ 50:0be413ac2e0a

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