nuclear@14: #include "goat3d_impl.h" nuclear@14: #include "chunk.h" nuclear@14: nuclear@47: using namespace g3dimpl; nuclear@47: nuclear@14: /* nuclear@14: static long save_env(const Scene *scn, long offset, goat3d_io *io); nuclear@14: static long save_materials(const Scene *scn, long offset, goat3d_io *io); nuclear@14: static long save_material(const Material *mat, long offset, goat3d_io *io); nuclear@14: static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io); nuclear@14: static long save_meshes(const Scene *scn, long offset, goat3d_io *io); nuclear@14: static long save_lights(const Scene *scn, long offset, goat3d_io *io); nuclear@14: static long save_cameras(const Scene *scn, long offset, goat3d_io *io); nuclear@14: static long save_nodes(const Scene *scn, long offset, goat3d_io *io); nuclear@14: nuclear@14: static long write_chunk_float(int id, float val, long offs, goat3d_io *io); nuclear@14: static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io); nuclear@14: static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io); nuclear@14: */ nuclear@14: nuclear@14: bool Scene::save(goat3d_io *io) const nuclear@14: { nuclear@14: /* nuclear@14: long res; nuclear@14: nuclear@14: ChunkHeader hdr; nuclear@14: hdr.id = CNK_SCENE; nuclear@14: hdr.size = sizeof hdr; nuclear@14: nuclear@14: if((res = save_env(this, hdr.size, io)) < 0) { nuclear@14: return false; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: nuclear@14: if((res = save_materials(this, hdr.size, io)) < 0) { nuclear@14: return false; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: nuclear@14: if((res = save_meshes(this, hdr.size, io)) < 0) { nuclear@14: return false; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: nuclear@14: if((res = save_lights(this, hdr.size, io)) < 0) { nuclear@14: return false; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: nuclear@14: if((res = save_cameras(this, hdr.size, io)) < 0) { nuclear@14: return false; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: nuclear@14: if((res = save_nodes(this, hdr.size, io)) < 0) { nuclear@14: return false; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: nuclear@14: // now go back and write the root chunk nuclear@14: io->seek(0, SEEK_SET, io->cls); nuclear@14: if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) { nuclear@14: return false; nuclear@14: } nuclear@14: nuclear@14: return true; nuclear@14: */ nuclear@14: return false; nuclear@14: } nuclear@14: nuclear@51: bool Scene::save_anim(const XFormNode *node, goat3d_io *io) const nuclear@51: { nuclear@51: return false; nuclear@51: } nuclear@51: nuclear@14: nuclear@14: #if 0 nuclear@14: static long save_env(const Scene *scn, long offset, goat3d_io *io) nuclear@14: { nuclear@14: long res; nuclear@14: nuclear@14: ChunkHeader hdr; nuclear@14: hdr.id = CNK_ENV; nuclear@14: hdr.size = sizeof hdr; nuclear@14: nuclear@14: if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) { nuclear@14: return -1; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: nuclear@14: // TODO add fog chunk nuclear@14: nuclear@14: io->seek(offset, SEEK_SET, io->cls); nuclear@14: if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) { nuclear@14: return -1; nuclear@14: } nuclear@14: return hdr.size; nuclear@14: } nuclear@14: nuclear@14: static long save_materials(const Scene *scn, long offset, goat3d_io *io) nuclear@14: { nuclear@14: long res; nuclear@14: nuclear@14: ChunkHeader hdr; nuclear@14: hdr.id = CNK_MTL_LIST; nuclear@14: hdr.size = sizeof hdr; nuclear@14: nuclear@14: for(int i=0; iget_material_count(); i++) { nuclear@14: if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) { nuclear@14: return -1; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: } nuclear@14: nuclear@14: io->seek(offset, SEEK_SET, io->cls); nuclear@14: if(io->write(&hdr, hdr.size, io->cls) < hdr.size) { nuclear@14: return -1; nuclear@14: } nuclear@14: return hdr.size; nuclear@14: } nuclear@14: nuclear@14: static long save_material(const Material *mat, long offset, goat3d_io *io) nuclear@14: { nuclear@14: long res; nuclear@14: nuclear@14: ChunkHeader hdr; nuclear@14: hdr.id = CNK_MTL; nuclear@14: hdr.size = sizeof hdr; nuclear@14: nuclear@14: for(int i=0; iget_attrib_count(); i++) { nuclear@14: const char *name = mat->get_attrib_name(i); nuclear@14: if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) { nuclear@14: return -1; nuclear@14: } nuclear@14: hdr.size += res; nuclear@14: } nuclear@14: nuclear@14: io->seek(offset, SEEK_SET, io->cls); nuclear@14: if(io->write(&hdr, hdr.size, io->cls) < hdr.size) { nuclear@14: return -1; nuclear@14: } nuclear@14: return hdr.size; nuclear@14: } nuclear@14: nuclear@14: static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io) nuclear@14: { nuclear@14: long res; nuclear@14: nuclear@14: ChunkHeader hdr; nuclear@14: hdr.id = CNK_MTL_ATTR; nuclear@14: hdr.size = sizeof hdr; nuclear@14: nuclear@14: // TODO cont. nuclear@14: return -1; nuclear@14: } nuclear@14: nuclear@14: static long save_meshes(const Scene *scn, long offset, goat3d_io *io) nuclear@14: { nuclear@14: return 0; nuclear@14: } nuclear@14: nuclear@14: static long save_lights(const Scene *scn, long offset, goat3d_io *io) nuclear@14: { nuclear@14: return 0; nuclear@14: } nuclear@14: nuclear@14: static long save_cameras(const Scene *scn, long offset, goat3d_io *io) nuclear@14: { nuclear@14: return 0; nuclear@14: } nuclear@14: nuclear@14: static long save_nodes(const Scene *scn, long offset, goat3d_io *io) nuclear@14: { nuclear@14: return 0; nuclear@14: } nuclear@14: nuclear@14: static long write_chunk_float(int id, float val, long offs, goat3d_io *io) nuclear@14: { nuclear@14: int size = sizeof(ChunkHeader) + sizeof val; nuclear@14: char *buf = (char*)alloca(size); nuclear@14: nuclear@14: Chunk *c = (Chunk*)buf; nuclear@14: c->hdr.id = id; nuclear@14: c->hdr.size = size; nuclear@14: *(float*)c->data = val; nuclear@14: nuclear@14: io->seek(offs, SEEK_SET, io->cls); nuclear@14: if(io->write(buf, size, io->cls) < size) { nuclear@14: return -1; nuclear@14: } nuclear@14: return size; nuclear@14: } nuclear@14: nuclear@14: static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io) nuclear@14: { nuclear@14: int size = sizeof(ChunkHeader) + sizeof vec; nuclear@14: char *buf = (char*)alloca(size); nuclear@14: nuclear@14: Chunk *c = (Chunk*)buf; nuclear@14: c->hdr.id = id; nuclear@14: c->hdr.size = size; nuclear@14: *(Vector3*)c->data = vec; nuclear@14: nuclear@14: io->seek(offs, SEEK_SET, io->cls); nuclear@14: if(io->write(buf, size, io->cls) < size) { nuclear@14: return -1; nuclear@14: } nuclear@14: return size; nuclear@14: } nuclear@14: nuclear@14: static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io) nuclear@14: { nuclear@14: int size = sizeof(ChunkHeader) + sizeof vec; nuclear@14: char *buf = (char*)alloca(size); nuclear@14: nuclear@14: Chunk *c = (Chunk*)buf; nuclear@14: c->hdr.id = id; nuclear@14: c->hdr.size = size; nuclear@14: *(Vector4*)c->data = vec; nuclear@14: nuclear@14: io->seek(offs, SEEK_SET, io->cls); nuclear@14: if(io->write(buf, size, io->cls) < size) { nuclear@14: return -1; nuclear@14: } nuclear@14: return size; nuclear@14: } nuclear@14: #endif nuclear@50: nuclear@50: bool Scene::save_anim(const XFormNode *node, goat3d_io *io) const nuclear@50: { nuclear@50: return false; nuclear@50: }