Copy texture files to output dir when appropriate. (#23)

When we've successfully located a referenced texture image on the local
filesystem and we're generating non-binary, non-embedded output, copy
the source folder wholesale into the destination directory.

This means the output folder is always a full, free-standing deployment,
one that can be dragged into e.g. https://gltf-viewer.donmccurdy.com/
This commit is contained in:
Pär Winzell 2017-10-23 19:30:51 -07:00 committed by GitHub
parent 5580dcfa92
commit 72eb620d87
5 changed files with 44 additions and 2 deletions

View File

@ -15,6 +15,7 @@
#include "FBX2glTF.h" #include "FBX2glTF.h"
#include "utils/String_Utils.h" #include "utils/String_Utils.h"
#include "utils/Image_Utils.h" #include "utils/Image_Utils.h"
#include <utils/File_Utils.h>
#include "RawModel.h" #include "RawModel.h"
#include "Raw2Gltf.h" #include "Raw2Gltf.h"
@ -249,6 +250,7 @@ static const std::string materialHash(const RawMaterial &m) {
ModelData *Raw2Gltf( ModelData *Raw2Gltf(
std::ofstream &gltfOutStream, std::ofstream &gltfOutStream,
const std::string &outputFolder,
const RawModel &raw, const RawModel &raw,
const GltfOptions &options const GltfOptions &options
) )
@ -373,8 +375,18 @@ ModelData *Raw2Gltf(
source = new ImageData(relativeFilename, *bufferView, suffixToMimeType(suffix)); source = new ImageData(relativeFilename, *bufferView, suffixToMimeType(suffix));
} }
} else { } else if (!relativeFilename.empty()) {
source = new ImageData(relativeFilename, relativeFilename); source = new ImageData(relativeFilename, relativeFilename);
std::string outputPath = outputFolder + relativeFilename;
if (FileUtils::CopyFile(texture.fileLocation, outputPath)) {
if (verboseOutput) {
fmt::printf("Copied texture '%s' to output folder: %s\n", textureName, outputPath);
}
} else {
// no point commenting further on read/write error; CopyFile() does enough of that, and we
// certainly want to to add an image struct to the glTF JSON, with the correct relative path
// reference, even if the copy failed.
}
} }
if (!source) { if (!source) {
// fallback is tiny transparent gif // fallback is tiny transparent gif

View File

@ -197,6 +197,7 @@ struct ModelData
ModelData *Raw2Gltf( ModelData *Raw2Gltf(
std::ofstream &gltfOutStream, std::ofstream &gltfOutStream,
const std::string &outputFolder,
const RawModel &raw, const RawModel &raw,
const GltfOptions &options const GltfOptions &options
); );

View File

@ -172,6 +172,7 @@ Copyright (c) 2016-2017 Oculus VR, LLC.
if (gltfOptions.outputBinary) { if (gltfOptions.outputBinary) {
// in binary mode, we write precisely where we're asked // in binary mode, we write precisely where we're asked
modelPath = outputPath + ".glb"; modelPath = outputPath + ".glb";
} else { } else {
// in gltf mode, we create a folder and write into that // in gltf mode, we create a folder and write into that
outputFolder = outputPath + "_out/"; outputFolder = outputPath + "_out/";
@ -206,7 +207,7 @@ Copyright (c) 2016-2017 Oculus VR, LLC.
fmt::fprintf(stderr, "ERROR:: Couldn't open file for writing: %s\n", modelPath.c_str()); fmt::fprintf(stderr, "ERROR:: Couldn't open file for writing: %s\n", modelPath.c_str());
return 1; return 1;
} }
data_render_model = Raw2Gltf(outStream, raw, gltfOptions); data_render_model = Raw2Gltf(outStream, outputFolder, raw, gltfOptions);
if (gltfOptions.outputBinary) { if (gltfOptions.outputBinary) {
fmt::printf( fmt::printf(

View File

@ -9,6 +9,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include <fstream>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@ -174,4 +175,29 @@ namespace FileUtils {
} }
return true; return true;
} }
bool CopyFile(const std::string &srcFilename, const std::string &dstFilename) {
std::ifstream srcFile(srcFilename, std::ios::binary);
if (!srcFile) {
fmt::printf("Warning: Couldn't open file %s for reading.\n", srcFilename);
return false;
}
// find source file length
srcFile.seekg(0, std::ios::end);
std::streamsize srcSize = srcFile.tellg();
srcFile.seekg(0, std::ios::beg);
std::ofstream dstFile(dstFilename, std::ios::binary | std::ios::trunc);
if (!dstFile) {
fmt::printf("Warning: Couldn't open file %s for writing.\n", srcFilename);
return false;
}
dstFile << srcFile.rdbuf();
std::streamsize dstSize = dstFile.tellp();
if (srcSize == dstSize) {
return true;
}
fmt::printf("Warning: Only copied %lu bytes to %s, when %s is %lu bytes long.\n", dstSize, dstFilename, srcFilename, srcSize);
return false;
}
} }

View File

@ -19,6 +19,8 @@ namespace FileUtils {
std::vector<std::string> ListFolderFiles(const char *folder, const char *matchExtensions); std::vector<std::string> ListFolderFiles(const char *folder, const char *matchExtensions);
bool CreatePath(const char *path); bool CreatePath(const char *path);
bool CopyFile(const std::string &srcFilename, const std::string &dstFilename);
} }
#endif // !__FILE_UTILS_H__ #endif // !__FILE_UTILS_H__