goat3d

view src/goat3d_writexml.cc @ 14:188c697b3b49

- added a document describing the goat3d file format chunk hierarchy - started an alternative XML-based file format - added the openctm library
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 26 Sep 2013 04:47:05 +0300
parents
children f1b4c27382ce
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\"/>\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 CTMfloat *uvarray = new CTMfloat[vnum * 2 * sizeof *uvarray];
98 CTMfloat *uvptr = uvarray;
100 for(int i=0; i<vnum; i++) {
101 *uvptr++ = mesh->texcoords[i].x;
102 *uvptr++ = mesh->texcoords[i].y;
103 }
104 ctmAddUVMap(ctm, uvarray, "texcoord", 0);
105 delete [] uvarray;
106 }
108 // vertex colors
109 if(!mesh->colors.empty()) {
110 ctmAddAttribMap(ctm, &mesh->colors[0].x, "color");
111 }
113 // skin weights
114 if(!mesh->skin_weights.empty()) {
115 ctmAddAttribMap(ctm, &mesh->skin_weights[0].x, "skin_weight");
116 }
118 // if either of the non-float4 attributes are present we need to make a tmp array
119 CTMfloat *attr_array = 0;
120 if(!mesh->tangents.empty() || !mesh->skin_matrices.empty()) {
121 attr_array = new CTMfloat[vnum * 4 * sizeof *attr_array];
122 }
124 // tangents
125 if(!mesh->tangents.empty()) {
126 CTMfloat *ptr = attr_array;
128 for(int i=0; i<vnum; i++) {
129 *ptr++ = mesh->tangents[i].x;
130 *ptr++ = mesh->tangents[i].y;
131 *ptr++ = mesh->tangents[i].z;
132 *ptr++ = 1.0;
133 }
134 ctmAddAttribMap(ctm, attr_array, "tangent");
135 }
137 // skin matrix indices (4 per vertex)
138 if(!mesh->skin_matrices.empty()) {
139 CTMfloat *ptr = attr_array;
141 for(int i=0; i<vnum; i++) {
142 *ptr++ = (float)mesh->skin_matrices[i].x;
143 *ptr++ = (float)mesh->skin_matrices[i].y;
144 *ptr++ = (float)mesh->skin_matrices[i].z;
145 *ptr++ = (float)mesh->skin_matrices[i].w;
146 }
147 ctmAddAttribMap(ctm, attr_array, "skin_matrix");
148 }
150 delete [] attr_array;
152 /* TODO find a way to specify the nodes participating in the skinning of this mesh
153 * probably in the comment field?
154 */
156 ctmSave(ctm, fname);
158 ctmFreeContext(ctm);
159 }
161 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level)
162 {
163 return true;
164 }
166 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level)
167 {
168 return true;
169 }
171 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level)
172 {
173 return true;
174 }
177 static void xmlout(goat3d_io *io, int level, const char *fmt, ...)
178 {
179 for(int i=0; i<level; i++) {
180 io_fprintf(io, " ");
181 }
183 va_list ap;
184 va_start(ap, fmt);
185 io_vfprintf(io, fmt, ap);
186 va_end(ap);
187 }