goat3d

view src/goat3d_writexml.cc @ 84:022b13ed975b

minor gui fix
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 12 May 2014 07:37:30 +0300
parents 99715321ad6d
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 <list>
19 #include <stdarg.h>
20 #include "goat3d_impl.h"
21 #include "anim/anim.h"
22 #include "log.h"
24 using namespace g3dimpl;
26 static bool write_material(const Scene *scn, goat3d_io *io, const Material *mat, int level);
27 static bool write_mesh(const Scene *scn, goat3d_io *io, const Mesh *mesh, int idx, int level);
28 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level);
29 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level);
30 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level);
31 static bool write_node_anim(goat3d_io *io, const XFormNode *node, int level);
32 static void xmlout(goat3d_io *io, int level, const char *fmt, ...);
34 bool Scene::savexml(goat3d_io *io) const
35 {
36 xmlout(io, 0, "<!-- vi:set filetype=xml: -->\n");
37 xmlout(io, 0, "<!-- Goat3D XML scene description -->\n");
38 xmlout(io, 0, "<scene>\n");
40 // write environment stuff
41 xmlout(io, 1, "<env>\n");
42 xmlout(io, 2, "<ambient float3=\"%g %g %g\"/>\n", ambient.x, ambient.y, ambient.z);
43 xmlout(io, 1, "</env>\n\n");
45 for(size_t i=0; i<materials.size(); i++) {
46 write_material(this, io, materials[i], 1);
47 }
48 for(size_t i=0; i<meshes.size(); i++) {
49 write_mesh(this, io, meshes[i], (int)i, 1);
50 }
51 for(size_t i=0; i<lights.size(); i++) {
52 write_light(this, io, lights[i], 1);
53 }
54 for(size_t i=0; i<cameras.size(); i++) {
55 write_camera(this, io, cameras[i], 1);
56 }
57 for(size_t i=0; i<nodes.size(); i++) {
58 write_node(this, io, nodes[i], 1);
59 }
61 xmlout(io, 0, "</scene>\n");
62 return true;
63 }
65 bool Scene::save_anim_xml(goat3d_io *io) const
66 {
67 xmlout(io, 0, "<!-- vi:set filetype=xml: -->\n");
68 xmlout(io, 0, "<!-- Goat3D XML animation -->\n");
69 xmlout(io, 0, "<anim>\n");
71 if(!nodes.empty()) {
72 const char *anim_name = nodes[0]->get_animation_name();
73 if(anim_name && *anim_name) {
74 xmlout(io, 1, "<name string=\"%s\"/>\n", anim_name);
75 }
76 }
78 for(size_t i=0; i<nodes.size(); i++) {
79 write_node_anim(io, nodes[i], 1);
80 }
82 xmlout(io, 0, "</anim>\n");
83 return true;
84 }
86 static bool write_material(const Scene *scn, goat3d_io *io, const Material *mat, int level)
87 {
88 xmlout(io, level, "<mtl>\n");
89 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mat->name.c_str());
91 for(int i=0; i<mat->get_attrib_count(); i++) {
92 xmlout(io, level + 1, "<attr>\n");
93 xmlout(io, level + 2, "<name string=\"%s\"/>\n", mat->get_attrib_name(i));
95 const MaterialAttrib &attr = (*mat)[i];
96 xmlout(io, level + 2, "<val float4=\"%g %g %g %g\"/>\n", attr.value.x,
97 attr.value.y, attr.value.z, attr.value.w);
98 if(!attr.map.empty()) {
99 xmlout(io, level + 2, "<map string=\"%s\"/>\n", attr.map.c_str());
100 }
101 xmlout(io, level + 1, "</attr>\n");
102 }
103 xmlout(io, level, "</mtl>\n\n");
104 return true;
105 }
107 static bool write_mesh(const Scene *scn, goat3d_io *io, const Mesh *mesh, int idx, int level)
108 {
109 // first write the external (openctm) mesh file
110 const char *prefix = scn->get_name();
111 if(!prefix) {
112 prefix = "goat";
113 }
115 char *mesh_filename = (char*)alloca(strlen(prefix) + 32);
116 sprintf(mesh_filename, "%s-mesh%04d.ctm", prefix, idx);
118 if(!mesh->save(mesh_filename)) {
119 return false;
120 }
122 // then refer to that filename in the XML tags
123 xmlout(io, level, "<mesh>\n");
124 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mesh->name.c_str());
125 if(mesh->material) {
126 xmlout(io, level + 1, "<material string=\"%s\"/>\n", mesh->material->name.c_str());
127 }
128 xmlout(io, level + 1, "<file string=\"%s\"/>\n", clean_filename(mesh_filename).c_str());
129 xmlout(io, level, "</mesh>\n\n");
130 return true;
131 }
133 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level)
134 {
135 return true;
136 }
138 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level)
139 {
140 return true;
141 }
143 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level)
144 {
145 xmlout(io, level, "<node>\n");
146 xmlout(io, level + 1, "<name string=\"%s\"/>\n", node->get_name());
148 const XFormNode *parent = node->get_parent();
149 if(parent) {
150 xmlout(io, level + 1, "<parent string=\"%s\"/>\n", parent->get_name());
151 }
153 const char *type = 0;
154 const Object *obj = node->get_object();
155 if(dynamic_cast<const Mesh*>(obj)) {
156 type = "mesh";
157 } else if(dynamic_cast<const Light*>(obj)) {
158 type = "light";
159 } else if(dynamic_cast<const Camera*>(obj)) {
160 type = "camera";
161 }
163 if(type) {
164 xmlout(io, level + 1, "<%s string=\"%s\"/>\n", type, obj->name.c_str());
165 }
167 Vector3 pos = node->get_node_position();
168 Quaternion rot = node->get_node_rotation();
169 Vector3 scale = node->get_node_scaling();
170 Vector3 pivot = node->get_pivot();
172 Matrix4x4 xform;
173 node->get_node_xform(0, &xform);
175 xmlout(io, level + 1, "<pos float3=\"%g %g %g\"/>\n", pos.x, pos.y, pos.z);
176 xmlout(io, level + 1, "<rot float4=\"%g %g %g %g\"/>\n", rot.v.x, rot.v.y, rot.v.z, rot.s);
177 xmlout(io, level + 1, "<scale float3=\"%g %g %g\"/>\n", scale.x, scale.y, scale.z);
178 xmlout(io, level + 1, "<pivot float3=\"%g %g %g\"/>\n", pivot.x, pivot.y, pivot.z);
180 xmlout(io, level + 1, "<matrix0 float4=\"%g %g %g %g\"/>\n", xform[0][0], xform[0][1], xform[0][2], xform[0][3]);
181 xmlout(io, level + 1, "<matrix1 float4=\"%g %g %g %g\"/>\n", xform[1][0], xform[1][1], xform[1][2], xform[1][3]);
182 xmlout(io, level + 1, "<matrix2 float4=\"%g %g %g %g\"/>\n", xform[2][0], xform[2][1], xform[2][2], xform[2][3]);
184 xmlout(io, level, "</node>\n");
185 return true;
186 }
188 static bool write_node_anim(goat3d_io *io, const XFormNode *node, int level)
189 {
190 /* NOTE: the order of names must correspond to the order of the
191 * XFormNode::POSITION_TRACK/ROTATION_TRACK/SCALING_TRACK enum
192 */
193 static const char *attr_names[] = { "position", "rotation", "scaling" };
195 // for each of: position/rotation/scaling
196 for(int i=0; i<3; i++) {
197 int num_keys = node->get_key_count(i);
198 if(!num_keys) continue;
200 xmlout(io, level + 1, "<track>\n");
201 xmlout(io, level + 2, "<node string=\"%s\"/>\n", node->get_name());
202 xmlout(io, level + 2, "<attr string=\"%s\"/>\n\n", attr_names[i]);
204 // for each key in that track
205 for(int j=0; j<num_keys; j++) {
206 long tm = node->get_key_time(i, j);
208 float value[4];
209 int num_elems = node->get_key_value(i, j, value);
211 if(num_elems == 3) {
212 xmlout(io, level + 2, "<key><time int=\"%ld\"/><value float3=\"%g %g %g\"/></key>\n",
213 tm, value[0], value[1], value[2]);
214 } else {
215 xmlout(io, level + 2, "<key><time int=\"%ld\"/><value float4=\"%g %g %g %g\"/></key>\n",
216 tm, value[0], value[1], value[2], value[3]);
217 }
218 }
220 xmlout(io, level + 1, "</track>\n");
221 }
222 return true;
223 }
225 static void xmlout(goat3d_io *io, int level, const char *fmt, ...)
226 {
227 for(int i=0; i<level; i++) {
228 io_fprintf(io, " ");
229 }
231 va_list ap;
232 va_start(ap, fmt);
233 io_vfprintf(io, fmt, ap);
234 va_end(ap);
235 }