goat3d
diff src/goat3d.cc @ 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 |
line diff
1.1 --- a/src/goat3d.cc Wed Aug 21 05:52:28 2013 +0300 1.2 +++ b/src/goat3d.cc Fri Aug 23 06:36:47 2013 +0300 1.3 @@ -1,5 +1,6 @@ 1.4 #include "goat3d.h" 1.5 #include "goat3d_impl.h" 1.6 +#include "chunk.h" 1.7 1.8 Scene::Scene() 1.9 : name("unnamed"), ambient(0.05, 0.05, 0.05) 1.10 @@ -81,6 +82,12 @@ 1.11 return 0; 1.12 } 1.13 1.14 +int Scene::get_material_count() const 1.15 +{ 1.16 + return (int)materials.size(); 1.17 +} 1.18 + 1.19 + 1.20 void Scene::add_mesh(Mesh *mesh) 1.21 { 1.22 meshes.push_back(mesh); 1.23 @@ -101,6 +108,12 @@ 1.24 return 0; 1.25 } 1.26 1.27 +int Scene::get_mesh_count() const 1.28 +{ 1.29 + return (int)meshes.size(); 1.30 +} 1.31 + 1.32 + 1.33 void Scene::add_light(Light *light) 1.34 { 1.35 lights.push_back(light); 1.36 @@ -121,6 +134,12 @@ 1.37 return 0; 1.38 } 1.39 1.40 +int Scene::get_light_count() const 1.41 +{ 1.42 + return (int)lights.size(); 1.43 +} 1.44 + 1.45 + 1.46 void Scene::add_camera(Camera *cam) 1.47 { 1.48 cameras.push_back(cam); 1.49 @@ -141,6 +160,12 @@ 1.50 return 0; 1.51 } 1.52 1.53 +int Scene::get_camera_count() const 1.54 +{ 1.55 + return (int)cameras.size(); 1.56 +} 1.57 + 1.58 + 1.59 void Scene::add_node(Node *node) 1.60 { 1.61 nodes.push_back(node); 1.62 @@ -161,12 +186,224 @@ 1.63 return 0; 1.64 } 1.65 1.66 +int Scene::get_node_count() const 1.67 +{ 1.68 + return (int)nodes.size(); 1.69 +} 1.70 + 1.71 + 1.72 bool Scene::load(goat3d_io *io) 1.73 { 1.74 return false; 1.75 } 1.76 1.77 +static long save_env(const Scene *scn, long offset, goat3d_io *io); 1.78 +static long save_materials(const Scene *scn, long offset, goat3d_io *io); 1.79 +static long save_material(const Material *mat, long offset, goat3d_io *io); 1.80 +static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io); 1.81 +static long save_meshes(const Scene *scn, long offset, goat3d_io *io); 1.82 +static long save_lights(const Scene *scn, long offset, goat3d_io *io); 1.83 +static long save_cameras(const Scene *scn, long offset, goat3d_io *io); 1.84 +static long save_nodes(const Scene *scn, long offset, goat3d_io *io); 1.85 + 1.86 +static long write_chunk_float(int id, float val, long offs, goat3d_io *io); 1.87 +static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io); 1.88 +static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io); 1.89 + 1.90 bool Scene::save(goat3d_io *io) const 1.91 { 1.92 - return false; 1.93 + long res; 1.94 + 1.95 + ChunkHeader hdr; 1.96 + hdr.id = CNK_SCENE; 1.97 + hdr.size = sizeof hdr; 1.98 + 1.99 + if((res = save_env(this, hdr.size, io)) < 0) { 1.100 + return false; 1.101 + } 1.102 + hdr.size += res; 1.103 + 1.104 + if((res = save_materials(this, hdr.size, io)) < 0) { 1.105 + return false; 1.106 + } 1.107 + hdr.size += res; 1.108 + 1.109 + if((res = save_meshes(this, hdr.size, io)) < 0) { 1.110 + return false; 1.111 + } 1.112 + hdr.size += res; 1.113 + 1.114 + if((res = save_lights(this, hdr.size, io)) < 0) { 1.115 + return false; 1.116 + } 1.117 + hdr.size += res; 1.118 + 1.119 + if((res = save_cameras(this, hdr.size, io)) < 0) { 1.120 + return false; 1.121 + } 1.122 + hdr.size += res; 1.123 + 1.124 + if((res = save_nodes(this, hdr.size, io)) < 0) { 1.125 + return false; 1.126 + } 1.127 + hdr.size += res; 1.128 + 1.129 + // now go back and write the root chunk 1.130 + io->seek(0, SEEK_SET, io->cls); 1.131 + if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) { 1.132 + return false; 1.133 + } 1.134 + 1.135 + return true; 1.136 } 1.137 + 1.138 + 1.139 +static long save_env(const Scene *scn, long offset, goat3d_io *io) 1.140 +{ 1.141 + long res; 1.142 + 1.143 + ChunkHeader hdr; 1.144 + hdr.id = CNK_ENV; 1.145 + hdr.size = sizeof hdr; 1.146 + 1.147 + if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) { 1.148 + return -1; 1.149 + } 1.150 + hdr.size += res; 1.151 + 1.152 + // TODO add fog chunk 1.153 + 1.154 + io->seek(offset, SEEK_SET, io->cls); 1.155 + if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) { 1.156 + return -1; 1.157 + } 1.158 + return hdr.size; 1.159 +} 1.160 + 1.161 +static long save_materials(const Scene *scn, long offset, goat3d_io *io) 1.162 +{ 1.163 + long res; 1.164 + 1.165 + ChunkHeader hdr; 1.166 + hdr.id = CNK_MTL_LIST; 1.167 + hdr.size = sizeof hdr; 1.168 + 1.169 + for(int i=0; i<scn->get_material_count(); i++) { 1.170 + if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) { 1.171 + return -1; 1.172 + } 1.173 + hdr.size += res; 1.174 + } 1.175 + 1.176 + io->seek(offset, SEEK_SET, io->cls); 1.177 + if(io->write(&hdr, hdr.size, io->cls) < hdr.size) { 1.178 + return -1; 1.179 + } 1.180 + return hdr.size; 1.181 +} 1.182 + 1.183 +static long save_material(const Material *mat, long offset, goat3d_io *io) 1.184 +{ 1.185 + long res; 1.186 + 1.187 + ChunkHeader hdr; 1.188 + hdr.id = CNK_MTL; 1.189 + hdr.size = sizeof hdr; 1.190 + 1.191 + for(int i=0; i<mat->get_attrib_count(); i++) { 1.192 + const char *name = mat->get_attrib_name(i); 1.193 + if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) { 1.194 + return -1; 1.195 + } 1.196 + hdr.size += res; 1.197 + } 1.198 + 1.199 + io->seek(offset, SEEK_SET, io->cls); 1.200 + if(io->write(&hdr, hdr.size, io->cls) < hdr.size) { 1.201 + return -1; 1.202 + } 1.203 + return hdr.size; 1.204 +} 1.205 + 1.206 +static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io) 1.207 +{ 1.208 + long res; 1.209 + 1.210 + ChunkHeader hdr; 1.211 + hdr.id = CNK_MTL_ATTR; 1.212 + hdr.size = sizeof hdr; 1.213 + 1.214 + // TODO cont. 1.215 + return -1; 1.216 +} 1.217 + 1.218 +static long save_meshes(const Scene *scn, long offset, goat3d_io *io) 1.219 +{ 1.220 + return 0; 1.221 +} 1.222 + 1.223 +static long save_lights(const Scene *scn, long offset, goat3d_io *io) 1.224 +{ 1.225 + return 0; 1.226 +} 1.227 + 1.228 +static long save_cameras(const Scene *scn, long offset, goat3d_io *io) 1.229 +{ 1.230 + return 0; 1.231 +} 1.232 + 1.233 +static long save_nodes(const Scene *scn, long offset, goat3d_io *io) 1.234 +{ 1.235 + return 0; 1.236 +} 1.237 + 1.238 +static long write_chunk_float(int id, float val, long offs, goat3d_io *io) 1.239 +{ 1.240 + int size = sizeof(ChunkHeader) + sizeof val; 1.241 + char *buf = (char*)alloca(size); 1.242 + 1.243 + Chunk *c = (Chunk*)buf; 1.244 + c->hdr.id = id; 1.245 + c->hdr.size = size; 1.246 + *(float*)c->data = val; 1.247 + 1.248 + io->seek(offs, SEEK_SET, io->cls); 1.249 + if(io->write(buf, size, io->cls) < size) { 1.250 + return -1; 1.251 + } 1.252 + return size; 1.253 +} 1.254 + 1.255 +static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io) 1.256 +{ 1.257 + int size = sizeof(ChunkHeader) + sizeof vec; 1.258 + char *buf = (char*)alloca(size); 1.259 + 1.260 + Chunk *c = (Chunk*)buf; 1.261 + c->hdr.id = id; 1.262 + c->hdr.size = size; 1.263 + *(Vector3*)c->data = vec; 1.264 + 1.265 + io->seek(offs, SEEK_SET, io->cls); 1.266 + if(io->write(buf, size, io->cls) < size) { 1.267 + return -1; 1.268 + } 1.269 + return size; 1.270 +} 1.271 + 1.272 +static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io) 1.273 +{ 1.274 + int size = sizeof(ChunkHeader) + sizeof vec; 1.275 + char *buf = (char*)alloca(size); 1.276 + 1.277 + Chunk *c = (Chunk*)buf; 1.278 + c->hdr.id = id; 1.279 + c->hdr.size = size; 1.280 + *(Vector4*)c->data = vec; 1.281 + 1.282 + io->seek(offs, SEEK_SET, io->cls); 1.283 + if(io->write(buf, size, io->cls) < size) { 1.284 + return -1; 1.285 + } 1.286 + return size; 1.287 +}