goat3d
view src/goat3d_writexml.cc @ 72:36e39632db75
- fixed exporter animation bounds calculation
- fixed missing scene name in exported meshes
- rewritting goatview as a full GUI app with Qt
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 06 May 2014 03:31:35 +0300 |
parents | dad392c710df d317eb4f83da |
children | 70b7c41a4f17 |
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, "<scene>\n");
38 // write environment stuff
39 xmlout(io, 1, "<env>\n");
40 xmlout(io, 2, "<ambient float3=\"%g %g %g\"/>\n", ambient.x, ambient.y, ambient.z);
41 xmlout(io, 1, "</env>\n\n");
43 for(size_t i=0; i<materials.size(); i++) {
44 write_material(this, io, materials[i], 1);
45 }
46 for(size_t i=0; i<meshes.size(); i++) {
47 write_mesh(this, io, meshes[i], (int)i, 1);
48 }
49 for(size_t i=0; i<lights.size(); i++) {
50 write_light(this, io, lights[i], 1);
51 }
52 for(size_t i=0; i<cameras.size(); i++) {
53 write_camera(this, io, cameras[i], 1);
54 }
55 for(size_t i=0; i<nodes.size(); i++) {
56 write_node(this, io, nodes[i], 1);
57 }
59 xmlout(io, 0, "</scene>\n");
60 return true;
61 }
63 bool Scene::save_anim_xml(goat3d_io *io) const
64 {
65 xmlout(io, 0, "<anim>\n");
67 if(!nodes.empty()) {
68 const char *anim_name = nodes[0]->get_animation_name();
69 if(anim_name && *anim_name) {
70 xmlout(io, 1, "<name string=\"%s\"/>\n", anim_name);
71 }
72 }
74 for(size_t i=0; i<nodes.size(); i++) {
75 write_node_anim(io, nodes[i], 1);
76 }
78 xmlout(io, 0, "</anim>\n");
79 return true;
80 }
82 static bool write_material(const Scene *scn, goat3d_io *io, const Material *mat, int level)
83 {
84 xmlout(io, level, "<mtl>\n");
85 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mat->name.c_str());
87 for(int i=0; i<mat->get_attrib_count(); i++) {
88 xmlout(io, level + 1, "<attr>\n");
89 xmlout(io, level + 2, "<name string=\"%s\"/>\n", mat->get_attrib_name(i));
91 const MaterialAttrib &attr = (*mat)[i];
92 xmlout(io, level + 2, "<val float4=\"%g %g %g %g\"/>\n", attr.value.x,
93 attr.value.y, attr.value.z, attr.value.w);
94 if(!attr.map.empty()) {
95 xmlout(io, level + 2, "<map string=\"%s\"/>\n", attr.map.c_str());
96 }
97 xmlout(io, level + 1, "</attr>\n");
98 }
99 xmlout(io, level, "</mtl>\n\n");
100 return true;
101 }
103 static bool write_mesh(const Scene *scn, goat3d_io *io, const Mesh *mesh, int idx, int level)
104 {
105 // first write the external (openctm) mesh file
106 const char *prefix = scn->get_name();
107 if(!prefix) {
108 prefix = "goat";
109 }
111 char *mesh_filename = (char*)alloca(strlen(prefix) + 32);
112 sprintf(mesh_filename, "%s-mesh%04d.ctm", prefix, idx);
114 if(!mesh->save(mesh_filename)) {
115 return false;
116 }
118 // then refer to that filename in the XML tags
119 xmlout(io, level, "<mesh>\n");
120 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mesh->name.c_str());
121 if(mesh->material) {
122 xmlout(io, level + 1, "<material string=\"%s\"/>\n", mesh->material->name.c_str());
123 }
124 xmlout(io, level + 1, "<file string=\"%s\"/>\n", clean_filename(mesh_filename).c_str());
125 xmlout(io, level, "</mesh>\n\n");
126 return true;
127 }
129 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level)
130 {
131 return true;
132 }
134 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level)
135 {
136 return true;
137 }
139 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level)
140 {
141 xmlout(io, level, "<node>\n");
142 xmlout(io, level + 1, "<name string=\"%s\"/>\n", node->get_name());
144 const XFormNode *parent = node->get_parent();
145 if(parent) {
146 xmlout(io, level + 1, "<parent string=\"%s\"/>\n", parent->get_name());
147 }
149 const char *type = 0;
150 const Object *obj = node->get_object();
151 if(dynamic_cast<const Mesh*>(obj)) {
152 type = "mesh";
153 } else if(dynamic_cast<const Light*>(obj)) {
154 type = "light";
155 } else if(dynamic_cast<const Camera*>(obj)) {
156 type = "camera";
157 }
159 if(type) {
160 xmlout(io, level + 1, "<%s string=\"%s\"/>\n", type, obj->name.c_str());
161 }
163 Vector3 pos = node->get_node_position();
164 Quaternion rot = node->get_node_rotation();
165 Vector3 scale = node->get_node_scaling();
166 Vector3 pivot = node->get_pivot();
168 Matrix4x4 xform;
169 node->get_node_xform(0, &xform);
171 xmlout(io, level + 1, "<pos float3=\"%g %g %g\"/>\n", pos.x, pos.y, pos.z);
172 xmlout(io, level + 1, "<rot float4=\"%g %g %g %g\"/>\n", rot.v.x, rot.v.y, rot.v.z, rot.s);
173 xmlout(io, level + 1, "<scale float3=\"%g %g %g\"/>\n", scale.x, scale.y, scale.z);
174 xmlout(io, level + 1, "<pivot float3=\"%g %g %g\"/>\n", pivot.x, pivot.y, pivot.z);
176 xmlout(io, level + 1, "<matrix0 float4=\"%g %g %g %g\"/>\n", xform[0][0], xform[0][1], xform[0][2], xform[0][3]);
177 xmlout(io, level + 1, "<matrix1 float4=\"%g %g %g %g\"/>\n", xform[1][0], xform[1][1], xform[1][2], xform[1][3]);
178 xmlout(io, level + 1, "<matrix2 float4=\"%g %g %g %g\"/>\n", xform[2][0], xform[2][1], xform[2][2], xform[2][3]);
180 xmlout(io, level, "</node>\n");
181 return true;
182 }
184 static bool write_node_anim(goat3d_io *io, const XFormNode *node, int level)
185 {
186 /* NOTE: the order of names must correspond to the order of the
187 * XFormNode::POSITION_TRACK/ROTATION_TRACK/SCALING_TRACK enum
188 */
189 static const char *attr_names[] = { "position", "rotation", "scaling" };
191 // for each of: position/rotation/scaling
192 for(int i=0; i<3; i++) {
193 int num_keys = node->get_key_count(i);
194 if(!num_keys) continue;
196 xmlout(io, level + 1, "<track>\n");
197 xmlout(io, level + 2, "<node string=\"%s\"/>\n", node->get_name());
198 xmlout(io, level + 2, "<attr string=\"%s\"/>\n\n", attr_names[i]);
200 // for each key in that track
201 for(int j=0; j<num_keys; j++) {
202 long tm = node->get_key_time(i, j);
204 float value[4];
205 int num_elems = node->get_key_value(i, j, value);
207 if(num_elems == 3) {
208 xmlout(io, level + 2, "<key><time int=\"%ld\"/><value float3=\"%g %g %g\"/></key>\n",
209 tm, value[0], value[1], value[2]);
210 } else {
211 xmlout(io, level + 2, "<key><time int=\"%ld\"/><value float4=\"%g %g %g %g\"/></key>\n",
212 tm, value[0], value[1], value[2], value[3]);
213 }
214 }
216 xmlout(io, level + 1, "</track>\n");
217 }
218 return true;
219 }
221 static void xmlout(goat3d_io *io, int level, const char *fmt, ...)
222 {
223 for(int i=0; i<level; i++) {
224 io_fprintf(io, " ");
225 }
227 va_list ap;
228 va_start(ap, fmt);
229 io_vfprintf(io, fmt, ap);
230 va_end(ap);
231 }