goat3d

view src/goat3d_writexml.cc @ 17:1d85d7dd0038

goatprim done
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 27 Sep 2013 02:29:52 +0300
parents f1b4c27382ce
children bdfc8dd14965
line source
1 #include <stdarg.h>
2 #include "goat3d_impl.h"
3 #include "log.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 if(mesh->material) {
80 xmlout(io, level + 1, "<material string=\"%s\"/>\n", mesh->material->name.c_str());
81 }
82 xmlout(io, level + 1, "<file string=\"%s\"/>\n", mesh_filename);
83 xmlout(io, level, "</mesh>\n");
84 return true;
85 }
87 static void write_ctm_mesh(const Mesh *mesh, const char *fname)
88 {
89 int vnum = (int)mesh->vertices.size();
91 CTMcontext ctm = ctmNewContext(CTM_EXPORT);
93 // vertices, normals, and face-vertex indices
94 ctmDefineMesh(ctm, &mesh->vertices[0].x, vnum, (CTMuint*)mesh->faces[0].v,
95 mesh->faces.size(), mesh->normals.empty() ? 0 : &mesh->normals[0].x);
97 // texture coordinates
98 if(!mesh->texcoords.empty()) {
99 ctmAddUVMap(ctm, &mesh->texcoords[0].x, "texcoord", 0);
100 }
102 // vertex colors
103 if(!mesh->colors.empty()) {
104 ctmAddAttribMap(ctm, &mesh->colors[0].x, "color");
105 }
107 // skin weights
108 if(!mesh->skin_weights.empty()) {
109 ctmAddAttribMap(ctm, &mesh->skin_weights[0].x, "skin_weight");
110 }
112 // if either of the non-float4 attributes are present we need to make a tmp array
113 CTMfloat *attr_array = 0;
114 if(!mesh->tangents.empty() || !mesh->skin_matrices.empty()) {
115 attr_array = new CTMfloat[vnum * 4 * sizeof *attr_array];
116 }
118 // tangents
119 if(!mesh->tangents.empty()) {
120 CTMfloat *ptr = attr_array;
122 for(int i=0; i<vnum; i++) {
123 *ptr++ = mesh->tangents[i].x;
124 *ptr++ = mesh->tangents[i].y;
125 *ptr++ = mesh->tangents[i].z;
126 *ptr++ = 1.0;
127 }
128 ctmAddAttribMap(ctm, attr_array, "tangent");
129 }
131 // skin matrix indices (4 per vertex)
132 if(!mesh->skin_matrices.empty()) {
133 CTMfloat *ptr = attr_array;
135 for(int i=0; i<vnum; i++) {
136 *ptr++ = (float)mesh->skin_matrices[i].x;
137 *ptr++ = (float)mesh->skin_matrices[i].y;
138 *ptr++ = (float)mesh->skin_matrices[i].z;
139 *ptr++ = (float)mesh->skin_matrices[i].w;
140 }
141 ctmAddAttribMap(ctm, attr_array, "skin_matrix");
142 }
144 delete [] attr_array;
146 /* TODO find a way to specify the nodes participating in the skinning of this mesh
147 * probably in the comment field?
148 */
150 logmsg(LOG_INFO, "saving CTM mesh file: %s\n", fname);
151 ctmSave(ctm, fname);
153 ctmFreeContext(ctm);
154 }
156 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level)
157 {
158 return true;
159 }
161 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level)
162 {
163 return true;
164 }
166 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level)
167 {
168 return true;
169 }
172 static void xmlout(goat3d_io *io, int level, const char *fmt, ...)
173 {
174 for(int i=0; i<level; i++) {
175 io_fprintf(io, " ");
176 }
178 va_list ap;
179 va_start(ap, fmt);
180 io_vfprintf(io, fmt, ap);
181 va_end(ap);
182 }