goat3d

changeset 9:04bb114fcf05

implementing Scene::save, lots to do still
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 23 Aug 2013 06:36:47 +0300
parents cd71f0b92f44
children 1f94a2107c64
files src/chunk.h src/goat3d.cc src/goat3d.h src/goat3d_impl.h src/material.cc src/material.h
diffstat 6 files changed, 312 insertions(+), 15 deletions(-) [+]
line diff
     1.1 --- a/src/chunk.h	Wed Aug 21 05:52:28 2013 +0300
     1.2 +++ b/src/chunk.h	Fri Aug 23 06:36:47 2013 +0300
     1.3 @@ -1,6 +1,8 @@
     1.4  #ifndef CHUNK_H_
     1.5  #define CHUNK_H_
     1.6  
     1.7 +#include <stdint.h>
     1.8 +
     1.9  enum {
    1.10  	CNK_INVALID,		// this shouldn't appear in files
    1.11  	CNK_SCENE,			// the root chunk
    1.12 @@ -100,6 +102,8 @@
    1.13  	MAX_NUM_CHUNKS
    1.14  };
    1.15  
    1.16 +#define UNKNOWN_SIZE	((uint32_t)0xbaadf00d)
    1.17 +
    1.18  struct ChunkHeader {
    1.19  	uint32_t id;
    1.20  	uint32_t size;
    1.21 @@ -110,4 +114,6 @@
    1.22  	char data[1];
    1.23  };
    1.24  
    1.25 +
    1.26 +
    1.27  #endif	// CHUNK_H_
     2.1 --- a/src/goat3d.cc	Wed Aug 21 05:52:28 2013 +0300
     2.2 +++ b/src/goat3d.cc	Fri Aug 23 06:36:47 2013 +0300
     2.3 @@ -1,5 +1,6 @@
     2.4  #include "goat3d.h"
     2.5  #include "goat3d_impl.h"
     2.6 +#include "chunk.h"
     2.7  
     2.8  Scene::Scene()
     2.9  	: name("unnamed"), ambient(0.05, 0.05, 0.05)
    2.10 @@ -81,6 +82,12 @@
    2.11  	return 0;
    2.12  }
    2.13  
    2.14 +int Scene::get_material_count() const
    2.15 +{
    2.16 +	return (int)materials.size();
    2.17 +}
    2.18 +
    2.19 +
    2.20  void Scene::add_mesh(Mesh *mesh)
    2.21  {
    2.22  	meshes.push_back(mesh);
    2.23 @@ -101,6 +108,12 @@
    2.24  	return 0;
    2.25  }
    2.26  
    2.27 +int Scene::get_mesh_count() const
    2.28 +{
    2.29 +	return (int)meshes.size();
    2.30 +}
    2.31 +
    2.32 +
    2.33  void Scene::add_light(Light *light)
    2.34  {
    2.35  	lights.push_back(light);
    2.36 @@ -121,6 +134,12 @@
    2.37  	return 0;
    2.38  }
    2.39  
    2.40 +int Scene::get_light_count() const
    2.41 +{
    2.42 +	return (int)lights.size();
    2.43 +}
    2.44 +
    2.45 +
    2.46  void Scene::add_camera(Camera *cam)
    2.47  {
    2.48  	cameras.push_back(cam);
    2.49 @@ -141,6 +160,12 @@
    2.50  	return 0;
    2.51  }
    2.52  
    2.53 +int Scene::get_camera_count() const
    2.54 +{
    2.55 +	return (int)cameras.size();
    2.56 +}
    2.57 +
    2.58 +
    2.59  void Scene::add_node(Node *node)
    2.60  {
    2.61  	nodes.push_back(node);
    2.62 @@ -161,12 +186,224 @@
    2.63  	return 0;
    2.64  }
    2.65  
    2.66 +int Scene::get_node_count() const
    2.67 +{
    2.68 +	return (int)nodes.size();
    2.69 +}
    2.70 +
    2.71 +
    2.72  bool Scene::load(goat3d_io *io)
    2.73  {
    2.74  	return false;
    2.75  }
    2.76  
    2.77 +static long save_env(const Scene *scn, long offset, goat3d_io *io);
    2.78 +static long save_materials(const Scene *scn, long offset, goat3d_io *io);
    2.79 +static long save_material(const Material *mat, long offset, goat3d_io *io);
    2.80 +static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io);
    2.81 +static long save_meshes(const Scene *scn, long offset, goat3d_io *io);
    2.82 +static long save_lights(const Scene *scn, long offset, goat3d_io *io);
    2.83 +static long save_cameras(const Scene *scn, long offset, goat3d_io *io);
    2.84 +static long save_nodes(const Scene *scn, long offset, goat3d_io *io);
    2.85 +
    2.86 +static long write_chunk_float(int id, float val, long offs, goat3d_io *io);
    2.87 +static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io);
    2.88 +static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io);
    2.89 +
    2.90  bool Scene::save(goat3d_io *io) const
    2.91  {
    2.92 -	return false;
    2.93 +	long res;
    2.94 +
    2.95 +	ChunkHeader hdr;
    2.96 +	hdr.id = CNK_SCENE;
    2.97 +	hdr.size = sizeof hdr;
    2.98 +
    2.99 +	if((res = save_env(this, hdr.size, io)) < 0) {
   2.100 +		return false;
   2.101 +	}
   2.102 +	hdr.size += res;
   2.103 +
   2.104 +	if((res = save_materials(this, hdr.size, io)) < 0) {
   2.105 +		return false;
   2.106 +	}
   2.107 +	hdr.size += res;
   2.108 +
   2.109 +	if((res = save_meshes(this, hdr.size, io)) < 0) {
   2.110 +		return false;
   2.111 +	}
   2.112 +	hdr.size += res;
   2.113 +
   2.114 +	if((res = save_lights(this, hdr.size, io)) < 0) {
   2.115 +		return false;
   2.116 +	}
   2.117 +	hdr.size += res;
   2.118 +
   2.119 +	if((res = save_cameras(this, hdr.size, io)) < 0) {
   2.120 +		return false;
   2.121 +	}
   2.122 +	hdr.size += res;
   2.123 +
   2.124 +	if((res = save_nodes(this, hdr.size, io)) < 0) {
   2.125 +		return false;
   2.126 +	}
   2.127 +	hdr.size += res;
   2.128 +
   2.129 +	// now go back and write the root chunk
   2.130 +	io->seek(0, SEEK_SET, io->cls);
   2.131 +	if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
   2.132 +		return false;
   2.133 +	}
   2.134 +
   2.135 +	return true;
   2.136  }
   2.137 +
   2.138 +
   2.139 +static long save_env(const Scene *scn, long offset, goat3d_io *io)
   2.140 +{
   2.141 +	long res;
   2.142 +
   2.143 +	ChunkHeader hdr;
   2.144 +	hdr.id = CNK_ENV;
   2.145 +	hdr.size = sizeof hdr;
   2.146 +
   2.147 +	if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
   2.148 +		return -1;
   2.149 +	}
   2.150 +	hdr.size += res;
   2.151 +
   2.152 +	// TODO add fog chunk
   2.153 +
   2.154 +	io->seek(offset, SEEK_SET, io->cls);
   2.155 +	if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
   2.156 +		return -1;
   2.157 +	}
   2.158 +	return hdr.size;
   2.159 +}
   2.160 +
   2.161 +static long save_materials(const Scene *scn, long offset, goat3d_io *io)
   2.162 +{
   2.163 +	long res;
   2.164 +
   2.165 +	ChunkHeader hdr;
   2.166 +	hdr.id = CNK_MTL_LIST;
   2.167 +	hdr.size = sizeof hdr;
   2.168 +
   2.169 +	for(int i=0; i<scn->get_material_count(); i++) {
   2.170 +		if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
   2.171 +			return -1;
   2.172 +		}
   2.173 +		hdr.size += res;
   2.174 +	}
   2.175 +
   2.176 +	io->seek(offset, SEEK_SET, io->cls);
   2.177 +	if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
   2.178 +		return -1;
   2.179 +	}
   2.180 +	return hdr.size;
   2.181 +}
   2.182 +
   2.183 +static long save_material(const Material *mat, long offset, goat3d_io *io)
   2.184 +{
   2.185 +	long res;
   2.186 +
   2.187 +	ChunkHeader hdr;
   2.188 +	hdr.id = CNK_MTL;
   2.189 +	hdr.size = sizeof hdr;
   2.190 +
   2.191 +	for(int i=0; i<mat->get_attrib_count(); i++) {
   2.192 +		const char *name = mat->get_attrib_name(i);
   2.193 +		if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
   2.194 +			return -1;
   2.195 +		}
   2.196 +		hdr.size += res;
   2.197 +	}
   2.198 +
   2.199 +	io->seek(offset, SEEK_SET, io->cls);
   2.200 +	if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
   2.201 +		return -1;
   2.202 +	}
   2.203 +	return hdr.size;
   2.204 +}
   2.205 +
   2.206 +static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
   2.207 +{
   2.208 +	long res;
   2.209 +
   2.210 +	ChunkHeader hdr;
   2.211 +	hdr.id = CNK_MTL_ATTR;
   2.212 +	hdr.size = sizeof hdr;
   2.213 +
   2.214 +	// TODO cont.
   2.215 +	return -1;
   2.216 +}
   2.217 +
   2.218 +static long save_meshes(const Scene *scn, long offset, goat3d_io *io)
   2.219 +{
   2.220 +	return 0;
   2.221 +}
   2.222 +
   2.223 +static long save_lights(const Scene *scn, long offset, goat3d_io *io)
   2.224 +{
   2.225 +	return 0;
   2.226 +}
   2.227 +
   2.228 +static long save_cameras(const Scene *scn, long offset, goat3d_io *io)
   2.229 +{
   2.230 +	return 0;
   2.231 +}
   2.232 +
   2.233 +static long save_nodes(const Scene *scn, long offset, goat3d_io *io)
   2.234 +{
   2.235 +	return 0;
   2.236 +}
   2.237 +
   2.238 +static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
   2.239 +{
   2.240 +	int size = sizeof(ChunkHeader) + sizeof val;
   2.241 +	char *buf = (char*)alloca(size);
   2.242 +
   2.243 +	Chunk *c = (Chunk*)buf;
   2.244 +	c->hdr.id = id;
   2.245 +	c->hdr.size = size;
   2.246 +	*(float*)c->data = val;
   2.247 +
   2.248 +	io->seek(offs, SEEK_SET, io->cls);
   2.249 +	if(io->write(buf, size, io->cls) < size) {
   2.250 +		return -1;
   2.251 +	}
   2.252 +	return size;
   2.253 +}
   2.254 +
   2.255 +static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io)
   2.256 +{
   2.257 +	int size = sizeof(ChunkHeader) + sizeof vec;
   2.258 +	char *buf = (char*)alloca(size);
   2.259 +
   2.260 +	Chunk *c = (Chunk*)buf;
   2.261 +	c->hdr.id = id;
   2.262 +	c->hdr.size = size;
   2.263 +	*(Vector3*)c->data = vec;
   2.264 +
   2.265 +	io->seek(offs, SEEK_SET, io->cls);
   2.266 +	if(io->write(buf, size, io->cls) < size) {
   2.267 +		return -1;
   2.268 +	}
   2.269 +	return size;
   2.270 +}
   2.271 +
   2.272 +static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
   2.273 +{
   2.274 +	int size = sizeof(ChunkHeader) + sizeof vec;
   2.275 +	char *buf = (char*)alloca(size);
   2.276 +
   2.277 +	Chunk *c = (Chunk*)buf;
   2.278 +	c->hdr.id = id;
   2.279 +	c->hdr.size = size;
   2.280 +	*(Vector4*)c->data = vec;
   2.281 +
   2.282 +	io->seek(offs, SEEK_SET, io->cls);
   2.283 +	if(io->write(buf, size, io->cls) < size) {
   2.284 +		return -1;
   2.285 +	}
   2.286 +	return size;
   2.287 +}
     3.1 --- a/src/goat3d.h	Wed Aug 21 05:52:28 2013 +0300
     3.2 +++ b/src/goat3d.h	Fri Aug 23 06:36:47 2013 +0300
     3.3 @@ -9,8 +9,8 @@
     3.4  struct goat3d_io {
     3.5  	void *cls;	/* closure data */
     3.6  
     3.7 -	size_t (*read)(void *buf, size_t bytes, void *uptr);
     3.8 -	size_t (*write)(void *buf, size_t bytes, void *uptr);
     3.9 +	ssize_t (*read)(void *buf, size_t bytes, void *uptr);
    3.10 +	ssize_t (*write)(void *buf, size_t bytes, void *uptr);
    3.11  	long (*seek)(long offs, int whence, void *uptr);
    3.12  };
    3.13  
     4.1 --- a/src/goat3d_impl.h	Wed Aug 21 05:52:28 2013 +0300
     4.2 +++ b/src/goat3d_impl.h	Fri Aug 23 06:36:47 2013 +0300
     4.3 @@ -36,22 +36,27 @@
     4.4  	void add_material(Material *mat);
     4.5  	Material *get_material(int idx) const;
     4.6  	Material *get_material(const char *name) const;
     4.7 +	int get_material_count() const;
     4.8  
     4.9  	void add_mesh(Mesh *mesh);
    4.10  	Mesh *get_mesh(int idx) const;
    4.11  	Mesh *get_mesh(const char *name) const;
    4.12 +	int get_mesh_count() const;
    4.13  
    4.14  	void add_light(Light *light);
    4.15  	Light *get_light(int idx) const;
    4.16  	Light *get_light(const char *name) const;
    4.17 +	int get_light_count() const;
    4.18  
    4.19  	void add_camera(Camera *cam);
    4.20  	Camera *get_camera(int idx) const;
    4.21  	Camera *get_camera(const char *name) const;
    4.22 +	int get_camera_count() const;
    4.23  
    4.24  	void add_node(Node *node);
    4.25  	Node *get_node(int idx) const;
    4.26  	Node *get_node(const char *name) const;
    4.27 +	int get_node_count() const;
    4.28  
    4.29  	bool load(goat3d_io *io);
    4.30  	bool save(goat3d_io *io) const;
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/material.cc	Fri Aug 23 06:36:47 2013 +0300
     5.3 @@ -0,0 +1,54 @@
     5.4 +#include "material.h"
     5.5 +
     5.6 +int Material::get_attrib_count() const
     5.7 +{
     5.8 +	return (int)attrib.size();
     5.9 +}
    5.10 +
    5.11 +const char *Material::get_attrib_name(int idx) const
    5.12 +{
    5.13 +	if(idx < 0 || idx >= get_attrib_count()) {
    5.14 +		return 0;
    5.15 +	}
    5.16 +
    5.17 +	std::map<std::string, MaterialAttrib>::const_iterator it = attrib.begin();
    5.18 +	for(int i=0; i<idx; i++) it++;
    5.19 +	return it->first.c_str();
    5.20 +}
    5.21 +
    5.22 +MaterialAttrib &Material::operator [](int idx)
    5.23 +{
    5.24 +	if(idx < 0 || idx >= get_attrib_count()) {
    5.25 +		return def_attr;
    5.26 +	}
    5.27 +
    5.28 +	std::map<std::string, MaterialAttrib>::iterator it = attrib.begin();
    5.29 +	for(int i=0; i<idx; i++) it++;
    5.30 +	return it->second;
    5.31 +}
    5.32 +
    5.33 +const MaterialAttrib &Material::operator [](int idx) const
    5.34 +{
    5.35 +	if(idx < 0 || idx >= get_attrib_count()) {
    5.36 +		return def_attr;
    5.37 +	}
    5.38 +
    5.39 +	std::map<std::string, MaterialAttrib>::const_iterator it = attrib.begin();
    5.40 +	for(int i=0; i<idx; i++) it++;
    5.41 +	return it->second;
    5.42 +}
    5.43 +
    5.44 +MaterialAttrib &Material::operator [](const std::string &name)
    5.45 +{
    5.46 +	return attrib[name];
    5.47 +}
    5.48 +
    5.49 +const MaterialAttrib &Material::operator [](const std::string &name) const
    5.50 +{
    5.51 +	std::map<std::string, MaterialAttrib>::const_iterator it;
    5.52 +	if((it = attrib.find(name)) != attrib.end()) {
    5.53 +		return it->second;
    5.54 +	}
    5.55 +	return def_attr;
    5.56 +}
    5.57 +
     6.1 --- a/src/material.h	Wed Aug 21 05:52:28 2013 +0300
     6.2 +++ b/src/material.h	Fri Aug 23 06:36:47 2013 +0300
     6.3 @@ -28,19 +28,14 @@
     6.4  public:
     6.5  	std::string name;
     6.6  
     6.7 -	MaterialAttrib &operator [](const std::string &name)
     6.8 -	{
     6.9 -		return attrib[name];
    6.10 -	}
    6.11 +	int get_attrib_count() const;
    6.12 +	const char *get_attrib_name(int idx) const;
    6.13  
    6.14 -	const MaterialAttrib &operator [](const std::string &name) const
    6.15 -	{
    6.16 -		std::map<std::string, MaterialAttrib>::const_iterator it;
    6.17 -		if((it = attrib.find(name)) != attrib.end()) {
    6.18 -			return it->second;
    6.19 -		}
    6.20 -		return def_attr;
    6.21 -	}
    6.22 +	MaterialAttrib &operator [](int idx);
    6.23 +	const MaterialAttrib &operator [](int idx) const;
    6.24 +
    6.25 +	MaterialAttrib &operator [](const std::string &name);
    6.26 +	const MaterialAttrib &operator [](const std::string &name) const;
    6.27  };
    6.28  
    6.29  #endif	// MATERIAL_H_