goat3d

annotate src/goat3d.cc @ 38:60f2037680ee

split the exporter into two files to make it more readable (and maybe make an importer too at some point?)
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 07 Oct 2013 20:02:57 +0300
parents 4058337fbb92
children a5c5cec3cb88
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@16 15 struct goat3d {
nuclear@16 16 Scene *scn;
nuclear@16 17 unsigned int flags;
nuclear@32 18 char *search_path;
nuclear@16 19 };
nuclear@16 20
nuclear@16 21 struct goat3d_material : public Material {};
nuclear@16 22 struct goat3d_mesh : public Mesh {};
nuclear@16 23 struct goat3d_light : public Light {};
nuclear@16 24 struct goat3d_camera : public Camera {};
nuclear@16 25 struct goat3d_node : public Node {};
nuclear@16 26
nuclear@16 27
nuclear@16 28 static long read_file(void *buf, size_t bytes, void *uptr);
nuclear@16 29 static long write_file(const void *buf, size_t bytes, void *uptr);
nuclear@16 30 static long seek_file(long offs, int whence, void *uptr);
nuclear@16 31
nuclear@31 32 static std::string clean_filename(const char *str);
nuclear@31 33
nuclear@16 34 extern "C" {
nuclear@16 35
nuclear@16 36 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@16 47 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@16 54 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@16 63 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@16 68 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@16 99 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@16 112 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@16 123 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@16 134 int goat3d_load_io(struct goat3d *g, struct goat3d_io *io)
nuclear@16 135 {
nuclear@16 136 if(!g->scn->load(io)) {
nuclear@16 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@16 144 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@16 152 int goat3d_set_name(struct goat3d *g, const char *name)
nuclear@16 153 {
nuclear@16 154 g->scn->set_name(name);
nuclear@16 155 return 0;
nuclear@16 156 }
nuclear@16 157
nuclear@16 158 const char *goat3d_get_name(const struct goat3d *g)
nuclear@16 159 {
nuclear@16 160 return g->scn->get_name();
nuclear@16 161 }
nuclear@16 162
nuclear@16 163 void goat3d_set_ambient(struct goat3d *g, const float *amb)
nuclear@16 164 {
nuclear@16 165 g->scn->set_ambient(Vector3(amb[0], amb[1], amb[2]));
nuclear@16 166 }
nuclear@16 167
nuclear@16 168 void goat3d_set_ambient3f(struct goat3d *g, float ar, float ag, float ab)
nuclear@16 169 {
nuclear@16 170 g->scn->set_ambient(Vector3(ar, ag, ab));
nuclear@16 171 }
nuclear@16 172
nuclear@16 173 const float *goat3d_get_ambient(const struct goat3d *g)
nuclear@16 174 {
nuclear@16 175 return &g->scn->get_ambient().x;
nuclear@16 176 }
nuclear@16 177
nuclear@16 178 // ---- materials ----
nuclear@27 179 void goat3d_add_mtl(struct goat3d *g, struct goat3d_material *mtl)
nuclear@27 180 {
nuclear@27 181 g->scn->add_material(mtl);
nuclear@27 182 }
nuclear@27 183
nuclear@16 184 struct goat3d_material *goat3d_create_mtl(void)
nuclear@16 185 {
nuclear@16 186 return new goat3d_material;
nuclear@16 187 }
nuclear@16 188
nuclear@16 189 void goat3d_destroy_mtl(struct goat3d_material *mtl)
nuclear@16 190 {
nuclear@16 191 delete mtl;
nuclear@16 192 }
nuclear@16 193
nuclear@16 194 void goat3d_set_mtl_name(struct goat3d_material *mtl, const char *name)
nuclear@16 195 {
nuclear@16 196 mtl->name = std::string(name);
nuclear@16 197 }
nuclear@16 198
nuclear@16 199 const char *goat3d_get_mtl_name(const struct goat3d_material *mtl)
nuclear@16 200 {
nuclear@16 201 return mtl->name.c_str();
nuclear@16 202 }
nuclear@16 203
nuclear@16 204 void goat3d_set_mtl_attrib(struct goat3d_material *mtl, const char *attrib, const float *val)
nuclear@16 205 {
nuclear@16 206 (*mtl)[attrib].value = Vector4(val[0], val[1], val[2], val[3]);
nuclear@16 207 }
nuclear@16 208
nuclear@16 209 void goat3d_set_mtl_attrib1f(struct goat3d_material *mtl, const char *attrib, float val)
nuclear@16 210 {
nuclear@16 211 goat3d_set_mtl_attrib4f(mtl, attrib, val, 0, 0, 1);
nuclear@16 212 }
nuclear@16 213
nuclear@16 214 void goat3d_set_mtl_attrib3f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b)
nuclear@16 215 {
nuclear@16 216 goat3d_set_mtl_attrib4f(mtl, attrib, r, g, b, 1);
nuclear@16 217 }
nuclear@16 218
nuclear@16 219 void goat3d_set_mtl_attrib4f(struct goat3d_material *mtl, const char *attrib, float r, float g, float b, float a)
nuclear@16 220 {
nuclear@16 221 (*mtl)[attrib].value = Vector4(r, g, b, a);
nuclear@16 222 }
nuclear@16 223
nuclear@16 224 const float *goat3d_get_mtl_attrib(struct goat3d_material *mtl, const char *attrib)
nuclear@16 225 {
nuclear@16 226 return &(*mtl)[attrib].value.x;
nuclear@16 227 }
nuclear@16 228
nuclear@16 229 void goat3d_set_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib, const char *mapname)
nuclear@16 230 {
nuclear@31 231 (*mtl)[attrib].map = clean_filename(mapname);
nuclear@16 232 }
nuclear@16 233
nuclear@16 234 const char *goat3d_get_mtl_attrib_map(struct goat3d_material *mtl, const char *attrib)
nuclear@16 235 {
nuclear@16 236 return (*mtl)[attrib].map.c_str();
nuclear@16 237 }
nuclear@16 238
nuclear@27 239 // ---- meshes ----
nuclear@27 240 void goat3d_add_mesh(struct goat3d *g, struct goat3d_mesh *mesh)
nuclear@16 241 {
nuclear@27 242 g->scn->add_mesh(mesh);
nuclear@16 243 }
nuclear@16 244
nuclear@27 245 int goat3d_get_mesh_count(struct goat3d *g)
nuclear@27 246 {
nuclear@27 247 return g->scn->get_mesh_count();
nuclear@27 248 }
nuclear@27 249
nuclear@27 250 struct goat3d_mesh *goat3d_get_mesh(struct goat3d *g, int idx)
nuclear@27 251 {
nuclear@27 252 return (goat3d_mesh*)g->scn->get_mesh(idx);
nuclear@27 253 }
nuclear@27 254
nuclear@27 255 struct goat3d_mesh *goat3d_get_mesh_by_name(struct goat3d *g, const char *name)
nuclear@27 256 {
nuclear@27 257 return (goat3d_mesh*)g->scn->get_mesh(name);
nuclear@27 258 }
nuclear@27 259
nuclear@16 260 struct goat3d_mesh *goat3d_create_mesh(void)
nuclear@16 261 {
nuclear@16 262 return new goat3d_mesh;
nuclear@16 263 }
nuclear@16 264
nuclear@16 265 void goat3d_destroy_mesh(struct goat3d_mesh *mesh)
nuclear@16 266 {
nuclear@16 267 delete mesh;
nuclear@16 268 }
nuclear@16 269
nuclear@16 270 void goat3d_set_mesh_name(struct goat3d_mesh *mesh, const char *name)
nuclear@16 271 {
nuclear@16 272 mesh->name = std::string(name);
nuclear@16 273 }
nuclear@16 274
nuclear@16 275 const char *goat3d_get_mesh_name(const struct goat3d_mesh *mesh)
nuclear@16 276 {
nuclear@16 277 return mesh->name.c_str();
nuclear@16 278 }
nuclear@16 279
nuclear@16 280 void goat3d_set_mesh_mtl(struct goat3d_mesh *mesh, struct goat3d_material *mtl)
nuclear@16 281 {
nuclear@16 282 mesh->material = mtl;
nuclear@16 283 }
nuclear@16 284
nuclear@16 285 struct goat3d_material *goat3d_get_mesh_mtl(struct goat3d_mesh *mesh)
nuclear@16 286 {
nuclear@16 287 return (goat3d_material*)mesh->material;
nuclear@16 288 }
nuclear@16 289
nuclear@16 290 int goat3d_get_mesh_attrib_count(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
nuclear@16 291 {
nuclear@16 292 return (int)mesh->vertices.size();
nuclear@16 293 }
nuclear@16 294
nuclear@16 295 int goat3d_get_mesh_face_count(struct goat3d_mesh *mesh)
nuclear@16 296 {
nuclear@16 297 return (int)mesh->faces.size();
nuclear@16 298 }
nuclear@16 299
nuclear@19 300 // VECDATA is in goat3d_impl.h
nuclear@16 301 void goat3d_set_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, const void *data, int vnum)
nuclear@16 302 {
nuclear@16 303 if(attrib == GOAT3D_MESH_ATTR_VERTEX) {
nuclear@16 304 mesh->vertices = VECDATA(Vector3, data, vnum);
nuclear@16 305 return;
nuclear@16 306 }
nuclear@16 307
nuclear@16 308 if(vnum != (int)mesh->vertices.size()) {
nuclear@16 309 logmsg(LOG_ERROR, "trying to set mesh attrib data with number of elements different than the vertex array\n");
nuclear@16 310 return;
nuclear@16 311 }
nuclear@16 312
nuclear@16 313 switch(attrib) {
nuclear@16 314 case GOAT3D_MESH_ATTR_NORMAL:
nuclear@16 315 mesh->normals = VECDATA(Vector3, data, vnum);
nuclear@16 316 break;
nuclear@16 317 case GOAT3D_MESH_ATTR_TANGENT:
nuclear@16 318 mesh->tangents = VECDATA(Vector3, data, vnum);
nuclear@16 319 break;
nuclear@16 320 case GOAT3D_MESH_ATTR_TEXCOORD:
nuclear@16 321 mesh->texcoords = VECDATA(Vector2, data, vnum);
nuclear@16 322 break;
nuclear@16 323 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
nuclear@16 324 mesh->skin_weights = VECDATA(Vector4, data, vnum);
nuclear@16 325 break;
nuclear@16 326 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
nuclear@16 327 mesh->skin_matrices = VECDATA(Int4, data, vnum);
nuclear@16 328 break;
nuclear@16 329 case GOAT3D_MESH_ATTR_COLOR:
nuclear@16 330 mesh->colors = VECDATA(Vector4, data, vnum);
nuclear@16 331 default:
nuclear@16 332 break;
nuclear@16 333 }
nuclear@16 334 }
nuclear@16 335
nuclear@16 336 void *goat3d_get_mesh_attribs(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib)
nuclear@16 337 {
nuclear@16 338 return goat3d_get_mesh_attrib(mesh, attrib, 0);
nuclear@16 339 }
nuclear@16 340
nuclear@16 341 void *goat3d_get_mesh_attrib(struct goat3d_mesh *mesh, enum goat3d_mesh_attrib attrib, int idx)
nuclear@16 342 {
nuclear@16 343 switch(attrib) {
nuclear@16 344 case GOAT3D_MESH_ATTR_VERTEX:
nuclear@16 345 return mesh->vertices.empty() ? 0 : (void*)&mesh->vertices[idx];
nuclear@16 346 case GOAT3D_MESH_ATTR_NORMAL:
nuclear@16 347 return mesh->normals.empty() ? 0 : (void*)&mesh->normals[idx];
nuclear@16 348 case GOAT3D_MESH_ATTR_TANGENT:
nuclear@16 349 return mesh->tangents.empty() ? 0 : (void*)&mesh->tangents[idx];
nuclear@16 350 case GOAT3D_MESH_ATTR_TEXCOORD:
nuclear@16 351 return mesh->texcoords.empty() ? 0 : (void*)&mesh->texcoords[idx];
nuclear@16 352 case GOAT3D_MESH_ATTR_SKIN_WEIGHT:
nuclear@16 353 return mesh->skin_weights.empty() ? 0 : (void*)&mesh->skin_weights[idx];
nuclear@16 354 case GOAT3D_MESH_ATTR_SKIN_MATRIX:
nuclear@16 355 return mesh->skin_matrices.empty() ? 0 : (void*)&mesh->skin_matrices[idx];
nuclear@16 356 case GOAT3D_MESH_ATTR_COLOR:
nuclear@16 357 return mesh->colors.empty() ? 0 : (void*)&mesh->colors[idx];
nuclear@16 358 default:
nuclear@16 359 break;
nuclear@16 360 }
nuclear@16 361 return 0;
nuclear@16 362 }
nuclear@16 363
nuclear@16 364
nuclear@16 365 void goat3d_set_mesh_faces(struct goat3d_mesh *mesh, const int *data, int num)
nuclear@16 366 {
nuclear@16 367 mesh->faces = VECDATA(Face, data, num);
nuclear@16 368 }
nuclear@16 369
nuclear@16 370 int *goat3d_get_mesh_faces(struct goat3d_mesh *mesh)
nuclear@16 371 {
nuclear@16 372 return goat3d_get_mesh_face(mesh, 0);
nuclear@16 373 }
nuclear@16 374
nuclear@16 375 int *goat3d_get_mesh_face(struct goat3d_mesh *mesh, int idx)
nuclear@16 376 {
nuclear@16 377 return mesh->faces.empty() ? 0 : mesh->faces[idx].v;
nuclear@16 378 }
nuclear@16 379
nuclear@16 380 // immedate mode state
nuclear@16 381 static enum goat3d_im_primitive im_prim;
nuclear@16 382 static struct goat3d_mesh *im_mesh;
nuclear@16 383 static Vector3 im_norm, im_tang;
nuclear@16 384 static Vector2 im_texcoord;
nuclear@16 385 static Vector4 im_skinw, im_color = Vector4(1, 1, 1, 1);
nuclear@16 386 static Int4 im_skinmat;
nuclear@16 387 static bool im_use[NUM_GOAT3D_MESH_ATTRIBS];
nuclear@16 388
nuclear@16 389
nuclear@16 390 void goat3d_begin(struct goat3d_mesh *mesh, enum goat3d_im_primitive prim)
nuclear@16 391 {
nuclear@16 392 mesh->vertices.clear();
nuclear@16 393 mesh->normals.clear();
nuclear@16 394 mesh->tangents.clear();
nuclear@16 395 mesh->texcoords.clear();
nuclear@16 396 mesh->skin_weights.clear();
nuclear@16 397 mesh->skin_matrices.clear();
nuclear@16 398 mesh->colors.clear();
nuclear@16 399 mesh->faces.clear();
nuclear@16 400
nuclear@16 401 im_mesh = mesh;
nuclear@16 402 memset(im_use, 0, sizeof im_use);
nuclear@16 403
nuclear@16 404 im_prim = prim;
nuclear@16 405 }
nuclear@16 406
nuclear@16 407 void goat3d_end(void)
nuclear@16 408 {
nuclear@16 409 switch(im_prim) {
nuclear@16 410 case GOAT3D_TRIANGLES:
nuclear@17 411 {
nuclear@17 412 int num_faces = (int)im_mesh->vertices.size() / 3;
nuclear@17 413 im_mesh->faces.resize(num_faces);
nuclear@17 414
nuclear@17 415 int vidx = 0;
nuclear@17 416 for(int i=0; i<num_faces; i++) {
nuclear@17 417 im_mesh->faces[i].v[0] = vidx++;
nuclear@17 418 im_mesh->faces[i].v[1] = vidx++;
nuclear@17 419 im_mesh->faces[i].v[2] = vidx++;
nuclear@17 420 }
nuclear@17 421 }
nuclear@16 422 break;
nuclear@16 423
nuclear@16 424 case GOAT3D_QUADS:
nuclear@17 425 {
nuclear@17 426 int num_quads = (int)im_mesh->vertices.size() / 4;
nuclear@17 427 im_mesh->faces.resize(num_quads * 2);
nuclear@17 428
nuclear@17 429 int vidx = 0;
nuclear@17 430 for(int i=0; i<num_quads; i++) {
nuclear@17 431 im_mesh->faces[i * 2].v[0] = vidx;
nuclear@17 432 im_mesh->faces[i * 2].v[1] = vidx + 1;
nuclear@17 433 im_mesh->faces[i * 2].v[2] = vidx + 2;
nuclear@17 434
nuclear@17 435 im_mesh->faces[i * 2 + 1].v[0] = vidx;
nuclear@17 436 im_mesh->faces[i * 2 + 1].v[1] = vidx + 2;
nuclear@17 437 im_mesh->faces[i * 2 + 1].v[2] = vidx + 3;
nuclear@17 438
nuclear@17 439 vidx += 4;
nuclear@17 440 }
nuclear@17 441 }
nuclear@16 442 break;
nuclear@16 443
nuclear@16 444 default:
nuclear@16 445 return;
nuclear@16 446 };
nuclear@16 447 }
nuclear@16 448
nuclear@16 449 void goat3d_vertex3f(float x, float y, float z)
nuclear@16 450 {
nuclear@16 451 im_mesh->vertices.push_back(Vector3(x, y, z));
nuclear@16 452 if(im_use[GOAT3D_MESH_ATTR_NORMAL]) {
nuclear@16 453 im_mesh->normals.push_back(im_norm);
nuclear@16 454 }
nuclear@16 455 if(im_use[GOAT3D_MESH_ATTR_TANGENT]) {
nuclear@16 456 im_mesh->tangents.push_back(im_tang);
nuclear@16 457 }
nuclear@16 458 if(im_use[GOAT3D_MESH_ATTR_TEXCOORD]) {
nuclear@16 459 im_mesh->texcoords.push_back(im_texcoord);
nuclear@16 460 }
nuclear@16 461 if(im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT]) {
nuclear@16 462 im_mesh->skin_weights.push_back(im_skinw);
nuclear@16 463 }
nuclear@16 464 if(im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX]) {
nuclear@16 465 im_mesh->skin_matrices.push_back(im_skinmat);
nuclear@16 466 }
nuclear@16 467 if(im_use[GOAT3D_MESH_ATTR_COLOR]) {
nuclear@16 468 im_mesh->colors.push_back(im_color);
nuclear@16 469 }
nuclear@16 470 }
nuclear@16 471
nuclear@16 472 void goat3d_normal3f(float x, float y, float z)
nuclear@16 473 {
nuclear@16 474 im_norm = Vector3(x, y, z);
nuclear@17 475 im_use[GOAT3D_MESH_ATTR_NORMAL] = true;
nuclear@16 476 }
nuclear@16 477
nuclear@16 478 void goat3d_tangent3f(float x, float y, float z)
nuclear@16 479 {
nuclear@16 480 im_tang = Vector3(x, y, z);
nuclear@17 481 im_use[GOAT3D_MESH_ATTR_TANGENT] = true;
nuclear@16 482 }
nuclear@16 483
nuclear@16 484 void goat3d_texcoord2f(float x, float y)
nuclear@16 485 {
nuclear@16 486 im_texcoord = Vector2(x, y);
nuclear@17 487 im_use[GOAT3D_MESH_ATTR_TEXCOORD] = true;
nuclear@16 488 }
nuclear@16 489
nuclear@16 490 void goat3d_skin_weight4f(float x, float y, float z, float w)
nuclear@16 491 {
nuclear@16 492 im_skinw = Vector4(x, y, z, w);
nuclear@17 493 im_use[GOAT3D_MESH_ATTR_SKIN_WEIGHT] = true;
nuclear@16 494 }
nuclear@16 495
nuclear@16 496 void goat3d_skin_matrix4i(int x, int y, int z, int w)
nuclear@16 497 {
nuclear@16 498 im_skinmat.x = x;
nuclear@16 499 im_skinmat.y = y;
nuclear@16 500 im_skinmat.z = z;
nuclear@16 501 im_skinmat.w = w;
nuclear@17 502 im_use[GOAT3D_MESH_ATTR_SKIN_MATRIX] = true;
nuclear@16 503 }
nuclear@16 504
nuclear@16 505 void goat3d_color3f(float x, float y, float z)
nuclear@16 506 {
nuclear@17 507 goat3d_color4f(x, y, z, 1.0f);
nuclear@16 508 }
nuclear@16 509
nuclear@16 510 void goat3d_color4f(float x, float y, float z, float w)
nuclear@16 511 {
nuclear@16 512 im_color = Vector4(x, y, z, w);
nuclear@17 513 im_use[GOAT3D_MESH_ATTR_COLOR] = true;
nuclear@16 514 }
nuclear@16 515
nuclear@27 516 /* lights */
nuclear@27 517 void goat3d_add_light(struct goat3d *g, struct goat3d_light *lt)
nuclear@16 518 {
nuclear@27 519 g->scn->add_light(lt);
nuclear@16 520 }
nuclear@16 521
nuclear@27 522 int goat3d_get_light_count(struct goat3d *g)
nuclear@19 523 {
nuclear@27 524 return g->scn->get_light_count();
nuclear@19 525 }
nuclear@19 526
nuclear@27 527 struct goat3d_light *goat3d_get_light(struct goat3d *g, int idx)
nuclear@19 528 {
nuclear@27 529 return (goat3d_light*)g->scn->get_light(idx);
nuclear@19 530 }
nuclear@19 531
nuclear@27 532 struct goat3d_light *goat3d_get_light_by_name(struct goat3d *g, const char *name)
nuclear@27 533 {
nuclear@27 534 return (goat3d_light*)g->scn->get_light(name);
nuclear@27 535 }
nuclear@27 536
nuclear@27 537
nuclear@27 538 struct goat3d_light *goat3d_create_light(void)
nuclear@27 539 {
nuclear@27 540 return new goat3d_light;
nuclear@27 541 }
nuclear@27 542
nuclear@27 543 void goat3d_destroy_light(struct goat3d_light *lt)
nuclear@27 544 {
nuclear@27 545 delete lt;
nuclear@27 546 }
nuclear@27 547
nuclear@27 548
nuclear@27 549 /* cameras */
nuclear@27 550 void goat3d_add_camera(struct goat3d *g, struct goat3d_camera *cam)
nuclear@27 551 {
nuclear@27 552 g->scn->add_camera(cam);
nuclear@27 553 }
nuclear@27 554
nuclear@27 555 int goat3d_get_camera_count(struct goat3d *g)
nuclear@27 556 {
nuclear@27 557 return g->scn->get_camera_count();
nuclear@27 558 }
nuclear@27 559
nuclear@27 560 struct goat3d_camera *goat3d_get_camera(struct goat3d *g, int idx)
nuclear@27 561 {
nuclear@27 562 return (goat3d_camera*)g->scn->get_camera(idx);
nuclear@27 563 }
nuclear@27 564
nuclear@27 565 struct goat3d_camera *goat3d_get_camera_by_name(struct goat3d *g, const char *name)
nuclear@27 566 {
nuclear@27 567 return (goat3d_camera*)g->scn->get_camera(name);
nuclear@27 568 }
nuclear@27 569
nuclear@27 570 struct goat3d_camera *goat3d_create_camera(void)
nuclear@27 571 {
nuclear@27 572 return new goat3d_camera;
nuclear@27 573 }
nuclear@27 574
nuclear@27 575 void goat3d_destroy_camera(struct goat3d_camera *cam)
nuclear@27 576 {
nuclear@27 577 delete cam;
nuclear@27 578 }
nuclear@27 579
nuclear@27 580
nuclear@16 581
nuclear@25 582 // node
nuclear@27 583 void goat3d_add_node(struct goat3d *g, struct goat3d_node *node)
nuclear@27 584 {
nuclear@27 585 g->scn->add_node(node);
nuclear@27 586 }
nuclear@27 587
nuclear@27 588 int goat3d_get_node_count(struct goat3d *g)
nuclear@27 589 {
nuclear@27 590 return g->scn->get_node_count();
nuclear@27 591 }
nuclear@27 592
nuclear@27 593 struct goat3d_node *goat3d_get_node(struct goat3d *g, int idx)
nuclear@27 594 {
nuclear@27 595 return (goat3d_node*)g->scn->get_node(idx);
nuclear@27 596 }
nuclear@27 597
nuclear@27 598 struct goat3d_node *goat3d_get_node_by_name(struct goat3d *g, const char *name)
nuclear@27 599 {
nuclear@27 600 return (goat3d_node*)g->scn->get_node(name);
nuclear@27 601 }
nuclear@25 602
nuclear@25 603 struct goat3d_node *goat3d_create_node(void)
nuclear@25 604 {
nuclear@25 605 return new goat3d_node;
nuclear@25 606 }
nuclear@25 607
nuclear@25 608 void goat3d_set_node_name(struct goat3d_node *node, const char *name)
nuclear@25 609 {
nuclear@25 610 node->set_name(name);
nuclear@25 611 }
nuclear@25 612
nuclear@26 613 const char *goat3d_get_node_name(const struct goat3d_node *node)
nuclear@25 614 {
nuclear@25 615 return node->get_name();
nuclear@25 616 }
nuclear@25 617
nuclear@25 618 void goat3d_set_node_object(struct goat3d_node *node, enum goat3d_node_type type, void *obj)
nuclear@25 619 {
nuclear@25 620 node->set_object((Object*)obj);
nuclear@25 621 }
nuclear@25 622
nuclear@26 623 void *goat3d_get_node_object(const struct goat3d_node *node)
nuclear@25 624 {
nuclear@26 625 return (void*)node->get_object();
nuclear@25 626 }
nuclear@25 627
nuclear@26 628 enum goat3d_node_type goat3d_get_node_type(const struct goat3d_node *node)
nuclear@25 629 {
nuclear@26 630 const Object *obj = node->get_object();
nuclear@26 631 if(dynamic_cast<const Mesh*>(obj)) {
nuclear@26 632 return GOAT3D_NODE_MESH;
nuclear@26 633 }
nuclear@26 634 if(dynamic_cast<const Light*>(obj)) {
nuclear@26 635 return GOAT3D_NODE_LIGHT;
nuclear@26 636 }
nuclear@26 637 if(dynamic_cast<const Camera*>(obj)) {
nuclear@26 638 return GOAT3D_NODE_CAMERA;
nuclear@26 639 }
nuclear@26 640
nuclear@26 641 return GOAT3D_NODE_NULL;
nuclear@25 642 }
nuclear@25 643
nuclear@25 644 void goat3d_add_node_child(struct goat3d_node *node, struct goat3d_node *child)
nuclear@25 645 {
nuclear@26 646 node->add_child(node);
nuclear@25 647 }
nuclear@25 648
nuclear@26 649 int goat3d_get_node_child_count(const struct goat3d_node *node)
nuclear@25 650 {
nuclear@26 651 return node->get_children_count();
nuclear@25 652 }
nuclear@25 653
nuclear@26 654 struct goat3d_node *goat3d_get_node_child(const struct goat3d_node *node, int idx)
nuclear@25 655 {
nuclear@26 656 return (goat3d_node*)node->get_child(idx);
nuclear@25 657 }
nuclear@25 658
nuclear@25 659 void goat3d_set_node_position(struct goat3d_node *node, float x, float y, float z, long tmsec)
nuclear@25 660 {
nuclear@26 661 node->set_position(Vector3(x, y, z), tmsec);
nuclear@25 662 }
nuclear@25 663
nuclear@25 664 void goat3d_set_node_rotation(struct goat3d_node *node, float qx, float qy, float qz, float qw, long tmsec)
nuclear@25 665 {
nuclear@26 666 node->set_rotation(Quaternion(qw, qx, qy, qz), tmsec);
nuclear@25 667 }
nuclear@25 668
nuclear@25 669 void goat3d_set_node_scaling(struct goat3d_node *node, float sx, float sy, float sz, long tmsec)
nuclear@25 670 {
nuclear@26 671 node->set_scaling(Vector3(sx, sy, sz), tmsec);
nuclear@25 672 }
nuclear@25 673
nuclear@25 674 void goat3d_set_node_pivot(struct goat3d_node *node, float px, float py, float pz)
nuclear@25 675 {
nuclear@26 676 node->set_pivot(Vector3(px, py, pz));
nuclear@25 677 }
nuclear@25 678
nuclear@25 679
nuclear@26 680 void goat3d_get_node_position(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
nuclear@25 681 {
nuclear@26 682 Vector3 pos = node->get_position(tmsec);
nuclear@26 683 *xptr = pos.x;
nuclear@26 684 *yptr = pos.y;
nuclear@26 685 *zptr = pos.z;
nuclear@25 686 }
nuclear@25 687
nuclear@26 688 void goat3d_get_node_rotation(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, float *wptr, long tmsec)
nuclear@25 689 {
nuclear@26 690 Quaternion q = node->get_rotation(tmsec);
nuclear@26 691 *xptr = q.v.x;
nuclear@26 692 *yptr = q.v.y;
nuclear@26 693 *zptr = q.v.z;
nuclear@26 694 *wptr = q.s;
nuclear@25 695 }
nuclear@25 696
nuclear@26 697 void goat3d_get_node_scaling(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr, long tmsec)
nuclear@25 698 {
nuclear@26 699 Vector3 scale = node->get_scaling(tmsec);
nuclear@26 700 *xptr = scale.x;
nuclear@26 701 *yptr = scale.y;
nuclear@26 702 *zptr = scale.z;
nuclear@25 703 }
nuclear@25 704
nuclear@26 705 void goat3d_get_node_pivot(const struct goat3d_node *node, float *xptr, float *yptr, float *zptr)
nuclear@25 706 {
nuclear@26 707 Vector3 pivot = node->get_pivot();
nuclear@26 708 *xptr = pivot.x;
nuclear@26 709 *yptr = pivot.y;
nuclear@26 710 *zptr = pivot.z;
nuclear@25 711 }
nuclear@25 712
nuclear@25 713
nuclear@26 714 void goat3d_get_node_matrix(const struct goat3d_node *node, float *matrix, long tmsec)
nuclear@25 715 {
nuclear@26 716 node->get_xform(tmsec, (Matrix4x4*)matrix);
nuclear@25 717 }
nuclear@25 718
nuclear@25 719
nuclear@16 720 } // extern "C"
nuclear@16 721
nuclear@16 722
nuclear@16 723 static long read_file(void *buf, size_t bytes, void *uptr)
nuclear@16 724 {
nuclear@16 725 return (long)fread(buf, 1, bytes, (FILE*)uptr);
nuclear@16 726 }
nuclear@16 727
nuclear@16 728 static long write_file(const void *buf, size_t bytes, void *uptr)
nuclear@16 729 {
nuclear@16 730 return (long)fwrite(buf, 1, bytes, (FILE*)uptr);
nuclear@16 731 }
nuclear@16 732
nuclear@16 733 static long seek_file(long offs, int whence, void *uptr)
nuclear@16 734 {
nuclear@16 735 if(fseek((FILE*)uptr, offs, whence) == -1) {
nuclear@16 736 return -1;
nuclear@16 737 }
nuclear@16 738 return ftell((FILE*)uptr);
nuclear@16 739 }
nuclear@31 740
nuclear@31 741 static std::string clean_filename(const char *str)
nuclear@31 742 {
nuclear@31 743 const char *last_slash = strrchr(str, '/');
nuclear@31 744 if(!last_slash) {
nuclear@31 745 last_slash = strrchr(str, '\\');
nuclear@31 746 }
nuclear@31 747
nuclear@31 748 if(last_slash) {
nuclear@31 749 str = last_slash + 1;
nuclear@31 750 }
nuclear@31 751
nuclear@31 752 char *buf = (char*)alloca(strlen(str) + 1);
nuclear@31 753 char *dest = buf;
nuclear@31 754 while(*str) {
nuclear@31 755 char c = *str++;
nuclear@31 756 *dest++ = tolower(c);
nuclear@31 757 }
nuclear@31 758 return buf;
nuclear@31 759 }