Skip to content

Commit

Permalink
Merge pull request #818 from zeux/gltf-bufferspec
Browse files Browse the repository at this point in the history
gltfpack: Output buffers[] list after extension list
  • Loading branch information
zeux authored Dec 10, 2024
2 parents b46844f + 8a5ae9d commit d95c7fe
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions gltf/gltfpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ struct hash<std::pair<uint64_t, uint64_t> >
};
} // namespace std

static void process(cgltf_data* data, const char* input_path, const char* output_path, const char* report_path, std::vector<Mesh>& meshes, std::vector<Animation>& animations, const Settings& settings, std::string& json, std::string& bin, std::string& fallback, size_t& fallback_size)
static size_t process(cgltf_data* data, const char* input_path, const char* output_path, const char* report_path, std::vector<Mesh>& meshes, std::vector<Animation>& animations, const Settings& settings, std::string& json, std::string& bin, std::string& fallback, size_t& fallback_size)
{
if (settings.verbose)
{
Expand Down Expand Up @@ -880,6 +880,9 @@ static void process(cgltf_data* data, const char* input_path, const char* output

writeExtensions(json, extensions, sizeof(extensions) / sizeof(extensions[0]));

// buffers[] array to be inserted by the caller
size_t bufferspec_pos = json.size();

std::string json_views;
finalizeBufferViews(json_views, views, bin, settings.fallback ? &fallback : NULL, fallback_size);

Expand Down Expand Up @@ -945,6 +948,8 @@ static void process(cgltf_data* data, const char* input_path, const char* output
fprintf(stderr, "Warning: cannot save report to %s\n", report_path);
}
}

return bufferspec_pos;
}

static void writeU32(FILE* out, uint32_t data)
Expand Down Expand Up @@ -1080,7 +1085,10 @@ int gltfpack(const char* input, const char* output, const char* report, Settings

std::string json, bin, fallback;
size_t fallback_size = 0;
process(data, input, output, report, meshes, animations, settings, json, bin, fallback, fallback_size);

json += '{';
size_t bufferspec_pos = process(data, input, output, report, meshes, animations, settings, json, bin, fallback, fallback_size);
json += '}';

cgltf_free(data);

Expand All @@ -1107,13 +1115,9 @@ int gltfpack(const char* input, const char* output, const char* report, Settings
}

std::string bufferspec = getBufferSpec(getBaseName(binpath.c_str()), bin.size(), settings.fallback ? getBaseName(fbpath.c_str()) : NULL, fallback_size, settings.compress);
json.insert(bufferspec_pos, "," + bufferspec);

fprintf(outjson, "{");
fwrite(bufferspec.c_str(), bufferspec.size(), 1, outjson);
fprintf(outjson, ",");
fwrite(json.c_str(), json.size(), 1, outjson);
fprintf(outjson, "}");

fwrite(bin.c_str(), bin.size(), 1, outbin);

if (settings.fallback)
Expand Down Expand Up @@ -1145,9 +1149,7 @@ int gltfpack(const char* input, const char* output, const char* report, Settings
}

std::string bufferspec = getBufferSpec(NULL, bin.size(), settings.fallback ? getBaseName(fbpath.c_str()) : NULL, fallback_size, settings.compress);

json.insert(0, "{" + bufferspec + ",");
json.push_back('}');
json.insert(bufferspec_pos, "," + bufferspec);

while (json.size() % 4)
json.push_back(' ');
Expand Down

0 comments on commit d95c7fe

Please sign in to comment.