goat3d

annotate src/goat3d_write.cc @ 60:0c3576325480

moving the exporter along slowly
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 30 Mar 2014 08:53:33 +0300
parents 6d514398a728
children 99715321ad6d
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@55 71 bool Scene::save_anim(goat3d_io *io) const
nuclear@51 72 {
nuclear@51 73 return false;
nuclear@51 74 }
nuclear@51 75
nuclear@14 76
nuclear@14 77 #if 0
nuclear@14 78 static long save_env(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 79 {
nuclear@14 80 long res;
nuclear@14 81
nuclear@14 82 ChunkHeader hdr;
nuclear@14 83 hdr.id = CNK_ENV;
nuclear@14 84 hdr.size = sizeof hdr;
nuclear@14 85
nuclear@14 86 if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
nuclear@14 87 return -1;
nuclear@14 88 }
nuclear@14 89 hdr.size += res;
nuclear@14 90
nuclear@14 91 // TODO add fog chunk
nuclear@14 92
nuclear@14 93 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 94 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
nuclear@14 95 return -1;
nuclear@14 96 }
nuclear@14 97 return hdr.size;
nuclear@14 98 }
nuclear@14 99
nuclear@14 100 static long save_materials(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 101 {
nuclear@14 102 long res;
nuclear@14 103
nuclear@14 104 ChunkHeader hdr;
nuclear@14 105 hdr.id = CNK_MTL_LIST;
nuclear@14 106 hdr.size = sizeof hdr;
nuclear@14 107
nuclear@14 108 for(int i=0; i<scn->get_material_count(); i++) {
nuclear@14 109 if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
nuclear@14 110 return -1;
nuclear@14 111 }
nuclear@14 112 hdr.size += res;
nuclear@14 113 }
nuclear@14 114
nuclear@14 115 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 116 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
nuclear@14 117 return -1;
nuclear@14 118 }
nuclear@14 119 return hdr.size;
nuclear@14 120 }
nuclear@14 121
nuclear@14 122 static long save_material(const Material *mat, long offset, goat3d_io *io)
nuclear@14 123 {
nuclear@14 124 long res;
nuclear@14 125
nuclear@14 126 ChunkHeader hdr;
nuclear@14 127 hdr.id = CNK_MTL;
nuclear@14 128 hdr.size = sizeof hdr;
nuclear@14 129
nuclear@14 130 for(int i=0; i<mat->get_attrib_count(); i++) {
nuclear@14 131 const char *name = mat->get_attrib_name(i);
nuclear@14 132 if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
nuclear@14 133 return -1;
nuclear@14 134 }
nuclear@14 135 hdr.size += res;
nuclear@14 136 }
nuclear@14 137
nuclear@14 138 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 139 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
nuclear@14 140 return -1;
nuclear@14 141 }
nuclear@14 142 return hdr.size;
nuclear@14 143 }
nuclear@14 144
nuclear@14 145 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
nuclear@14 146 {
nuclear@14 147 long res;
nuclear@14 148
nuclear@14 149 ChunkHeader hdr;
nuclear@14 150 hdr.id = CNK_MTL_ATTR;
nuclear@14 151 hdr.size = sizeof hdr;
nuclear@14 152
nuclear@14 153 // TODO cont.
nuclear@14 154 return -1;
nuclear@14 155 }
nuclear@14 156
nuclear@14 157 static long save_meshes(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_lights(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_cameras(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 save_nodes(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 173 {
nuclear@14 174 return 0;
nuclear@14 175 }
nuclear@14 176
nuclear@14 177 static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
nuclear@14 178 {
nuclear@14 179 int size = sizeof(ChunkHeader) + sizeof val;
nuclear@14 180 char *buf = (char*)alloca(size);
nuclear@14 181
nuclear@14 182 Chunk *c = (Chunk*)buf;
nuclear@14 183 c->hdr.id = id;
nuclear@14 184 c->hdr.size = size;
nuclear@14 185 *(float*)c->data = val;
nuclear@14 186
nuclear@14 187 io->seek(offs, SEEK_SET, io->cls);
nuclear@14 188 if(io->write(buf, size, io->cls) < size) {
nuclear@14 189 return -1;
nuclear@14 190 }
nuclear@14 191 return size;
nuclear@14 192 }
nuclear@14 193
nuclear@14 194 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io)
nuclear@14 195 {
nuclear@14 196 int size = sizeof(ChunkHeader) + sizeof vec;
nuclear@14 197 char *buf = (char*)alloca(size);
nuclear@14 198
nuclear@14 199 Chunk *c = (Chunk*)buf;
nuclear@14 200 c->hdr.id = id;
nuclear@14 201 c->hdr.size = size;
nuclear@14 202 *(Vector3*)c->data = vec;
nuclear@14 203
nuclear@14 204 io->seek(offs, SEEK_SET, io->cls);
nuclear@14 205 if(io->write(buf, size, io->cls) < size) {
nuclear@14 206 return -1;
nuclear@14 207 }
nuclear@14 208 return size;
nuclear@14 209 }
nuclear@14 210
nuclear@14 211 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
nuclear@14 212 {
nuclear@14 213 int size = sizeof(ChunkHeader) + sizeof vec;
nuclear@14 214 char *buf = (char*)alloca(size);
nuclear@14 215
nuclear@14 216 Chunk *c = (Chunk*)buf;
nuclear@14 217 c->hdr.id = id;
nuclear@14 218 c->hdr.size = size;
nuclear@14 219 *(Vector4*)c->data = vec;
nuclear@14 220
nuclear@14 221 io->seek(offs, SEEK_SET, io->cls);
nuclear@14 222 if(io->write(buf, size, io->cls) < size) {
nuclear@14 223 return -1;
nuclear@14 224 }
nuclear@14 225 return size;
nuclear@14 226 }
nuclear@14 227 #endif