goat3d

annotate src/goat3d.cc @ 54:dad392c710df

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