goat3d

view src/goat3d_writexml.cc @ 15:f1b4c27382ce

blah
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 14:06:14 +0300
parents 188c697b3b49
children 1d85d7dd0038
line source
1 #include <stdarg.h>
2 #include "goat3d_impl.h"
3 #include "chunk.h"
4 #include "openctm.h"
6 static bool write_material(const Scene *scn, goat3d_io *io, const Material *mat, int level);
7 static bool write_mesh(const Scene *scn, goat3d_io *io, const Mesh *mesh, int idx, int level);
8 static void write_ctm_mesh(const Mesh *mesh, const char *fname);
9 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level);
10 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level);
11 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level);
12 static void xmlout(goat3d_io *io, int level, const char *fmt, ...);
14 bool Scene::savexml(goat3d_io *io) const
15 {
16 xmlout(io, 0, "<scene>\n");
18 // write environment stuff
19 xmlout(io, 1, "<env>\n");
20 xmlout(io, 1, "</env>\n");
22 for(size_t i=0; i<materials.size(); i++) {
23 write_material(this, io, materials[i], 1);
24 }
25 for(size_t i=0; i<meshes.size(); i++) {
26 write_mesh(this, io, meshes[i], i, 1);
27 }
28 for(size_t i=0; i<lights.size(); i++) {
29 write_light(this, io, lights[i], 1);
30 }
31 for(size_t i=0; i<cameras.size(); i++) {
32 write_camera(this, io, cameras[i], 1);
33 }
34 for(size_t i=0; i<nodes.size(); i++) {
35 write_node(this, io, nodes[i], 1);
36 }
38 xmlout(io, 0, "</scene>\n");
39 return true;
40 }
42 static bool write_material(const Scene *scn, goat3d_io *io, const Material *mat, int level)
43 {
44 xmlout(io, level, "<mtl>\n");
45 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mat->name.c_str());
47 for(int i=0; i<mat->get_attrib_count(); i++) {
48 xmlout(io, level + 1, "<attr>\n");
49 xmlout(io, level + 2, "<name string=\"%s\"/>\n", mat->get_attrib_name(i));
51 const MaterialAttrib &attr = (*mat)[i];
52 xmlout(io, level + 2, "<val float4=\"%.3f %.3f %.3f %.3f\"/>\n", attr.value.x,
53 attr.value.y, attr.value.z, attr.value.w);
54 if(!attr.map.empty()) {
55 xmlout(io, level + 2, "<map string=\"%s\"/>\n", attr.map.c_str());
56 }
57 xmlout(io, level + 1, "</attr>\n");
58 }
59 xmlout(io, level, "</mtl>\n");
60 return true;
61 }
63 static bool write_mesh(const Scene *scn, goat3d_io *io, const Mesh *mesh, int idx, int level)
64 {
65 // first write the external (openctm) mesh file
66 const char *prefix = scn->get_name();
67 if(!prefix) {
68 prefix = "goat";
69 }
71 char *mesh_filename = (char*)alloca(strlen(prefix) + 32);
72 sprintf(mesh_filename, "%s-mesh%04d.ctm", prefix, idx);
74 write_ctm_mesh(mesh, mesh_filename);
76 // then refer to that filename in the XML tags
77 xmlout(io, level, "<mesh>\n");
78 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mesh->name.c_str());
79 xmlout(io, level + 1, "<material string=\"%s\"/>\n", mesh->material->name.c_str());
80 xmlout(io, level + 1, "<file string=\"%s\"/>\n", mesh_filename);
81 xmlout(io, level, "</mesh>\n");
82 return true;
83 }
85 static void write_ctm_mesh(const Mesh *mesh, const char *fname)
86 {
87 int vnum = (int)mesh->vertices.size();
89 CTMcontext ctm = ctmNewContext(CTM_EXPORT);
91 // vertices, normals, and face-vertex indices
92 ctmDefineMesh(ctm, &mesh->vertices[0].x, vnum, (CTMuint*)mesh->faces[0].v,
93 mesh->faces.size(), mesh->normals.empty() ? 0 : &mesh->normals[0].x);
95 // texture coordinates
96 if(!mesh->texcoords.empty()) {
97 ctmAddUVMap(ctm, &mesh->texcoords[0].x, "texcoord", 0);
98 }
100 // vertex colors
101 if(!mesh->colors.empty()) {
102 ctmAddAttribMap(ctm, &mesh->colors[0].x, "color");
103 }
105 // skin weights
106 if(!mesh->skin_weights.empty()) {
107 ctmAddAttribMap(ctm, &mesh->skin_weights[0].x, "skin_weight");
108 }
110 // if either of the non-float4 attributes are present we need to make a tmp array
111 CTMfloat *attr_array = 0;
112 if(!mesh->tangents.empty() || !mesh->skin_matrices.empty()) {
113 attr_array = new CTMfloat[vnum * 4 * sizeof *attr_array];
114 }
116 // tangents
117 if(!mesh->tangents.empty()) {
118 CTMfloat *ptr = attr_array;
120 for(int i=0; i<vnum; i++) {
121 *ptr++ = mesh->tangents[i].x;
122 *ptr++ = mesh->tangents[i].y;
123 *ptr++ = mesh->tangents[i].z;
124 *ptr++ = 1.0;
125 }
126 ctmAddAttribMap(ctm, attr_array, "tangent");
127 }
129 // skin matrix indices (4 per vertex)
130 if(!mesh->skin_matrices.empty()) {
131 CTMfloat *ptr = attr_array;
133 for(int i=0; i<vnum; i++) {
134 *ptr++ = (float)mesh->skin_matrices[i].x;
135 *ptr++ = (float)mesh->skin_matrices[i].y;
136 *ptr++ = (float)mesh->skin_matrices[i].z;
137 *ptr++ = (float)mesh->skin_matrices[i].w;
138 }
139 ctmAddAttribMap(ctm, attr_array, "skin_matrix");
140 }
142 delete [] attr_array;
144 /* TODO find a way to specify the nodes participating in the skinning of this mesh
145 * probably in the comment field?
146 */
148 ctmSave(ctm, fname);
150 ctmFreeContext(ctm);
151 }
153 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level)
154 {
155 return true;
156 }
158 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level)
159 {
160 return true;
161 }
163 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level)
164 {
165 return true;
166 }
169 static void xmlout(goat3d_io *io, int level, const char *fmt, ...)
170 {
171 for(int i=0; i<level; i++) {
172 io_fprintf(io, " ");
173 }
175 va_list ap;
176 va_start(ap, fmt);
177 io_vfprintf(io, fmt, ap);
178 va_end(ap);
179 }