OSDN Git Service

separate vertex with uv or normal.
[meshio/meshio.git] / src / mqo.cpp
index 88325f1..7c6ff3c 100644 (file)
@@ -1,6 +1,8 @@
 #include "mqo.h"
 #include <iostream>
 #include <fstream>
+#include <sstream>
+#include <iomanip>
 #include <vector>
 
 #include "text.h"
@@ -84,7 +86,7 @@ public:
                                else if(key=="Eof"){
                                        if(materials.empty()){
                                                // fallback
-                                               materials.push_back(Material());
+                                               //materials.push_back(Material());
                                        }
                                        return true;
                                }
@@ -305,7 +307,7 @@ private:
                                        material.ambient=splitter.getFloat();
                                }
                                else if(key=="emi"){
-                                       material.emmit=splitter.getFloat();
+                                       material.emit=splitter.getFloat();
                                }
                                else if(key=="spc"){
                                        material.specular=splitter.getFloat();
@@ -437,11 +439,102 @@ bool IO::read(const wchar_t *path)
 }
 #endif
 
-bool IO::write(std::ostream &os)
+bool IO::write(binary::IWriter &writer)
 {
-       return false;
+       // header
+       writer.printLn("Metasequoia Document");
+       writer.printLn("Format Text Ver 1.0");
+       writer.printLn("");
+
+       // scene
+       writer.printLn("Scene {");
+       writer.printLn("\tpos 0.0000 0.0000 1500.0000");
+       writer.printLn("\tlookat 0.0000 0.0000 0.0000");
+       writer.printLn("\thead -0.5236");
+       writer.printLn("\tpich 0.5236");
+       writer.printLn("\tortho 0");
+       writer.printLn("\tzoom2 5.0000");
+       writer.printLn("\tamb 0.250 0.250 0.250");
+       writer.printLn("}");
+
+       // materials
+       if(materials.size()>0){
+               writer.printLn("Material %d {", materials.size());
+               for(size_t i=0; i<materials.size(); ++i){
+                       Material &m=materials[i];
+               }
+               writer.printLn("}");
+       }
+
+       // objects
+       for(size_t i=0; i<objects.size(); ++i){
+               Object &o=objects[i];
+               writer.printLn("Object \"%s\" {", o.name.c_str());
+               writer.printLn("\tdepth 0");
+               writer.printLn("\tfolding 0");
+               writer.printLn("\tscale 1.000000 1.000000 1.000000");
+               writer.printLn("\trotation 0.000000 0.000000 0.000000");
+               writer.printLn("\ttranslation 0.000000 0.000000 0.000000");
+               writer.printLn("\tvisible 15");
+               writer.printLn("\tlocking 0");
+               writer.printLn("\tshading 1");
+               writer.printLn("\tfacet 59.5");
+               writer.printLn("\tcolor 0.898 0.400 0.137");
+               writer.printLn("\tcolor_type 0");
+               // vertex
+               writer.printLn("\tvertex %d {", o.vertices.size());
+               for(size_t j=0; j<o.vertices.size(); ++j){
+                       Vector3 &v=o.vertices[j];
+                       writer.printLn("\t\t%.4f %.4f %.4f", v.x, v.y, v.z);
+               }
+               writer.printLn("\t}");
+               // face
+               writer.printLn("\tface %d {", o.faces.size());
+               for(size_t j=0; j<o.faces.size(); ++j){
+                       Face &f=o.faces[j];
+
+                       std::stringstream ss;
+                       ss.setf(std::ios_base::fixed, std::ios_base::floatfield);
+                       ss 
+                               << "\t\t"
+                               << f.index_count
+                               ;
+                       ss << " V(";
+                       for(size_t k=0; k<f.index_count; ++k){
+                               if(k){
+                                       ss << ' ';
+                               }
+                               ss << f.indices[k];
+                       }
+                       ss << ") UV(";
+                       for(size_t k=0; k<f.index_count; ++k){
+                               if(k){
+                                       ss << ' ';
+                               }
+                               Vector2 &uv=f.uv[k];
+                               ss 
+                                       << std::setprecision(5) << uv.x 
+                                       << ' ' << std::setprecision(5) << uv.y;
+                       }
+                       ss << ")";
+
+                       writer.printLn(ss.str().c_str());
+               }
+               writer.printLn("\t}");
+               // close
+               writer.printLn("}");
+       }
+       // Eof
+       writer.printLn("Eof");
+
+       return true;
 }
 
+bool IO::write(const char *path)
+{
+       binary::FileWriter writer(path);
+       return write(writer);
+}
 
 }
 }