goat3d

view src/goat3d_writexml.cc @ 33:f43f4849c86a

started writing blender exporter
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Oct 2013 03:07:45 +0300
parents bdfc8dd14965
children 0fe02696fb1e
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 return true;
101 }
104 static void xmlout(goat3d_io *io, int level, const char *fmt, ...)
105 {
106 for(int i=0; i<level; i++) {
107 io_fprintf(io, " ");
108 }
110 va_list ap;
111 va_start(ap, fmt);
112 io_vfprintf(io, fmt, ap);
113 va_end(ap);
114 }