diff --git a/src/BadgerConverter.cc b/src/BadgerConverter.cc index 3a3ee01..ea5858c 100644 --- a/src/BadgerConverter.cc +++ b/src/BadgerConverter.cc @@ -2,13 +2,12 @@ #include #include -#include BadgerConverter::BadgerConverter() : model() { manager = FbxManager::Create(); auto ioSettings = FbxIOSettings::Create(manager, IOSROOT); manager->SetIOSettings(ioSettings); - scene = nullptr; + scene = FbxScene::Create(manager, "ExportedScene"); model.formatVersion = "1.14.0"; } @@ -27,7 +26,6 @@ bool BadgerConverter::convertToBadger(const char* fbx, const char* outputDirecto return false; } - scene = FbxScene::Create(manager, "ExportedScene"); importer->Import(scene); importer->Destroy(); @@ -50,7 +48,7 @@ bool BadgerConverter::convertToBadger(const char* fbx, const char* outputDirecto continue; } - auto mesh = static_cast(attribute); + auto mesh = dynamic_cast(attribute); if (!exportMesh(geometry, mesh, child)) { std::cerr << "Error: failed to export mesh." << std::endl; return false; @@ -101,7 +99,7 @@ bool BadgerConverter::convertToBadger(const char* fbx, const char* outputDirecto std::filesystem::create_directories(materialDir); std::ofstream materialOutputStream; - for (auto pair : exportedMaterials) { + for (const auto& pair : exportedMaterials) { auto materialOutputPath = std::filesystem::path(materialDir); materialOutputPath /= (pair.first + ".json"); json materialJson(pair.second); @@ -224,10 +222,7 @@ bool BadgerConverter::exportMesh(Badger::Geometry& geometry, const FbxMesh* mesh auto material = node->GetMaterial(materialIndex); badgerMesh.material = material->GetName(); - if (!exportMaterial(material)) { - std::cerr << "Error: failed to export material." << std::endl; - return false; - } + exportMaterial(material); geometry.meshes.push_back(badgerMesh); diff --git a/src/BadgerModel.cc b/src/BadgerModel.cc index d3e1de2..62285ab 100644 --- a/src/BadgerModel.cc +++ b/src/BadgerModel.cc @@ -1,8 +1,6 @@ -#include #include #include -#include #include "BadgerModel.hh" @@ -12,23 +10,23 @@ namespace Badger { #pragma region Model Structs - void from_json(const nlohmann::json& j, BoneInfo& p) { + [[maybe_unused]] void from_json(const nlohmann::json& j, BoneInfo& p) { j.at("bind_pose_rotation").get_to(p.bindPoseRotation); } - void to_json(json& j, const BoneInfo& p) { + [[maybe_unused]] void to_json(json& j, const BoneInfo& p) { j = json { {"bind_pose_rotation", p.bindPoseRotation} }; } - void from_json(const nlohmann::json& j, BoneLocator& p) { + [[maybe_unused]] void from_json(const nlohmann::json& j, BoneLocator& p) { j.at("discard_scale").get_to(p.discardScale); j.at("offset").get_to(p.offset); j.at("rotation").get_to(p.rotation); } - void to_json(json& j, const BoneLocator& p) { + [[maybe_unused]] void to_json(json& j, const BoneLocator& p) { j = json { {"discard_scale", p.discardScale}, {"offset", p.offset}, @@ -36,7 +34,7 @@ namespace Badger { }; } - void from_json(const json& j, Bone& p) { + [[maybe_unused]] void from_json(const json& j, Bone& p) { j.at("\x1\x2\x3\x4\x5__").get_to(p.info); if (j.contains("locators")) j.at("locators").get_to(p.locators); j.at("name").get_to(p.name); @@ -45,7 +43,7 @@ namespace Badger { j.at("scale").get_to(p.scale); } - void to_json(json& j, const Bone& p) { + [[maybe_unused]] void to_json(json& j, const Bone& p) { j = json { {"\x1\x2\x3\x4\x5__", p.info}, {"name", p.name}, @@ -58,7 +56,7 @@ namespace Badger { j["locators"] = p.locators; } - void from_json(const json& j, Mesh& p) { + [[maybe_unused]] void from_json(const json& j, Mesh& p) { if (j.contains("indices")) j.at("indices").get_to(p.indices); if (j.contains("color_sets")) j.at("color_sets").get_to(p.colors); if (j.contains("model_name")) j.at("model_name").get_to(p.name); @@ -70,7 +68,7 @@ namespace Badger { j.at("weights").get_to(p.weights); } - void to_json(json& j, const Mesh& p) { + [[maybe_unused]] void to_json(json& j, const Mesh& p) { j = json { {"meta_material", p.material}, {"normal_sets", p.normals}, @@ -90,23 +88,23 @@ namespace Badger { j["indices"] = p.indices; } - void from_json(const json& j, GeometryDescription& p) { + [[maybe_unused]] void from_json(const json& j, GeometryDescription& p) { j.at("identifier").get_to(p.identifier); } - void to_json(json& j, const GeometryDescription& p) { + [[maybe_unused]] void to_json(json& j, const GeometryDescription& p) { j = json { {"identifier", p.identifier}, }; } - void from_json(const json& j, Geometry& p) { + [[maybe_unused]] void from_json(const json& j, Geometry& p) { j.at("bones").get_to(p.bones); j.at("meshes").get_to(p.meshes); j.at("description").get_to(p.description); } - void to_json(json& j, const Geometry& p) { + [[maybe_unused]] void to_json(json& j, const Geometry& p) { j = json { {"bones", p.bones}, {"meshes", p.meshes}, @@ -114,19 +112,19 @@ namespace Badger { }; } - void from_json(const json& j, Model& p) { + [[maybe_unused]] void from_json(const json& j, Model& p) { j.at("format_version").get_to(p.formatVersion); j.at("minecraft:geometry").get_to(p.geometry); } - void to_json(json& j, const Model& p) { + [[maybe_unused]] void to_json(json& j, const Model& p) { j = json { {"format_version", p.formatVersion}, {"minecraft:geometry", p.geometry} }; } - void from_json(const json& j, MetaMaterialTextures& p) { + [[maybe_unused]] void from_json(const json& j, MetaMaterialTextures& p) { if (j.contains("diffuseMap")) j.at("diffuseMap").get_to(p.diffuse); else p.diffuse = std::string(); @@ -140,7 +138,7 @@ namespace Badger { else p.normal = std::string(); } - void to_json(json& j, const MetaMaterialTextures& p) { + [[maybe_unused]] void to_json(json& j, const MetaMaterialTextures& p) { j = json {}; if (!p.diffuse.empty()) j["diffuseMap"] = p.diffuse; @@ -152,13 +150,13 @@ namespace Badger { j["normalMap"] = p.normal; } - void from_json(const json& j, MetaMaterialInfo& p) { + [[maybe_unused]] void from_json(const json& j, MetaMaterialInfo& p) { if (j.contains("material")) j.at("material").get_to(p.material); if (j.contains("culling")) j.at("culling").get_to(p.culling); j.at("textures").get_to(p.textures); } - void to_json(json& j, const MetaMaterialInfo& p) { + [[maybe_unused]] void to_json(json& j, const MetaMaterialInfo& p) { j = json { {"textures", p.textures} }; @@ -169,11 +167,11 @@ namespace Badger { j["culling"] = p.culling; } - void from_json(const json& j, MetaMaterial& p) { + [[maybe_unused]] void from_json(const json& j, MetaMaterial& p) { j.at("format_version").get_to(p.formatVersion); for (auto it = ++j.begin(); it != j.end(); ++it) { - auto matName = it.key(); - auto matNameDelim = matName.rfind(":"); + const auto& matName = it.key(); + auto matNameDelim = matName.rfind(':'); if (matNameDelim != std::string::npos) { p.name = matName.substr(0, matNameDelim); p.baseName = matName.substr(matNameDelim + 1); @@ -184,7 +182,7 @@ namespace Badger { } } - void to_json(json& j, const MetaMaterial& p) { + [[maybe_unused]] void to_json(json& j, const MetaMaterial& p) { j = json { {"format_version", p.formatVersion} }; @@ -196,33 +194,33 @@ namespace Badger { #pragma endregion #pragma region Entity Structs - void from_json(const json& j, Entity& p) { + [[maybe_unused]] void from_json(const json& j, Entity& p) { j.at("format_version").get_to(p.formatVersion); j.at("minecraft:client_entity").get_to(p.info); } - void to_json(json& j, const Entity& p) { + [[maybe_unused]] void to_json(json& j, const Entity& p) { j = json { {"format_version", p.formatVersion}, {"minecraft:client_entity", p.info} }; } - void from_json(const json& j, EntityInfo& p) { + [[maybe_unused]] void from_json(const json& j, EntityInfo& p) { j.at("components").get_to(p.components); } - void to_json(json& j, const EntityInfo& p) { + [[maybe_unused]] void to_json(json& j, const EntityInfo& p) { j = json { {"components", p.components} }; } - void from_json(const json& j, EntityComponents& p) { + [[maybe_unused]] void from_json(const json& j, EntityComponents& p) { if (j.contains("badger:face_animation")) p.faceAnimation = j.at("badger:face_animation").get(); if (j.contains("badger:template")) { - auto templateObj = j.at("badger:template"); + const auto& templateObj = j.at("badger:template"); if (templateObj.is_string()) { p.templates.push_back(templateObj.get()); } else { @@ -231,7 +229,7 @@ namespace Badger { } } - void to_json(json& j, const EntityComponents& p) { + [[maybe_unused]] void to_json(json& j, const EntityComponents& p) { j = json {}; if (p.faceAnimation) j["badger:face_animation"] = p.faceAnimation.value(); @@ -244,14 +242,14 @@ namespace Badger { } } - void from_json(const json& j, FaceAnimation& p) { + [[maybe_unused]] void from_json(const json& j, FaceAnimation& p) { j.at("anim_columns").get_to(p.colums); j.at("anim_rows").get_to(p.rows); j.at("blink_frame").get_to(p.blinkFrame); j.at("default_frame").get_to(p.defaultFrame); } - void to_json(json& j, const FaceAnimation& p) { + [[maybe_unused]] void to_json(json& j, const FaceAnimation& p) { j = json { {"anim_columns", p.colums}, {"anim_rows", p.rows}, diff --git a/src/FbxConverter.cc b/src/FbxConverter.cc index 655753c..abe0fc0 100644 --- a/src/FbxConverter.cc +++ b/src/FbxConverter.cc @@ -29,8 +29,7 @@ FbxConverter::~FbxConverter() { if (manager != nullptr) manager->Destroy(); - if (loader != nullptr) - delete loader; + delete loader; } bool FbxConverter::convertToFbx(const char* model, const char* output) { @@ -54,7 +53,7 @@ bool FbxConverter::convertToFbx(const char* model, const char* output) { std::cout << "Importing bones." << std::endl; - for (auto bone : geometry.bones) { + for (const auto& bone : geometry.bones) { std::cout << "Importing bone " << bone.name << std::endl; if (!importBone(bone)) { std::cerr << "Error: failed to import bone." << std::endl; @@ -65,7 +64,7 @@ bool FbxConverter::convertToFbx(const char* model, const char* output) { std::cout << "Importing meshes." << std::endl; auto meshId = 0; - for (const auto mesh : geometry.meshes) { + for (const auto& mesh : geometry.meshes) { std::cout << "Importing mesh #" << meshId << std::endl; if (!loader->getMaterial(mesh.material)) { return false; @@ -89,8 +88,8 @@ bool FbxConverter::convertToFbx(const char* model, const char* output) { } else { auto faceMaterial = it->second; auto faceAnim = entity->info.components.faceAnimation.value(); - auto widthOffset = 1.0L / faceAnim.colums; - auto heightOffset = 1.0L / faceAnim.rows; + auto widthOffset = 1.0 / faceAnim.colums; + auto heightOffset = 1.0 / faceAnim.rows; faceMaterial->FindPropertyHierarchical("Maya|uv_scale").Set(FbxVector2(widthOffset, heightOffset)); faceMaterial->FindPropertyHierarchical("Maya|uv_offset").Set(FbxVector2(widthOffset * faceAnim.defaultFrame, heightOffset * faceAnim.defaultFrame)); } @@ -158,10 +157,10 @@ bool FbxConverter::importMesh(const Badger::Mesh& meshData, size_t meshId) { mesh->InitControlPoints(meshData.positions.capacity()); auto controlPoints = mesh->GetControlPoints(); - auto i = 0; - for (auto pos : meshData.positions) { + + for (auto i = 0; i < meshData.positions.capacity(); i++) { + auto pos = meshData.positions[i]; controlPoints[i] = FbxVector4(pos[0], pos[1], pos[2]); - i++; } std::cout << "Importing polygons." << std::endl; @@ -174,7 +173,7 @@ bool FbxConverter::importMesh(const Badger::Mesh& meshData, size_t meshId) { } std::cout << "Importing normals." << std::endl; - for (auto normalSet : meshData.normals) { + for (const auto& normalSet : meshData.normals) { std::cout << "Importing Normals Set." << std::endl; if (positionsCount != normalSet.capacity()) { @@ -191,7 +190,7 @@ bool FbxConverter::importMesh(const Badger::Mesh& meshData, size_t meshId) { } std::cout << "Importing UVs." << std::endl; - for (auto uvSet : meshData.uvs) { + for (const auto& uvSet : meshData.uvs) { std::cout << "Importing UV Set." << std::endl; if (positionsCount != uvSet.capacity()) { @@ -208,7 +207,7 @@ bool FbxConverter::importMesh(const Badger::Mesh& meshData, size_t meshId) { } std::cout << "Importing colors." << std::endl; - for (auto colorSet : meshData.colors) { + for (const auto& colorSet : meshData.colors) { std::cout << "Importing color set." << std::endl; if (positionsCount != colorSet.capacity()) { @@ -307,7 +306,7 @@ bool FbxConverter::importBone(const Badger::Bone& badgerBone) { if (!badgerBone.locators.empty()) { std::cout << "Importing locators." << std::endl; - for (auto locatorPair : badgerBone.locators) { + for (const auto& locatorPair : badgerBone.locators) { auto info = locatorPair.second; auto locator = FbxNull::Create(scene, (locatorPair.first + "_locator").c_str()); auto locatorNode = FbxNode::Create(scene, locatorPair.first.c_str()); diff --git a/src/ResourceLoader.cc b/src/ResourceLoader.cc index 39b07ff..9783c84 100644 --- a/src/ResourceLoader.cc +++ b/src/ResourceLoader.cc @@ -8,7 +8,7 @@ #include ResourceLoader::ResourceLoader(const char* resourcePacksDirectory) { - for (auto entry : std::filesystem::directory_iterator(resourcePacksDirectory)) { + for (const auto& entry : std::filesystem::directory_iterator(resourcePacksDirectory)) { if (entry.is_directory()) { resourcePacks.push_back(entry); } @@ -16,21 +16,21 @@ ResourceLoader::ResourceLoader(const char* resourcePacksDirectory) { } std::optional ResourceLoader::getModel(const std::string& name) { - if (auto it = modelCache.find(name); it != modelCache.end()) + if (const auto& it = modelCache.find(name); it != modelCache.end()) return it->second; return loadModel(name); } std::optional ResourceLoader::getMaterial(const std::string& name) { - if (auto it = materialCache.find(name); it != materialCache.end()) + if (const auto& it = materialCache.find(name); it != materialCache.end()) return it->second; return loadMaterial(name); } std::optional ResourceLoader::getEntity(const std::string& name) { - if (auto it = entityCache.find(name); it != entityCache.end()) + if (const auto& it = entityCache.find(name); it != entityCache.end()) return it->second; return loadEntity(name); @@ -38,7 +38,7 @@ std::optional ResourceLoader::getEntity(const std::string& std::optional ResourceLoader::loadModel(const std::string& name) { auto filename = name + ".model.json"; - for (auto packDirectory : resourcePacks) { + for (const auto& packDirectory : resourcePacks) { auto modelPath = packDirectory / "models" / "entity" / filename; if (!std::filesystem::exists(modelPath)) { continue; @@ -76,7 +76,7 @@ std::optional ResourceLoader::loadModel(const std::string& }; modelCache.insert({name, model}); return model; - } catch (json::exception e) { + } catch (json::exception& e) { std::cerr << "Error: failed to parse json (" << e.what() << ")" << std::endl; return {}; } @@ -88,7 +88,7 @@ std::optional ResourceLoader::loadModel(const std::string& std::optional ResourceLoader::loadMaterial(const std::string& name) { auto filename = name + ".json"; - for (auto packDirectory : resourcePacks) { + for (const auto& packDirectory : resourcePacks) { auto materialPath = packDirectory / "materials" / "meta_materials" / filename; if (!std::filesystem::exists(materialPath)) { continue; @@ -106,13 +106,13 @@ std::optional ResourceLoader::loadMaterial(const std auto texturePackDirectory = packDirectory; if (!std::filesystem::exists(texturePackDirectory / material.info.textures.diffuse)) { auto texturesFound = false; - for (auto textureDirectory : resourcePacks) { + for (const auto& textureDirectory : resourcePacks) { if (std::filesystem::exists(textureDirectory / (material.info.textures.diffuse + ".png"))) { texturesFound = true; texturePackDirectory = textureDirectory; break; } - }; + } if (!texturesFound) { std::cerr << "Error: failed to find texture " << material.info.textures.diffuse << " in any resource pack." << std::endl; @@ -132,7 +132,7 @@ std::optional ResourceLoader::loadMaterial(const std // TODO: Add base material importing materialCache.insert({name, material}); return material; - } catch (json::exception e) { + } catch (json::exception& e) { std::cerr << "Error: failed to parse json (" << e.what() << ")" << std::endl; return {}; } @@ -144,7 +144,7 @@ std::optional ResourceLoader::loadMaterial(const std std::optional ResourceLoader::loadEntity(const std::string& name) { auto filename = name + ".entity.json"; - for (auto packDirectory : resourcePacks) { + for (const auto& packDirectory : resourcePacks) { auto path = packDirectory / "entity" / filename; if (!std::filesystem::exists(path)) { continue; @@ -159,7 +159,7 @@ std::optional ResourceLoader::loadEntity(const std::string try { auto entity = json::parse(file).get(); if (!entity.info.components.templates.empty()) { - for (auto parent : entity.info.components.templates) { + for (const auto& parent : entity.info.components.templates) { auto parentEntity = getEntity(parent.substr(parent.find(':') + 1)); if (!parentEntity) { return {}; @@ -170,7 +170,7 @@ std::optional ResourceLoader::loadEntity(const std::string entityCache.insert({name, entity}); return entity; - } catch (json::exception e) { + } catch (json::exception& e) { std::cerr << "Error: failed to parse json (" << e.what() << ")" << std::endl; return {}; } diff --git a/src/main.cc b/src/main.cc index 1d0b399..be0ad6e 100644 --- a/src/main.cc +++ b/src/main.cc @@ -1,19 +1,13 @@ #include -#include #include #include //#define USE_ASSIMP -#include -#include - #include "FbxConverter.hh" #include "BadgerConverter.hh" #include "AssimpConverter.hh" -#include - int main(int argc, char** argv) { std::string command; @@ -29,7 +23,6 @@ int main(int argc, char** argv) std::cout << "Usage: " << argv[0] << " " << std::endl; std::cout << "Command 'export': " << std::endl; std::cout << "Command 'import': " << std::endl; - //std::cout << "Usage: " << argv[0] << " " << std::endl; return -1; }