From 26f276ff9474555c578b2ce10cf862f09d43ba7b Mon Sep 17 00:00:00 2001 From: lzwdgc Date: Thu, 27 Jul 2017 00:34:21 +0300 Subject: [PATCH] Print two variants for ue4 and usual fbx. --- cppan.yml | 1 + src/CMakeLists.txt | 5 ++- src/mod_converter/model.cpp | 90 ++++++++++++++++++++++++++----------- src/mod_converter/model.h | 8 ++-- 4 files changed, 74 insertions(+), 30 deletions(-) diff --git a/cppan.yml b/cppan.yml index ad18700..2a05418 100644 --- a/cppan.yml +++ b/cppan.yml @@ -1,4 +1,5 @@ local_settings: dependencies: - pvt.lzwdgc.polygon4.data_manager.data_manager: master + - pvt.cppan.demo.eigen: 3 diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 17bd3b9..91b3b3b 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -23,7 +23,10 @@ target_link_libraries(mmp_extractor common) file(GLOB mod_converter_src "mod_converter/*") add_executable(mod_converter ${mod_converter_src}) -target_link_libraries(mod_converter common) +target_link_libraries(mod_converter + common + pvt.cppan.demo.eigen +) file(GLOB mpj_loader_src "mpj_loader/*") add_executable(mpj_loader ${mpj_loader_src}) diff --git a/src/mod_converter/model.cpp b/src/mod_converter/model.cpp index 4570fed..d646441 100644 --- a/src/mod_converter/model.cpp +++ b/src/mod_converter/model.cpp @@ -23,9 +23,13 @@ #include #include #include +#include #include +//#include +//#include + #include using namespace std; @@ -150,15 +154,36 @@ void vertex::load(const buffer &b, uint32_t flags) READ(b, texture_coordinates); } -std::string vertex::printVertex() const +std::string vertex::printVertex(bool rotate_x_90) const { + // rotate by 90 grad over Ox axis +/*#define M_PI_2 1.57079632679489661923 + Eigen::Vector3f x; + x << -coordinates.x, coordinates.y, -coordinates.z; + + Eigen::AngleAxis rx(M_PI_2, Eigen::Vector3f(1, 0, 0)); + auto x2 = rx * x;*/ + string s; - s = "v " + - to_string(-coordinates.x) + " " + - to_string(coordinates.y) + " " + - to_string(-coordinates.z) + " " + - to_string(coordinates.w) - ; + if (rotate_x_90) + { + // that rotation is really equivalent to exchanging y and z and z sign + s = "v " + + to_string(-coordinates.x) + " " + + to_string(coordinates.z) + " " + + to_string(coordinates.y) + " " + + to_string(coordinates.w) + ; + } + else + { + s = "v " + + to_string(-coordinates.x) + " " + + to_string(coordinates.y) + " " + + to_string(-coordinates.z) + " " + + to_string(coordinates.w) + ; + } return s; } @@ -267,7 +292,7 @@ std::string block::printMtl() const return s; } -std::string block::printObj(int group_offset) const +std::string block::printObj(int group_offset, bool rotate_x_90) const { string s; s += "usemtl " + name + "\n"; @@ -277,7 +302,7 @@ std::string block::printObj(int group_offset) const s += "\n"; for (auto &v : vertices) - s += v.printVertex() + "\n"; + s += v.printVertex(rotate_x_90) + "\n"; s += "\n"; for (auto &v : vertices) s += v.printNormal() + "\n"; @@ -390,6 +415,17 @@ void block::load(const buffer &b) throw std::logic_error(s); } +bool block::canPrint() const +{ + if (type == BlockType::ParticleEmitter) + return false; + if (type != BlockType::VisibleObject) + return false; + if (!(all_lods == 15 || LODs.lod1)) + return false; + return true; +} + void model::load(const buffer &b) { READ(b, n_blocks); @@ -411,27 +447,29 @@ void model::print(const std::string &fn) o << "\n"; }; - auto obj_fn = fn + ".obj"; - ofstream o(obj_fn); - title(o); - o << "mtllib " + fn + ".mtl\n\n"; - o << "o " << fn << "\n\n"; + auto print_obj = [&](const auto &n, bool rotate_x_90 = false) + { + ofstream o(n); + title(o); + o << "mtllib " + fn + ".mtl\n\n"; + o << "o " << fn << "\n\n"; + int n_vert = 0; + for (auto &b : blocks) + { + if (!b.canPrint()) + continue; + + o << b.printObj(n_vert, rotate_x_90) << "\n"; + n_vert += b.n_vertex; + } + }; auto mtl_fn = fn + ".mtl"; ofstream m(mtl_fn); title(m); - - int n_vert = 0; for (auto &b : blocks) - { - if (b.type == BlockType::ParticleEmitter) - continue; - if (b.type != BlockType::VisibleObject) - continue; - if (!(b.all_lods == 15 || b.LODs.lod1)) - continue; - o << b.printObj(n_vert) << "\n"; m << b.printMtl() << "\n"; - n_vert += b.n_vertex; - } + + print_obj(fn + "_fbx.obj"); + print_obj(fn + "_ue4.obj", true); } diff --git a/src/mod_converter/model.h b/src/mod_converter/model.h index fa22924..51dcb17 100644 --- a/src/mod_converter/model.h +++ b/src/mod_converter/model.h @@ -26,7 +26,7 @@ class buffer; enum { - F_USE_W_COORDINATE = 0x4, + F_USE_W_COORDINATE = 0x4, // F_USE_QUANTERNION? }; enum class AdditionalParameter : uint32_t @@ -97,7 +97,7 @@ struct vertex void load(const buffer &b, uint32_t flags); - std::string printVertex() const; + std::string printVertex(bool rotate_x_90 = false) const; std::string printNormal() const; std::string printTex() const; }; @@ -236,7 +236,9 @@ struct block void load(const buffer &b); std::string printMtl() const; - std::string printObj(int group_offset) const; + std::string printObj(int group_offset, bool rotate_x_90 = false) const; + + bool canPrint() const; }; struct model