OSDN Git Service

separate vertex with uv or normal.
[meshio/meshio.git] / src / mqo.cpp
index 0389326..7c6ff3c 100644 (file)
@@ -1,61 +1,31 @@
 #include "mqo.h"
 #include <iostream>
 #include <fstream>
+#include <sstream>
+#include <iomanip>
 #include <vector>
 
-#include <windows.h>
-
-static std::wstring to_WideChar(UINT uCodePage, const std::string &text)
-{
-       int size=MultiByteToWideChar(uCodePage, 0, text.c_str(), -1, NULL, 0);
-       std::vector<wchar_t> buf(size);
-       size=MultiByteToWideChar(uCodePage, 0, text.c_str(), -1, &buf[0], buf.size());
-       return std::wstring(buf.begin(), buf.begin()+size);
-}
-
-static std::string to_MultiByte(UINT uCodePage, const std::wstring &text)
-{
-       int size=WideCharToMultiByte(uCodePage, 0, text.c_str(), -1, NULL, 0, 0, NULL);
-       std::vector<char> buf(size);
-       size=WideCharToMultiByte(uCodePage, 0, text.c_str(), -1, &buf[0], buf.size(), 0, NULL);
-       return std::string(buf.begin(), buf.begin()+size);
-}
-
-static std::string cp932_to_utf8(const std::string &text)
-{
-       return to_MultiByte(CP_UTF8, to_WideChar(CP_OEMCP, text));
-}
+#include "text.h"
 
 namespace meshio {
 namespace mqo {
 
-static std::wstring trim(const std::wstring &src){
-       std::wstring::const_iterator end=src.begin();
-       for(; end!=src.end(); ++end){
-               if(*end==L'\0'){
-                       break;
-               }
-       }
-       return std::wstring(src.begin(), end);
-}
-
-// for python3 unicode
 std::wstring 
        Material::getName()const
        {
-               return trim(to_WideChar(CP_OEMCP, name));
+               return text::trim(text::to_WideChar(CP_OEMCP, name));
        }
 
 std::wstring 
        Material::getTexture()const
        {
-               return trim(to_WideChar(CP_OEMCP, texture));
+               return text::trim(text::to_WideChar(CP_OEMCP, texture));
        }
 
 std::wstring 
        Object::getName()const
        {
-               return trim(to_WideChar(CP_OEMCP, name));
+               return text::trim(text::to_WideChar(CP_OEMCP, name));
        }
 
 //! Tokenizer
@@ -116,7 +86,7 @@ public:
                                else if(key=="Eof"){
                                        if(materials.empty()){
                                                // fallback
-                                               materials.push_back(Material());
+                                               //materials.push_back(Material());
                                        }
                                        return true;
                                }
@@ -337,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();
@@ -469,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);
+}
 
 }
 }