OSDN Git Service

pmd_import sphere map.
[meshio/meshio.git] / src / mqo.cpp
index 982d47d..7c6ff3c 100644 (file)
@@ -1,11 +1,33 @@
 #include "mqo.h"
 #include <iostream>
 #include <fstream>
+#include <sstream>
+#include <iomanip>
 #include <vector>
 
+#include "text.h"
+
 namespace meshio {
 namespace mqo {
 
+std::wstring 
+       Material::getName()const
+       {
+               return text::trim(text::to_WideChar(CP_OEMCP, name));
+       }
+
+std::wstring 
+       Material::getTexture()const
+       {
+               return text::trim(text::to_WideChar(CP_OEMCP, texture));
+       }
+
+std::wstring 
+       Object::getName()const
+       {
+               return text::trim(text::to_WideChar(CP_OEMCP, name));
+       }
+
 //! Tokenizer
 struct DELIMITER
 {
@@ -14,6 +36,7 @@ struct DELIMITER
                switch(c)
                {
                case ' ':
+               case '\t':
                case '(':
                case ')':
                        return true;
@@ -63,7 +86,7 @@ public:
                                else if(key=="Eof"){
                                        if(materials.empty()){
                                                // fallback
-                                               materials.push_back(Material());
+                                               //materials.push_back(Material());
                                        }
                                        return true;
                                }
@@ -138,6 +161,9 @@ private:
                                        // ToDo
                                        continue;
                                }
+                               else if(key=="mirror"){
+                                       object.mirror=splitter.getInt();
+                               }
                                else{
                                        std::cout << "unknown object key: " << key << std::endl;
                                }
@@ -220,7 +246,7 @@ private:
                                else if(key=="COL"){
                                        for(size_t i=0; i<face.index_count; ++i){
                                                face.color[i]=
-                                                       color::bRGBA::createFromUInt(splitter.getInt());
+                                                       color::fRGBA::createFromUInt(splitter.getInt());
                                        }
                                }
                                else{
@@ -272,7 +298,7 @@ private:
                                        material.shader=splitter.getInt();
                                }
                                else if(key=="col"){
-                                       material.color=splitter.getByteRGBA();
+                                       material.color=splitter.getFloatRGBA();
                                }
                                else if(key=="dif"){
                                        material.diffuse=splitter.getFloat();
@@ -281,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();
@@ -389,11 +415,126 @@ bool IO::read(binary::IReader &input)
        return Implementation(scene, materials, objects).parse(reader);
 }
 
-bool IO::write(std::ostream &os)
+bool IO::read(const char *path)
+{
+       std::vector<char> all;
+       binary::readAll(path, all);
+       if(all.empty()){
+               return false;
+       }
+       binary::MemoryReader reader(&all[0], all.size());
+       return read(reader);
+}
+
+#ifdef _WIN32
+bool IO::read(const wchar_t *path)
+{
+       std::vector<char> all;
+       binary::readAll(path, all);
+       if(all.empty()){
+               return false;
+       }
+       binary::MemoryReader reader(&all[0], all.size());
+       return read(reader);
+}
+#endif
+
+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);
+}
 
 }
 }