goat3d

annotate src/goat3d_write.cc @ 83:57e745dd13c2

almost working
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 12 May 2014 07:22:52 +0300
parents dad392c710df af1310ed212b
children
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@14 18 #include "goat3d_impl.h"
nuclear@14 19 #include "chunk.h"
nuclear@14 20
nuclear@47 21 using namespace g3dimpl;
nuclear@47 22
nuclear@14 23 /*
nuclear@14 24 static long save_env(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 25 static long save_materials(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 26 static long save_material(const Material *mat, long offset, goat3d_io *io);
nuclear@14 27 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io);
nuclear@14 28 static long save_meshes(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 29 static long save_lights(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 30 static long save_cameras(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 31 static long save_nodes(const Scene *scn, long offset, goat3d_io *io);
nuclear@14 32
nuclear@14 33 static long write_chunk_float(int id, float val, long offs, goat3d_io *io);
nuclear@14 34 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io);
nuclear@14 35 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io);
nuclear@14 36 */
nuclear@14 37
nuclear@14 38 bool Scene::save(goat3d_io *io) const
nuclear@14 39 {
nuclear@14 40 /*
nuclear@14 41 long res;
nuclear@14 42
nuclear@14 43 ChunkHeader hdr;
nuclear@14 44 hdr.id = CNK_SCENE;
nuclear@14 45 hdr.size = sizeof hdr;
nuclear@14 46
nuclear@14 47 if((res = save_env(this, hdr.size, io)) < 0) {
nuclear@14 48 return false;
nuclear@14 49 }
nuclear@14 50 hdr.size += res;
nuclear@14 51
nuclear@14 52 if((res = save_materials(this, hdr.size, io)) < 0) {
nuclear@14 53 return false;
nuclear@14 54 }
nuclear@14 55 hdr.size += res;
nuclear@14 56
nuclear@14 57 if((res = save_meshes(this, hdr.size, io)) < 0) {
nuclear@14 58 return false;
nuclear@14 59 }
nuclear@14 60 hdr.size += res;
nuclear@14 61
nuclear@14 62 if((res = save_lights(this, hdr.size, io)) < 0) {
nuclear@14 63 return false;
nuclear@14 64 }
nuclear@14 65 hdr.size += res;
nuclear@14 66
nuclear@14 67 if((res = save_cameras(this, hdr.size, io)) < 0) {
nuclear@14 68 return false;
nuclear@14 69 }
nuclear@14 70 hdr.size += res;
nuclear@14 71
nuclear@14 72 if((res = save_nodes(this, hdr.size, io)) < 0) {
nuclear@14 73 return false;
nuclear@14 74 }
nuclear@14 75 hdr.size += res;
nuclear@14 76
nuclear@14 77 // now go back and write the root chunk
nuclear@14 78 io->seek(0, SEEK_SET, io->cls);
nuclear@14 79 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
nuclear@14 80 return false;
nuclear@14 81 }
nuclear@14 82
nuclear@14 83 return true;
nuclear@14 84 */
nuclear@14 85 return false;
nuclear@14 86 }
nuclear@14 87
nuclear@55 88 bool Scene::save_anim(goat3d_io *io) const
nuclear@51 89 {
nuclear@51 90 return false;
nuclear@51 91 }
nuclear@51 92
nuclear@14 93
nuclear@14 94 #if 0
nuclear@14 95 static long save_env(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_ENV;
nuclear@14 101 hdr.size = sizeof hdr;
nuclear@14 102
nuclear@14 103 if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
nuclear@14 104 return -1;
nuclear@14 105 }
nuclear@14 106 hdr.size += res;
nuclear@14 107
nuclear@14 108 // TODO add fog chunk
nuclear@14 109
nuclear@14 110 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 111 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
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_materials(const Scene *scn, 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_LIST;
nuclear@14 123 hdr.size = sizeof hdr;
nuclear@14 124
nuclear@14 125 for(int i=0; i<scn->get_material_count(); i++) {
nuclear@14 126 if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
nuclear@14 127 return -1;
nuclear@14 128 }
nuclear@14 129 hdr.size += res;
nuclear@14 130 }
nuclear@14 131
nuclear@14 132 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 133 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
nuclear@14 134 return -1;
nuclear@14 135 }
nuclear@14 136 return hdr.size;
nuclear@14 137 }
nuclear@14 138
nuclear@14 139 static long save_material(const Material *mat, long offset, goat3d_io *io)
nuclear@14 140 {
nuclear@14 141 long res;
nuclear@14 142
nuclear@14 143 ChunkHeader hdr;
nuclear@14 144 hdr.id = CNK_MTL;
nuclear@14 145 hdr.size = sizeof hdr;
nuclear@14 146
nuclear@14 147 for(int i=0; i<mat->get_attrib_count(); i++) {
nuclear@14 148 const char *name = mat->get_attrib_name(i);
nuclear@14 149 if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
nuclear@14 150 return -1;
nuclear@14 151 }
nuclear@14 152 hdr.size += res;
nuclear@14 153 }
nuclear@14 154
nuclear@14 155 io->seek(offset, SEEK_SET, io->cls);
nuclear@14 156 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
nuclear@14 157 return -1;
nuclear@14 158 }
nuclear@14 159 return hdr.size;
nuclear@14 160 }
nuclear@14 161
nuclear@14 162 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
nuclear@14 163 {
nuclear@14 164 long res;
nuclear@14 165
nuclear@14 166 ChunkHeader hdr;
nuclear@14 167 hdr.id = CNK_MTL_ATTR;
nuclear@14 168 hdr.size = sizeof hdr;
nuclear@14 169
nuclear@14 170 // TODO cont.
nuclear@14 171 return -1;
nuclear@14 172 }
nuclear@14 173
nuclear@14 174 static long save_meshes(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 175 {
nuclear@14 176 return 0;
nuclear@14 177 }
nuclear@14 178
nuclear@14 179 static long save_lights(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 180 {
nuclear@14 181 return 0;
nuclear@14 182 }
nuclear@14 183
nuclear@14 184 static long save_cameras(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 185 {
nuclear@14 186 return 0;
nuclear@14 187 }
nuclear@14 188
nuclear@14 189 static long save_nodes(const Scene *scn, long offset, goat3d_io *io)
nuclear@14 190 {
nuclear@14 191 return 0;
nuclear@14 192 }
nuclear@14 193
nuclear@14 194 static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
nuclear@14 195 {
nuclear@14 196 int size = sizeof(ChunkHeader) + sizeof val;
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 *(float*)c->data = val;
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_float3(int id, const Vector3 &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 *(Vector3*)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
nuclear@14 228 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
nuclear@14 229 {
nuclear@14 230 int size = sizeof(ChunkHeader) + sizeof vec;
nuclear@14 231 char *buf = (char*)alloca(size);
nuclear@14 232
nuclear@14 233 Chunk *c = (Chunk*)buf;
nuclear@14 234 c->hdr.id = id;
nuclear@14 235 c->hdr.size = size;
nuclear@14 236 *(Vector4*)c->data = vec;
nuclear@14 237
nuclear@14 238 io->seek(offs, SEEK_SET, io->cls);
nuclear@14 239 if(io->write(buf, size, io->cls) < size) {
nuclear@14 240 return -1;
nuclear@14 241 }
nuclear@14 242 return size;
nuclear@14 243 }
nuclear@14 244 #endif