Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
shrinktofit committed Sep 11, 2020
0 parents commit 717e7f2
Show file tree
Hide file tree
Showing 34 changed files with 5,432 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

BasedOnStyle: LLVM

AllowShortFunctionsOnASingleLine: false

CommentPragmas: '^/ .+'

BinPackParameters: false
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

.vs/

CMakeSettings.json

out/

5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
cmake_minimum_required (VERSION 3.8)

project ("FBXInglTF")

include ("./Cli/CMakeLists.txt")
19 changes: 19 additions & 0 deletions Cli/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

include ("${CMAKE_CURRENT_LIST_DIR}/../Core/CMakeLists.txt")

add_executable (FBXInglTFCli "${CMAKE_CURRENT_LIST_DIR}/Cli.cpp")

set_target_properties (FBXInglTFCli PROPERTIES CXX_STANDARD 17)

target_include_directories (FBXInglTFCli PRIVATE ${BeeCoreIncludeDirectories})

target_link_libraries (FBXInglTFCli PRIVATE BeeCore)

find_package(clipp CONFIG REQUIRED)

target_link_libraries(FBXInglTFCli PRIVATE clipp::clipp)

add_custom_command(TARGET FBXInglTFCli POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
"${FbxSdkHome}/lib/vs2017/x64/release/libfbxsdk.dll"
$<TARGET_FILE_DIR:FBXInglTFCli>)
137 changes: 137 additions & 0 deletions Cli/Cli.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@

#include <array>
#include <clipp.h>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <numeric>
#include <bee/Converter.h>
#include <string>

std::string relativeUriBetweenPath(std::filesystem::path &from_,
std::filesystem::path &to_) {
namespace fs = std::filesystem;
auto rel = to_.lexically_relative(from_);
return std::accumulate(rel.begin(), rel.end(), std::string{},
[](const std::string &init_, const fs::path &seg_) {
return init_ + (init_.size() > 0 ? "/" : "") +
seg_.string();
});
}

int main(int argc_, char *argv_[]) {
namespace fs = std::filesystem;

std::string inputFile;
std::string outFile;
std::string fbmDir;
bee::ConvertOptions convertOptions;

auto cli = (

clipp::value("input file", inputFile),

clipp::option("--out").set(outFile).doc(
"The output path to the .gltf or .glb file. Defaults to "
"`<working-directory>/<FBX-filename-basename>.gltf`"),

clipp::option("--fbm-dir")
.set(fbmDir)
.doc("The directory to store the embedded media."),

clipp::option("--no-flip-v")
.set(convertOptions.noFlipV)
.doc("Do not flip V texture coordinates."),

clipp::option("--animation-bake-rate")
.set(convertOptions.animationBakeRate)
.doc("Animation bake rate(in FPS)."),

clipp::option("--suspected-animation-duration-limit")
.set(convertOptions.suspectedAnimationDurationLimit)
.doc("The suspected animation duration limit.")

);

if (!clipp::parse(argc_, argv_, cli)) {
std::cout << make_man_page(cli, argv_[0]);
return -1;
}
if (!fbmDir.empty()) {
convertOptions.fbmDir = fbmDir;
}

if (outFile.empty()) {
const auto inputFilePath = fs::path{inputFile};
const auto inputBaseNameNoExt = inputFilePath.stem().string();
auto outFilePath = fs::current_path() / (inputBaseNameNoExt + "_glTF") /
(inputBaseNameNoExt + ".gltf");
fs::create_directories(outFilePath.parent_path());
outFile = outFilePath.string();
}
convertOptions.out = outFile;

class MyWriter : public bee::GLTFWriter {
public:
MyWriter(std::string_view in_file_, std::string_view out_file_)
: _inFile(in_file_), _outFile(out_file_) {
}

virtual std::optional<std::string> buffer(const std::byte *data_,
std::size_t size_,
std::uint32_t index_,
bool multi_) {
auto glTFOutBaseName = fs::path(_outFile).stem();
auto bufferOutPath =
fs::path(_outFile).parent_path() /
(multi_ ? (glTFOutBaseName.string() + std::to_string(index_) + ".bin")
: (glTFOutBaseName.string() + ".bin"));
fs::create_directories(bufferOutPath.parent_path());

std::ofstream ofs(bufferOutPath, std::ios::binary);
ofs.exceptions(std::ios::badbit | std::ios::failbit);
ofs.write(reinterpret_cast<const char *>(data_), size_);
ofs.flush();

auto outFileDir = fs::path(_outFile).parent_path();
return relativeUriBetweenPath(outFileDir, bufferOutPath);
}

private:
std::string_view _inFile;
std::string_view _outFile;
bool _dataUriForBuffers;
};

MyWriter writer{inputFile, outFile};
convertOptions.useDataUriForBuffers = false;
convertOptions.writer = &writer;

const auto imageSearchDepth = 2;
const std::array<const char *, 4> searchDirName = {"texture", "textures",
"material", "materials"};
auto searchParentDir = fs::path(inputFile).parent_path();
for (int i = 0; i < imageSearchDepth && !searchParentDir.empty();
++i, searchParentDir = searchParentDir.parent_path()) {
convertOptions.textureSearch.locations.push_back(searchParentDir.string());
for (auto &name : searchDirName) {
convertOptions.textureSearch.locations.push_back(
(searchParentDir / name).string());
}
}

convertOptions.pathMode = bee::ConvertOptions::PathMode::copy;

try {
auto glTFJson = bee::convert(inputFile, convertOptions);

std::ofstream glTFJsonOStream(outFile);
glTFJsonOStream.exceptions(std::ios::badbit | std::ios::failbit);
glTFJsonOStream << glTFJson;
glTFJsonOStream.flush();
} catch (const std::exception &exception) {
std::cerr << exception.what() << "\n";
}

return 0;
}
61 changes: 61 additions & 0 deletions Core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

set (BeeCoreIncludeDirectories "${CMAKE_CURRENT_LIST_DIR}/Source")

set (BeeCoreSource
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/BEE_API.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Converter.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Converter.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/GLTFBuilder.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/GLTFBuilder.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/UntypedVertex.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/UntypedVertex.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/GLTFUtilities.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/GLTFUtilities.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/fbxsdk/ObjectDestroyer.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/fbxsdk/LayerelementAccessor.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/fbxsdk/Spreader.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.Mesh.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.BlendShape.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.Skin.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.Animation.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.Material.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/SceneConverter.Texture.cpp"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/GLTFSamplerHash.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/FbxMeshVertexLayout.h"
"${CMAKE_CURRENT_LIST_DIR}/Source/bee/Convert/DirectSpreader.h"
)

add_library (BeeCore SHARED ${BeeCoreSource})

set_target_properties (BeeCore PROPERTIES CXX_STANDARD 20)

target_compile_definitions (BeeCore PRIVATE BEE_EXPORT FBXSDK_SHARED NOMINMAX)

target_include_directories (BeeCore PRIVATE ${BeeCoreIncludeDirectories} "${CMAKE_CURRENT_LIST_DIR}/fx/include")

message (STATUS "FBXSDK: ${FbxSdkHome}")

set (FbxSdkIncludeDirectories "${FbxSdkHome}/include")
set (FbxSdkLibraries "${FbxSdkHome}/lib/vs2017/x64/release/libfbxsdk.lib")
set (FbxSdkDynLibraries "${FbxSdkHome}/lib/vs2017/x64/release/libfbxsdk.dll")

target_include_directories (BeeCore PRIVATE ${FbxSdkIncludeDirectories})

target_link_libraries (BeeCore PRIVATE ${FbxSdkLibraries})

find_package(nlohmann_json CONFIG REQUIRED)
target_link_libraries(BeeCore PRIVATE nlohmann_json nlohmann_json::nlohmann_json)

find_package(fmt CONFIG REQUIRED)
target_link_libraries(BeeCore PRIVATE fmt::fmt fmt::fmt-header-only)

find_package(skyr-url CONFIG REQUIRED)
## https://github.com/cpp-netlib/url/issues/143
set_property(TARGET skyr::skyr-url APPEND PROPERTY IMPORTED_CONFIGURATIONS DEBUG)
set_target_properties(skyr::skyr-url PROPERTIES
IMPORTED_LINK_INTERFACE_LANGUAGES_DEBUG "CXX"
IMPORTED_LOCATION_DEBUG "${_VCPKG_ROOT_DIR}/installed/x64-windows/debug/lib/skyr-urld.lib"
)
target_link_libraries(BeeCore PRIVATE skyr::skyr-url skyr::skyr-json skyr::skyr-filesystem)
8 changes: 8 additions & 0 deletions Core/Source/bee/BEE_API.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

#pragma once

#ifdef BEE_EXPORT
#define BEE_API __declspec(dllexport)
#else
#define BEE_API __declspec(dllimport)
#endif
17 changes: 17 additions & 0 deletions Core/Source/bee/Convert/DirectSpreader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#pragma once

#include <cstdint>

namespace bee {
template <typename Ty_> struct DirectSpreader {
using type = Ty_;

constexpr static std::size_t size = 1;

template <typename TargetTy_>
static void spread(const type &in_, TargetTy_ *out_) {
*out_ = static_cast<TargetTy_>(in_);
}
};
} // namespace bee
61 changes: 61 additions & 0 deletions Core/Source/bee/Convert/FbxMeshVertexLayout.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@

#pragma once

#include <cstdint>
#include <optional>
#include <bee/Convert/NeutralType.h>
#include <bee/Convert/fbxsdk/LayerelementAccessor.h>
#include <vector>

namespace bee {
template <typename Element_> struct FbxMeshAttributeLayout {
std::uint32_t offset = 0;
Element_ element;

FbxMeshAttributeLayout() = default;

FbxMeshAttributeLayout(std::uint32_t offset_, Element_ element_)
: offset(offset_), element(element_) {
}
};

struct FbxMeshVertexLayout {
std::uint32_t size = sizeof(NeutralVertexComponent) * 3;

std::optional<FbxMeshAttributeLayout<
FbxLayerElementAccessor<fbxsdk::FbxLayerElementNormal::ArrayElementType>>>
normal;

std::vector<FbxMeshAttributeLayout<
FbxLayerElementAccessor<fbxsdk::FbxLayerElementUV::ArrayElementType>>>
uvs;

std::vector<FbxMeshAttributeLayout<FbxLayerElementAccessor<
fbxsdk::FbxLayerElementVertexColor::ArrayElementType>>>
colors;

struct Skinning {
std::uint32_t channelCount;
/// <summary>
/// Layout offset.
/// </summary>
std::uint32_t joints;
/// <summary>
/// Layout offset.
/// </summary>
std::uint32_t weights;
};

std::optional<Skinning> skinning;

struct ShapeLayout {
FbxMeshAttributeLayout<fbxsdk::FbxVector4 *> constrolPoints;

std::optional<FbxMeshAttributeLayout<FbxLayerElementAccessor<
fbxsdk::FbxLayerElementNormal::ArrayElementType>>>
normal;
};

std::vector<ShapeLayout> shapes;
};
} // namespace bee
33 changes: 33 additions & 0 deletions Core/Source/bee/Convert/GLTFSamplerHash.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

#pragma once

#include <fx/gltf.h>

namespace bee {
struct GLTFSamplerKeys {
fx::gltf::Sampler::MagFilter magFilter{fx::gltf::Sampler::MagFilter::None};
fx::gltf::Sampler::MinFilter minFilter{fx::gltf::Sampler::MinFilter::None};
fx::gltf::Sampler::WrappingMode wrapS{
fx::gltf::Sampler::WrappingMode::Repeat};
fx::gltf::Sampler::WrappingMode wrapT{
fx::gltf::Sampler::WrappingMode::Repeat};

bool operator==(const GLTFSamplerKeys &rhs_) const {
return minFilter == rhs_.minFilter && magFilter == rhs_.magFilter &&
wrapS == rhs_.wrapS && wrapT == rhs_.wrapT;
}

void set(fx::gltf::Sampler &sampler_) {
sampler_.minFilter = minFilter;
sampler_.magFilter = magFilter;
sampler_.wrapS = wrapS;
sampler_.wrapT = wrapT;
}
};

struct GLTFSamplerHash {
std::size_t operator()(const GLTFSamplerKeys &sampler_) const {
return 0;
}
};
} // namespace bee
18 changes: 18 additions & 0 deletions Core/Source/bee/Convert/NeutralType.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@

#pragma once

#include <cstdint>

namespace bee {
using NeutralVertexComponent = float;

using NeutralNormalComponent = float;

using NeutralUVComponent = float;

using NeutralVertexColorComponent = float;

using NeutralVertexWeightComponent = float;

using NeutralVertexJointComponent = std::uint32_t;
} // namespace bee
Loading

0 comments on commit 717e7f2

Please sign in to comment.