Skip to content

Commit 72eb620

Browse files
authored
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/
1 parent 5580dcf commit 72eb620

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

src/Raw2Gltf.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "FBX2glTF.h"
1616
#include "utils/String_Utils.h"
1717
#include "utils/Image_Utils.h"
18+
#include <utils/File_Utils.h>
1819
#include "RawModel.h"
1920
#include "Raw2Gltf.h"
2021

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

250251
ModelData *Raw2Gltf(
251252
std::ofstream &gltfOutStream,
253+
const std::string &outputFolder,
252254
const RawModel &raw,
253255
const GltfOptions &options
254256
)
@@ -373,8 +375,18 @@ ModelData *Raw2Gltf(
373375
source = new ImageData(relativeFilename, *bufferView, suffixToMimeType(suffix));
374376
}
375377

376-
} else {
378+
} else if (!relativeFilename.empty()) {
377379
source = new ImageData(relativeFilename, relativeFilename);
380+
std::string outputPath = outputFolder + relativeFilename;
381+
if (FileUtils::CopyFile(texture.fileLocation, outputPath)) {
382+
if (verboseOutput) {
383+
fmt::printf("Copied texture '%s' to output folder: %s\n", textureName, outputPath);
384+
}
385+
} else {
386+
// no point commenting further on read/write error; CopyFile() does enough of that, and we
387+
// certainly want to to add an image struct to the glTF JSON, with the correct relative path
388+
// reference, even if the copy failed.
389+
}
378390
}
379391
if (!source) {
380392
// fallback is tiny transparent gif

src/Raw2Gltf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ struct ModelData
197197

198198
ModelData *Raw2Gltf(
199199
std::ofstream &gltfOutStream,
200+
const std::string &outputFolder,
200201
const RawModel &raw,
201202
const GltfOptions &options
202203
);

src/main.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ Copyright (c) 2016-2017 Oculus VR, LLC.
172172
if (gltfOptions.outputBinary) {
173173
// in binary mode, we write precisely where we're asked
174174
modelPath = outputPath + ".glb";
175+
175176
} else {
176177
// in gltf mode, we create a folder and write into that
177178
outputFolder = outputPath + "_out/";
@@ -206,7 +207,7 @@ Copyright (c) 2016-2017 Oculus VR, LLC.
206207
fmt::fprintf(stderr, "ERROR:: Couldn't open file for writing: %s\n", modelPath.c_str());
207208
return 1;
208209
}
209-
data_render_model = Raw2Gltf(outStream, raw, gltfOptions);
210+
data_render_model = Raw2Gltf(outStream, outputFolder, raw, gltfOptions);
210211

211212
if (gltfOptions.outputBinary) {
212213
fmt::printf(

src/utils/File_Utils.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <string>
1111
#include <vector>
12+
#include <fstream>
1213

1314
#include <stdint.h>
1415
#include <stdio.h>
@@ -174,4 +175,29 @@ namespace FileUtils {
174175
}
175176
return true;
176177
}
178+
179+
bool CopyFile(const std::string &srcFilename, const std::string &dstFilename) {
180+
std::ifstream srcFile(srcFilename, std::ios::binary);
181+
if (!srcFile) {
182+
fmt::printf("Warning: Couldn't open file %s for reading.\n", srcFilename);
183+
return false;
184+
}
185+
// find source file length
186+
srcFile.seekg(0, std::ios::end);
187+
std::streamsize srcSize = srcFile.tellg();
188+
srcFile.seekg(0, std::ios::beg);
189+
190+
std::ofstream dstFile(dstFilename, std::ios::binary | std::ios::trunc);
191+
if (!dstFile) {
192+
fmt::printf("Warning: Couldn't open file %s for writing.\n", srcFilename);
193+
return false;
194+
}
195+
dstFile << srcFile.rdbuf();
196+
std::streamsize dstSize = dstFile.tellp();
197+
if (srcSize == dstSize) {
198+
return true;
199+
}
200+
fmt::printf("Warning: Only copied %lu bytes to %s, when %s is %lu bytes long.\n", dstSize, dstFilename, srcFilename, srcSize);
201+
return false;
202+
}
177203
}

src/utils/File_Utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ namespace FileUtils {
1919
std::vector<std::string> ListFolderFiles(const char *folder, const char *matchExtensions);
2020

2121
bool CreatePath(const char *path);
22+
23+
bool CopyFile(const std::string &srcFilename, const std::string &dstFilename);
2224
}
2325

2426
#endif // !__FILE_UTILS_H__

0 commit comments

Comments
 (0)