goat3d

annotate src/goat3d.cc @ 33:f43f4849c86a

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