goat3d
view src/goat3d_write.cc @ 90:8b156bc5205b
[maxgoat] fixed the transform export bug
[goatview] added widgets for the animation controls
[goatview] added a grid ground plane with automatic sizing and transitions from size to size
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sat, 17 May 2014 06:26:24 +0300 |
parents | dad392c710df af1310ed212b |
children |
line source
1 /*
2 goat3d - 3D scene, character, and animation file format library.
3 Copyright (C) 2013-2014 John Tsiombikas <nuclear@member.fsf.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include "goat3d_impl.h"
19 #include "chunk.h"
21 using namespace g3dimpl;
23 /*
24 static long save_env(const Scene *scn, long offset, goat3d_io *io);
25 static long save_materials(const Scene *scn, long offset, goat3d_io *io);
26 static long save_material(const Material *mat, long offset, goat3d_io *io);
27 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io);
28 static long save_meshes(const Scene *scn, long offset, goat3d_io *io);
29 static long save_lights(const Scene *scn, long offset, goat3d_io *io);
30 static long save_cameras(const Scene *scn, long offset, goat3d_io *io);
31 static long save_nodes(const Scene *scn, long offset, goat3d_io *io);
33 static long write_chunk_float(int id, float val, long offs, goat3d_io *io);
34 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io);
35 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io);
36 */
38 bool Scene::save(goat3d_io *io) const
39 {
40 /*
41 long res;
43 ChunkHeader hdr;
44 hdr.id = CNK_SCENE;
45 hdr.size = sizeof hdr;
47 if((res = save_env(this, hdr.size, io)) < 0) {
48 return false;
49 }
50 hdr.size += res;
52 if((res = save_materials(this, hdr.size, io)) < 0) {
53 return false;
54 }
55 hdr.size += res;
57 if((res = save_meshes(this, hdr.size, io)) < 0) {
58 return false;
59 }
60 hdr.size += res;
62 if((res = save_lights(this, hdr.size, io)) < 0) {
63 return false;
64 }
65 hdr.size += res;
67 if((res = save_cameras(this, hdr.size, io)) < 0) {
68 return false;
69 }
70 hdr.size += res;
72 if((res = save_nodes(this, hdr.size, io)) < 0) {
73 return false;
74 }
75 hdr.size += res;
77 // now go back and write the root chunk
78 io->seek(0, SEEK_SET, io->cls);
79 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
80 return false;
81 }
83 return true;
84 */
85 return false;
86 }
88 bool Scene::save_anim(goat3d_io *io) const
89 {
90 return false;
91 }
94 #if 0
95 static long save_env(const Scene *scn, long offset, goat3d_io *io)
96 {
97 long res;
99 ChunkHeader hdr;
100 hdr.id = CNK_ENV;
101 hdr.size = sizeof hdr;
103 if((res = write_chunk_float3(CNK_ENV_AMBIENT, scn->get_ambient(), offset, io)) < 0) {
104 return -1;
105 }
106 hdr.size += res;
108 // TODO add fog chunk
110 io->seek(offset, SEEK_SET, io->cls);
111 if(io->write(&hdr, sizeof hdr, io->cls) < (ssize_t)sizeof hdr) {
112 return -1;
113 }
114 return hdr.size;
115 }
117 static long save_materials(const Scene *scn, long offset, goat3d_io *io)
118 {
119 long res;
121 ChunkHeader hdr;
122 hdr.id = CNK_MTL_LIST;
123 hdr.size = sizeof hdr;
125 for(int i=0; i<scn->get_material_count(); i++) {
126 if((res = save_material(scn->get_material(i), offset + hdr.size, io)) < 0) {
127 return -1;
128 }
129 hdr.size += res;
130 }
132 io->seek(offset, SEEK_SET, io->cls);
133 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
134 return -1;
135 }
136 return hdr.size;
137 }
139 static long save_material(const Material *mat, long offset, goat3d_io *io)
140 {
141 long res;
143 ChunkHeader hdr;
144 hdr.id = CNK_MTL;
145 hdr.size = sizeof hdr;
147 for(int i=0; i<mat->get_attrib_count(); i++) {
148 const char *name = mat->get_attrib_name(i);
149 if((res = save_mat_attrib(name, (*mat)[i], offset + hdr.size, io)) < 0) {
150 return -1;
151 }
152 hdr.size += res;
153 }
155 io->seek(offset, SEEK_SET, io->cls);
156 if(io->write(&hdr, hdr.size, io->cls) < hdr.size) {
157 return -1;
158 }
159 return hdr.size;
160 }
162 static long save_mat_attrib(const char *name, const MaterialAttrib &attr, long offset, goat3d_io *io)
163 {
164 long res;
166 ChunkHeader hdr;
167 hdr.id = CNK_MTL_ATTR;
168 hdr.size = sizeof hdr;
170 // TODO cont.
171 return -1;
172 }
174 static long save_meshes(const Scene *scn, long offset, goat3d_io *io)
175 {
176 return 0;
177 }
179 static long save_lights(const Scene *scn, long offset, goat3d_io *io)
180 {
181 return 0;
182 }
184 static long save_cameras(const Scene *scn, long offset, goat3d_io *io)
185 {
186 return 0;
187 }
189 static long save_nodes(const Scene *scn, long offset, goat3d_io *io)
190 {
191 return 0;
192 }
194 static long write_chunk_float(int id, float val, long offs, goat3d_io *io)
195 {
196 int size = sizeof(ChunkHeader) + sizeof val;
197 char *buf = (char*)alloca(size);
199 Chunk *c = (Chunk*)buf;
200 c->hdr.id = id;
201 c->hdr.size = size;
202 *(float*)c->data = val;
204 io->seek(offs, SEEK_SET, io->cls);
205 if(io->write(buf, size, io->cls) < size) {
206 return -1;
207 }
208 return size;
209 }
211 static long write_chunk_float3(int id, const Vector3 &vec, long offs, goat3d_io *io)
212 {
213 int size = sizeof(ChunkHeader) + sizeof vec;
214 char *buf = (char*)alloca(size);
216 Chunk *c = (Chunk*)buf;
217 c->hdr.id = id;
218 c->hdr.size = size;
219 *(Vector3*)c->data = vec;
221 io->seek(offs, SEEK_SET, io->cls);
222 if(io->write(buf, size, io->cls) < size) {
223 return -1;
224 }
225 return size;
226 }
228 static long write_chunk_float4(int id, const Vector4 &vec, long offs, goat3d_io *io)
229 {
230 int size = sizeof(ChunkHeader) + sizeof vec;
231 char *buf = (char*)alloca(size);
233 Chunk *c = (Chunk*)buf;
234 c->hdr.id = id;
235 c->hdr.size = size;
236 *(Vector4*)c->data = vec;
238 io->seek(offs, SEEK_SET, io->cls);
239 if(io->write(buf, size, io->cls) < size) {
240 return -1;
241 }
242 return size;
243 }
244 #endif