Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
cleanup + use const auto& in loops
Browse files Browse the repository at this point in the history
  • Loading branch information
LukeFZ committed May 12, 2023
1 parent 631bdd0 commit 4218553
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 75 deletions.
13 changes: 4 additions & 9 deletions src/BadgerConverter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

#include <iostream>
#include <fstream>
#include <algorithm>

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";
}
Expand All @@ -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();

Expand All @@ -50,7 +48,7 @@ bool BadgerConverter::convertToBadger(const char* fbx, const char* outputDirecto
continue;
}

auto mesh = static_cast<FbxMesh*>(attribute);
auto mesh = dynamic_cast<FbxMesh*>(attribute);
if (!exportMesh(geometry, mesh, child)) {
std::cerr << "Error: failed to export mesh." << std::endl;
return false;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
64 changes: 31 additions & 33 deletions src/BadgerModel.cc
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "BadgerModel.hh"

Expand All @@ -12,31 +10,31 @@ 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},
{"rotation", p.rotation}
};
}

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);
Expand All @@ -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},
Expand All @@ -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);
Expand All @@ -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},
Expand All @@ -90,43 +88,43 @@ 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},
{"description", p.description}
};
}

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();

Expand All @@ -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;
Expand All @@ -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}
};
Expand All @@ -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);
Expand All @@ -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}
};
Expand All @@ -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<Badger::FaceAnimation>();
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<std::string>());
} else {
Expand All @@ -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();
Expand All @@ -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},
Expand Down
25 changes: 12 additions & 13 deletions src/FbxConverter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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));
}
Expand Down Expand Up @@ -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;
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand All @@ -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()) {
Expand Down Expand Up @@ -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());
Expand Down
Loading

0 comments on commit 4218553

Please sign in to comment.