goat3d
view src/goat3d_writexml.cc @ 39:0e48907847ad
slugishly progressing with the blender exporter
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Wed, 09 Oct 2013 16:40:59 +0300 |
parents | b35427826b60 |
children | a5c5cec3cb88 |
line source
1 #include <stdarg.h>
2 #include "goat3d_impl.h"
3 #include "log.h"
5 static bool write_material(const Scene *scn, goat3d_io *io, const Material *mat, int level);
6 static bool write_mesh(const Scene *scn, goat3d_io *io, const Mesh *mesh, int idx, int level);
7 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level);
8 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level);
9 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level);
10 static void xmlout(goat3d_io *io, int level, const char *fmt, ...);
12 bool Scene::savexml(goat3d_io *io) const
13 {
14 xmlout(io, 0, "<scene>\n");
16 // write environment stuff
17 xmlout(io, 1, "<env>\n");
18 xmlout(io, 2, "<ambient float3=\"%g %g %g\"/>\n", ambient.x, ambient.y, ambient.z);
19 xmlout(io, 1, "</env>\n\n");
21 for(size_t i=0; i<materials.size(); i++) {
22 write_material(this, io, materials[i], 1);
23 }
24 for(size_t i=0; i<meshes.size(); i++) {
25 write_mesh(this, io, meshes[i], i, 1);
26 }
27 for(size_t i=0; i<lights.size(); i++) {
28 write_light(this, io, lights[i], 1);
29 }
30 for(size_t i=0; i<cameras.size(); i++) {
31 write_camera(this, io, cameras[i], 1);
32 }
33 for(size_t i=0; i<nodes.size(); i++) {
34 write_node(this, io, nodes[i], 1);
35 }
37 xmlout(io, 0, "</scene>\n");
38 return true;
39 }
41 static bool write_material(const Scene *scn, goat3d_io *io, const Material *mat, int level)
42 {
43 xmlout(io, level, "<mtl>\n");
44 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mat->name.c_str());
46 for(int i=0; i<mat->get_attrib_count(); i++) {
47 xmlout(io, level + 1, "<attr>\n");
48 xmlout(io, level + 2, "<name string=\"%s\"/>\n", mat->get_attrib_name(i));
50 const MaterialAttrib &attr = (*mat)[i];
51 xmlout(io, level + 2, "<val float4=\"%g %g %g %g\"/>\n", attr.value.x,
52 attr.value.y, attr.value.z, attr.value.w);
53 if(!attr.map.empty()) {
54 xmlout(io, level + 2, "<map string=\"%s\"/>\n", attr.map.c_str());
55 }
56 xmlout(io, level + 1, "</attr>\n");
57 }
58 xmlout(io, level, "</mtl>\n\n");
59 return true;
60 }
62 static bool write_mesh(const Scene *scn, goat3d_io *io, const Mesh *mesh, int idx, int level)
63 {
64 // first write the external (openctm) mesh file
65 const char *prefix = scn->get_name();
66 if(!prefix) {
67 prefix = "goat";
68 }
70 char *mesh_filename = (char*)alloca(strlen(prefix) + 32);
71 sprintf(mesh_filename, "%s-mesh%04d.ctm", prefix, idx);
73 if(!mesh->save(mesh_filename)) {
74 return false;
75 }
77 // then refer to that filename in the XML tags
78 xmlout(io, level, "<mesh>\n");
79 xmlout(io, level + 1, "<name string=\"%s\"/>\n", mesh->name.c_str());
80 if(mesh->material) {
81 xmlout(io, level + 1, "<material string=\"%s\"/>\n", mesh->material->name.c_str());
82 }
83 xmlout(io, level + 1, "<file string=\"%s\"/>\n", mesh_filename);
84 xmlout(io, level, "</mesh>\n\n");
85 return true;
86 }
88 static bool write_light(const Scene *scn, goat3d_io *io, const Light *light, int level)
89 {
90 return true;
91 }
93 static bool write_camera(const Scene *scn, goat3d_io *io, const Camera *cam, int level)
94 {
95 return true;
96 }
98 static bool write_node(const Scene *scn, goat3d_io *io, const Node *node, int level)
99 {
100 xmlout(io, level, "<node>\n");
101 xmlout(io, level + 1, "<name string=\"%s\"/>\n", node->get_name());
103 XFormNode *parent = node->get_parent();
104 if(parent) {
105 xmlout(io, level + 1, "<parent string=\"%s\"/>\n", parent->get_name());
106 }
108 const char *type = 0;
109 const Object *obj = node->get_object();
110 if(dynamic_cast<const Mesh*>(obj)) {
111 type = "mesh";
112 } else if(dynamic_cast<const Light*>(obj)) {
113 type = "light";
114 } else if(dynamic_cast<const Camera*>(obj)) {
115 type = "camera";
116 }
118 if(type) {
119 xmlout(io, level + 1, "<%s string=\"%s\"/>\n", type, obj->name.c_str());
120 }
122 Vector3 pos = node->get_node_position();
123 Quaternion rot = node->get_node_rotation();
124 Vector3 scale = node->get_node_scaling();
125 Vector3 pivot = node->get_pivot();
127 Matrix4x4 xform;
128 node->get_node_xform(0, &xform);
130 xmlout(io, level + 1, "<pos float3=\"%g %g %g\"/>\n", pos.x, pos.y, pos.z);
131 xmlout(io, level + 1, "<rot float4=\"%g %g %g %g\"/>\n", rot.v.x, rot.v.y, rot.v.z, rot.s);
132 xmlout(io, level + 1, "<scale float3=\"%g %g %g\"/>\n", scale.x, scale.y, scale.z);
133 xmlout(io, level + 1, "<pivot float3=\"%g %g %g\"/>\n", pivot.x, pivot.y, pivot.z);
135 xmlout(io, level + 1, "<matrix0 float4=\"%g %g %g %g\"/>\n", xform[0][0], xform[0][1], xform[0][2], xform[0][3]);
136 xmlout(io, level + 1, "<matrix1 float4=\"%g %g %g %g\"/>\n", xform[1][0], xform[1][1], xform[1][2], xform[1][3]);
137 xmlout(io, level + 1, "<matrix2 float4=\"%g %g %g %g\"/>\n", xform[2][0], xform[2][1], xform[2][2], xform[2][3]);
139 xmlout(io, level, "</node>\n");
140 return true;
141 }
144 static void xmlout(goat3d_io *io, int level, const char *fmt, ...)
145 {
146 for(int i=0; i<level; i++) {
147 io_fprintf(io, " ");
148 }
150 va_list ap;
151 va_start(ap, fmt);
152 io_vfprintf(io, fmt, ap);
153 va_end(ap);
154 }