mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-07-22 22:18:54 +00:00
Compare commits
28 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
98eef6ee75 | ||
|
|
c7bf800075 | ||
|
|
006ab90c67 | ||
|
|
6f7518255f | ||
|
|
d6b0d0a61f | ||
|
|
5f180aaf74 | ||
|
|
dab0daaedf | ||
|
|
0ccc8dc262 | ||
|
|
811e1d3899 | ||
|
|
f65e06c5ca | ||
|
|
f29ae1a653 | ||
|
|
3bf16e4be5 | ||
|
|
fbc4295770 | ||
|
|
edf8d5cae1 | ||
|
|
b7f2473225 | ||
|
|
ed7bf66255 | ||
|
|
caa36dd050 | ||
|
|
389d5016ad | ||
|
|
d02ad0dede | ||
|
|
8f67121deb | ||
|
|
62be8d04cf | ||
|
|
d11a4c4d71 | ||
|
|
19b806e052 | ||
|
|
e0ab69cb31 | ||
|
|
8915252407 | ||
|
|
9446f65667 | ||
|
|
fb9f709166 | ||
|
|
a63cc6373d |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -67,4 +67,5 @@ imgui.ini
|
|||||||
loader_example
|
loader_example
|
||||||
tests/tester
|
tests/tester
|
||||||
tests/tester_noexcept
|
tests/tester_noexcept
|
||||||
|
tests/issue-97.gltf
|
||||||
|
|
||||||
|
|||||||
@@ -42,6 +42,10 @@ script:
|
|||||||
- ${CC} -v
|
- ${CC} -v
|
||||||
- ${CXX} ${EXTRA_CXXFLAGS} -std=c++11 -Wall -g -o loader_example loader_example.cc
|
- ${CXX} ${EXTRA_CXXFLAGS} -std=c++11 -Wall -g -o loader_example loader_example.cc
|
||||||
- ./loader_example ./models/Cube/Cube.gltf
|
- ./loader_example ./models/Cube/Cube.gltf
|
||||||
- cd examples/raytrace
|
- cd tests
|
||||||
|
- make
|
||||||
|
- ./tester
|
||||||
|
- ./tester_noexcept
|
||||||
|
- cd ../examples/raytrace
|
||||||
- ../../premake5 gmake
|
- ../../premake5 gmake
|
||||||
- make
|
- make
|
||||||
|
|||||||
25
LICENSE.tinyktx
Normal file
25
LICENSE.tinyktx
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
BSD 2-Clause License
|
||||||
|
|
||||||
|
Copyright (c) 2019, DeanoC
|
||||||
|
All rights reserved.
|
||||||
|
|
||||||
|
Redistribution and use in source and binary forms, with or without
|
||||||
|
modification, are permitted provided that the following conditions are met:
|
||||||
|
|
||||||
|
1. Redistributions of source code must retain the above copyright notice, this
|
||||||
|
list of conditions and the following disclaimer.
|
||||||
|
|
||||||
|
2. Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
this list of conditions and the following disclaimer in the documentation
|
||||||
|
and/or other materials provided with the distribution.
|
||||||
|
|
||||||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||||
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||||
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||||
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||||
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||||
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||||
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
15
README.md
15
README.md
@@ -7,6 +7,7 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch.
|
|||||||
|
|
||||||
## Status
|
## Status
|
||||||
|
|
||||||
|
- v2.3.0 release(Support loading KTX image through tiny_ktx)
|
||||||
- v2.2.0 release(Support loading 16bit PNG. Sparse accessor support)
|
- v2.2.0 release(Support loading 16bit PNG. Sparse accessor support)
|
||||||
- v2.1.0 release(Draco support)
|
- v2.1.0 release(Draco support)
|
||||||
- v2.0.0 release(22 Aug, 2018)!
|
- v2.0.0 release(22 Aug, 2018)!
|
||||||
@@ -52,7 +53,8 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch.
|
|||||||
* [x] Image load
|
* [x] Image load
|
||||||
* [x] Image save
|
* [x] Image save
|
||||||
* Extensions
|
* Extensions
|
||||||
* [x] Draco mesh decoding
|
* [x] Draco mesh decoding(`TINYGLTF_ENABLE_DRACO` required)
|
||||||
|
* [x] KTX image support(no mipmap. `TINYGLTF_ENABLE_KTX` required)
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
@@ -66,6 +68,7 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch.
|
|||||||
* Physical based rendering with Vulkan using glTF 2.0 models https://github.com/SaschaWillems/Vulkan-glTF-PBR
|
* Physical based rendering with Vulkan using glTF 2.0 models https://github.com/SaschaWillems/Vulkan-glTF-PBR
|
||||||
* GLTF loader plugin for OGRE 2.1. Support for PBR materials via HLMS/PBS https://github.com/Ybalrid/Ogre_glTF
|
* GLTF loader plugin for OGRE 2.1. Support for PBR materials via HLMS/PBS https://github.com/Ybalrid/Ogre_glTF
|
||||||
* [TinyGltfImporter](http://doc.magnum.graphics/magnum/classMagnum_1_1Trade_1_1TinyGltfImporter.html) plugin for [Magnum](https://github.com/mosra/magnum), a lightweight and modular C++11/C++14 graphics middleware for games and data visualization.
|
* [TinyGltfImporter](http://doc.magnum.graphics/magnum/classMagnum_1_1Trade_1_1TinyGltfImporter.html) plugin for [Magnum](https://github.com/mosra/magnum), a lightweight and modular C++11/C++14 graphics middleware for games and data visualization.
|
||||||
|
* [Diligent Engine](https://github.com/DiligentGraphics/DiligentEngine) - A modern cross-platform low-level graphics library and rendering framework
|
||||||
* Your projects here! (Please send PR)
|
* Your projects here! (Please send PR)
|
||||||
|
|
||||||
## TODOs
|
## TODOs
|
||||||
@@ -80,6 +83,7 @@ If you are looking for old, C++03 version, please use `devel-picojson` branch.
|
|||||||
* [ ] OpenEXR extension through TinyEXR.
|
* [ ] OpenEXR extension through TinyEXR.
|
||||||
* [ ] 16bit PNG support in Serialization
|
* [ ] 16bit PNG support in Serialization
|
||||||
* [ ] Write example and tests for `animation` and `skin`
|
* [ ] Write example and tests for `animation` and `skin`
|
||||||
|
* [ ] mipmap support for KTX image.
|
||||||
|
|
||||||
## Licenses
|
## Licenses
|
||||||
|
|
||||||
@@ -91,12 +95,15 @@ TinyGLTF uses the following third party libraries.
|
|||||||
* base64 : Copyright (C) 2004-2008 René Nyffenegger
|
* base64 : Copyright (C) 2004-2008 René Nyffenegger
|
||||||
* stb_image.h : v2.08 - public domain image loader - [Github link](https://github.com/nothings/stb/blob/master/stb_image.h)
|
* stb_image.h : v2.08 - public domain image loader - [Github link](https://github.com/nothings/stb/blob/master/stb_image.h)
|
||||||
* stb_image_write.h : v1.09 - public domain image writer - [Github link](https://github.com/nothings/stb/blob/master/stb_image_write.h)
|
* stb_image_write.h : v1.09 - public domain image writer - [Github link](https://github.com/nothings/stb/blob/master/stb_image_write.h)
|
||||||
|
* tinyktx.h : Copyright (c) 2019, DeanoC. Licensed under 2 clause BSD license.
|
||||||
|
|
||||||
|
|
||||||
## Build and example
|
## Build and example
|
||||||
|
|
||||||
Copy `stb_image.h`, `stb_image_write.h`, `json.hpp` and `tiny_gltf.h` to your project.
|
Copy `stb_image.h`, `stb_image_write.h`, `json.hpp` and `tiny_gltf.h` to your project.
|
||||||
|
|
||||||
|
If you enable KTX support(`TINYGLTF_ENABLE_KTX`), Copy `tinyktx.h` to your project.
|
||||||
|
|
||||||
### Loading glTF 2.0 model
|
### Loading glTF 2.0 model
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
@@ -142,6 +149,8 @@ if (!ret) {
|
|||||||
* `TINYGLTF_NO_INCLUDE_JSON `: Disable including `json.hpp` 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_JSON `: Disable including `json.hpp` 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 `: 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_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_ENABLE_KTX` : Enable loading KTX images( https://www.khronos.org/opengles/sdk/tools/KTX/ ) using `tiny_ktx.h`. Supported MIME is `image/ktx` ( https://github.com/KhronosGroup/glTF/issues/835 ). See `models/Cube-KTX` for details. Application also defined `TINYKTX_IMPLEMENTATION` in **one** .cc file.
|
||||||
|
* `TINYGLTF_NO_INCLUDE_TINYKTX` : Disable including `tinyktx.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`.
|
||||||
|
|
||||||
|
|
||||||
### Saving gltTF 2.0 model
|
### Saving gltTF 2.0 model
|
||||||
@@ -180,8 +189,6 @@ $ ./tester
|
|||||||
$ ./tester_noexcept
|
$ ./tester_noexcept
|
||||||
```
|
```
|
||||||
|
|
||||||
## Third party licenses
|
## Third party licenses used in unit tests
|
||||||
|
|
||||||
* json.hpp : Licensed under the MIT License <http://opensource.org/licenses/MIT>. Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
|
|
||||||
* stb_image : Public domain.
|
|
||||||
* catch : Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. Distributed under the Boost Software License, Version 1.0.
|
* catch : Copyright (c) 2012 Two Blue Cubes Ltd. All rights reserved. Distributed under the Boost Software License, Version 1.0.
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <GL/glew.h>
|
||||||
@@ -8,14 +8,34 @@
|
|||||||
#include "shaders.h"
|
#include "shaders.h"
|
||||||
#include "window.h"
|
#include "window.h"
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma clang diagnostic ignored "-Weverything"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Inlude tinyktx.h before tiny_gltf.h
|
||||||
|
// to get TKTX_*** definitions
|
||||||
|
#define TINYKTX_IMPLEMENTATION
|
||||||
|
#include "../../tinyktx.h"
|
||||||
|
|
||||||
|
#ifdef __clang__
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
#endif
|
||||||
|
|
||||||
#define TINYGLTF_IMPLEMENTATION
|
#define TINYGLTF_IMPLEMENTATION
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
#define TINYGLTF_NOEXCEPTION
|
#define TINYGLTF_NOEXCEPTION
|
||||||
#define JSON_NOEXCEPTION
|
#define JSON_NOEXCEPTION
|
||||||
|
#define TINYGLTF_ENABLE_KTX
|
||||||
|
// tinyktx.h is already included above,
|
||||||
|
// so let tiny_gltf.h know do not include tinyktx.h anymore
|
||||||
|
#define TINYGLTF_NO_INCLUDE_TINY_KTX
|
||||||
#include "../../tiny_gltf.h"
|
#include "../../tiny_gltf.h"
|
||||||
|
|
||||||
#define BUFFER_OFFSET(i) ((char *)NULL + (i))
|
//#define BUFFER_OFFSET(i) ((char *)nullptr + (i))
|
||||||
|
#define BUFFER_OFFSET(i) \
|
||||||
|
(reinterpret_cast<void *>(i)) // TODO(syoyo): Is this right way?
|
||||||
|
|
||||||
bool loadModel(tinygltf::Model &model, const char *filename) {
|
bool loadModel(tinygltf::Model &model, const char *filename) {
|
||||||
tinygltf::TinyGLTF loader;
|
tinygltf::TinyGLTF loader;
|
||||||
@@ -39,6 +59,30 @@ bool loadModel(tinygltf::Model &model, const char *filename) {
|
|||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static bool GetOpenGLFormatFromKTX(int ktx_fmt, GLint *internal_format, GLenum *format, GLenum *type) {
|
||||||
|
#if defined(TINYGLTF_ENABLE_KTX)
|
||||||
|
bool ret = true;
|
||||||
|
|
||||||
|
std::cout << "fmt = " << ktx_fmt << ", rgb8 fmt = " << TKTX_R8G8B8_UNORM << "\n";
|
||||||
|
|
||||||
|
if (ktx_fmt == TKTX_R8G8B8_UNORM) {
|
||||||
|
(*internal_format) = GL_RGB;
|
||||||
|
(*format) = GL_RGB;
|
||||||
|
(*type) = GL_UNSIGNED_BYTE;
|
||||||
|
} else {
|
||||||
|
// TODO(syoyo): Support more KTX formats.
|
||||||
|
ret = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret;
|
||||||
|
#else
|
||||||
|
(void)fmt;
|
||||||
|
(void)internal_format;
|
||||||
|
(void)internal_format;
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
|
std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
|
||||||
tinygltf::Model &model, tinygltf::Mesh &mesh) {
|
tinygltf::Model &model, tinygltf::Mesh &mesh) {
|
||||||
for (size_t i = 0; i < model.bufferViews.size(); ++i) {
|
for (size_t i = 0; i < model.bufferViews.size(); ++i) {
|
||||||
@@ -51,8 +95,8 @@ std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
|
|||||||
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0
|
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0
|
||||||
... drawArrays function should be used with a count equal to
|
... drawArrays function should be used with a count equal to
|
||||||
the count property of any of the accessors referenced by the
|
the count property of any of the accessors referenced by the
|
||||||
attributes property (they are all equal for a given
|
attributes property (they are all equal for a
|
||||||
primitive).
|
given primitive).
|
||||||
*/
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,7 +141,7 @@ std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
|
|||||||
accessor.normalized ? GL_TRUE : GL_FALSE,
|
accessor.normalized ? GL_TRUE : GL_FALSE,
|
||||||
byteStride, BUFFER_OFFSET(accessor.byteOffset));
|
byteStride, BUFFER_OFFSET(accessor.byteOffset));
|
||||||
} else
|
} else
|
||||||
std::cout << "vaa missing: " << attrib.first << std::endl;
|
std::cout << "Unsupported vertex attribute: " << attrib.first << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
GLuint texid;
|
GLuint texid;
|
||||||
@@ -113,29 +157,54 @@ std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
|
|||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
||||||
|
|
||||||
|
GLint internal_format = GL_RGBA;
|
||||||
GLenum format = GL_RGBA;
|
GLenum format = GL_RGBA;
|
||||||
|
|
||||||
if (image.component == 1) {
|
|
||||||
format = GL_RED;
|
|
||||||
} else if (image.component == 2) {
|
|
||||||
format = GL_RG;
|
|
||||||
} else if (image.component == 3) {
|
|
||||||
format = GL_RGB;
|
|
||||||
} else {
|
|
||||||
// ???
|
|
||||||
}
|
|
||||||
|
|
||||||
GLenum type = GL_UNSIGNED_BYTE;
|
GLenum type = GL_UNSIGNED_BYTE;
|
||||||
if (image.bits == 8) {
|
|
||||||
// ok
|
bool valid = false;
|
||||||
} else if (image.bits == 16) {
|
|
||||||
type = GL_UNSIGNED_SHORT;
|
// KTX extension
|
||||||
|
if (image.extras.Has("ktx_format")) {
|
||||||
|
|
||||||
|
valid = GetOpenGLFormatFromKTX(image.extras.Get("ktx_format").Get<int>(), &internal_format, &format, &type);
|
||||||
|
|
||||||
|
if (valid) {
|
||||||
|
|
||||||
|
std::cout << "ktx_format: " << image.extras.Get("ktx_format").Get<int>() << std::endl;
|
||||||
|
std::cout << "ktx image size: " << image.width << ", " << image.height << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// ???
|
|
||||||
|
valid = true;
|
||||||
|
|
||||||
|
if (image.component == 1) {
|
||||||
|
format = GL_RED;
|
||||||
|
} else if (image.component == 2) {
|
||||||
|
format = GL_RG;
|
||||||
|
} else if (image.component == 3) {
|
||||||
|
format = GL_RGB;
|
||||||
|
} else if (image.component == 4) {
|
||||||
|
format = GL_RGBA;
|
||||||
|
} else {
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (image.bits == 8) {
|
||||||
|
type = GL_UNSIGNED_BYTE;
|
||||||
|
} else if (image.bits == 16) {
|
||||||
|
type = GL_UNSIGNED_SHORT;
|
||||||
|
} else {
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0,
|
if (valid) {
|
||||||
format, type, &image.image.at(0));
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, image.width, image.height, 0,
|
||||||
|
format, type, &image.image.at(0));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return vbos;
|
return vbos;
|
||||||
@@ -314,6 +383,14 @@ static void error_callback(int error, const char *description) {
|
|||||||
fprintf(stderr, "Error: %s\n", description);
|
fprintf(stderr, "Error: %s\n", description);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void drop_callback(GLFWwindow *window, int num, const char **paths) {
|
||||||
|
(void)window;
|
||||||
|
printf("dropCB %d\n", num);
|
||||||
|
for (int i = 0; i < num; i++) {
|
||||||
|
printf("%s\n", paths[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
std::string filename = "../../models/Cube/Cube.gltf";
|
std::string filename = "../../models/Cube/Cube.gltf";
|
||||||
|
|
||||||
@@ -325,17 +402,26 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
if (!glfwInit()) return -1;
|
if (!glfwInit()) return -1;
|
||||||
|
|
||||||
// Force create OpenGL 3.3
|
// NOTE(syoyo): For some reason, Linux + NVIDIA driver + apt-installed
|
||||||
// NOTE(syoyo): Linux + NVIDIA driver segfaults for some reason? commenting out glfwWindowHint will work.
|
// glew(1.13) cannot initialize some ARB functions when CONTEXT_VERSION are
|
||||||
|
// explicitly given. Proably we need to compile app with recent glfw and
|
||||||
|
// glew(or use glad) package
|
||||||
|
#if !defined(__linux__)
|
||||||
|
// Try to create OpenGL 3.3 context on Windows and macOS
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
#ifdef __APPLE__
|
|
||||||
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
|
#ifdef __APPLE__
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
Window window = Window(800, 600, "TinyGLTF basic example");
|
Window window = Window(800, 600, "TinyGLTF basic example");
|
||||||
|
|
||||||
|
glfwSetDropCallback(window.window, drop_callback);
|
||||||
|
|
||||||
glfwMakeContextCurrent(window.window);
|
glfwMakeContextCurrent(window.window);
|
||||||
|
|
||||||
#ifdef __APPLE__
|
#ifdef __APPLE__
|
||||||
@@ -343,10 +429,19 @@ int main(int argc, char **argv) {
|
|||||||
glewExperimental = GL_TRUE;
|
glewExperimental = GL_TRUE;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
glewInit();
|
if (glewInit() != GLEW_OK) {
|
||||||
|
std::cerr << "Failed to initialie glew." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
std::cout << glGetString(GL_RENDERER) << ", " << glGetString(GL_VERSION)
|
std::cout << glGetString(GL_RENDERER) << ", " << glGetString(GL_VERSION)
|
||||||
<< std::endl;
|
<< std::endl;
|
||||||
|
|
||||||
|
if (!GLEW_ARB_vertex_array_object) {
|
||||||
|
std::cerr << "GLEW_ARB_vertex_array_object was not available." << std::endl;
|
||||||
|
return EXIT_FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
if (!GLEW_VERSION_3_3) {
|
if (!GLEW_VERSION_3_3) {
|
||||||
std::cerr << "OpenGL 3.3 is required to execute this app." << std::endl;
|
std::cerr << "OpenGL 3.3 is required to execute this app." << std::endl;
|
||||||
return EXIT_FAILURE;
|
return EXIT_FAILURE;
|
||||||
|
|||||||
67
models/BoundsChecking/integer-out-of-bounds.gltf
Normal file
67
models/BoundsChecking/integer-out-of-bounds.gltf
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
{
|
||||||
|
"scenes": [
|
||||||
|
{
|
||||||
|
"nodes": [0]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nodes": [
|
||||||
|
{
|
||||||
|
"mesh": 0
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"meshes": [
|
||||||
|
{
|
||||||
|
"primitives": [
|
||||||
|
{
|
||||||
|
"attributes": {
|
||||||
|
"POSITION": 1
|
||||||
|
},
|
||||||
|
"indices": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"buffers": [
|
||||||
|
{
|
||||||
|
"uri": "simpleTriangle.bin",
|
||||||
|
"byteLength": 44
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bufferViews": [
|
||||||
|
{
|
||||||
|
"buffer": 0,
|
||||||
|
"byteOffset": 0,
|
||||||
|
"byteLength": 1e300,
|
||||||
|
"target": 34963
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer": 0,
|
||||||
|
"byteOffset": 8,
|
||||||
|
"byteLength": 36,
|
||||||
|
"target": 34962
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"accessors": [
|
||||||
|
{
|
||||||
|
"bufferView": 0,
|
||||||
|
"byteOffset": 0,
|
||||||
|
"componentType": 5123,
|
||||||
|
"count": 3,
|
||||||
|
"type": "SCALAR",
|
||||||
|
"max": [2],
|
||||||
|
"min": [0]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView": 1,
|
||||||
|
"byteOffset": 0,
|
||||||
|
"componentType": 5126,
|
||||||
|
"count": 3,
|
||||||
|
"type": "VEC3",
|
||||||
|
"max": [1, 1, 0],
|
||||||
|
"min": [0, 0, 0]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"asset": {
|
||||||
|
"version": "2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
53
models/BoundsChecking/invalid-buffer-index.gltf
Normal file
53
models/BoundsChecking/invalid-buffer-index.gltf
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
{
|
||||||
|
"scenes": [],
|
||||||
|
"nodes": [],
|
||||||
|
"meshes": [
|
||||||
|
{
|
||||||
|
"primitives": [
|
||||||
|
{
|
||||||
|
"attributes": {},
|
||||||
|
"indices": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"buffers": [
|
||||||
|
{
|
||||||
|
"uri": "simpleTriangle.bin",
|
||||||
|
"byteLength": 44
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bufferViews": [
|
||||||
|
{
|
||||||
|
"buffer": 0,
|
||||||
|
"byteOffset": 0,
|
||||||
|
"byteLength": 6,
|
||||||
|
"target": 34963
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer": 1,
|
||||||
|
"byteOffset": 0,
|
||||||
|
"byteLength": 6,
|
||||||
|
"target": 34963
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"images": [
|
||||||
|
{
|
||||||
|
"bufferView": 1,
|
||||||
|
"mimeType": "image/png"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"accessors": [
|
||||||
|
{
|
||||||
|
"bufferView": 0,
|
||||||
|
"componentType": 5123,
|
||||||
|
"count": 3,
|
||||||
|
"type": "SCALAR",
|
||||||
|
"max": [2],
|
||||||
|
"min": [0]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"asset": {
|
||||||
|
"version": "2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
36
models/BoundsChecking/invalid-buffer-view-index.gltf
Normal file
36
models/BoundsChecking/invalid-buffer-view-index.gltf
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"scenes": [],
|
||||||
|
"nodes": [],
|
||||||
|
"buffers": [],
|
||||||
|
"meshes": [
|
||||||
|
{
|
||||||
|
"primitives": [
|
||||||
|
{
|
||||||
|
"attributes": {},
|
||||||
|
"indices": 0
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bufferViews": [
|
||||||
|
{
|
||||||
|
"buffer": 0,
|
||||||
|
"byteOffset": 0,
|
||||||
|
"byteLength": 6,
|
||||||
|
"target": 34963
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"accessors": [
|
||||||
|
{
|
||||||
|
"bufferView": 1,
|
||||||
|
"componentType": 5123,
|
||||||
|
"count": 3,
|
||||||
|
"type": "SCALAR",
|
||||||
|
"max": [2],
|
||||||
|
"min": [0]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"asset": {
|
||||||
|
"version": "2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
36
models/BoundsChecking/invalid-primitive-indices.gltf
Normal file
36
models/BoundsChecking/invalid-primitive-indices.gltf
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"scenes": [],
|
||||||
|
"nodes": [],
|
||||||
|
"buffers": [],
|
||||||
|
"meshes": [
|
||||||
|
{
|
||||||
|
"primitives": [
|
||||||
|
{
|
||||||
|
"attributes": {},
|
||||||
|
"indices": 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"bufferViews": [
|
||||||
|
{
|
||||||
|
"buffer": 0,
|
||||||
|
"byteOffset": 0,
|
||||||
|
"byteLength": 6,
|
||||||
|
"target": 34963
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"accessors": [
|
||||||
|
{
|
||||||
|
"bufferView": 1,
|
||||||
|
"componentType": 5123,
|
||||||
|
"count": 3,
|
||||||
|
"type": "SCALAR",
|
||||||
|
"max": [2],
|
||||||
|
"min": [0]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"asset": {
|
||||||
|
"version": "2.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
models/BoundsChecking/simpleTriangle.bin
Normal file
BIN
models/BoundsChecking/simpleTriangle.bin
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 44 B |
BIN
models/Cube-KTX/Cube.bin
Normal file
BIN
models/Cube-KTX/Cube.bin
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.8 KiB |
193
models/Cube-KTX/Cube.gltf
Normal file
193
models/Cube-KTX/Cube.gltf
Normal file
@@ -0,0 +1,193 @@
|
|||||||
|
{
|
||||||
|
"accessors" : [
|
||||||
|
{
|
||||||
|
"bufferView" : 0,
|
||||||
|
"byteOffset" : 0,
|
||||||
|
"componentType" : 5123,
|
||||||
|
"count" : 36,
|
||||||
|
"max" : [
|
||||||
|
35
|
||||||
|
],
|
||||||
|
"min" : [
|
||||||
|
0
|
||||||
|
],
|
||||||
|
"type" : "SCALAR"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView" : 1,
|
||||||
|
"byteOffset" : 0,
|
||||||
|
"componentType" : 5126,
|
||||||
|
"count" : 36,
|
||||||
|
"max" : [
|
||||||
|
1.000000,
|
||||||
|
1.000000,
|
||||||
|
1.000001
|
||||||
|
],
|
||||||
|
"min" : [
|
||||||
|
-1.000000,
|
||||||
|
-1.000000,
|
||||||
|
-1.000000
|
||||||
|
],
|
||||||
|
"type" : "VEC3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView" : 2,
|
||||||
|
"byteOffset" : 0,
|
||||||
|
"componentType" : 5126,
|
||||||
|
"count" : 36,
|
||||||
|
"max" : [
|
||||||
|
1.000000,
|
||||||
|
1.000000,
|
||||||
|
1.000000
|
||||||
|
],
|
||||||
|
"min" : [
|
||||||
|
-1.000000,
|
||||||
|
-1.000000,
|
||||||
|
-1.000000
|
||||||
|
],
|
||||||
|
"type" : "VEC3"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView" : 3,
|
||||||
|
"byteOffset" : 0,
|
||||||
|
"componentType" : 5126,
|
||||||
|
"count" : 36,
|
||||||
|
"max" : [
|
||||||
|
1.000000,
|
||||||
|
-0.000000,
|
||||||
|
-0.000000,
|
||||||
|
1.000000
|
||||||
|
],
|
||||||
|
"min" : [
|
||||||
|
0.000000,
|
||||||
|
-0.000000,
|
||||||
|
-1.000000,
|
||||||
|
-1.000000
|
||||||
|
],
|
||||||
|
"type" : "VEC4"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"bufferView" : 4,
|
||||||
|
"byteOffset" : 0,
|
||||||
|
"componentType" : 5126,
|
||||||
|
"count" : 36,
|
||||||
|
"max" : [
|
||||||
|
1.000000,
|
||||||
|
1.000000
|
||||||
|
],
|
||||||
|
"min" : [
|
||||||
|
-1.000000,
|
||||||
|
-1.000000
|
||||||
|
],
|
||||||
|
"type" : "VEC2"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"asset" : {
|
||||||
|
"generator" : "VKTS glTF 2.0 exporter",
|
||||||
|
"version" : "2.0"
|
||||||
|
},
|
||||||
|
"bufferViews" : [
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 72,
|
||||||
|
"byteOffset" : 0,
|
||||||
|
"target" : 34963
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 432,
|
||||||
|
"byteOffset" : 72,
|
||||||
|
"target" : 34962
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 432,
|
||||||
|
"byteOffset" : 504,
|
||||||
|
"target" : 34962
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 576,
|
||||||
|
"byteOffset" : 936,
|
||||||
|
"target" : 34962
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"buffer" : 0,
|
||||||
|
"byteLength" : 288,
|
||||||
|
"byteOffset" : 1512,
|
||||||
|
"target" : 34962
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"buffers" : [
|
||||||
|
{
|
||||||
|
"byteLength" : 1800,
|
||||||
|
"uri" : "Cube.bin"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"images" : [
|
||||||
|
{
|
||||||
|
"uri" : "Cube_BaseColor.ktx"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"uri" : "Cube_MetallicRoughness.ktx"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"materials" : [
|
||||||
|
{
|
||||||
|
"name" : "Cube",
|
||||||
|
"pbrMetallicRoughness" : {
|
||||||
|
"baseColorTexture" : {
|
||||||
|
"index" : 0
|
||||||
|
},
|
||||||
|
"metallicRoughnessTexture" : {
|
||||||
|
"index" : 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"meshes" : [
|
||||||
|
{
|
||||||
|
"name" : "Cube",
|
||||||
|
"primitives" : [
|
||||||
|
{
|
||||||
|
"attributes" : {
|
||||||
|
"NORMAL" : 2,
|
||||||
|
"POSITION" : 1,
|
||||||
|
"TANGENT" : 3,
|
||||||
|
"TEXCOORD_0" : 4
|
||||||
|
},
|
||||||
|
"indices" : 0,
|
||||||
|
"material" : 0,
|
||||||
|
"mode" : 4
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"nodes" : [
|
||||||
|
{
|
||||||
|
"mesh" : 0,
|
||||||
|
"name" : "Cube"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"samplers" : [
|
||||||
|
{}
|
||||||
|
],
|
||||||
|
"scene" : 0,
|
||||||
|
"scenes" : [
|
||||||
|
{
|
||||||
|
"nodes" : [
|
||||||
|
0
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"textures" : [
|
||||||
|
{
|
||||||
|
"sampler" : 0,
|
||||||
|
"source" : 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"sampler" : 0,
|
||||||
|
"source" : 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
BIN
models/Cube-KTX/Cube_BaseColor.ktx
Normal file
BIN
models/Cube-KTX/Cube_BaseColor.ktx
Normal file
Binary file not shown.
BIN
models/Cube-KTX/Cube_MetallicRoughness.ktx
Normal file
BIN
models/Cube-KTX/Cube_MetallicRoughness.ktx
Normal file
Binary file not shown.
12
models/Cube-KTX/README.md
Normal file
12
models/Cube-KTX/README.md
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
License: Donated by Norbert Nopper for glTF testing.
|
||||||
|
|
||||||
|
https://github.com/KhronosGroup/glTF-Sample-Models/tree/master/2.0/Cube
|
||||||
|
|
||||||
|
|
||||||
|
----
|
||||||
|
|
||||||
|
Converted .png to .ktx image by Syoyo Fujita.
|
||||||
|
|
||||||
|
.png -> .ppm using image magic(resize with 25% to reduce file size)
|
||||||
|
.ppm -> .ktx using `toktx` in KTX-Software https://github.com/KhronosGroup/KTX-Software
|
||||||
|
|
||||||
193
tests/tester.cc
193
tests/tester.cc
@@ -85,4 +85,197 @@ TEST_CASE("extension-with-empty-object", "[issue-97]") {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("invalid-primitive-indices", "[bounds-checking]") {
|
||||||
|
tinygltf::Model model;
|
||||||
|
tinygltf::TinyGLTF ctx;
|
||||||
|
std::string err;
|
||||||
|
std::string warn;
|
||||||
|
|
||||||
|
// Loading is expected to fail, but not crash.
|
||||||
|
bool ret = ctx.LoadASCIIFromFile(
|
||||||
|
&model, &err, &warn,
|
||||||
|
"../models/BoundsChecking/invalid-primitive-indices.gltf");
|
||||||
|
REQUIRE_THAT(err,
|
||||||
|
Catch::Contains("primitive indices accessor out of bounds"));
|
||||||
|
REQUIRE_FALSE(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("invalid-buffer-view-index", "[bounds-checking]") {
|
||||||
|
tinygltf::Model model;
|
||||||
|
tinygltf::TinyGLTF ctx;
|
||||||
|
std::string err;
|
||||||
|
std::string warn;
|
||||||
|
|
||||||
|
// Loading is expected to fail, but not crash.
|
||||||
|
bool ret = ctx.LoadASCIIFromFile(
|
||||||
|
&model, &err, &warn,
|
||||||
|
"../models/BoundsChecking/invalid-buffer-view-index.gltf");
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("accessor[0] invalid bufferView"));
|
||||||
|
REQUIRE_FALSE(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("invalid-buffer-index", "[bounds-checking]") {
|
||||||
|
tinygltf::Model model;
|
||||||
|
tinygltf::TinyGLTF ctx;
|
||||||
|
std::string err;
|
||||||
|
std::string warn;
|
||||||
|
|
||||||
|
// Loading is expected to fail, but not crash.
|
||||||
|
bool ret = ctx.LoadASCIIFromFile(
|
||||||
|
&model, &err, &warn,
|
||||||
|
"../models/BoundsChecking/invalid-buffer-index.gltf");
|
||||||
|
REQUIRE_THAT(
|
||||||
|
err, Catch::Contains("image[0] buffer \"1\" not found in the scene."));
|
||||||
|
REQUIRE_FALSE(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("glb-invalid-length", "[bounds-checking]") {
|
||||||
|
tinygltf::Model model;
|
||||||
|
tinygltf::TinyGLTF ctx;
|
||||||
|
std::string err;
|
||||||
|
std::string warn;
|
||||||
|
|
||||||
|
// This glb has a much longer length than the provided data and should fail
|
||||||
|
// initial range checks.
|
||||||
|
const unsigned char glb_invalid_length[] = "glTF"
|
||||||
|
"\x20\x00\x00\x00" "\x6c\x66\x00\x00" //
|
||||||
|
// | version | length |
|
||||||
|
"\x02\x00\x00\x00" "\x4a\x53\x4f\x4e{}"; //
|
||||||
|
// | model length | model format |
|
||||||
|
|
||||||
|
bool ret = ctx.LoadBinaryFromMemory(&model, &err, &warn, glb_invalid_length,
|
||||||
|
sizeof(glb_invalid_length));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("Invalid glTF binary."));
|
||||||
|
REQUIRE_FALSE(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("integer-out-of-bounds", "[bounds-checking]") {
|
||||||
|
tinygltf::Model model;
|
||||||
|
tinygltf::TinyGLTF ctx;
|
||||||
|
std::string err;
|
||||||
|
std::string warn;
|
||||||
|
|
||||||
|
// Loading is expected to fail, but not crash.
|
||||||
|
bool ret = ctx.LoadASCIIFromFile(
|
||||||
|
&model, &err, &warn,
|
||||||
|
"../models/BoundsChecking/integer-out-of-bounds.gltf");
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
|
||||||
|
REQUIRE_FALSE(ret);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parse-integer", "[bounds-checking]") {
|
||||||
|
SECTION("parses valid numbers") {
|
||||||
|
std::string err;
|
||||||
|
int result = 123;
|
||||||
|
CHECK(tinygltf::ParseIntegerProperty(&result, &err, {{"zero", 0}}, "zero",
|
||||||
|
true));
|
||||||
|
REQUIRE(err == "");
|
||||||
|
REQUIRE(result == 0);
|
||||||
|
|
||||||
|
CHECK(tinygltf::ParseIntegerProperty(&result, &err, {{"int", -1234}}, "int",
|
||||||
|
true));
|
||||||
|
REQUIRE(err == "");
|
||||||
|
REQUIRE(result == -1234);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("detects missing properties") {
|
||||||
|
std::string err;
|
||||||
|
int result = -1;
|
||||||
|
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, {}, "int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("'int' property is missing"));
|
||||||
|
REQUIRE(result == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("handled missing but not required properties") {
|
||||||
|
std::string err;
|
||||||
|
int result = -1;
|
||||||
|
CHECK_FALSE(
|
||||||
|
tinygltf::ParseIntegerProperty(&result, &err, {}, "int", false));
|
||||||
|
REQUIRE(err == "");
|
||||||
|
REQUIRE(result == -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("invalid integers") {
|
||||||
|
std::string err;
|
||||||
|
int result = -1;
|
||||||
|
|
||||||
|
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, {{"int", 0.5}},
|
||||||
|
"int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not an integer type"));
|
||||||
|
|
||||||
|
// Excessively large values and NaN aren't allowed either.
|
||||||
|
err.clear();
|
||||||
|
CHECK_FALSE(tinygltf::ParseIntegerProperty(&result, &err, {{"int", 1e300}},
|
||||||
|
"int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not an integer type"));
|
||||||
|
|
||||||
|
err.clear();
|
||||||
|
CHECK_FALSE(tinygltf::ParseIntegerProperty(
|
||||||
|
&result, &err, {{"int", std::numeric_limits<double>::quiet_NaN()}},
|
||||||
|
"int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not an integer type"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parse-unsigned", "[bounds-checking]") {
|
||||||
|
SECTION("parses valid unsigned integers") {
|
||||||
|
// Use string-based parsing here, using the initializer list syntax doesn't
|
||||||
|
// parse 0 as unsigned.
|
||||||
|
json zero_obj = json::parse("{\"zero\": 0}");
|
||||||
|
|
||||||
|
std::string err;
|
||||||
|
size_t result = 123;
|
||||||
|
CHECK(
|
||||||
|
tinygltf::ParseUnsignedProperty(&result, &err, zero_obj, "zero", true));
|
||||||
|
REQUIRE(err == "");
|
||||||
|
REQUIRE(result == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("invalid integers") {
|
||||||
|
std::string err;
|
||||||
|
size_t result = -1;
|
||||||
|
|
||||||
|
CHECK_FALSE(tinygltf::ParseUnsignedProperty(&result, &err, {{"int", -1234}},
|
||||||
|
"int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
|
||||||
|
|
||||||
|
err.clear();
|
||||||
|
CHECK_FALSE(tinygltf::ParseUnsignedProperty(&result, &err, {{"int", 0.5}},
|
||||||
|
"int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
|
||||||
|
|
||||||
|
// Excessively large values and NaN aren't allowed either.
|
||||||
|
err.clear();
|
||||||
|
CHECK_FALSE(tinygltf::ParseUnsignedProperty(&result, &err, {{"int", 1e300}},
|
||||||
|
"int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
|
||||||
|
|
||||||
|
err.clear();
|
||||||
|
CHECK_FALSE(tinygltf::ParseUnsignedProperty(
|
||||||
|
&result, &err, {{"int", std::numeric_limits<double>::quiet_NaN()}},
|
||||||
|
"int", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not a positive integer"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("parse-integer-array", "[bounds-checking]") {
|
||||||
|
SECTION("parses valid integers") {
|
||||||
|
std::string err;
|
||||||
|
std::vector<int> result;
|
||||||
|
CHECK(tinygltf::ParseIntegerArrayProperty(&result, &err,
|
||||||
|
{{"x", {-1, 2, 3}}}, "x", true));
|
||||||
|
REQUIRE(err == "");
|
||||||
|
REQUIRE(result.size() == 3);
|
||||||
|
REQUIRE(result[0] == -1);
|
||||||
|
REQUIRE(result[1] == 2);
|
||||||
|
REQUIRE(result[2] == 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("invalid integers") {
|
||||||
|
std::string err;
|
||||||
|
std::vector<int> result;
|
||||||
|
CHECK_FALSE(tinygltf::ParseIntegerArrayProperty(
|
||||||
|
&result, &err, {{"x", {-1, 1e300, 3}}}, "x", true));
|
||||||
|
REQUIRE_THAT(err, Catch::Contains("not an integer type"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
828
tiny_gltf.h
828
tiny_gltf.h
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user