goat3d

annotate src/goat3d_write.cc @ 50:0be413ac2e0a

ass2goat compiles...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 16 Jan 2014 19:27:09 +0200
parents 498ca7ac7047
children cb5414f406eb
rev   line source
nuclear@14 1 #include "goat3d_impl.h"
nuclear@14 2 #include "chunk.h"
nuclear@14 3
nuclear@47 4 using namespace g3dimpl;
nuclear@47 5
nuclear@14 6 /*
nuclear@14 7 static long save_env(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 8 static long save_materials(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 9 static long save_material(const Material *mat, long offset, goat3d_io *io);
nuclear@14 10 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io);
nuclear@14 11 static long save_meshes(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 12 static long save_lights(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 13 static long save_cameras(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 14 static long save_nodes(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 15
nuclear@14 16 static long write_chunk_float(int id, float val, long offs, goat3d_io *io);
nuclear@14 17 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io);
nuclear@14 18 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io);
nuclear@14 19 */
nuclear@14 20
nuclear@14 21 bool Scene::save(goat3d_io *io) const
nuclear@14 22 {
nuclear@14 23 /*
nuclear@14 24 long res;
nuclear@14 25
nuclear@14 26 ChunkHeader hdr;
nuclear@14 27 hdr.id = CNK_SCENE;
nuclear@14 28 hdr.size = sizeof hdr;
nuclear@14 29
nuclear@14 30 if((res = save_env(this, hdr.size, io)) < 0) {
nuclear@14 31 return false;
nuclear@14 32 }
nuclear@14 33 hdr.size += res;
nuclear@14 34
nuclear@14 35 if((res = save_materials(this, hdr.size, io)) < 0) {
nuclear@14 36 return false;
nuclear@14 37 }
nuclear@14 38 hdr.size += res;
nuclear@14 39
nuclear@14 40 if((res = save_meshes(this, hdr.size, io)) < 0) {
nuclear@14 41 return false;
nuclear@14 42 }
nuclear@14 43 hdr.size += res;
nuclear@14 44
nuclear@14 45 if((res = save_lights(this, hdr.size, io)) < 0) {
nuclear@14 46 return false;
nuclear@14 47 }
nuclear@14 48 hdr.size += res;
nuclear@14 49
nuclear@14 50 if((res = save_cameras(this, hdr.size, io)) < 0) {
nuclear@14 51 return false;
nuclear@14 52 }
nuclear@14 53 hdr.size += res;
nuclear@14 54
nuclear@14 55 if((res = save_nodes(this, hdr.size, io)) < 0) {
nuclear@14 56 return false;
nuclear@14 57 }
nuclear@14 58 hdr.size += res;
nuclear@14 59
nuclear@14 60 // now go back and write the root chunk
nuclear@14 61 io->seek(0, SEEK_SET, io->cls);
nuclear@14 62 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
nuclear@14 63 return false;
nuclear@14 64 }
nuclear@14 65
nuclear@14 66 return true;
nuclear@14 67 */
nuclear@14 68 return false;
nuclear@14 69 }
nuclear@14 70
nuclear@14 71
nuclear@14 72 #if 0
nuclear@14 73 static long save_env(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 74 {
nuclear@14 75 long res;
nuclear@14 76
nuclear@14 77 ChunkHeader hdr;
nuclear@14 78 hdr.id = CNK_ENV;
nuclear@14 79 hdr.size = sizeof hdr;
nuclear@14 80
nuclear@14 81 if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
nuclear@14 82 return -1;
nuclear@14 83 }
nuclear@14 84 hdr.size += res;
nuclear@14 85
nuclear@14 86 // TODO add fog chunk
nuclear@14 87
nuclear@14 88 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 89 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
nuclear@14 90 return -1;
nuclear@14 91 }
nuclear@14 92 return hdr.size;
nuclear@14 93 }
nuclear@14 94
nuclear@14 95 static long save_materials(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 96 {
nuclear@14 97 long res;
nuclear@14 98
nuclear@14 99 ChunkHeader hdr;
nuclear@14 100 hdr.id = CNK_MTL_LIST;
nuclear@14 101 hdr.size = sizeof hdr;
nuclear@14 102
nuclear@14 103 for(int i=0; i<scn->get_material_count(); i++) {
nuclear@14 104 if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
nuclear@14 105 return -1;
nuclear@14 106 }
nuclear@14 107 hdr.size += res;
nuclear@14 108 }
nuclear@14 109
nuclear@14 110 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 111 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
nuclear@14 112 return -1;
nuclear@14 113 }
nuclear@14 114 return hdr.size;
nuclear@14 115 }
nuclear@14 116
nuclear@14 117 static long save_material(const Material *mat, long offset, goat3d_io *io)
nuclear@14 118 {
nuclear@14 119 long res;
nuclear@14 120
nuclear@14 121 ChunkHeader hdr;
nuclear@14 122 hdr.id = CNK_MTL;
nuclear@14 123 hdr.size = sizeof hdr;
nuclear@14 124
nuclear@14 125 for(int i=0; i<mat->get_attrib_count(); i++) {
nuclear@14 126 const char *name = mat->get_attrib_name(i);
nuclear@14 127 if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
nuclear@14 128 return -1;
nuclear@14 129 }
nuclear@14 130 hdr.size += res;
nuclear@14 131 }
nuclear@14 132
nuclear@14 133 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 134 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
nuclear@14 135 return -1;
nuclear@14 136 }
nuclear@14 137 return hdr.size;
nuclear@14 138 }
nuclear@14 139
nuclear@14 140 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
nuclear@14 141 {
nuclear@14 142 long res;
nuclear@14 143
nuclear@14 144 ChunkHeader hdr;
nuclear@14 145 hdr.id = CNK_MTL_ATTR;
nuclear@14 146 hdr.size = sizeof hdr;
nuclear@14 147
nuclear@14 148 // TODO cont.
nuclear@14 149 return -1;
nuclear@14 150 }
nuclear@14 151
nuclear@14 152 static long save_meshes(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 153 {
nuclear@14 154 return 0;
nuclear@14 155 }
nuclear@14 156
nuclear@14 157 static long save_lights(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 158 {
nuclear@14 159 return 0;
nuclear@14 160 }
nuclear@14 161
nuclear@14 162 static long save_cameras(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 163 {
nuclear@14 164 return 0;
nuclear@14 165 }
nuclear@14 166
nuclear@14 167 static long save_nodes(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 168 {
nuclear@14 169 return 0;
nuclear@14 170 }
nuclear@14 171
nuclear@14 172 static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
nuclear@14 173 {
nuclear@14 174 int size = sizeof(ChunkHeader) + sizeof val;
nuclear@14 175 char *buf = (char*)alloca(size);
nuclear@14 176
nuclear@14 177 Chunk *c = (Chunk*)buf;
nuclear@14 178 c->hdr.id = id;
nuclear@14 179 c->hdr.size = size;
nuclear@14 180 *(float*)c->data = val;
nuclear@14 181
nuclear@14 182 io->seek(offs, SEEK_SET, io->cls);
nuclear@14 183 if(io->write(buf, size, io->cls) < size) {
nuclear@14 184 return -1;
nuclear@14 185 }
nuclear@14 186 return size;
nuclear@14 187 }
nuclear@14 188
nuclear@14 189 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io)
nuclear@14 190 {
nuclear@14 191 int size = sizeof(ChunkHeader) + sizeof vec;
nuclear@14 192 char *buf = (char*)alloca(size);
nuclear@14 193
nuclear@14 194 Chunk *c = (Chunk*)buf;
nuclear@14 195 c->hdr.id = id;
nuclear@14 196 c->hdr.size = size;
nuclear@14 197 *(Vector3*)c->data = vec;
nuclear@14 198
nuclear@14 199 io->seek(offs, SEEK_SET, io->cls);
nuclear@14 200 if(io->write(buf, size, io->cls) < size) {
nuclear@14 201 return -1;
nuclear@14 202 }
nuclear@14 203 return size;
nuclear@14 204 }
nuclear@14 205
nuclear@14 206 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
nuclear@14 207 {
nuclear@14 208 int size = sizeof(ChunkHeader) + sizeof vec;
nuclear@14 209 char *buf = (char*)alloca(size);
nuclear@14 210
nuclear@14 211 Chunk *c = (Chunk*)buf;
nuclear@14 212 c->hdr.id = id;
nuclear@14 213 c->hdr.size = size;
nuclear@14 214 *(Vector4*)c->data = vec;
nuclear@14 215
nuclear@14 216 io->seek(offs, SEEK_SET, io->cls);
nuclear@14 217 if(io->write(buf, size, io->cls) < size) {
nuclear@14 218 return -1;
nuclear@14 219 }
nuclear@14 220 return size;
nuclear@14 221 }
nuclear@14 222 #endif
nuclear@50 223
nuclear@50 224 bool Scene::save_anim(const XFormNode *node, goat3d_io *io) const
nuclear@50 225 {
nuclear@50 226 return false;
nuclear@50 227 }