goat3d

annotate src/goat3d.cc @ 70:0bb33d04f279

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