mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-08 11:13:50 +00:00
Compare commits
65 Commits
fix-negati
...
glb_chunk_
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
46ee8b4276 | ||
|
|
3bddc09af4 | ||
|
|
9117abb45d | ||
|
|
52ff00a384 | ||
|
|
0fa56e239c | ||
|
|
5c2dd83964 | ||
|
|
1a8814d687 | ||
|
|
666ab8661c | ||
|
|
0b31543098 | ||
|
|
81f7dbe53a | ||
|
|
544969b732 | ||
|
|
3d4150419e | ||
|
|
41e1102e57 | ||
|
|
b901dd62d8 | ||
|
|
bc03ceaac1 | ||
|
|
2c9b25642b | ||
|
|
a894904f11 | ||
|
|
912856823d | ||
|
|
1a7c7d2001 | ||
|
|
fae6543b7a | ||
|
|
6ed7c39d71 | ||
|
|
5a90ab76ee | ||
|
|
0ffdc4334a | ||
|
|
53338352f2 | ||
|
|
d642c80a86 | ||
|
|
a1952053ee | ||
|
|
cbd3885049 | ||
|
|
9471517c1e | ||
|
|
e59028d68f | ||
|
|
cfab524125 | ||
|
|
9229971bcb | ||
|
|
051149311c | ||
|
|
0c7789b1e5 | ||
|
|
51b12d2546 | ||
|
|
4749b0989d | ||
|
|
0bfcb4f49e | ||
|
|
17e6e310ce | ||
|
|
07ceb4c313 | ||
|
|
e7f1ff5c59 | ||
|
|
fd904063c9 | ||
|
|
38398cc2da | ||
|
|
ed1a294bc4 | ||
|
|
b86eb6c079 | ||
|
|
e4bc6c7bd4 | ||
|
|
13803659f8 | ||
|
|
a3d5d5d1c9 | ||
|
|
9e3d1f6db5 | ||
|
|
190382aecd | ||
|
|
c6501530e3 | ||
|
|
ed48224f4b | ||
|
|
19a41d20ec | ||
|
|
0598a20cce | ||
|
|
514167b475 | ||
|
|
0f76561ebf | ||
|
|
f3dc4acaa4 | ||
|
|
1588e75e26 | ||
|
|
d1453b5e5e | ||
|
|
7e83ef9fb4 | ||
|
|
425195bf9c | ||
|
|
ce3a30e612 | ||
|
|
8a35b8f6fe | ||
|
|
f66f3bfeb3 | ||
|
|
e29ba7c49a | ||
|
|
bc753d4ac5 | ||
|
|
001c870051 |
6
.github/workflows/c-cpp.yml
vendored
6
.github/workflows/c-cpp.yml
vendored
@@ -47,7 +47,8 @@ jobs:
|
||||
sudo apt-get install -y mingw-w64
|
||||
x86_64-w64-mingw32-g++ -std=c++11 -o loader_example loader_example.cc
|
||||
|
||||
# Windows(x64) + Visual Studio 2019 build
|
||||
# Windows(x64) + Visual Studio 2022 build
|
||||
# Assume windows-latest have VS2022 installed
|
||||
build-windows-msvc:
|
||||
|
||||
runs-on: windows-latest
|
||||
@@ -62,7 +63,8 @@ jobs:
|
||||
run: |
|
||||
mkdir build
|
||||
cd build
|
||||
cmake -G "Visual Studio 16 2019" -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=On ..
|
||||
cmake --help
|
||||
cmake -G "Visual Studio 17 2022" -A x64 -DTINYGLTF_BUILD_LOADER_EXAMPLE=On -DTINYGLTF_BUILD_GL_EXAMPLES=Off -DTINYGLTF_BUILD_VALIDATOR_EXAMPLE=On ..
|
||||
cd ..
|
||||
- name: Build
|
||||
run: cmake --build build --config Release
|
||||
|
||||
@@ -4,10 +4,12 @@ PROJECT (tinygltf)
|
||||
|
||||
SET(CMAKE_CXX_STANDARD 11)
|
||||
|
||||
option(TINYGLTF_BUILD_LOADER_EXAMPLE "Build loader_example" ON)
|
||||
option(TINYGLTF_BUILD_LOADER_EXAMPLE "Build loader_example(load glTF and dump infos)" ON)
|
||||
option(TINYGLTF_BUILD_GL_EXAMPLES "Build GL exampels(requires glfw, OpenGL, etc)" OFF)
|
||||
option(TINYGLTF_BUILD_VALIDATOR_EXAMPLE "Build validator example" OFF)
|
||||
option(TINYGLTF_BUILD_VALIDATOR_EXAMPLE "Build validator exampe" OFF)
|
||||
option(TINYGLTF_BUILD_BUILDER_EXAMPLE "Build glTF builder example" OFF)
|
||||
option(TINYGLTF_HEADER_ONLY "On: header-only mode. Off: create tinygltf library(No TINYGLTF_IMPLEMENTATION required in your project)" OFF)
|
||||
option(TINYGLTF_INSTALL "Install tinygltf files during install step. Usually set to OFF if you include tinygltf through add_subdirectory()" ON)
|
||||
|
||||
if (TINYGLTF_BUILD_LOADER_EXAMPLE)
|
||||
ADD_EXECUTABLE ( loader_example
|
||||
@@ -29,19 +31,46 @@ if (TINYGLTF_BUILD_BUILDER_EXAMPLE)
|
||||
endif (TINYGLTF_BUILD_BUILDER_EXAMPLE)
|
||||
|
||||
#
|
||||
# TinuGLTF is a header-only library, so no library build. just install header files.
|
||||
# for add_subdirectory and standalone build
|
||||
#
|
||||
INSTALL ( FILES
|
||||
json.hpp
|
||||
stb_image.h
|
||||
stb_image_write.h
|
||||
tiny_gltf.h
|
||||
DESTINATION
|
||||
include
|
||||
if (TINYGLTF_HEADER_ONLY)
|
||||
add_library(tinygltf INTERFACE)
|
||||
|
||||
target_include_directories(tinygltf
|
||||
INTERFACE
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
|
||||
INSTALL ( FILES
|
||||
cmake/TinyGLTFConfig.cmake
|
||||
DESTINATION
|
||||
cmake
|
||||
)
|
||||
else (TINYGLTF_HEADER_ONLY)
|
||||
add_library(tinygltf)
|
||||
target_sources(tinygltf PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tiny_gltf.cc)
|
||||
target_include_directories(tinygltf
|
||||
INTERFACE
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
|
||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||
)
|
||||
endif (TINYGLTF_HEADER_ONLY)
|
||||
|
||||
if (TINYGLTF_INSTALL)
|
||||
|
||||
# Do not install .lib even if !TINYGLTF_HEADER_ONLY
|
||||
|
||||
INSTALL ( FILES
|
||||
json.hpp
|
||||
stb_image.h
|
||||
stb_image_write.h
|
||||
tiny_gltf.h
|
||||
${TINYGLTF_EXTRA_SOUECES}
|
||||
DESTINATION
|
||||
include
|
||||
)
|
||||
|
||||
INSTALL ( FILES
|
||||
cmake/TinyGLTFConfig.cmake
|
||||
DESTINATION
|
||||
cmake
|
||||
)
|
||||
|
||||
endif(TINYGLTF_INSTALL)
|
||||
|
||||
32
README.md
32
README.md
@@ -3,10 +3,15 @@
|
||||
`TinyGLTF` is a header only C++11 glTF 2.0 https://github.com/KhronosGroup/glTF library.
|
||||
|
||||
`TinyGLTF` uses Niels Lohmann's json library(https://github.com/nlohmann/json), so now it requires C++11 compiler.
|
||||
(Also, you can use RadpidJSON as an JSON backend)
|
||||
If you are looking for old, C++03 version, please use `devel-picojson` branch(but not maintained anymore).
|
||||
|
||||
## Status
|
||||
|
||||
Currently TinyGLTF is stable and maintainance mode. No drastic changes and feature additions planned.
|
||||
|
||||
- v2.6.0 Support serializing sparse accessor(Thanks to @fynv).
|
||||
- v2.5.0 Add SetPreserveImageChannels() option to load image data as is.
|
||||
- v2.4.0 Experimental RapidJSON support. Experimental C++14 support(C++14 may give better performance)
|
||||
- v2.3.0 Modified Material representation according to glTF 2.0 schema(and introduced TextureInfo class)
|
||||
- v2.2.0 release(Support loading 16bit PNG. Sparse accessor support)
|
||||
@@ -15,7 +20,7 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch(bu
|
||||
|
||||
### Branches
|
||||
|
||||
* `sajson` : Use sajson to parse JSON. Parsing only but faster compile time(2x reduction compared to json.hpp and RapidJson)
|
||||
* `sajson` : Use sajson to parse JSON. Parsing only but faster compile time(2x reduction compared to json.hpp and RapidJson), but not well maintained.
|
||||
|
||||
## Builds
|
||||
|
||||
@@ -27,6 +32,8 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch(bu
|
||||
|
||||
## Features
|
||||
|
||||
Probably mostly feature-complete. Last missing feature is Draco encoding: https://github.com/syoyo/tinygltf/issues/207
|
||||
|
||||
* Written in portable C++. C++-11 with STL dependency only.
|
||||
* [x] macOS + clang(LLVM)
|
||||
* [x] iOS + clang
|
||||
@@ -92,11 +99,12 @@ In extension(`ExtensionMap`), JSON number value is parsed as int or float(number
|
||||
* [Vulkan-Samples](https://github.com/KhronosGroup/Vulkan-Samples) - The Vulkan Samples is collection of resources to help you develop optimized Vulkan applications.
|
||||
* [TDME2](https://github.com/andreasdr/tdme2) - TDME2 - ThreeDeeMiniEngine2 is a lightweight 3D engine including tools suited for 3D game development using C++11
|
||||
* [SanityEngine](https://github.com/DethRaid/SanityEngine) - A C++/D3D12 renderer focused on the personal and proessional development of its developer
|
||||
* [Open3D](http://www.open3d.org/) - A Modern Library for 3D Data Processing
|
||||
* Your projects here! (Please send PR)
|
||||
|
||||
## TODOs
|
||||
|
||||
* [ ] Write C++ code generator which emits C++ code from JSON schema for robust parsing.
|
||||
* [ ] Robust URI decoding/encoding. https://github.com/syoyo/tinygltf/issues/369
|
||||
* [ ] Mesh Compression/decompression(Open3DGC, etc)
|
||||
* [x] Load Draco compressed mesh
|
||||
* [ ] Save Draco compressed mesh
|
||||
@@ -107,6 +115,10 @@ In extension(`ExtensionMap`), JSON number value is parsed as int or float(number
|
||||
* [ ] 16bit PNG support in Serialization
|
||||
* [ ] Write example and tests for `animation` and `skin`
|
||||
|
||||
### Optional
|
||||
|
||||
* [ ] Write C++ code generator which emits C++ code from JSON schema for robust parsing?
|
||||
|
||||
## Licenses
|
||||
|
||||
TinyGLTF is licensed under MIT license.
|
||||
@@ -173,9 +185,23 @@ if (!ret) {
|
||||
* `TINYGLTF_NO_INCLUDE_RAPIDJSON `: Disable including RapidJson's header files from within `tiny_gltf.h` because it has been already included before or you want to include it using custom path before including `tiny_gltf.h`.
|
||||
* `TINYGLTF_NO_INCLUDE_STB_IMAGE `: Disable including `stb_image.h` from within `tiny_gltf.h` because it has been already included before or you want to include it using custom path before including `tiny_gltf.h`.
|
||||
* `TINYGLTF_NO_INCLUDE_STB_IMAGE_WRITE `: Disable including `stb_image_write.h` from within `tiny_gltf.h` because it has been already included before or you want to include it using custom path before including `tiny_gltf.h`.
|
||||
* `TINYGLTF_USE_RAPIDJSON` : Use RapidJSON as a JSON parser/serializer. RapidJSON files are not included in TinyGLTF repo. Please set an include path to RapidJSON if you enable this featrure.
|
||||
* `TINYGLTF_USE_RAPIDJSON` : Use RapidJSON as a JSON parser/serializer. RapidJSON files are not included in TinyGLTF repo. Please set an include path to RapidJSON if you enable this feature.
|
||||
* `TINYGLTF_USE_CPP14` : Use C++14 feature(requires C++14 compiler). This may give better performance than C++11.
|
||||
|
||||
## CMake options
|
||||
|
||||
You can add tinygltf using `add_subdirectory` feature.
|
||||
If you add tinygltf to your project using `add_subdirectory`, it would be better to set `TINYGLTF_HEADER_ONLY` on(just add an include path to tinygltf) and `TINYGLTF_INSTALL` off(Which does not install tinygltf files).
|
||||
|
||||
```
|
||||
// Your project's CMakeLists.txt
|
||||
...
|
||||
|
||||
set(TINYGLTF_HEADER_ONLY ON CACHE INTERNAL "" FORCE)
|
||||
set(TINYGLTF_INSTALL OFF CACHE INTERNAL "" FORCE)
|
||||
add_subdirectory(/path/to/tinygltf)
|
||||
```
|
||||
|
||||
|
||||
### Saving gltTF 2.0 model
|
||||
|
||||
|
||||
@@ -39,8 +39,8 @@ bool loadModel(tinygltf::Model &model, const char *filename) {
|
||||
return res;
|
||||
}
|
||||
|
||||
std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
|
||||
tinygltf::Model &model, tinygltf::Mesh &mesh) {
|
||||
void bindMesh(std::map<int, GLuint>& vbos,
|
||||
tinygltf::Model &model, tinygltf::Mesh &mesh) {
|
||||
for (size_t i = 0; i < model.bufferViews.size(); ++i) {
|
||||
const tinygltf::BufferView &bufferView = model.bufferViews[i];
|
||||
if (bufferView.target == 0) { // TODO impl drawarrays
|
||||
@@ -144,12 +144,10 @@ std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return vbos;
|
||||
}
|
||||
|
||||
// bind models
|
||||
void bindModelNodes(std::map<int, GLuint> vbos, tinygltf::Model &model,
|
||||
void bindModelNodes(std::map<int, GLuint>& vbos, tinygltf::Model &model,
|
||||
tinygltf::Node &node) {
|
||||
if ((node.mesh >= 0) && (node.mesh < model.meshes.size())) {
|
||||
bindMesh(vbos, model, model.meshes[node.mesh]);
|
||||
@@ -160,7 +158,8 @@ void bindModelNodes(std::map<int, GLuint> vbos, tinygltf::Model &model,
|
||||
bindModelNodes(vbos, model, model.nodes[node.children[i]]);
|
||||
}
|
||||
}
|
||||
GLuint bindModel(tinygltf::Model &model) {
|
||||
|
||||
std::pair<GLuint, std::map<int, GLuint>> bindModel(tinygltf::Model &model) {
|
||||
std::map<int, GLuint> vbos;
|
||||
GLuint vao;
|
||||
glGenVertexArrays(1, &vao);
|
||||
@@ -173,19 +172,29 @@ GLuint bindModel(tinygltf::Model &model) {
|
||||
}
|
||||
|
||||
glBindVertexArray(0);
|
||||
// cleanup vbos
|
||||
for (size_t i = 0; i < vbos.size(); ++i) {
|
||||
glDeleteBuffers(1, &vbos[i]);
|
||||
// cleanup vbos but do not delete index buffers yet
|
||||
for (auto it = vbos.cbegin(); it != vbos.cend();) {
|
||||
tinygltf::BufferView bufferView = model.bufferViews[it->first];
|
||||
if (bufferView.target != GL_ELEMENT_ARRAY_BUFFER) {
|
||||
glDeleteBuffers(1, &vbos[it->first]);
|
||||
vbos.erase(it++);
|
||||
}
|
||||
else {
|
||||
++it;
|
||||
}
|
||||
}
|
||||
|
||||
return vao;
|
||||
return {vao, vbos};
|
||||
}
|
||||
|
||||
void drawMesh(tinygltf::Model &model, tinygltf::Mesh &mesh) {
|
||||
void drawMesh(const std::map<int, GLuint>& vbos,
|
||||
tinygltf::Model &model, tinygltf::Mesh &mesh) {
|
||||
for (size_t i = 0; i < mesh.primitives.size(); ++i) {
|
||||
tinygltf::Primitive primitive = mesh.primitives[i];
|
||||
tinygltf::Accessor indexAccessor = model.accessors[primitive.indices];
|
||||
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbos.at(indexAccessor.bufferView));
|
||||
|
||||
glDrawElements(primitive.mode, indexAccessor.count,
|
||||
indexAccessor.componentType,
|
||||
BUFFER_OFFSET(indexAccessor.byteOffset));
|
||||
@@ -193,20 +202,22 @@ void drawMesh(tinygltf::Model &model, tinygltf::Mesh &mesh) {
|
||||
}
|
||||
|
||||
// recursively draw node and children nodes of model
|
||||
void drawModelNodes(tinygltf::Model &model, tinygltf::Node &node) {
|
||||
void drawModelNodes(const std::pair<GLuint, std::map<int, GLuint>>& vaoAndEbos,
|
||||
tinygltf::Model &model, tinygltf::Node &node) {
|
||||
if ((node.mesh >= 0) && (node.mesh < model.meshes.size())) {
|
||||
drawMesh(model, model.meshes[node.mesh]);
|
||||
drawMesh(vaoAndEbos.second, model, model.meshes[node.mesh]);
|
||||
}
|
||||
for (size_t i = 0; i < node.children.size(); i++) {
|
||||
drawModelNodes(model, model.nodes[node.children[i]]);
|
||||
drawModelNodes(vaoAndEbos, model, model.nodes[node.children[i]]);
|
||||
}
|
||||
}
|
||||
void drawModel(GLuint vao, tinygltf::Model &model) {
|
||||
glBindVertexArray(vao);
|
||||
void drawModel(const std::pair<GLuint, std::map<int, GLuint>>& vaoAndEbos,
|
||||
tinygltf::Model &model) {
|
||||
glBindVertexArray(vaoAndEbos.first);
|
||||
|
||||
const tinygltf::Scene &scene = model.scenes[model.defaultScene];
|
||||
for (size_t i = 0; i < scene.nodes.size(); ++i) {
|
||||
drawModelNodes(model, model.nodes[scene.nodes[i]]);
|
||||
drawModelNodes(vaoAndEbos, model, model.nodes[scene.nodes[i]]);
|
||||
}
|
||||
|
||||
glBindVertexArray(0);
|
||||
@@ -282,7 +293,7 @@ void displayLoop(Window &window, const std::string &filename) {
|
||||
tinygltf::Model model;
|
||||
if (!loadModel(model, filename.c_str())) return;
|
||||
|
||||
GLuint vao = bindModel(model);
|
||||
std::pair<GLuint, std::map<int, GLuint>> vaoAndEbos = bindModel(model);
|
||||
// dbgModel(model); return;
|
||||
|
||||
// Model matrix : an identity matrix (model will be at the origin)
|
||||
@@ -317,10 +328,12 @@ void displayLoop(Window &window, const std::string &filename) {
|
||||
glUniform3fv(sun_position_u, 1, &sun_position[0]);
|
||||
glUniform3fv(sun_color_u, 1, &sun_color[0]);
|
||||
|
||||
drawModel(vao, model);
|
||||
drawModel(vaoAndEbos, model);
|
||||
glfwSwapBuffers(window.window);
|
||||
glfwPollEvents();
|
||||
}
|
||||
|
||||
glDeleteVertexArrays(1, &vaoAndEbos.first);
|
||||
}
|
||||
|
||||
static void error_callback(int error, const char *description) {
|
||||
|
||||
@@ -29,7 +29,7 @@ if (DEFINED DRACO_DIR)
|
||||
# TODO(syoyo): better CMake script for draco
|
||||
add_definitions(-DTINYGLTF_ENABLE_DRACO)
|
||||
include_directories(${DRACO_DIR}/include)
|
||||
|
||||
|
||||
link_directories(${DRACO_DIR}/lib)
|
||||
set(DRACO_LIBRARY draco)
|
||||
endif ()
|
||||
@@ -49,9 +49,9 @@ add_executable(glview
|
||||
)
|
||||
|
||||
target_link_libraries ( glview
|
||||
${DRACO_LIBRARY}
|
||||
${DRACO_LIBRARY}
|
||||
${GLFW3_UNIX_LINK_LIBRARIES}
|
||||
${GLEW_LIBRARY}
|
||||
${GLEW_LIBRARIES}
|
||||
${GLFW3_glfw_LIBRARY}
|
||||
${OPENGL_gl_LIBRARY}
|
||||
${OPENGL_glu_LIBRARY}
|
||||
|
||||
@@ -771,6 +771,30 @@ static void DrawCurves(tinygltf::Scene &scene, const tinygltf::Mesh &mesh) {
|
||||
}
|
||||
#endif
|
||||
|
||||
static void QuatToAngleAxis(const std::vector<double> quaternion,
|
||||
double &outAngleDegrees,
|
||||
double *axis) {
|
||||
double qx = quaternion[0];
|
||||
double qy = quaternion[1];
|
||||
double qz = quaternion[2];
|
||||
double qw = quaternion[3];
|
||||
|
||||
double angleRadians = 2 * acos(qw);
|
||||
if (angleRadians == 0.0) {
|
||||
outAngleDegrees = 0.0;
|
||||
axis[0] = 0.0;
|
||||
axis[1] = 0.0;
|
||||
axis[2] = 1.0;
|
||||
return;
|
||||
}
|
||||
|
||||
double denom = sqrt(1-qw*qw);
|
||||
outAngleDegrees = angleRadians * 180.0 / M_PI;
|
||||
axis[0] = qx / denom;
|
||||
axis[1] = qy / denom;
|
||||
axis[2] = qz / denom;
|
||||
}
|
||||
|
||||
// Hierarchically draw nodes
|
||||
static void DrawNode(tinygltf::Model &model, const tinygltf::Node &node) {
|
||||
// Apply xform
|
||||
@@ -781,18 +805,22 @@ static void DrawNode(tinygltf::Model &model, const tinygltf::Node &node) {
|
||||
glMultMatrixd(node.matrix.data());
|
||||
} else {
|
||||
// Assume Trans x Rotate x Scale order
|
||||
if (node.scale.size() == 3) {
|
||||
glScaled(node.scale[0], node.scale[1], node.scale[2]);
|
||||
}
|
||||
|
||||
if (node.rotation.size() == 4) {
|
||||
glRotated(node.rotation[0], node.rotation[1], node.rotation[2],
|
||||
node.rotation[3]);
|
||||
}
|
||||
|
||||
if (node.translation.size() == 3) {
|
||||
glTranslated(node.translation[0], node.translation[1],
|
||||
node.translation[2]);
|
||||
}
|
||||
|
||||
if (node.rotation.size() == 4) {
|
||||
double angleDegrees;
|
||||
double axis[3];
|
||||
|
||||
QuatToAngleAxis(node.rotation, angleDegrees, axis);
|
||||
|
||||
glRotated(angleDegrees, axis[0], axis[1], axis[2]);
|
||||
}
|
||||
|
||||
if (node.scale.size() == 3) {
|
||||
glScaled(node.scale[0], node.scale[1], node.scale[2]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -524,7 +524,7 @@ static void Dump(const tinygltf::Model &model) {
|
||||
<< std::endl;
|
||||
|
||||
std::cout << Indent(1) << "channels : [ " << std::endl;
|
||||
for (size_t j = 0; i < animation.channels.size(); i++) {
|
||||
for (size_t j = 0; j < animation.channels.size(); j++) {
|
||||
std::cout << Indent(2)
|
||||
<< "sampler : " << animation.channels[j].sampler
|
||||
<< std::endl;
|
||||
|
||||
4
tiny_gltf.cc
Normal file
4
tiny_gltf.cc
Normal file
@@ -0,0 +1,4 @@
|
||||
#define TINYGLTF_IMPLEMENTATION
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#include "tiny_gltf.h"
|
||||
356
tiny_gltf.h
356
tiny_gltf.h
@@ -26,6 +26,8 @@
|
||||
// THE SOFTWARE.
|
||||
|
||||
// Version:
|
||||
// - v2.6.0 Support serializing sparse accessor(Thanks to @fynv).
|
||||
// Disable expanding file path for security(no use of awkward `wordexp` anymore).
|
||||
// - v2.5.0 Add SetPreserveImageChannels() option to load image data as is.
|
||||
// - v2.4.3 Fix null object output when when material has all default
|
||||
// parameters.
|
||||
@@ -108,7 +110,11 @@ namespace tinygltf {
|
||||
#define TINYGLTF_COMPONENT_TYPE_INT (5124)
|
||||
#define TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT (5125)
|
||||
#define TINYGLTF_COMPONENT_TYPE_FLOAT (5126)
|
||||
#define TINYGLTF_COMPONENT_TYPE_DOUBLE (5130)
|
||||
#define TINYGLTF_COMPONENT_TYPE_DOUBLE \
|
||||
(5130) // OpenGL double type. Note that some of glTF 2.0 validator does not
|
||||
// support double type even the schema seems allow any value of
|
||||
// integer:
|
||||
// https://github.com/KhronosGroup/glTF/blob/b9884a2fd45130b4d673dd6c8a706ee21ee5c5f7/specification/2.0/schema/accessor.schema.json#L22
|
||||
|
||||
#define TINYGLTF_TEXTURE_FILTER_NEAREST (9728)
|
||||
#define TINYGLTF_TEXTURE_FILTER_LINEAR (9729)
|
||||
@@ -191,14 +197,14 @@ AAssetManager *asset_manager = nullptr;
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
NULL_TYPE = 0,
|
||||
REAL_TYPE = 1,
|
||||
INT_TYPE = 2,
|
||||
BOOL_TYPE = 3,
|
||||
STRING_TYPE = 4,
|
||||
ARRAY_TYPE = 5,
|
||||
BINARY_TYPE = 6,
|
||||
OBJECT_TYPE = 7
|
||||
NULL_TYPE,
|
||||
REAL_TYPE,
|
||||
INT_TYPE,
|
||||
BOOL_TYPE,
|
||||
STRING_TYPE,
|
||||
ARRAY_TYPE,
|
||||
BINARY_TYPE,
|
||||
OBJECT_TYPE
|
||||
} Type;
|
||||
|
||||
static inline int32_t GetComponentSizeInBytes(uint32_t componentType) {
|
||||
@@ -253,7 +259,6 @@ bool DecodeDataURI(std::vector<unsigned char> *out, std::string &mime_type,
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
// Suppress warning for : static Value null_value
|
||||
// https://stackoverflow.com/questions/15708411/how-to-deal-with-global-constructor-warning-in-clang
|
||||
#pragma clang diagnostic ignored "-Wexit-time-destructors"
|
||||
#pragma clang diagnostic ignored "-Wpadded"
|
||||
#endif
|
||||
@@ -604,7 +609,7 @@ struct Sampler {
|
||||
// `magFilter`. Set -1 in TinyGLTF(issue #186)
|
||||
int minFilter =
|
||||
-1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR",
|
||||
// "NEAREST_MIPMAP_LINEAR", "LINEAR_MIPMAP_NEAREST",
|
||||
// "NEAREST_MIPMAP_NEAREST", "LINEAR_MIPMAP_NEAREST",
|
||||
// "NEAREST_MIPMAP_LINEAR", "LINEAR_MIPMAP_LINEAR"]
|
||||
int magFilter =
|
||||
-1; // optional. -1 = no filter defined. ["NEAREST", "LINEAR"]
|
||||
@@ -614,7 +619,8 @@ struct Sampler {
|
||||
int wrapT =
|
||||
TINYGLTF_TEXTURE_WRAP_REPEAT; // ["CLAMP_TO_EDGE", "MIRRORED_REPEAT",
|
||||
// "REPEAT"], default "REPEAT"
|
||||
//int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; // TinyGLTF extension. currently not used.
|
||||
// int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT; // TinyGLTF extension. currently
|
||||
// not used.
|
||||
|
||||
Value extras;
|
||||
ExtensionMap extensions;
|
||||
@@ -1303,8 +1309,10 @@ class TinyGLTF {
|
||||
///
|
||||
/// Loads glTF ASCII asset from string(memory).
|
||||
/// `length` = strlen(str);
|
||||
/// Set warning message to `warn` for example it fails to load asserts.
|
||||
/// Returns false and set error string to `err` if there's an error.
|
||||
/// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an
|
||||
/// expanded path (e.g. no tilde(`~`), no environment variables). Set warning
|
||||
/// message to `warn` for example it fails to load asserts. Returns false and
|
||||
/// set error string to `err` if there's an error.
|
||||
///
|
||||
bool LoadASCIIFromString(Model *model, std::string *err, std::string *warn,
|
||||
const char *str, const unsigned int length,
|
||||
@@ -1323,6 +1331,8 @@ class TinyGLTF {
|
||||
///
|
||||
/// Loads glTF binary asset from memory.
|
||||
/// `length` = strlen(str);
|
||||
/// `base_dir` is a search path of glTF asset(e.g. images). Path Must be an
|
||||
/// expanded path (e.g. no tilde(`~`), no environment variables).
|
||||
/// Set warning message to `warn` for example it fails to load asserts.
|
||||
/// Returns false and set error string to `err` if there's an error.
|
||||
///
|
||||
@@ -1425,6 +1435,10 @@ class TinyGLTF {
|
||||
bool preserve_image_channels_ = false; /// Default false(expand channels to
|
||||
/// RGBA) for backward compatibility.
|
||||
|
||||
// Warning & error messages
|
||||
std::string warn_;
|
||||
std::string err_;
|
||||
|
||||
FsCallbacks fs = {
|
||||
#ifndef TINYGLTF_NO_FS
|
||||
&tinygltf::FileExists, &tinygltf::ExpandFilePath,
|
||||
@@ -1605,11 +1619,11 @@ class TinyGLTF {
|
||||
|
||||
#endif
|
||||
|
||||
#elif !defined(__ANDROID__)
|
||||
#include <wordexp.h>
|
||||
#elif !defined(__ANDROID__) && !defined(__OpenBSD__)
|
||||
//#include <wordexp.h>
|
||||
#endif
|
||||
|
||||
#if defined(__sparcv9)
|
||||
#if defined(__sparcv9) || defined(__powerpc__)
|
||||
// Big endian
|
||||
#else
|
||||
#if (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || MINIZ_X86_OR_X64_CPU
|
||||
@@ -1934,9 +1948,9 @@ bool Sampler::operator==(const Sampler &other) const {
|
||||
return this->extensions == other.extensions && this->extras == other.extras &&
|
||||
this->magFilter == other.magFilter &&
|
||||
this->minFilter == other.minFilter && this->name == other.name &&
|
||||
this->wrapT == other.wrapT;
|
||||
this->wrapS == other.wrapS && this->wrapT == other.wrapT;
|
||||
|
||||
//this->wrapR == other.wrapR && this->wrapS == other.wrapS &&
|
||||
// this->wrapR == other.wrapR
|
||||
}
|
||||
bool Scene::operator==(const Scene &other) const {
|
||||
return this->extensions == other.extensions && this->extras == other.extras &&
|
||||
@@ -2040,9 +2054,10 @@ static std::string GetBaseDir(const std::string &filepath) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/8520560/get-a-file-name-from-a-path
|
||||
static std::string GetBaseFilename(const std::string &filepath) {
|
||||
return filepath.substr(filepath.find_last_of("/\\") + 1);
|
||||
auto idx = filepath.find_last_of("/\\");
|
||||
if (idx != std::string::npos) return filepath.substr(idx + 1);
|
||||
return filepath;
|
||||
}
|
||||
|
||||
std::string base64_encode(unsigned char const *, unsigned int len);
|
||||
@@ -2188,47 +2203,35 @@ std::string base64_decode(std::string const &encoded_string) {
|
||||
// TODO(syoyo): Use uriparser https://uriparser.github.io/ for stricter Uri
|
||||
// decoding?
|
||||
//
|
||||
// https://stackoverflow.com/questions/18307429/encode-decode-url-in-c
|
||||
// Uri Decoding from DLIB
|
||||
// http://dlib.net/dlib/server/server_http.cpp.html
|
||||
|
||||
// --- dlib beign ------------------------------------------------------------
|
||||
// --- dlib begin ------------------------------------------------------------
|
||||
// Copyright (C) 2003 Davis E. King (davis@dlib.net)
|
||||
// License: Boost Software License See LICENSE.txt for the full license.
|
||||
// License: Boost Software License
|
||||
// Boost Software License - Version 1.0 - August 17th, 2003
|
||||
|
||||
// Permission is hereby granted, free of charge, to any person or organization
|
||||
// obtaining a copy of the software and accompanying documentation covered by
|
||||
// this license (the "Software") to use, reproduce, display, distribute,
|
||||
// execute, and transmit the Software, and to prepare derivative works of the
|
||||
// Software, and to permit third-parties to whom the Software is furnished to
|
||||
// do so, all subject to the following:
|
||||
// The copyright notices in the Software and this entire statement, including
|
||||
// the above license grant, this restriction and the following disclaimer,
|
||||
// must be included in all copies of the Software, in whole or in part, and
|
||||
// all derivative works of the Software, unless such copies or derivative
|
||||
// works are solely in the form of machine-executable object code generated by
|
||||
// a source language processor.
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
|
||||
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
|
||||
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
|
||||
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
namespace dlib {
|
||||
|
||||
#if 0
|
||||
inline unsigned char to_hex( unsigned char x )
|
||||
{
|
||||
return x + (x > 9 ? ('A'-10) : '0');
|
||||
}
|
||||
|
||||
const std::string urlencode( const std::string& s )
|
||||
{
|
||||
std::ostringstream os;
|
||||
|
||||
for ( std::string::const_iterator ci = s.begin(); ci != s.end(); ++ci )
|
||||
{
|
||||
if ( (*ci >= 'a' && *ci <= 'z') ||
|
||||
(*ci >= 'A' && *ci <= 'Z') ||
|
||||
(*ci >= '0' && *ci <= '9') )
|
||||
{ // allowed
|
||||
os << *ci;
|
||||
}
|
||||
else if ( *ci == ' ')
|
||||
{
|
||||
os << '+';
|
||||
}
|
||||
else
|
||||
{
|
||||
os << '%' << to_hex(static_cast<unsigned char>(*ci >> 4)) << to_hex(static_cast<unsigned char>(*ci % 16));
|
||||
}
|
||||
}
|
||||
|
||||
return os.str();
|
||||
}
|
||||
#endif
|
||||
|
||||
inline unsigned char from_hex(unsigned char ch) {
|
||||
if (ch <= '9' && ch >= '0')
|
||||
ch -= '0';
|
||||
@@ -2615,6 +2618,18 @@ bool FileExists(const std::string &abs_filename, void *) {
|
||||
}
|
||||
|
||||
std::string ExpandFilePath(const std::string &filepath, void *) {
|
||||
// https://github.com/syoyo/tinygltf/issues/368
|
||||
//
|
||||
// No file path expansion in built-in FS function anymore, since glTF URI
|
||||
// should not contain tilde('~') and environment variables, and for security
|
||||
// reason(`wordexp`).
|
||||
//
|
||||
// Users need to supply `base_dir`(in `LoadASCIIFromString`,
|
||||
// `LoadBinaryFromMemory`) in expanded absolute path.
|
||||
|
||||
return filepath;
|
||||
|
||||
#if 0
|
||||
#ifdef _WIN32
|
||||
// Assume input `filepath` is encoded in UTF-8
|
||||
std::wstring wfilepath = UTF8ToWchar(filepath);
|
||||
@@ -2629,7 +2644,7 @@ std::string ExpandFilePath(const std::string &filepath, void *) {
|
||||
#else
|
||||
|
||||
#if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || \
|
||||
defined(__ANDROID__) || defined(__EMSCRIPTEN__)
|
||||
defined(__ANDROID__) || defined(__EMSCRIPTEN__) || defined(__OpenBSD__)
|
||||
// no expansion
|
||||
std::string s = filepath;
|
||||
#else
|
||||
@@ -2662,6 +2677,7 @@ std::string ExpandFilePath(const std::string &filepath, void *) {
|
||||
|
||||
return s;
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ReadWholeFile(std::vector<unsigned char> *out, std::string *err,
|
||||
@@ -3102,11 +3118,17 @@ std::string JsonToString(const json &o, int spacing = -1) {
|
||||
StringBuffer buffer;
|
||||
if (spacing == -1) {
|
||||
Writer<StringBuffer> writer(buffer);
|
||||
o.Accept(writer);
|
||||
// TODO: Better error handling.
|
||||
// https://github.com/syoyo/tinygltf/issues/332
|
||||
if (!o.Accept(writer)) {
|
||||
return "tiny_gltf::JsonToString() failed rapidjson conversion";
|
||||
}
|
||||
} else {
|
||||
PrettyWriter<StringBuffer> writer(buffer);
|
||||
writer.SetIndent(' ', uint32_t(spacing));
|
||||
o.Accept(writer);
|
||||
if (!o.Accept(writer)) {
|
||||
return "tiny_gltf::JsonToString() failed rapidjson conversion";
|
||||
}
|
||||
}
|
||||
return buffer.GetString();
|
||||
#else
|
||||
@@ -3202,6 +3224,7 @@ static bool ParseJsonAsValue(Value *ret, const json &o) {
|
||||
break;
|
||||
case json::value_t::null:
|
||||
case json::value_t::discarded:
|
||||
case json::value_t::binary:
|
||||
// default:
|
||||
break;
|
||||
}
|
||||
@@ -4225,7 +4248,9 @@ static bool ParseSparseAccessor(Accessor *accessor, std::string *err,
|
||||
accessor->sparse.isSparse = true;
|
||||
|
||||
int count = 0;
|
||||
ParseIntegerProperty(&count, err, o, "count", true);
|
||||
if (!ParseIntegerProperty(&count, err, o, "count", true, "SparseAccessor")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
json_const_iterator indices_iterator;
|
||||
json_const_iterator values_iterator;
|
||||
@@ -4243,18 +4268,24 @@ static bool ParseSparseAccessor(Accessor *accessor, std::string *err,
|
||||
const json &values_obj = GetValue(values_iterator);
|
||||
|
||||
int indices_buffer_view = 0, indices_byte_offset = 0, component_type = 0;
|
||||
ParseIntegerProperty(&indices_buffer_view, err, indices_obj, "bufferView",
|
||||
true);
|
||||
if (!ParseIntegerProperty(&indices_buffer_view, err, indices_obj,
|
||||
"bufferView", true, "SparseAccessor")) {
|
||||
return false;
|
||||
}
|
||||
ParseIntegerProperty(&indices_byte_offset, err, indices_obj, "byteOffset",
|
||||
true);
|
||||
ParseIntegerProperty(&component_type, err, indices_obj, "componentType",
|
||||
true);
|
||||
false);
|
||||
if (!ParseIntegerProperty(&component_type, err, indices_obj, "componentType",
|
||||
true, "SparseAccessor")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int values_buffer_view = 0, values_byte_offset = 0;
|
||||
ParseIntegerProperty(&values_buffer_view, err, values_obj, "bufferView",
|
||||
true);
|
||||
if (!ParseIntegerProperty(&values_buffer_view, err, values_obj, "bufferView",
|
||||
true, "SparseAccessor")) {
|
||||
return false;
|
||||
}
|
||||
ParseIntegerProperty(&values_byte_offset, err, values_obj, "byteOffset",
|
||||
true);
|
||||
false);
|
||||
|
||||
accessor->sparse.count = count;
|
||||
accessor->sparse.indices.bufferView = indices_buffer_view;
|
||||
@@ -4263,8 +4294,6 @@ static bool ParseSparseAccessor(Accessor *accessor, std::string *err,
|
||||
accessor->sparse.values.bufferView = values_buffer_view;
|
||||
accessor->sparse.values.byteOffset = values_byte_offset;
|
||||
|
||||
// todo check theses values
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -5091,12 +5120,13 @@ static bool ParseSampler(Sampler *sampler, std::string *err, const json &o,
|
||||
int magFilter = -1;
|
||||
int wrapS = TINYGLTF_TEXTURE_WRAP_REPEAT;
|
||||
int wrapT = TINYGLTF_TEXTURE_WRAP_REPEAT;
|
||||
//int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT;
|
||||
// int wrapR = TINYGLTF_TEXTURE_WRAP_REPEAT;
|
||||
ParseIntegerProperty(&minFilter, err, o, "minFilter", false);
|
||||
ParseIntegerProperty(&magFilter, err, o, "magFilter", false);
|
||||
ParseIntegerProperty(&wrapS, err, o, "wrapS", false);
|
||||
ParseIntegerProperty(&wrapT, err, o, "wrapT", false);
|
||||
//ParseIntegerProperty(&wrapR, err, o, "wrapR", false); // tinygltf extension
|
||||
// ParseIntegerProperty(&wrapR, err, o, "wrapR", false); // tinygltf
|
||||
// extension
|
||||
|
||||
// TODO(syoyo): Check the value is alloed one.
|
||||
// (e.g. we allow 9728(NEAREST), but don't allow 9727)
|
||||
@@ -5105,7 +5135,7 @@ static bool ParseSampler(Sampler *sampler, std::string *err, const json &o,
|
||||
sampler->magFilter = magFilter;
|
||||
sampler->wrapS = wrapS;
|
||||
sampler->wrapT = wrapT;
|
||||
//sampler->wrapR = wrapR;
|
||||
// sampler->wrapR = wrapR;
|
||||
|
||||
ParseExtensionsProperty(&(sampler->extensions), err, o);
|
||||
ParseExtrasProperty(&(sampler->extras), o);
|
||||
@@ -5832,13 +5862,13 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
{
|
||||
json_const_iterator it;
|
||||
if (FindMember(o, "extensions", it)) {
|
||||
model->extensions_json_string = JsonToString(GetValue(it));
|
||||
scene.extensions_json_string = JsonToString(GetValue(it));
|
||||
}
|
||||
}
|
||||
{
|
||||
json_const_iterator it;
|
||||
if (FindMember(o, "extras", it)) {
|
||||
model->extras_json_string = JsonToString(GetValue(it));
|
||||
scene.extras_json_string = JsonToString(GetValue(it));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6218,44 +6248,109 @@ bool TinyGLTF::LoadBinaryFromMemory(Model *model, std::string *err,
|
||||
|
||||
unsigned int version; // 4 bytes
|
||||
unsigned int length; // 4 bytes
|
||||
unsigned int model_length; // 4 bytes
|
||||
unsigned int model_format; // 4 bytes;
|
||||
unsigned int chunk0_length; // 4 bytes
|
||||
unsigned int chunk0_format; // 4 bytes;
|
||||
|
||||
// @todo { Endian swap for big endian machine. }
|
||||
memcpy(&version, bytes + 4, 4);
|
||||
swap4(&version);
|
||||
memcpy(&length, bytes + 8, 4);
|
||||
swap4(&length);
|
||||
memcpy(&model_length, bytes + 12, 4);
|
||||
swap4(&model_length);
|
||||
memcpy(&model_format, bytes + 16, 4);
|
||||
swap4(&model_format);
|
||||
memcpy(&chunk0_length, bytes + 12, 4); // JSON data length
|
||||
swap4(&chunk0_length);
|
||||
memcpy(&chunk0_format, bytes + 16, 4);
|
||||
swap4(&chunk0_format);
|
||||
|
||||
// https://registry.khronos.org/glTF/specs/2.0/glTF-2.0.html#binary-gltf-layout
|
||||
//
|
||||
// In case the Bin buffer is not present, the size is exactly 20 + size of
|
||||
// JSON contents,
|
||||
// so use "greater than" operator.
|
||||
if ((20 + model_length > size) || (model_length < 1) || (length > size) ||
|
||||
(20 + model_length > length) ||
|
||||
(model_format != 0x4E4F534A)) { // 0x4E4F534A = JSON format.
|
||||
//
|
||||
// https://github.com/syoyo/tinygltf/issues/372
|
||||
// Use 64bit uint to avoid integer overflow.
|
||||
uint64_t json_size = 20ull + uint64_t(chunk0_length);
|
||||
|
||||
if (json_size > std::numeric_limits<uint32_t>::max()) {
|
||||
// Do not allow 4GB or more GLB data.
|
||||
(*err) = "Invalid glTF binary. GLB data exceeds 4GB.";
|
||||
}
|
||||
|
||||
if ((json_size > uint64_t(size)) || (chunk0_length < 1) || (length > size) ||
|
||||
(json_size > uint64_t(length)) ||
|
||||
(chunk0_format != 0x4E4F534A)) { // 0x4E4F534A = JSON format.
|
||||
if (err) {
|
||||
(*err) = "Invalid glTF binary.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// Padding check
|
||||
// The start and the end of each chunk must be aligned to a 4-byte boundary.
|
||||
// No padding check for chunk0 start since its 4byte-boundary is ensured.
|
||||
if ((json_size % 4) != 0) {
|
||||
if (err) {
|
||||
(*err) = "JSON Chunk end does not aligned to a 4-byte boundary.";
|
||||
}
|
||||
}
|
||||
|
||||
// Read Chunk1 info(BIN data)
|
||||
if ((json_size + 8ull) > uint64_t(length)) {
|
||||
if (err) {
|
||||
(*err) = "Insufficient storage space for Chunk1(BIN data).";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
unsigned int chunk1_length; // 4 bytes
|
||||
unsigned int chunk1_format; // 4 bytes;
|
||||
memcpy(&chunk1_length, bytes + json_size, 4); // JSON data length
|
||||
swap4(&chunk1_length);
|
||||
memcpy(&chunk1_format, bytes + json_size + 4, 4);
|
||||
swap4(&chunk1_format);
|
||||
|
||||
if (chunk1_length < 4) {
|
||||
// TODO: Do we allow 0byte BIN data?
|
||||
if (err) {
|
||||
(*err) = "Insufficient Chunk1(BIN) data size.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((chunk1_length % 4) != 0) {
|
||||
if (err) {
|
||||
(*err) = "BIN Chunk end does not aligned to a 4-byte boundary.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (uint64_t(chunk1_length) + json_size > uint64_t(length)) {
|
||||
if (err) {
|
||||
(*err) = "BIN Chunk data length exceeds the GLB size.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
if (chunk1_format != 0x004e4942) {
|
||||
if (err) {
|
||||
(*err) = "Invlid type for chunk1 data.";
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bin_data_ = bytes + json_size +
|
||||
8; // 4 bytes (bin_buffer_length) + 4 bytes(bin_buffer_format)
|
||||
|
||||
bin_size_ = size_t(chunk1_length);
|
||||
|
||||
// Extract JSON string.
|
||||
std::string jsonString(reinterpret_cast<const char *>(&bytes[20]),
|
||||
model_length);
|
||||
chunk0_length);
|
||||
|
||||
is_binary_ = true;
|
||||
bin_data_ = bytes + 20 + model_length +
|
||||
8; // 4 bytes (buffer_length) + 4 bytes(buffer_format)
|
||||
bin_size_ =
|
||||
length - (20 + model_length); // extract header + JSON scene data.
|
||||
|
||||
bool ret = LoadFromString(model, err, warn,
|
||||
reinterpret_cast<const char *>(&bytes[20]),
|
||||
model_length, base_dir, check_sections);
|
||||
chunk0_length, base_dir, check_sections);
|
||||
if (!ret) {
|
||||
return ret;
|
||||
}
|
||||
@@ -6686,6 +6781,27 @@ static void SerializeGltfAccessor(Accessor &accessor, json &o) {
|
||||
if (accessor.extras.Type() != NULL_TYPE) {
|
||||
SerializeValue("extras", accessor.extras, o);
|
||||
}
|
||||
|
||||
// sparse
|
||||
if (accessor.sparse.isSparse)
|
||||
{
|
||||
json sparse;
|
||||
SerializeNumberProperty<int>("count", accessor.sparse.count, sparse);
|
||||
{
|
||||
json indices;
|
||||
SerializeNumberProperty<int>("bufferView", accessor.sparse.indices.bufferView, indices);
|
||||
SerializeNumberProperty<int>("byteOffset", accessor.sparse.indices.byteOffset, indices);
|
||||
SerializeNumberProperty<int>("componentType", accessor.sparse.indices.componentType, indices);
|
||||
JsonAddMember(sparse, "indices", std::move(indices));
|
||||
}
|
||||
{
|
||||
json values;
|
||||
SerializeNumberProperty<int>("bufferView", accessor.sparse.values.bufferView, values);
|
||||
SerializeNumberProperty<int>("byteOffset", accessor.sparse.values.byteOffset, values);
|
||||
JsonAddMember(sparse, "values", std::move(values));
|
||||
}
|
||||
JsonAddMember(o, "sparse", std::move(sparse));
|
||||
}
|
||||
}
|
||||
|
||||
static void SerializeGltfAnimationChannel(AnimationChannel &channel, json &o) {
|
||||
@@ -7168,7 +7284,7 @@ static void SerializeGltfSampler(Sampler &sampler, json &o) {
|
||||
if (sampler.minFilter != -1) {
|
||||
SerializeNumberProperty("minFilter", sampler.minFilter, o);
|
||||
}
|
||||
//SerializeNumberProperty("wrapR", sampler.wrapR, o);
|
||||
// SerializeNumberProperty("wrapR", sampler.wrapR, o);
|
||||
SerializeNumberProperty("wrapS", sampler.wrapS, o);
|
||||
SerializeNumberProperty("wrapT", sampler.wrapT, o);
|
||||
|
||||
@@ -7522,37 +7638,32 @@ static bool WriteGltfFile(const std::string &output,
|
||||
return WriteGltfStream(gltfFile, content);
|
||||
}
|
||||
|
||||
static void WriteBinaryGltfStream(std::ostream &stream,
|
||||
static bool WriteBinaryGltfStream(std::ostream &stream,
|
||||
const std::string &content,
|
||||
const std::vector<unsigned char> &binBuffer) {
|
||||
const std::string header = "glTF";
|
||||
const int version = 2;
|
||||
|
||||
// https://stackoverflow.com/questions/3407012/c-rounding-up-to-the-nearest-multiple-of-a-number
|
||||
auto roundUp = [](uint32_t numToRound, uint32_t multiple) {
|
||||
if (multiple == 0) return numToRound;
|
||||
|
||||
uint32_t remainder = numToRound % multiple;
|
||||
if (remainder == 0) return numToRound;
|
||||
|
||||
return numToRound + multiple - remainder;
|
||||
};
|
||||
|
||||
const uint32_t padding_size =
|
||||
roundUp(uint32_t(content.size()), 4) - uint32_t(content.size());
|
||||
const uint32_t content_size = uint32_t(content.size());
|
||||
const uint32_t binBuffer_size = uint32_t(binBuffer.size());
|
||||
// determine number of padding bytes required to ensure 4 byte alignment
|
||||
const uint32_t content_padding_size =
|
||||
content_size % 4 == 0 ? 0 : 4 - content_size % 4;
|
||||
const uint32_t bin_padding_size =
|
||||
binBuffer_size % 4 == 0 ? 0 : 4 - binBuffer_size % 4;
|
||||
|
||||
// 12 bytes for header, JSON content length, 8 bytes for JSON chunk info.
|
||||
// Chunk data must be located at 4-byte boundary.
|
||||
// Chunk data must be located at 4-byte boundary, which may require padding
|
||||
const uint32_t length =
|
||||
12 + 8 + roundUp(uint32_t(content.size()), 4) +
|
||||
(binBuffer.size() ? (8 + roundUp(uint32_t(binBuffer.size()), 4)) : 0);
|
||||
12 + 8 + content_size + content_padding_size +
|
||||
(binBuffer_size ? (8 + binBuffer_size + bin_padding_size) : 0);
|
||||
|
||||
stream.write(header.c_str(), std::streamsize(header.size()));
|
||||
stream.write(reinterpret_cast<const char *>(&version), sizeof(version));
|
||||
stream.write(reinterpret_cast<const char *>(&length), sizeof(length));
|
||||
|
||||
// JSON chunk info, then JSON data
|
||||
const uint32_t model_length = uint32_t(content.size()) + padding_size;
|
||||
const uint32_t model_length = uint32_t(content.size()) + content_padding_size;
|
||||
const uint32_t model_format = 0x4E4F534A;
|
||||
stream.write(reinterpret_cast<const char *>(&model_length),
|
||||
sizeof(model_length));
|
||||
@@ -7561,13 +7672,11 @@ static void WriteBinaryGltfStream(std::ostream &stream,
|
||||
stream.write(content.c_str(), std::streamsize(content.size()));
|
||||
|
||||
// Chunk must be multiplies of 4, so pad with spaces
|
||||
if (padding_size > 0) {
|
||||
const std::string padding = std::string(size_t(padding_size), ' ');
|
||||
if (content_padding_size > 0) {
|
||||
const std::string padding = std::string(size_t(content_padding_size), ' ');
|
||||
stream.write(padding.c_str(), std::streamsize(padding.size()));
|
||||
}
|
||||
if (binBuffer.size() > 0) {
|
||||
const uint32_t bin_padding_size =
|
||||
roundUp(uint32_t(binBuffer.size()), 4) - uint32_t(binBuffer.size());
|
||||
// BIN chunk info, then BIN data
|
||||
const uint32_t bin_length = uint32_t(binBuffer.size()) + bin_padding_size;
|
||||
const uint32_t bin_format = 0x004e4942;
|
||||
@@ -7585,9 +7694,12 @@ static void WriteBinaryGltfStream(std::ostream &stream,
|
||||
std::streamsize(padding.size()));
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Check error on stream.write
|
||||
return true;
|
||||
}
|
||||
|
||||
static void WriteBinaryGltfFile(const std::string &output,
|
||||
static bool WriteBinaryGltfFile(const std::string &output,
|
||||
const std::string &content,
|
||||
const std::vector<unsigned char> &binBuffer) {
|
||||
#ifdef _WIN32
|
||||
@@ -7605,7 +7717,7 @@ static void WriteBinaryGltfFile(const std::string &output,
|
||||
#else
|
||||
std::ofstream gltfFile(output.c_str(), std::ios::binary);
|
||||
#endif
|
||||
WriteBinaryGltfStream(gltfFile, content, binBuffer);
|
||||
return WriteBinaryGltfStream(gltfFile, content, binBuffer);
|
||||
}
|
||||
|
||||
bool TinyGLTF::WriteGltfSceneToStream(Model *model, std::ostream &stream,
|
||||
@@ -7644,7 +7756,7 @@ bool TinyGLTF::WriteGltfSceneToStream(Model *model, std::ostream &stream,
|
||||
// UpdateImageObject need baseDir but only uses it if embeddedImages is
|
||||
// enabled, since we won't write separate images when writing to a stream
|
||||
// we
|
||||
UpdateImageObject(model->images[i], dummystring, int(i), false,
|
||||
UpdateImageObject(model->images[i], dummystring, int(i), true,
|
||||
&this->WriteImageData, this->write_image_user_data_);
|
||||
SerializeGltfImage(model->images[i], image);
|
||||
JsonPushBack(images, std::move(image));
|
||||
@@ -7653,12 +7765,11 @@ bool TinyGLTF::WriteGltfSceneToStream(Model *model, std::ostream &stream,
|
||||
}
|
||||
|
||||
if (writeBinary) {
|
||||
WriteBinaryGltfStream(stream, JsonToString(output), binBuffer);
|
||||
return WriteBinaryGltfStream(stream, JsonToString(output), binBuffer);
|
||||
} else {
|
||||
WriteGltfStream(stream, JsonToString(output, prettyPrint ? 2 : -1));
|
||||
return WriteGltfStream(stream, JsonToString(output, prettyPrint ? 2 : -1));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename,
|
||||
@@ -7743,12 +7854,11 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename,
|
||||
}
|
||||
|
||||
if (writeBinary) {
|
||||
WriteBinaryGltfFile(filename, JsonToString(output), binBuffer);
|
||||
return WriteBinaryGltfFile(filename, JsonToString(output), binBuffer);
|
||||
} else {
|
||||
WriteGltfFile(filename, JsonToString(output, (prettyPrint ? 2 : -1)));
|
||||
return WriteGltfFile(filename, JsonToString(output, (prettyPrint ? 2 : -1)));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace tinygltf
|
||||
|
||||
Reference in New Issue
Block a user