goat3d
view src/goat3d_write.cc @ 47:498ca7ac7047
- placed all the implementation stuff in the g3dimpl namespace
- added animation stuff to the public API
- started writing animation saving/loading
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 28 Dec 2013 06:47:39 +0200 |
parents | 188c697b3b49 |
children | 0be413ac2e0a fa5c52ea9d59 |
line source
1 #include "goat3d_impl.h"
2 #include "chunk.h"
4 using namespace g3dimpl;
6 /*
7 static long save_env(const Scene *scn, long offset, goat3d_io *io);
8 static long save_materials(const Scene *scn, long offset, goat3d_io *io);
9 static long save_material(const Material *mat, long offset, goat3d_io *io);
10 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io);
11 static long save_meshes(const Scene *scn, long offset, goat3d_io *io);
12 static long save_lights(const Scene *scn, long offset, goat3d_io *io);
13 static long save_cameras(const Scene *scn, long offset, goat3d_io *io);
14 static long save_nodes(const Scene *scn, long offset, goat3d_io *io);
16 static long write_chunk_float(int id, float val, long offs, goat3d_io *io);
17 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io);
18 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io);
19 */
21 bool Scene::save(goat3d_io *io) const
22 {
23 /*
24 long res;
26 ChunkHeader hdr;
27 hdr.id = CNK_SCENE;
28 hdr.size = sizeof hdr;
30 if((res = save_env(this, hdr.size, io)) < 0) {
31 return false;
32 }
33 hdr.size += res;
35 if((res = save_materials(this, hdr.size, io)) < 0) {
36 return false;
37 }
38 hdr.size += res;
40 if((res = save_meshes(this, hdr.size, io)) < 0) {
41 return false;
42 }
43 hdr.size += res;
45 if((res = save_lights(this, hdr.size, io)) < 0) {
46 return false;
47 }
48 hdr.size += res;
50 if((res = save_cameras(this, hdr.size, io)) < 0) {
51 return false;
52 }
53 hdr.size += res;
55 if((res = save_nodes(this, hdr.size, io)) < 0) {
56 return false;
57 }
58 hdr.size += res;
60 // now go back and write the root chunk
61 io->seek(0, SEEK_SET, io->cls);
62 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
63 return false;
64 }
66 return true;
67 */
68 return false;
69 }
72 #if 0
73 static long save_env(const Scene *scn, long offset, goat3d_io *io)
74 {
75 long res;
77 ChunkHeader hdr;
78 hdr.id = CNK_ENV;
79 hdr.size = sizeof hdr;
81 if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
82 return -1;
83 }
84 hdr.size += res;
86 // TODO add fog chunk
88 io->seek(offset, SEEK_SET, io->cls);
89 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
90 return -1;
91 }
92 return hdr.size;
93 }
95 static long save_materials(const Scene *scn, long offset, goat3d_io *io)
96 {
97 long res;
99 ChunkHeader hdr;
100 hdr.id = CNK_MTL_LIST;
101 hdr.size = sizeof hdr;
103 for(int i=0; i<scn->get_material_count(); i++) {
104 if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
105 return -1;
106 }
107 hdr.size += res;
108 }
110 io->seek(offset, SEEK_SET, io->cls);
111 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
112 return -1;
113 }
114 return hdr.size;
115 }
117 static long save_material(const Material *mat, long offset, goat3d_io *io)
118 {
119 long res;
121 ChunkHeader hdr;
122 hdr.id = CNK_MTL;
123 hdr.size = sizeof hdr;
125 for(int i=0; i<mat->get_attrib_count(); i++) {
126 const char *name = mat->get_attrib_name(i);
127 if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
128 return -1;
129 }
130 hdr.size += res;
131 }
133 io->seek(offset, SEEK_SET, io->cls);
134 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
135 return -1;
136 }
137 return hdr.size;
138 }
140 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
141 {
142 long res;
144 ChunkHeader hdr;
145 hdr.id = CNK_MTL_ATTR;
146 hdr.size = sizeof hdr;
148 // TODO cont.
149 return -1;
150 }
152 static long save_meshes(const Scene *scn, long offset, goat3d_io *io)
153 {
154 return 0;
155 }
157 static long save_lights(const Scene *scn, long offset, goat3d_io *io)
158 {
159 return 0;
160 }
162 static long save_cameras(const Scene *scn, long offset, goat3d_io *io)
163 {
164 return 0;
165 }
167 static long save_nodes(const Scene *scn, long offset, goat3d_io *io)
168 {
169 return 0;
170 }
172 static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
173 {
174 int size = sizeof(ChunkHeader) + sizeof val;
175 char *buf = (char*)alloca(size);
177 Chunk *c = (Chunk*)buf;
178 c->hdr.id = id;
179 c->hdr.size = size;
180 *(float*)c->data = val;
182 io->seek(offs, SEEK_SET, io->cls);
183 if(io->write(buf, size, io->cls) < size) {
184 return -1;
185 }
186 return size;
187 }
189 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io)
190 {
191 int size = sizeof(ChunkHeader) + sizeof vec;
192 char *buf = (char*)alloca(size);
194 Chunk *c = (Chunk*)buf;
195 c->hdr.id = id;
196 c->hdr.size = size;
197 *(Vector3*)c->data = vec;
199 io->seek(offs, SEEK_SET, io->cls);
200 if(io->write(buf, size, io->cls) < size) {
201 return -1;
202 }
203 return size;
204 }
206 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
207 {
208 int size = sizeof(ChunkHeader) + sizeof vec;
209 char *buf = (char*)alloca(size);
211 Chunk *c = (Chunk*)buf;
212 c->hdr.id = id;
213 c->hdr.size = size;
214 *(Vector4*)c->data = vec;
216 io->seek(offs, SEEK_SET, io->cls);
217 if(io->write(buf, size, io->cls) < size) {
218 return -1;
219 }
220 return size;
221 }
222 #endif