From c127b734d08abbe9c4e20168bdcdd3199ed2581d Mon Sep 17 00:00:00 2001 From: Gareth Morgan Date: Mon, 30 Mar 2020 10:51:16 -0400 Subject: [PATCH] Add TGA support (via ImageMagick conversion) --- src/FBX2glTF.cpp | 2 +- src/gltf/TextureBuilder.cpp | 56 ++++++++++++++++++++++++++++++++++--- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/FBX2glTF.cpp b/src/FBX2glTF.cpp index 7c8948d..3f545b6 100644 --- a/src/FBX2glTF.cpp +++ b/src/FBX2glTF.cpp @@ -326,7 +326,7 @@ int main(int argc, char* argv[]) { if (verboseOutput) { fmt::printf("Loading FBX File: %s\n", inputPath); } - if (!LoadFBXFile(raw, inputPath, {"png", "jpg", "jpeg"}, gltfOptions)) { + if (!LoadFBXFile(raw, inputPath, {"png", "jpg", "jpeg", "tga"}, gltfOptions)) { fmt::fprintf(stderr, "ERROR:: Failed to parse FBX: %s\n", inputPath); return 1; } diff --git a/src/gltf/TextureBuilder.cpp b/src/gltf/TextureBuilder.cpp index 7424a5a..19235f6 100644 --- a/src/gltf/TextureBuilder.cpp +++ b/src/gltf/TextureBuilder.cpp @@ -177,15 +177,61 @@ std::shared_ptr TextureBuilder::simple(int rawTexIndex, const std:: return iter->second; } - const RawTexture& rawTexture = raw.GetTexture(rawTexIndex); - const std::string textureName = FileUtils::GetFileBase(rawTexture.name); - const std::string relativeFilename = FileUtils::GetFileName(rawTexture.fileLocation); + + RawTexture rawTexture = raw.GetTexture(rawTexIndex); + std::string textureName = FileUtils::GetFileBase(rawTexture.name); + std::string relativeFilename = FileUtils::GetFileName(rawTexture.fileLocation); + auto suffix = FileUtils::GetFileSuffix(rawTexture.fileLocation); + + //TGAs are not supported by GLTF spec, convert to PNG + if(suffix.has_value() && suffix->compare("tga")==0) { + std::string tmpFolder; + if(outputFolder.empty()) + tmpFolder = "./convertedTextures"; + else + tmpFolder = outputFolder+"/convertedTextures"; + + //Check for existence of ImageMagick convert binary in path + if(std::system("convert -version")!=0) { //GAM TODO - Pipe to /dev/null or NUL to avoid console spew (also should check the output to make sure its really ImageMagick) + fmt::printf("Warning: Failed to find installed version of ImageMagick 'convert'. TGA conversion requires ImageMagick (https://imagemagick.org/) installation in system path)\n"); + //If we can't find convert set the file path to empty string, so next step will fail and set to error texture + rawTexture.fileLocation = ""; + rawTexture.name = ""; + } else { + //Ensure output folder exists + std::string mkdirCmd = "mkdir -p "+tmpFolder; + std::system(mkdirCmd.c_str()); + + std::string baseName= FileUtils::GetFileBase(relativeFilename); + std::string tmpPath = tmpFolder+"/"+baseName+".png"; + + std::string cmdStr = "convert \""+rawTexture.fileLocation+"\" \""+tmpPath+"\"";//GAM TODO - Pipe to /dev/null or NUL to avoid console spew + auto res = std::system(cmdStr.c_str()); + if(res==0) { + rawTexture.fileLocation = tmpPath; + rawTexture.name = baseName+".png"; + if (verboseOutput) { + fmt::printf("Converted TGA texture '%s' to output folder: %s\n", rawTexture.fileLocation, tmpPath); + } + } else { + //If we fail, set the file path to empty string, so next step will fail and set to error texture + fmt::printf("Warning: Failed to convert TGA:%s to %s\n", rawTexture.fileLocation, tmpPath); + rawTexture.fileLocation = ""; + rawTexture.name = ""; + } + } + + //Update the texture details + textureName = FileUtils::GetFileBase(rawTexture.name); + relativeFilename = FileUtils::GetFileName(rawTexture.fileLocation); + suffix = FileUtils::GetFileSuffix(rawTexture.fileLocation); + } ImageData* image = nullptr; + if (options.outputBinary) { auto bufferView = gltf.AddBufferViewForFile(*gltf.defaultBuffer, rawTexture.fileLocation); if (bufferView) { - const auto& suffix = FileUtils::GetFileSuffix(rawTexture.fileLocation); std::string mimeType; if (suffix) { mimeType = ImageUtils::suffixToMimeType(suffix.value()); @@ -212,6 +258,8 @@ std::shared_ptr TextureBuilder::simple(int rawTexIndex, const std:: // reference, even if the copy failed. } } + + if (!image) { // fallback is tiny transparent PNG image = new ImageData(