goat3d

annotate src/goat3d_write.cc @ 33:f43f4849c86a

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