Skip to content

Commit

Permalink
Copy texture files to output dir when appropriate. (#23)
Browse files Browse the repository at this point in the history
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/
  • Loading branch information
zellski authored Oct 24, 2017
1 parent 5580dcf commit 72eb620
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
14 changes: 13 additions & 1 deletion src/Raw2Gltf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "FBX2glTF.h"
#include "utils/String_Utils.h"
#include "utils/Image_Utils.h"
#include <utils/File_Utils.h>
#include "RawModel.h"
#include "Raw2Gltf.h"

Expand Down Expand Up @@ -249,6 +250,7 @@ static const std::string materialHash(const RawMaterial &m) {

ModelData *Raw2Gltf(
std::ofstream &gltfOutStream,
const std::string &outputFolder,
const RawModel &raw,
const GltfOptions &options
)
Expand Down Expand Up @@ -373,8 +375,18 @@ ModelData *Raw2Gltf(
source = new ImageData(relativeFilename, *bufferView, suffixToMimeType(suffix));
}

} else {
} else if (!relativeFilename.empty()) {
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) {
// fallback is tiny transparent gif
Expand Down
1 change: 1 addition & 0 deletions src/Raw2Gltf.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ struct ModelData

ModelData *Raw2Gltf(
std::ofstream &gltfOutStream,
const std::string &outputFolder,
const RawModel &raw,
const GltfOptions &options
);
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Copyright (c) 2016-2017 Oculus VR, LLC.
if (gltfOptions.outputBinary) {
// in binary mode, we write precisely where we're asked
modelPath = outputPath + ".glb";

} else {
// in gltf mode, we create a folder and write into that
outputFolder = outputPath + "_out/";
Expand Down Expand Up @@ -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());
return 1;
}
data_render_model = Raw2Gltf(outStream, raw, gltfOptions);
data_render_model = Raw2Gltf(outStream, outputFolder, raw, gltfOptions);

if (gltfOptions.outputBinary) {
fmt::printf(
Expand Down
26 changes: 26 additions & 0 deletions src/utils/File_Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

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

#include <stdint.h>
#include <stdio.h>
Expand Down Expand Up @@ -174,4 +175,29 @@ namespace FileUtils {
}
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;
}
}
2 changes: 2 additions & 0 deletions src/utils/File_Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace FileUtils {
std::vector<std::string> ListFolderFiles(const char *folder, const char *matchExtensions);

bool CreatePath(const char *path);

bool CopyFile(const std::string &srcFilename, const std::string &dstFilename);
}

#endif // !__FILE_UTILS_H__

0 comments on commit 72eb620

Please sign in to comment.