goat3d

annotate src/goat3d.cc @ 103:45a9d493e98c

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