goat3d

annotate src/goat3d.cc @ 27:4deb0b12fe14

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