goat3d

diff src/goat3d_write.cc @ 14:188c697b3b49

- added a document describing the goat3d file format chunk hierarchy - started an alternative XML-based file format - added the openctm library
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 04:47:05 +0300
parents
children 498ca7ac7047
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/goat3d_write.cc	Thu Sep 26 04:47:05 2013 +0300
     1.3 @@ -0,0 +1,220 @@
     1.4 +#include "goat3d_impl.h"
     1.5 +#include "chunk.h"
     1.6 +
     1.7 +/*
     1.8 +static long save_env(const Scene *scn, long offset, goat3d_io *io);
     1.9 +static long save_materials(const Scene *scn, long offset, goat3d_io *io);
    1.10 +static long save_material(const Material *mat, long offset, goat3d_io *io);
    1.11 +static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io);
    1.12 +static long save_meshes(const Scene *scn, long offset, goat3d_io *io);
    1.13 +static long save_lights(const Scene *scn, long offset, goat3d_io *io);
    1.14 +static long save_cameras(const Scene *scn, long offset, goat3d_io *io);
    1.15 +static long save_nodes(const Scene *scn, long offset, goat3d_io *io);
    1.16 +
    1.17 +static long write_chunk_float(int id, float val, long offs, goat3d_io *io);
    1.18 +static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io);
    1.19 +static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io);
    1.20 +*/
    1.21 +
    1.22 +bool Scene::save(goat3d_io *io) const
    1.23 +{
    1.24 +	/*
    1.25 +	long res;
    1.26 +
    1.27 +	ChunkHeader hdr;
    1.28 +	hdr.id = CNK_SCENE;
    1.29 +	hdr.size = sizeof hdr;
    1.30 +
    1.31 +	if((res = save_env(this, hdr.size, io)) < 0) {
    1.32 +		return false;
    1.33 +	}
    1.34 +	hdr.size += res;
    1.35 +
    1.36 +	if((res = save_materials(this, hdr.size, io)) < 0) {
    1.37 +		return false;
    1.38 +	}
    1.39 +	hdr.size += res;
    1.40 +
    1.41 +	if((res = save_meshes(this, hdr.size, io)) < 0) {
    1.42 +		return false;
    1.43 +	}
    1.44 +	hdr.size += res;
    1.45 +
    1.46 +	if((res = save_lights(this, hdr.size, io)) < 0) {
    1.47 +		return false;
    1.48 +	}
    1.49 +	hdr.size += res;
    1.50 +
    1.51 +	if((res = save_cameras(this, hdr.size, io)) < 0) {
    1.52 +		return false;
    1.53 +	}
    1.54 +	hdr.size += res;
    1.55 +
    1.56 +	if((res = save_nodes(this, hdr.size, io)) < 0) {
    1.57 +		return false;
    1.58 +	}
    1.59 +	hdr.size += res;
    1.60 +
    1.61 +	// now go back and write the root chunk
    1.62 +	io->seek(0, SEEK_SET, io->cls);
    1.63 +	if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
    1.64 +		return false;
    1.65 +	}
    1.66 +
    1.67 +	return true;
    1.68 +	*/
    1.69 +	return false;
    1.70 +}
    1.71 +
    1.72 +
    1.73 +#if 0
    1.74 +static long save_env(const Scene *scn, long offset, goat3d_io *io)
    1.75 +{
    1.76 +	long res;
    1.77 +
    1.78 +	ChunkHeader hdr;
    1.79 +	hdr.id = CNK_ENV;
    1.80 +	hdr.size = sizeof hdr;
    1.81 +
    1.82 +	if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
    1.83 +		return -1;
    1.84 +	}
    1.85 +	hdr.size += res;
    1.86 +
    1.87 +	// TODO add fog chunk
    1.88 +
    1.89 +	io->seek(offset, SEEK_SET, io->cls);
    1.90 +	if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
    1.91 +		return -1;
    1.92 +	}
    1.93 +	return hdr.size;
    1.94 +}
    1.95 +
    1.96 +static long save_materials(const Scene *scn, long offset, goat3d_io *io)
    1.97 +{
    1.98 +	long res;
    1.99 +
   1.100 +	ChunkHeader hdr;
   1.101 +	hdr.id = CNK_MTL_LIST;
   1.102 +	hdr.size = sizeof hdr;
   1.103 +
   1.104 +	for(int i=0; i<scn->get_material_count(); i++) {
   1.105 +		if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
   1.106 +			return -1;
   1.107 +		}
   1.108 +		hdr.size += res;
   1.109 +	}
   1.110 +
   1.111 +	io->seek(offset, SEEK_SET, io->cls);
   1.112 +	if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
   1.113 +		return -1;
   1.114 +	}
   1.115 +	return hdr.size;
   1.116 +}
   1.117 +
   1.118 +static long save_material(const Material *mat, long offset, goat3d_io *io)
   1.119 +{
   1.120 +	long res;
   1.121 +
   1.122 +	ChunkHeader hdr;
   1.123 +	hdr.id = CNK_MTL;
   1.124 +	hdr.size = sizeof hdr;
   1.125 +
   1.126 +	for(int i=0; i<mat->get_attrib_count(); i++) {
   1.127 +		const char *name = mat->get_attrib_name(i);
   1.128 +		if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
   1.129 +			return -1;
   1.130 +		}
   1.131 +		hdr.size += res;
   1.132 +	}
   1.133 +
   1.134 +	io->seek(offset, SEEK_SET, io->cls);
   1.135 +	if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
   1.136 +		return -1;
   1.137 +	}
   1.138 +	return hdr.size;
   1.139 +}
   1.140 +
   1.141 +static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
   1.142 +{
   1.143 +	long res;
   1.144 +
   1.145 +	ChunkHeader hdr;
   1.146 +	hdr.id = CNK_MTL_ATTR;
   1.147 +	hdr.size = sizeof hdr;
   1.148 +
   1.149 +	// TODO cont.
   1.150 +	return -1;
   1.151 +}
   1.152 +
   1.153 +static long save_meshes(const Scene *scn, long offset, goat3d_io *io)
   1.154 +{
   1.155 +	return 0;
   1.156 +}
   1.157 +
   1.158 +static long save_lights(const Scene *scn, long offset, goat3d_io *io)
   1.159 +{
   1.160 +	return 0;
   1.161 +}
   1.162 +
   1.163 +static long save_cameras(const Scene *scn, long offset, goat3d_io *io)
   1.164 +{
   1.165 +	return 0;
   1.166 +}
   1.167 +
   1.168 +static long save_nodes(const Scene *scn, long offset, goat3d_io *io)
   1.169 +{
   1.170 +	return 0;
   1.171 +}
   1.172 +
   1.173 +static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
   1.174 +{
   1.175 +	int size = sizeof(ChunkHeader) + sizeof val;
   1.176 +	char *buf = (char*)alloca(size);
   1.177 +
   1.178 +	Chunk *c = (Chunk*)buf;
   1.179 +	c->hdr.id = id;
   1.180 +	c->hdr.size = size;
   1.181 +	*(float*)c->data = val;
   1.182 +
   1.183 +	io->seek(offs, SEEK_SET, io->cls);
   1.184 +	if(io->write(buf, size, io->cls) < size) {
   1.185 +		return -1;
   1.186 +	}
   1.187 +	return size;
   1.188 +}
   1.189 +
   1.190 +static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io)
   1.191 +{
   1.192 +	int size = sizeof(ChunkHeader) + sizeof vec;
   1.193 +	char *buf = (char*)alloca(size);
   1.194 +
   1.195 +	Chunk *c = (Chunk*)buf;
   1.196 +	c->hdr.id = id;
   1.197 +	c->hdr.size = size;
   1.198 +	*(Vector3*)c->data = vec;
   1.199 +
   1.200 +	io->seek(offs, SEEK_SET, io->cls);
   1.201 +	if(io->write(buf, size, io->cls) < size) {
   1.202 +		return -1;
   1.203 +	}
   1.204 +	return size;
   1.205 +}
   1.206 +
   1.207 +static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
   1.208 +{
   1.209 +	int size = sizeof(ChunkHeader) + sizeof vec;
   1.210 +	char *buf = (char*)alloca(size);
   1.211 +
   1.212 +	Chunk *c = (Chunk*)buf;
   1.213 +	c->hdr.id = id;
   1.214 +	c->hdr.size = size;
   1.215 +	*(Vector4*)c->data = vec;
   1.216 +
   1.217 +	io->seek(offs, SEEK_SET, io->cls);
   1.218 +	if(io->write(buf, size, io->cls) < size) {
   1.219 +		return -1;
   1.220 +	}
   1.221 +	return size;
   1.222 +}
   1.223 +#endif