mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-06-14 19:28:52 +00:00
Compare commits
27 Commits
uri-decode
...
mesh-dump
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb0a8794c3 | ||
|
|
60e22f3281 | ||
|
|
4cfd2849fb | ||
|
|
a5416707c9 | ||
|
|
8ec9af7f2e | ||
|
|
40982716f9 | ||
|
|
4bdc96fb9b | ||
|
|
96ecea080a | ||
|
|
22bfc843ee | ||
|
|
eaa25a307a | ||
|
|
ab600b8e72 | ||
|
|
eb9d29c06e | ||
|
|
57356933c5 | ||
|
|
c3d6716c56 | ||
|
|
ac5e09f8c3 | ||
|
|
e2c3fe1c0b | ||
|
|
973d9b3394 | ||
|
|
28ad4ab7b8 | ||
|
|
0227ae5d9b | ||
|
|
a0a62bde1c | ||
|
|
23d1ff2880 | ||
|
|
bcf2ce586e | ||
|
|
aa3c5a1cad | ||
|
|
1cf179d377 | ||
|
|
ddc14f8ba6 | ||
|
|
379bb612f1 | ||
|
|
1da4e5d633 |
@@ -202,6 +202,13 @@ $ ./tester
|
||||
$ ./tester_noexcept
|
||||
```
|
||||
|
||||
### Fuzzing tests
|
||||
|
||||
See `tests/fuzzer` for details.
|
||||
|
||||
After running fuzzer on Ryzen9 3950X a week, at least `LoadASCIIFromString` looks safe except for out-of-memory error in Fuzzer.
|
||||
We may be better to introduce bounded memory size checking when parsing glTF data.
|
||||
|
||||
## Third party licenses
|
||||
|
||||
* json.hpp : Licensed under the MIT License <http://opensource.org/licenses/MIT>. Copyright (c) 2013-2017 Niels Lohmann <http://nlohmann.me>.
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
5
examples/mesh-modify/Makefile
Normal file
5
examples/mesh-modify/Makefile
Normal file
@@ -0,0 +1,5 @@
|
||||
#EXTRA_CXXFLAGS := -fsanitize=address -fno-omit-frame-pointer -Wall -Werror -Weverything -Wno-c++11-long-long -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-padded
|
||||
EXTRA_CXXFLAGS :=
|
||||
|
||||
all:
|
||||
clang++ -std=c++11 -g -O1 -I../../ -I../common $(EXTRA_CXXFLAGS) -o mesh-modify mesh-modify.cc mesh-util.cc tinygltf_impl.cc
|
||||
13
examples/mesh-modify/README.md
Normal file
13
examples/mesh-modify/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
# Mesh modify experiment
|
||||
|
||||
Sometimes we want to tweak mesh attributes(e.g. vertex position, uv coord, etc).
|
||||
glTF itself does not allow ASCII representation of such data.
|
||||
|
||||
This example show how to
|
||||
|
||||
- Export mesh data from .bin to .obj
|
||||
- Import mesh data to .bin(update corresponding buffer data) from .obj
|
||||
|
||||
## Requirement
|
||||
|
||||
Assume Buffer is stored as external file(`.bin`)
|
||||
760
examples/mesh-modify/mesh-modify.cc
Normal file
760
examples/mesh-modify/mesh-modify.cc
Normal file
@@ -0,0 +1,760 @@
|
||||
#include <cassert>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <limits>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <fstream>
|
||||
|
||||
#if !defined(__ANDROID__) && !defined(_WIN32)
|
||||
#include <wordexp.h>
|
||||
#endif
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic push
|
||||
#pragma clang diagnostic ignored "-Weverything"
|
||||
#endif
|
||||
|
||||
#include "../../json.hpp"
|
||||
|
||||
using json = nlohmann::json;
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic pop
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include "../../tiny_gltf.h"
|
||||
#else
|
||||
#include "tiny_gltf.h"
|
||||
#endif
|
||||
|
||||
#include "mesh-util.hh"
|
||||
|
||||
#ifdef __clang__
|
||||
#pragma clang diagnostic ignored "-Wunused-function"
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
static std::string PrintMode(int mode) {
|
||||
if (mode == TINYGLTF_MODE_POINTS) {
|
||||
return "POINTS";
|
||||
} else if (mode == TINYGLTF_MODE_LINE) {
|
||||
return "LINE";
|
||||
} else if (mode == TINYGLTF_MODE_LINE_LOOP) {
|
||||
return "LINE_LOOP";
|
||||
} else if (mode == TINYGLTF_MODE_TRIANGLES) {
|
||||
return "TRIANGLES";
|
||||
} else if (mode == TINYGLTF_MODE_TRIANGLE_FAN) {
|
||||
return "TRIANGLE_FAN";
|
||||
} else if (mode == TINYGLTF_MODE_TRIANGLE_STRIP) {
|
||||
return "TRIANGLE_STRIP";
|
||||
}
|
||||
return "**UNKNOWN**";
|
||||
}
|
||||
|
||||
static std::string PrintTarget(int target) {
|
||||
if (target == 34962) {
|
||||
return "GL_ARRAY_BUFFER";
|
||||
} else if (target == 34963) {
|
||||
return "GL_ELEMENT_ARRAY_BUFFER";
|
||||
} else {
|
||||
return "**UNKNOWN**";
|
||||
}
|
||||
}
|
||||
|
||||
static std::string PrintType(int ty) {
|
||||
if (ty == TINYGLTF_TYPE_SCALAR) {
|
||||
return "SCALAR";
|
||||
} else if (ty == TINYGLTF_TYPE_VECTOR) {
|
||||
return "VECTOR";
|
||||
} else if (ty == TINYGLTF_TYPE_VEC2) {
|
||||
return "VEC2";
|
||||
} else if (ty == TINYGLTF_TYPE_VEC3) {
|
||||
return "VEC3";
|
||||
} else if (ty == TINYGLTF_TYPE_VEC4) {
|
||||
return "VEC4";
|
||||
} else if (ty == TINYGLTF_TYPE_MATRIX) {
|
||||
return "MATRIX";
|
||||
} else if (ty == TINYGLTF_TYPE_MAT2) {
|
||||
return "MAT2";
|
||||
} else if (ty == TINYGLTF_TYPE_MAT3) {
|
||||
return "MAT3";
|
||||
} else if (ty == TINYGLTF_TYPE_MAT4) {
|
||||
return "MAT4";
|
||||
}
|
||||
return "**UNKNOWN**";
|
||||
}
|
||||
|
||||
static std::string PrintComponentType(int ty) {
|
||||
if (ty == TINYGLTF_COMPONENT_TYPE_BYTE) {
|
||||
return "BYTE";
|
||||
} else if (ty == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) {
|
||||
return "UNSIGNED_BYTE";
|
||||
} else if (ty == TINYGLTF_COMPONENT_TYPE_SHORT) {
|
||||
return "SHORT";
|
||||
} else if (ty == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) {
|
||||
return "UNSIGNED_SHORT";
|
||||
} else if (ty == TINYGLTF_COMPONENT_TYPE_INT) {
|
||||
return "INT";
|
||||
} else if (ty == TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT) {
|
||||
return "UNSIGNED_INT";
|
||||
} else if (ty == TINYGLTF_COMPONENT_TYPE_FLOAT) {
|
||||
return "FLOAT";
|
||||
} else if (ty == TINYGLTF_COMPONENT_TYPE_DOUBLE) {
|
||||
return "DOUBLE";
|
||||
}
|
||||
|
||||
return "**UNKNOWN**";
|
||||
}
|
||||
|
||||
#if 0
|
||||
// TODO(syoyo): Support sparse accessor(sparse vertex attribute).
|
||||
// TODO(syoyo): Support more data type
|
||||
struct VertexAttrib {
|
||||
std::string name;
|
||||
|
||||
// Value are converted to float type.
|
||||
std::vector<float> data;
|
||||
|
||||
// Corresponding info in binary data
|
||||
int data_type; // e.g. TINYGLTF_TYPE_VEC2
|
||||
int component_type; // storage type. e.g.
|
||||
// TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT
|
||||
uint64_t buffer_offset{0};
|
||||
size_t buffer_length{0};
|
||||
};
|
||||
|
||||
struct MeshPrim {
|
||||
std::string name;
|
||||
int32_t id{-1};
|
||||
|
||||
int mode{TINYGLTF_MODE_TRIANGLES};
|
||||
|
||||
VertexAttrib position; // vec3
|
||||
VertexAttrib normal; // vec3
|
||||
VertexAttrib tangent; // vec4
|
||||
VertexAttrib color; // vec3 or vec4
|
||||
std::map<int, VertexAttrib> texcoords; // <slot, attrib> vec2
|
||||
std::map<int, VertexAttrib> weights; // <slot, attrib>
|
||||
std::map<int, VertexAttrib>
|
||||
joints; // <slot, attrib> store data as float type
|
||||
|
||||
int indices_type{-1}; // storage type(componentType) of `indices`.
|
||||
std::vector<uint32_t> indices; // vertex indices
|
||||
};
|
||||
#endif
|
||||
|
||||
#if 0
|
||||
static std::string GetFilePathExtension(const std::string &FileName) {
|
||||
if (FileName.find_last_of(".") != std::string::npos)
|
||||
return FileName.substr(FileName.find_last_of(".") + 1);
|
||||
return "";
|
||||
}
|
||||
#endif
|
||||
|
||||
static size_t ComponentTypeByteSize(int type) {
|
||||
switch (type) {
|
||||
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE:
|
||||
case TINYGLTF_COMPONENT_TYPE_BYTE:
|
||||
return sizeof(char);
|
||||
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT:
|
||||
case TINYGLTF_COMPONENT_TYPE_SHORT:
|
||||
return sizeof(short);
|
||||
case TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT:
|
||||
case TINYGLTF_COMPONENT_TYPE_INT:
|
||||
return sizeof(int);
|
||||
case TINYGLTF_COMPONENT_TYPE_FLOAT:
|
||||
return sizeof(float);
|
||||
case TINYGLTF_COMPONENT_TYPE_DOUBLE:
|
||||
return sizeof(double);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint8_t> LoadBin(const std::string &filename) {
|
||||
std::vector<uint8_t> data;
|
||||
|
||||
std::ifstream is(filename, std::ios::binary | std::ios::in | std::ios::ate);
|
||||
|
||||
if (is.is_open()) {
|
||||
size_t size = size_t(is.tellg());
|
||||
is.seekg(0, std::ios::beg);
|
||||
if (size < 4) {
|
||||
std::cerr << "File size is zero or too short: " << size << "\n";
|
||||
return data;
|
||||
}
|
||||
|
||||
data.resize(size);
|
||||
is.read(reinterpret_cast<char *>(data.data()), std::streamsize(size));
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
// TODO(syoyo): Use C++17 like filesystem library
|
||||
|
||||
bool FileExists(const std::string &abs_filename) {
|
||||
bool ret;
|
||||
#ifdef _WIN32
|
||||
// TODO(syoyo): Support utf8 filepath
|
||||
FILE *fp = nullptr;
|
||||
errno_t err = fopen_s(&fp, abs_filename.c_str(), "rb");
|
||||
if (err != 0) {
|
||||
return false;
|
||||
}
|
||||
#else
|
||||
FILE *fp = fopen(abs_filename.c_str(), "rb");
|
||||
#endif
|
||||
if (fp) {
|
||||
ret = true;
|
||||
fclose(fp);
|
||||
} else {
|
||||
ret = false;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static std::string JoinPath(const std::string &path0,
|
||||
const std::string &path1) {
|
||||
if (path0.empty()) {
|
||||
return path1;
|
||||
} else {
|
||||
// check '/'
|
||||
char lastChar = *path0.rbegin();
|
||||
if (lastChar != '/') {
|
||||
return path0 + std::string("/") + path1;
|
||||
} else {
|
||||
return path0 + path1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::string ExpandFilePath(const std::string &filepath) {
|
||||
#ifdef _WIN32
|
||||
DWORD len = ExpandEnvironmentStringsA(filepath.c_str(), NULL, 0);
|
||||
char *str = new char[len];
|
||||
ExpandEnvironmentStringsA(filepath.c_str(), str, len);
|
||||
|
||||
std::string s(str);
|
||||
|
||||
delete[] str;
|
||||
|
||||
return s;
|
||||
#else
|
||||
|
||||
#if defined(TARGET_OS_IPHONE) || defined(TARGET_IPHONE_SIMULATOR) || \
|
||||
defined(__ANDROID__) || defined(__EMSCRIPTEN__)
|
||||
// no expansion
|
||||
std::string s = filepath;
|
||||
#else
|
||||
std::string s;
|
||||
wordexp_t p;
|
||||
|
||||
if (filepath.empty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Quote the string to keep any spaces in filepath intact.
|
||||
std::string quoted_path = "\"" + filepath + "\"";
|
||||
// char** w;
|
||||
int ret = wordexp(quoted_path.c_str(), &p, 0);
|
||||
if (ret) {
|
||||
// err
|
||||
s = filepath;
|
||||
return s;
|
||||
}
|
||||
|
||||
// Use first element only.
|
||||
if (p.we_wordv) {
|
||||
s = std::string(p.we_wordv[0]);
|
||||
wordfree(&p);
|
||||
} else {
|
||||
s = filepath;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
return s;
|
||||
#endif
|
||||
}
|
||||
|
||||
static std::string FindFile(const std::vector<std::string> &paths,
|
||||
const std::string &filepath) {
|
||||
for (size_t i = 0; i < paths.size(); i++) {
|
||||
std::string absPath = ExpandFilePath(JoinPath(paths[i], filepath));
|
||||
if (FileExists(absPath)) {
|
||||
return absPath;
|
||||
}
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
|
||||
#if 0
|
||||
static std::string GetBaseDir(const std::string &filepath) {
|
||||
if (filepath.find_last_of("/\\") != std::string::npos)
|
||||
return filepath.substr(0, filepath.find_last_of("/\\"));
|
||||
return "";
|
||||
}
|
||||
#endif
|
||||
|
||||
static int GetSlotId(const std::string &name) {
|
||||
if (name.rfind("TEXCOORD_", 0) == 0) {
|
||||
int id = 0;
|
||||
sscanf(name.c_str(), "TEXCOORD_%d", &id);
|
||||
return id;
|
||||
} else if (name.rfind("WEIGHTS_", 0) == 0) {
|
||||
int id = 0;
|
||||
sscanf(name.c_str(), "WEIGHTS_%d", &id);
|
||||
return id;
|
||||
} else if (name.rfind("JOINTS_", 0) == 0) {
|
||||
int id = 0;
|
||||
sscanf(name.c_str(), "JOINTS_%d", &id);
|
||||
return id;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static bool IsAttributeSupported(const std::string &name) {
|
||||
constexpr int max_slots = 8;
|
||||
|
||||
if (name.compare("POSITION") == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (name.compare("NORMAL") == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (name.compare("TANGENT") == 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int i = 0; i < max_slots; i++) {
|
||||
std::string n = "TEXCOORD_" + std::to_string(i);
|
||||
if (n.compare(name) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < max_slots; i++) {
|
||||
std::string n = "WEIGHTS_" + std::to_string(i);
|
||||
if (n.compare(name) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < max_slots; i++) {
|
||||
std::string n = "JOINTS_" + std::to_string(i);
|
||||
if (n.compare(name) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static float Unpack(const unsigned char *ptr, int type) {
|
||||
if (type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) {
|
||||
unsigned char data = *ptr;
|
||||
return float(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_BYTE) {
|
||||
char data = static_cast<char>(*ptr);
|
||||
return float(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) {
|
||||
uint16_t data = *reinterpret_cast<const uint16_t *>(ptr);
|
||||
return float(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_SHORT) {
|
||||
int16_t data = *reinterpret_cast<const int16_t *>(ptr);
|
||||
return float(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_FLOAT) {
|
||||
float data = *reinterpret_cast<const float *>(ptr);
|
||||
return data;
|
||||
} else {
|
||||
std::cerr << "???: Unsupported type: " << PrintComponentType(type) << "\n";
|
||||
return 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
static uint32_t UnpackIndex(const unsigned char *ptr, int type) {
|
||||
if (type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_BYTE) {
|
||||
unsigned char data = *ptr;
|
||||
return uint32_t(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_BYTE) {
|
||||
char data = static_cast<char>(*ptr);
|
||||
return uint32_t(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) {
|
||||
uint16_t data = *reinterpret_cast<const uint16_t *>(ptr);
|
||||
return uint32_t(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_SHORT) {
|
||||
int16_t data = *reinterpret_cast<const int16_t *>(ptr);
|
||||
return uint32_t(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_INT) {
|
||||
// TODO(syoyo): Check overflow(2G+ index)
|
||||
int32_t data = *reinterpret_cast<const int32_t *>(ptr);
|
||||
return uint32_t(data);
|
||||
} else if (type == TINYGLTF_COMPONENT_TYPE_SHORT) {
|
||||
uint32_t data = *reinterpret_cast<const uint32_t *>(ptr);
|
||||
return data;
|
||||
} else {
|
||||
std::cerr << "???: Unsupported type: " << PrintComponentType(type) << "\n";
|
||||
return static_cast<uint32_t>(-1);
|
||||
}
|
||||
}
|
||||
|
||||
static bool DumpMesh(const tinygltf::Model &model, const tinygltf::Mesh &mesh,
|
||||
example::MeshPrim *out) {
|
||||
for (size_t i = 0; i < mesh.primitives.size(); i++) {
|
||||
const tinygltf::Primitive &primitive = mesh.primitives[i];
|
||||
|
||||
if (primitive.indices < 0) {
|
||||
std::cerr << "Primitive indices must be provided\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
// indices.
|
||||
{
|
||||
const tinygltf::Accessor &indexAccessor =
|
||||
model.accessors[size_t(primitive.indices)];
|
||||
|
||||
size_t num_elements = indexAccessor.count;
|
||||
std::cout << "index.elements = " << num_elements << "\n";
|
||||
|
||||
size_t byte_stride = ComponentTypeByteSize(indexAccessor.componentType);
|
||||
|
||||
const tinygltf::BufferView &indexBufferView =
|
||||
model.bufferViews[size_t(indexAccessor.bufferView)];
|
||||
|
||||
// should be 34963(ELEMENT_ARRAY_BUFFER)
|
||||
std::cout << "index.target = " << PrintTarget(indexBufferView.target) << "\n";
|
||||
if (indexBufferView.target != TINYGLTF_TARGET_ELEMENT_ARRAY_BUFFER) {
|
||||
std::cerr << "indexBufferView.target must be ELEMENT_ARRAY_BUFFER\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
const tinygltf::Buffer &indexBuffer =
|
||||
model.buffers[size_t(indexBufferView.buffer)];
|
||||
|
||||
std::vector<uint32_t> indices;
|
||||
|
||||
for (size_t k = 0; k < num_elements; k++) {
|
||||
|
||||
// TODO(syoyo): out-of-bounds check.
|
||||
const unsigned char *ptr = indexBuffer.data.data() +
|
||||
indexBufferView.byteOffset + (k * byte_stride) +
|
||||
indexAccessor.byteOffset;
|
||||
|
||||
uint32_t idx = UnpackIndex(ptr, indexAccessor.componentType);
|
||||
|
||||
std::cout << "vertex_index[" << k << "] = " << idx << "\n";
|
||||
|
||||
indices.push_back(idx);
|
||||
}
|
||||
|
||||
out->indices = indices;
|
||||
out->indices_type = indexAccessor.componentType;
|
||||
}
|
||||
|
||||
// attributes
|
||||
{
|
||||
std::map<std::string, int>::const_iterator it(
|
||||
primitive.attributes.begin());
|
||||
std::map<std::string, int>::const_iterator itEnd(
|
||||
primitive.attributes.end());
|
||||
|
||||
for (; it != itEnd; it++) {
|
||||
// it->first would be "POSITION", "NORMAL", "TEXCOORD_0", ...
|
||||
if (!IsAttributeSupported(it->first)) {
|
||||
std::cout << "Unsupported attribute: " << it->first << "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
if (size_t(it->second) >= model.accessors.size()) {
|
||||
std::cerr << "Invalid accessor id: " << it->second << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
const tinygltf::Accessor &accessor =
|
||||
model.accessors[size_t(it->second)];
|
||||
|
||||
size_t elem_size = 1;
|
||||
if (accessor.type == TINYGLTF_TYPE_SCALAR) {
|
||||
elem_size = 1;
|
||||
} else if (accessor.type == TINYGLTF_TYPE_VEC2) {
|
||||
elem_size = 2;
|
||||
} else if (accessor.type == TINYGLTF_TYPE_VEC3) {
|
||||
elem_size = 3;
|
||||
} else if (accessor.type == TINYGLTF_TYPE_VEC4) {
|
||||
elem_size = 4;
|
||||
} else {
|
||||
std::cerr << "Invalid or unsupported accessor type: "
|
||||
<< PrintType(accessor.type) << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::cout << PrintComponentType(accessor.componentType) << "\n";
|
||||
|
||||
size_t byte_stride = ComponentTypeByteSize(accessor.componentType);
|
||||
|
||||
std::cout << "attribute: " << it->first << "\n";
|
||||
// if (gGLProgramState.attribs[it->first] >= 0) {
|
||||
// Compute byteStride from Accessor + BufferView combination.
|
||||
int byteStride =
|
||||
accessor.ByteStride(model.bufferViews[size_t(accessor.bufferView)]);
|
||||
assert(byteStride != -1);
|
||||
|
||||
std::cout << "byteOffset: " << accessor.byteOffset << "\n";
|
||||
|
||||
const tinygltf::BufferView &bufferView =
|
||||
model.bufferViews[size_t(accessor.bufferView)];
|
||||
const tinygltf::Buffer &buffer =
|
||||
model.buffers[size_t(bufferView.buffer)];
|
||||
|
||||
size_t num_elems = accessor.count * elem_size;
|
||||
|
||||
example::VertexAttrib attrib;
|
||||
for (size_t k = 0; k < num_elems; k++) {
|
||||
// TODO(syoyo): out-of-bounds check.
|
||||
const unsigned char *ptr = buffer.data.data() +
|
||||
bufferView.byteOffset + (k * byte_stride) +
|
||||
accessor.byteOffset;
|
||||
float value = Unpack(ptr, accessor.componentType);
|
||||
std::cout << "[" << k << "] value = " << value << "\n";
|
||||
attrib.data.push_back(value);
|
||||
}
|
||||
attrib.component_type = accessor.componentType;
|
||||
attrib.data_type = accessor.type;
|
||||
attrib.name = it->first;
|
||||
|
||||
if (attrib.name.compare("POSITION") == 0) {
|
||||
out->position = attrib;
|
||||
} else if (attrib.name.compare("NORMAL") == 0) {
|
||||
out->normal = attrib;
|
||||
} else if (attrib.name.compare("TANGENT") == 0) {
|
||||
out->tangent = attrib;
|
||||
} else if (attrib.name.rfind("TEXCOORD_", 0) == 0) {
|
||||
int id = GetSlotId(attrib.name);
|
||||
std::cout << "texcoord[" << id << "]\n";
|
||||
out->texcoords[id] = attrib;
|
||||
} else if (attrib.name.rfind("JOINTS_", 0) == 0) {
|
||||
int id = GetSlotId(attrib.name);
|
||||
std::cout << "joints[" << id << "]\n";
|
||||
out->joints[id] = attrib;
|
||||
} else if (attrib.name.rfind("WEIGHTS_", 0) == 0) {
|
||||
int id = GetSlotId(attrib.name);
|
||||
std::cout << "weights[" << id << "]\n";
|
||||
out->weights[id] = attrib;
|
||||
} else {
|
||||
std::cerr << "???: attrib.name = " << attrib.name << "\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const tinygltf::Accessor &indexAccessor =
|
||||
model.accessors[size_t(primitive.indices)];
|
||||
(void)indexAccessor;
|
||||
PrintMode(primitive.mode);
|
||||
if (primitive.mode != TINYGLTF_MODE_TRIANGLES) {
|
||||
std::cerr << "Supported Primitive mode is TRIANGLES only at the moment\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
out->mode = primitive.mode;
|
||||
out->name = mesh.name;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
#if 0
|
||||
static bool ExtractMesh(const std::string &asset_path, tinygltf::Model &model,
|
||||
std::vector<example::MeshPrim> *outs) {
|
||||
// Get .bin data
|
||||
{
|
||||
if (model.buffers.size() != 1) {
|
||||
std::cerr << "Single element of `buffers` is supported at the moment.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
const tinygltf::Buffer &buffer = model.buffers[0];
|
||||
if (buffer.uri.empty()) {
|
||||
std::cerr << "buffer.uri must be a filepath.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (buffer.data.size() < 4) {
|
||||
std::cerr << "Invalid buffer.byteLength.\n";
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<std::string> search_paths;
|
||||
search_paths.push_back(asset_path);
|
||||
|
||||
std::string abs_filepath = FindFile(search_paths, buffer.uri);
|
||||
std::vector<uint8_t> bin = LoadBin(buffer.uri);
|
||||
if (bin.size() != buffer.data.size()) {
|
||||
std::cerr << "Byte size mismatch. Failed to load file: " << buffer.uri
|
||||
<< "\n";
|
||||
std::cerr << " .bin size = " << bin.size() << ", size in 'buffer.uri' = " << buffer.data.size() << "\n";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &mesh : model.meshes) {
|
||||
std::cout << "mesh.name: " << mesh.name << "\n";
|
||||
|
||||
example::MeshPrim output;
|
||||
bool ret = DumpMesh(model, mesh, &output);
|
||||
if (!ret) {
|
||||
return false;
|
||||
}
|
||||
|
||||
outs->push_back(output);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc < 2) {
|
||||
std::cout << "mesh-dump input.gltf" << std::endl;
|
||||
}
|
||||
|
||||
#if 0
|
||||
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF loader;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
#ifdef _WIN32
|
||||
std::string input_filename(argv[1] ? argv[1]
|
||||
: "../../../models/Cube/Cube.gltf");
|
||||
#else
|
||||
std::string input_filename(argv[1] ? argv[1] : "../../models/Cube/Cube.gltf");
|
||||
#endif
|
||||
|
||||
std::string ext = GetFilePathExtension(input_filename);
|
||||
|
||||
{
|
||||
bool ret = false;
|
||||
if (ext.compare("glb") == 0) {
|
||||
// assume binary glTF.
|
||||
ret = loader.LoadBinaryFromFile(&model, &err, &warn,
|
||||
input_filename.c_str());
|
||||
} else {
|
||||
// assume ascii glTF.
|
||||
ret =
|
||||
loader.LoadASCIIFromFile(&model, &err, &warn, input_filename.c_str());
|
||||
}
|
||||
|
||||
if (!warn.empty()) {
|
||||
printf("Warn: %s\n", warn.c_str());
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
printf("ERR: %s\n", err.c_str());
|
||||
}
|
||||
if (!ret) {
|
||||
printf("Failed to load .glTF : %s\n", argv[1]);
|
||||
exit(-1);
|
||||
}
|
||||
}
|
||||
|
||||
json j;
|
||||
{
|
||||
std::ifstream i(input_filename);
|
||||
i >> j;
|
||||
}
|
||||
std::cout << "j = " << j << "\n";
|
||||
|
||||
json j_patch = R"([
|
||||
{ "op": "add", "path": "/buffers/-", "value": {
|
||||
"name": "plane/data",
|
||||
"byteLength": 480,
|
||||
"uri": "plane1.bin"
|
||||
} }
|
||||
])"_json;
|
||||
|
||||
// a JSON value
|
||||
json j_original = R"({
|
||||
"baz": ["one", "two", "three"],
|
||||
"foo": "bar"
|
||||
})"_json;
|
||||
|
||||
//json j_patch = R"([
|
||||
// { "op": "remove", "path": "/buffers" }
|
||||
//])"_json;
|
||||
|
||||
std::cout << "patch = " << j_patch.dump(2) << "\n";
|
||||
|
||||
json j_ret = j.patch(j_patch);
|
||||
std::cout << "patched = " << j_ret.dump(2) << "\n";
|
||||
|
||||
std::string basedir = GetBaseDir(input_filename);
|
||||
std::vector<example::MeshPrim> meshes;
|
||||
bool ret = ExtractMesh(basedir, model, &meshes);
|
||||
|
||||
size_t n = 0;
|
||||
for (const auto &mesh : meshes) {
|
||||
// Assume no duplicated name in .glTF data
|
||||
std::string filename;
|
||||
if (mesh.name.empty()) {
|
||||
filename = "untitled-" + std::to_string(n) + ".obj";
|
||||
} else {
|
||||
filename = mesh.name + ".obj";
|
||||
}
|
||||
|
||||
bool flip_y = true; // flip texcoord Y?
|
||||
bool ok = example::SaveAsObjMesh(filename, mesh,);
|
||||
if (!ok) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
n++;
|
||||
}
|
||||
|
||||
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
#else
|
||||
|
||||
{
|
||||
std::string input_filename(argv[1]);
|
||||
|
||||
// Require facevarying layout?
|
||||
// false = try to keep GL-like mesh data as much as possible.
|
||||
// true = reorder vertex data and re-assign vertex indices.
|
||||
bool facevarying = false;
|
||||
|
||||
example::MeshPrim mesh;
|
||||
bool ok = example::LoadObjMesh(input_filename, facevarying, &mesh);
|
||||
if (!ok) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
PrintMeshPrim(mesh);
|
||||
|
||||
std::string output_filename("output.gltf");
|
||||
|
||||
ok = example::SaveAsGLTFMesh(output_filename, mesh);
|
||||
if (!ok) {
|
||||
std::cerr << "Failed to save mesh as glTF\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
std::cout << "Write glTF: " << output_filename << "\n";
|
||||
}
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
#endif
|
||||
|
||||
}
|
||||
1369
examples/mesh-modify/mesh-util.cc
Normal file
1369
examples/mesh-modify/mesh-util.cc
Normal file
File diff suppressed because it is too large
Load Diff
76
examples/mesh-modify/mesh-util.hh
Normal file
76
examples/mesh-modify/mesh-util.hh
Normal file
@@ -0,0 +1,76 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
namespace example {
|
||||
|
||||
// TODO(syoyo): Support sparse accessor(sparse vertex attribute).
|
||||
// TODO(syoyo): Support more data type
|
||||
struct VertexAttrib {
|
||||
std::string name;
|
||||
|
||||
// Value are converted to float type.
|
||||
std::vector<float> data;
|
||||
|
||||
// Corresponding info in binary data
|
||||
int data_type; // e.g. TINYGLTF_TYPE_VEC2
|
||||
int component_type; // storage type. e.g.
|
||||
// TINYGLTF_COMPONENT_TYPE_UNSIGNED_INT
|
||||
uint64_t buffer_offset{0};
|
||||
size_t buffer_length{0};
|
||||
|
||||
// Optional.
|
||||
std::vector<double> minValues;
|
||||
std::vector<double> maxValues;
|
||||
|
||||
size_t numElements() const;
|
||||
|
||||
};
|
||||
|
||||
struct MeshPrim {
|
||||
std::string name;
|
||||
int32_t id{-1};
|
||||
|
||||
int mode; // e.g. TRIANGLES
|
||||
|
||||
VertexAttrib position; // vec3
|
||||
VertexAttrib normal; // vec3
|
||||
VertexAttrib tangent; // vec4
|
||||
VertexAttrib color; // vec3 or vec4
|
||||
std::map<int, VertexAttrib> texcoords; // <slot, attrib> vec2
|
||||
std::map<int, VertexAttrib> weights; // <slot, attrib> vec4
|
||||
std::map<int, VertexAttrib>
|
||||
joints; // <slot, attrib> store data as float type
|
||||
|
||||
|
||||
// min/max of index value. -1 = undef
|
||||
int indices_min{-1};
|
||||
int indices_max{-1};
|
||||
int indices_type{-1}; // storage type(componentType) of `indices`.
|
||||
std::vector<uint32_t> indices; // vertex indices
|
||||
};
|
||||
|
||||
///
|
||||
/// Save MeshPrim as wavefront .obj
|
||||
///
|
||||
bool SaveAsObjMesh(const std::string &filename, const MeshPrim &mesh);
|
||||
|
||||
///
|
||||
/// Save MeshPrim as glTF mesh
|
||||
///
|
||||
bool SaveAsGLTFMesh(const std::string &filename, const MeshPrim &mesh);
|
||||
|
||||
///
|
||||
/// Loads .obj and convert to MeshPrim
|
||||
///
|
||||
/// @param[in] facevarying Construct mesh with facevarying vertex layout(default false)
|
||||
///
|
||||
bool LoadObjMesh(const std::string &filename, bool facevarying, MeshPrim *mesh);
|
||||
|
||||
///
|
||||
/// Print MeshPrim datra
|
||||
///
|
||||
void PrintMeshPrim(const MeshPrim &mesh);
|
||||
|
||||
} // namespace example
|
||||
7
examples/mesh-modify/meson.build
Normal file
7
examples/mesh-modify/meson.build
Normal file
@@ -0,0 +1,7 @@
|
||||
project('mesh-modify', 'cpp', default_options : ['cpp_std=c++11'])
|
||||
|
||||
thread_dep = dependency('threads')
|
||||
|
||||
incdir = include_directories(['../../', '../common'])
|
||||
|
||||
executable('mesh-modify', ['mesh-modify.cc', 'mesh-util.cc', 'tinygltf_impl.cc'], include_directories : incdir, dependencies : thread_dep)
|
||||
@@ -150,6 +150,7 @@ bool LoadObj(const std::string &filename, float scale,
|
||||
tinyobj::attrib_t attrib;
|
||||
std::vector<tinyobj::shape_t> shapes;
|
||||
std::vector<tinyobj::material_t> materials;
|
||||
std::string warn;
|
||||
std::string err;
|
||||
|
||||
std::string basedir = GetBaseDir(filename) + "/";
|
||||
@@ -158,12 +159,16 @@ bool LoadObj(const std::string &filename, float scale,
|
||||
// auto t_start = std::chrono::system_clock::now();
|
||||
|
||||
bool ret =
|
||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &err, filename.c_str(),
|
||||
tinyobj::LoadObj(&attrib, &shapes, &materials, &warn, &err, filename.c_str(),
|
||||
basepath, /* triangulate */ true);
|
||||
|
||||
// auto t_end = std::chrono::system_clock::now();
|
||||
// std::chrono::duration<double, std::milli> ms = t_end - t_start;
|
||||
|
||||
if (!warn.empty()) {
|
||||
std::cout << warn << std::endl;
|
||||
}
|
||||
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
}
|
||||
|
||||
BIN
models/CubeImageUriSpaces/ 2x2 image has multiple spaces.png
Normal file
BIN
models/CubeImageUriSpaces/ 2x2 image has multiple spaces.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 79 B |
BIN
models/CubeImageUriSpaces/2x2 image has spaces.png
Normal file
BIN
models/CubeImageUriSpaces/2x2 image has spaces.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 79 B |
171
models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf
Normal file
171
models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf
Normal file
@@ -0,0 +1,171 @@
|
||||
{
|
||||
"asset": {
|
||||
"version": "2.0"
|
||||
},
|
||||
"scenes": [
|
||||
{
|
||||
"nodes": [
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"scene": 0,
|
||||
"nodes": [
|
||||
{
|
||||
"mesh": 0
|
||||
}
|
||||
],
|
||||
"meshes": [
|
||||
{
|
||||
"primitives": [
|
||||
{
|
||||
"attributes": {
|
||||
"NORMAL": 2,
|
||||
"POSITION": 1,
|
||||
"TEXCOORD_0": 3
|
||||
},
|
||||
"indices": 0,
|
||||
"mode": 4,
|
||||
"material": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"materials": [
|
||||
{
|
||||
"pbrMetallicRoughness": {
|
||||
"baseColorTexture": {
|
||||
"index": 0,
|
||||
"texCoord": 0
|
||||
},
|
||||
"baseColorFactor": [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"metallicFactor": 1,
|
||||
"roughnessFactor": 1
|
||||
},
|
||||
"emissiveFactor": [
|
||||
0,
|
||||
0,
|
||||
0
|
||||
],
|
||||
"alphaMode": "OPAQUE"
|
||||
}
|
||||
],
|
||||
"textures": [
|
||||
{
|
||||
"source": 0,
|
||||
"sampler": 0
|
||||
}
|
||||
],
|
||||
"samplers": [
|
||||
{
|
||||
"wrapS": 33071,
|
||||
"wrapT": 33071
|
||||
}
|
||||
],
|
||||
"images": [
|
||||
{
|
||||
"uri": " 2x2 image has multiple spaces.png"
|
||||
}
|
||||
],
|
||||
"accessors": [
|
||||
{
|
||||
"bufferView": 0,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5121,
|
||||
"count": 36,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
23
|
||||
],
|
||||
"min": [
|
||||
0
|
||||
],
|
||||
"type": "SCALAR"
|
||||
},
|
||||
{
|
||||
"bufferView": 1,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
0.5,
|
||||
0.5,
|
||||
0.5
|
||||
],
|
||||
"min": [
|
||||
-0.5,
|
||||
-0.5,
|
||||
-0.5
|
||||
],
|
||||
"type": "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView": 2,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"min": [
|
||||
-1,
|
||||
-1,
|
||||
-1
|
||||
],
|
||||
"type": "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView": 3,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"min": [
|
||||
0,
|
||||
0
|
||||
],
|
||||
"type": "VEC2"
|
||||
}
|
||||
],
|
||||
"bufferViews": [
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 0,
|
||||
"byteLength": 36
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 36,
|
||||
"byteLength": 288
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 324,
|
||||
"byteLength": 288
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 612,
|
||||
"byteLength": 192
|
||||
}
|
||||
],
|
||||
"buffers": [
|
||||
{
|
||||
"byteLength": 804,
|
||||
"uri": "CubeImageUriSpaces.bin"
|
||||
}
|
||||
]
|
||||
}
|
||||
BIN
models/CubeImageUriSpaces/CubeImageUriSpaces.bin
Normal file
BIN
models/CubeImageUriSpaces/CubeImageUriSpaces.bin
Normal file
Binary file not shown.
171
models/CubeImageUriSpaces/CubeImageUriSpaces.gltf
Normal file
171
models/CubeImageUriSpaces/CubeImageUriSpaces.gltf
Normal file
@@ -0,0 +1,171 @@
|
||||
{
|
||||
"asset": {
|
||||
"version": "2.0"
|
||||
},
|
||||
"scenes": [
|
||||
{
|
||||
"nodes": [
|
||||
0
|
||||
]
|
||||
}
|
||||
],
|
||||
"scene": 0,
|
||||
"nodes": [
|
||||
{
|
||||
"mesh": 0
|
||||
}
|
||||
],
|
||||
"meshes": [
|
||||
{
|
||||
"primitives": [
|
||||
{
|
||||
"attributes": {
|
||||
"NORMAL": 2,
|
||||
"POSITION": 1,
|
||||
"TEXCOORD_0": 3
|
||||
},
|
||||
"indices": 0,
|
||||
"mode": 4,
|
||||
"material": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
],
|
||||
"materials": [
|
||||
{
|
||||
"pbrMetallicRoughness": {
|
||||
"baseColorTexture": {
|
||||
"index": 0,
|
||||
"texCoord": 0
|
||||
},
|
||||
"baseColorFactor": [
|
||||
1,
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"metallicFactor": 1,
|
||||
"roughnessFactor": 1
|
||||
},
|
||||
"emissiveFactor": [
|
||||
0,
|
||||
0,
|
||||
0
|
||||
],
|
||||
"alphaMode": "OPAQUE"
|
||||
}
|
||||
],
|
||||
"textures": [
|
||||
{
|
||||
"source": 0,
|
||||
"sampler": 0
|
||||
}
|
||||
],
|
||||
"samplers": [
|
||||
{
|
||||
"wrapS": 33071,
|
||||
"wrapT": 33071
|
||||
}
|
||||
],
|
||||
"images": [
|
||||
{
|
||||
"uri": "2x2 image has spaces.png"
|
||||
}
|
||||
],
|
||||
"accessors": [
|
||||
{
|
||||
"bufferView": 0,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5121,
|
||||
"count": 36,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
23
|
||||
],
|
||||
"min": [
|
||||
0
|
||||
],
|
||||
"type": "SCALAR"
|
||||
},
|
||||
{
|
||||
"bufferView": 1,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
0.5,
|
||||
0.5,
|
||||
0.5
|
||||
],
|
||||
"min": [
|
||||
-0.5,
|
||||
-0.5,
|
||||
-0.5
|
||||
],
|
||||
"type": "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView": 2,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
1,
|
||||
1,
|
||||
1
|
||||
],
|
||||
"min": [
|
||||
-1,
|
||||
-1,
|
||||
-1
|
||||
],
|
||||
"type": "VEC3"
|
||||
},
|
||||
{
|
||||
"bufferView": 3,
|
||||
"byteOffset": 0,
|
||||
"componentType": 5126,
|
||||
"count": 24,
|
||||
"normalized": false,
|
||||
"max": [
|
||||
1,
|
||||
1
|
||||
],
|
||||
"min": [
|
||||
0,
|
||||
0
|
||||
],
|
||||
"type": "VEC2"
|
||||
}
|
||||
],
|
||||
"bufferViews": [
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 0,
|
||||
"byteLength": 36
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 36,
|
||||
"byteLength": 288
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 324,
|
||||
"byteLength": 288
|
||||
},
|
||||
{
|
||||
"buffer": 0,
|
||||
"byteOffset": 612,
|
||||
"byteLength": 192
|
||||
}
|
||||
],
|
||||
"buffers": [
|
||||
{
|
||||
"byteLength": 804,
|
||||
"uri": "CubeImageUriSpaces.bin"
|
||||
}
|
||||
]
|
||||
}
|
||||
46
tests/fuzzer/README.md
Normal file
46
tests/fuzzer/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# Fuzzing test
|
||||
|
||||
Do fuzzing test for TinyGLTF API.
|
||||
|
||||
## Supported API
|
||||
|
||||
* [x] LoadASCIIFromMemory
|
||||
* [ ] LoadBinaryFromMemory
|
||||
|
||||
## Requirements
|
||||
|
||||
* meson
|
||||
* clang with fuzzer support(`-fsanitize=fuzzer`. at least clang 8.0 should work)
|
||||
|
||||
## Setup
|
||||
|
||||
### Ubuntu 18.04
|
||||
|
||||
```
|
||||
$ sudo apt install clang++-8
|
||||
$ sudo apt install libfuzzer-8-dev
|
||||
```
|
||||
|
||||
Optionally, if you didn't set `update-alternatives` you can set `clang++` to point to `clang++8`
|
||||
|
||||
```
|
||||
$ sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-8 10
|
||||
$ sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-8 10
|
||||
```
|
||||
|
||||
## How to compile
|
||||
|
||||
```
|
||||
$ CXX=clang++ CC=clang meson build
|
||||
$ cd build
|
||||
$ ninja
|
||||
```
|
||||
|
||||
## How to run
|
||||
|
||||
Increase memory limit. e.g. `-rss_limit_mb=50000`
|
||||
|
||||
```
|
||||
$ ./fuzz_gltf -rss_limit_mb=20000 -jobs 4
|
||||
```
|
||||
|
||||
33
tests/fuzzer/fuzz_gltf.cc
Normal file
33
tests/fuzzer/fuzz_gltf.cc
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||
#define TINYGLTF_IMPLEMENTATION
|
||||
#include "tiny_gltf.h"
|
||||
|
||||
static void parse_intCoding4(const uint8_t *data, size_t size)
|
||||
{
|
||||
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
const char *str = reinterpret_cast<const char *>(data);
|
||||
|
||||
bool ret = ctx.LoadASCIIFromString(&model, &err, &warn, str, size, /* base_dir */"" );
|
||||
(void)ret;
|
||||
|
||||
}
|
||||
|
||||
extern "C"
|
||||
int LLVMFuzzerTestOneInput(std::uint8_t const* data, std::size_t size)
|
||||
{
|
||||
parse_intCoding4(data, size);
|
||||
return 0;
|
||||
}
|
||||
|
||||
9
tests/fuzzer/meson.build
Normal file
9
tests/fuzzer/meson.build
Normal file
@@ -0,0 +1,9 @@
|
||||
project('fuzz_tinygltf', 'cpp', default_options : ['cpp_std=c++11'])
|
||||
|
||||
incdirs = include_directories('../../')
|
||||
executable('fuzz_gltf',
|
||||
'fuzz_gltf.cc',
|
||||
include_directories : incdirs,
|
||||
cpp_args : '-fsanitize=address,fuzzer',
|
||||
link_args : '-fsanitize=address,fuzzer' )
|
||||
|
||||
@@ -333,3 +333,29 @@ TEST_CASE("pbr-khr-texture-transform", "[material]") {
|
||||
REQUIRE(scale[1] == Approx(-1.0));
|
||||
|
||||
}
|
||||
|
||||
TEST_CASE("image-uri-spaces", "[issue-236]") {
|
||||
|
||||
tinygltf::Model model;
|
||||
tinygltf::TinyGLTF ctx;
|
||||
std::string err;
|
||||
std::string warn;
|
||||
|
||||
// Test image file with single spaces.
|
||||
bool ret = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/CubeImageUriSpaces/CubeImageUriSpaces.gltf");
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
|
||||
// Test image file with a beginning space, trailing space, and greater than
|
||||
// one consecutive spaces.
|
||||
ret = ctx.LoadASCIIFromFile(&model, &err, &warn, "../models/CubeImageUriSpaces/CubeImageUriMultipleSpaces.gltf");
|
||||
if (!err.empty()) {
|
||||
std::cerr << err << std::endl;
|
||||
}
|
||||
|
||||
REQUIRE(true == ret);
|
||||
}
|
||||
|
||||
|
||||
153
tiny_gltf.h
153
tiny_gltf.h
@@ -4,7 +4,7 @@
|
||||
//
|
||||
// The MIT License (MIT)
|
||||
//
|
||||
// Copyright (c) 2015 - 2019 Syoyo Fujita, Aurélien Chatelain and many
|
||||
// Copyright (c) 2015 - 2020 Syoyo Fujita, Aurélien Chatelain and many
|
||||
// contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
@@ -527,10 +527,12 @@ struct AnimationChannel {
|
||||
// "weights"]
|
||||
Value extras;
|
||||
ExtensionMap extensions;
|
||||
ExtensionMap target_extensions;
|
||||
|
||||
// Filled when SetStoreOriginalJSONForExtrasAndExtensions is enabled.
|
||||
std::string extras_json_string;
|
||||
std::string extensions_json_string;
|
||||
std::string target_extensions_json_string;
|
||||
|
||||
AnimationChannel() : sampler(-1), target_node(-1) {}
|
||||
DEFAULT_METHODS(AnimationChannel)
|
||||
@@ -1151,7 +1153,7 @@ class Model {
|
||||
std::vector<Scene> scenes;
|
||||
std::vector<Light> lights;
|
||||
|
||||
int defaultScene;
|
||||
int defaultScene = -1;
|
||||
std::vector<std::string> extensionsUsed;
|
||||
std::vector<std::string> extensionsRequired;
|
||||
|
||||
@@ -2144,12 +2146,12 @@ std::string base64_decode(std::string const &encoded_string) {
|
||||
namespace dlib {
|
||||
|
||||
#if 0
|
||||
inline unsigned char to_hex( unsigned char x )
|
||||
inline unsigned char to_hex( unsigned char x )
|
||||
{
|
||||
return x + (x > 9 ? ('A'-10) : '0');
|
||||
}
|
||||
|
||||
const std::string urlencode( const std::string& s )
|
||||
const std::string urlencode( const std::string& s )
|
||||
{
|
||||
std::ostringstream os;
|
||||
|
||||
@@ -2553,8 +2555,10 @@ std::string ExpandFilePath(const std::string &filepath, void *) {
|
||||
return "";
|
||||
}
|
||||
|
||||
// Quote the string to keep any spaces in filepath intact.
|
||||
std::string quoted_path = "\"" + filepath + "\"";
|
||||
// char** w;
|
||||
int ret = wordexp(filepath.c_str(), &p, 0);
|
||||
int ret = wordexp(quoted_path.c_str(), &p, 0);
|
||||
if (ret) {
|
||||
// err
|
||||
s = filepath;
|
||||
@@ -3606,6 +3610,7 @@ static bool ParseAsset(Asset *asset, std::string *err, const json &o,
|
||||
ParseStringProperty(&asset->version, err, o, "version", true, "Asset");
|
||||
ParseStringProperty(&asset->generator, err, o, "generator", false, "Asset");
|
||||
ParseStringProperty(&asset->minVersion, err, o, "minVersion", false, "Asset");
|
||||
ParseStringProperty(&asset->copyright, err, o, "copyright", false, "Asset");
|
||||
|
||||
ParseExtensionsProperty(&asset->extensions, err, o);
|
||||
|
||||
@@ -4855,6 +4860,13 @@ static bool ParseAnimationChannel(
|
||||
}
|
||||
return false;
|
||||
}
|
||||
ParseExtensionsProperty(&channel->target_extensions, err, target_object);
|
||||
if (store_original_json_for_extras_and_extensions) {
|
||||
json_const_iterator it;
|
||||
if (FindMember(target_object, "extensions", it)) {
|
||||
channel->target_extensions_json_string = JsonToString(GetValue(it));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
channel->sampler = samplerIndex;
|
||||
@@ -5664,6 +5676,13 @@ bool TinyGLTF::LoadFromString(Model *model, std::string *err, std::string *warn,
|
||||
model->accessors[size_t(attribute.second)].bufferView)]
|
||||
.target = TINYGLTF_TARGET_ARRAY_BUFFER;
|
||||
}
|
||||
|
||||
for(auto &target : primitive.targets) {
|
||||
for(auto &attribute : target) {
|
||||
model->bufferViews[size_t(model->accessors[size_t(attribute.second)].bufferView)]
|
||||
.target = TINYGLTF_TARGET_ARRAY_BUFFER;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6532,6 +6551,8 @@ static void SerializeGltfAnimationChannel(AnimationChannel &channel, json &o) {
|
||||
SerializeNumberProperty("node", channel.target_node, target);
|
||||
SerializeStringProperty("path", channel.target_path, target);
|
||||
|
||||
SerializeExtensionMap(channel.target_extensions, target);
|
||||
|
||||
JsonAddMember(o, "target", std::move(target));
|
||||
}
|
||||
|
||||
@@ -6571,6 +6592,7 @@ static void SerializeGltfAnimation(Animation &animation, json &o) {
|
||||
|
||||
{
|
||||
json samplers;
|
||||
JsonReserveArray(samplers, animation.samplers.size());
|
||||
for (unsigned int i = 0; i < animation.samplers.size(); ++i) {
|
||||
json sampler;
|
||||
AnimationSampler gltfSampler = animation.samplers[i];
|
||||
@@ -7096,14 +7118,16 @@ static void SerializeGltfTexture(Texture &texture, json &o) {
|
||||
///
|
||||
static void SerializeGltfModel(Model *model, json &o) {
|
||||
// ACCESSORS
|
||||
json accessors;
|
||||
JsonReserveArray(accessors, model->accessors.size());
|
||||
for (unsigned int i = 0; i < model->accessors.size(); ++i) {
|
||||
json accessor;
|
||||
SerializeGltfAccessor(model->accessors[i], accessor);
|
||||
JsonPushBack(accessors, std::move(accessor));
|
||||
if (model->accessors.size()) {
|
||||
json accessors;
|
||||
JsonReserveArray(accessors, model->accessors.size());
|
||||
for (unsigned int i = 0; i < model->accessors.size(); ++i) {
|
||||
json accessor;
|
||||
SerializeGltfAccessor(model->accessors[i], accessor);
|
||||
JsonPushBack(accessors, std::move(accessor));
|
||||
}
|
||||
JsonAddMember(o, "accessors", std::move(accessors));
|
||||
}
|
||||
JsonAddMember(o, "accessors", std::move(accessors));
|
||||
|
||||
// ANIMATIONS
|
||||
if (model->animations.size()) {
|
||||
@@ -7126,14 +7150,16 @@ static void SerializeGltfModel(Model *model, json &o) {
|
||||
JsonAddMember(o, "asset", std::move(asset));
|
||||
|
||||
// BUFFERVIEWS
|
||||
json bufferViews;
|
||||
JsonReserveArray(bufferViews, model->bufferViews.size());
|
||||
for (unsigned int i = 0; i < model->bufferViews.size(); ++i) {
|
||||
json bufferView;
|
||||
SerializeGltfBufferView(model->bufferViews[i], bufferView);
|
||||
JsonPushBack(bufferViews, std::move(bufferView));
|
||||
if(model->bufferViews.size()) {
|
||||
json bufferViews;
|
||||
JsonReserveArray(bufferViews, model->bufferViews.size());
|
||||
for (unsigned int i = 0; i < model->bufferViews.size(); ++i) {
|
||||
json bufferView;
|
||||
SerializeGltfBufferView(model->bufferViews[i], bufferView);
|
||||
JsonPushBack(bufferViews, std::move(bufferView));
|
||||
}
|
||||
JsonAddMember(o, "bufferViews", std::move(bufferViews));
|
||||
}
|
||||
JsonAddMember(o, "bufferViews", std::move(bufferViews));
|
||||
|
||||
// Extensions used
|
||||
if (model->extensionsUsed.size()) {
|
||||
@@ -7402,20 +7428,21 @@ bool TinyGLTF::WriteGltfSceneToStream(Model *model, std::ostream &stream,
|
||||
SerializeGltfModel(model, output);
|
||||
|
||||
// BUFFERS
|
||||
std::vector<std::string> usedUris;
|
||||
std::vector<unsigned char> binBuffer;
|
||||
json buffers;
|
||||
JsonReserveArray(buffers, model->buffers.size());
|
||||
for (unsigned int i = 0; i < model->buffers.size(); ++i) {
|
||||
json buffer;
|
||||
if (writeBinary && i == 0 && model->buffers[i].uri.empty()) {
|
||||
SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer);
|
||||
} else {
|
||||
SerializeGltfBuffer(model->buffers[i], buffer);
|
||||
if(model->buffers.size()) {
|
||||
json buffers;
|
||||
JsonReserveArray(buffers, model->buffers.size());
|
||||
for (unsigned int i = 0; i < model->buffers.size(); ++i) {
|
||||
json buffer;
|
||||
if (writeBinary && i==0 && model->buffers[i].uri.empty()){
|
||||
SerializeGltfBufferBin(model->buffers[i], buffer,binBuffer);
|
||||
} else {
|
||||
SerializeGltfBuffer(model->buffers[i], buffer);
|
||||
}
|
||||
JsonPushBack(buffers, std::move(buffer));
|
||||
}
|
||||
JsonPushBack(buffers, std::move(buffer));
|
||||
JsonAddMember(output, "buffers", std::move(buffers));
|
||||
}
|
||||
JsonAddMember(output, "buffers", std::move(buffers));
|
||||
|
||||
// IMAGES
|
||||
if (model->images.size()) {
|
||||
@@ -7469,44 +7496,46 @@ bool TinyGLTF::WriteGltfSceneToFile(Model *model, const std::string &filename,
|
||||
// BUFFERS
|
||||
std::vector<std::string> usedUris;
|
||||
std::vector<unsigned char> binBuffer;
|
||||
json buffers;
|
||||
JsonReserveArray(buffers, model->buffers.size());
|
||||
for (unsigned int i = 0; i < model->buffers.size(); ++i) {
|
||||
json buffer;
|
||||
if (writeBinary && i == 0 && model->buffers[i].uri.empty()) {
|
||||
SerializeGltfBufferBin(model->buffers[i], buffer, binBuffer);
|
||||
} else if (embedBuffers) {
|
||||
SerializeGltfBuffer(model->buffers[i], buffer);
|
||||
} else {
|
||||
std::string binSavePath;
|
||||
std::string binUri;
|
||||
if (!model->buffers[i].uri.empty() && !IsDataURI(model->buffers[i].uri)) {
|
||||
binUri = model->buffers[i].uri;
|
||||
if (model->buffers.size()) {
|
||||
json buffers;
|
||||
JsonReserveArray(buffers, model->buffers.size());
|
||||
for (unsigned int i = 0; i < model->buffers.size(); ++i) {
|
||||
json buffer;
|
||||
if (writeBinary && i==0 && model->buffers[i].uri.empty()){
|
||||
SerializeGltfBufferBin(model->buffers[i], buffer,binBuffer);
|
||||
} else if (embedBuffers) {
|
||||
SerializeGltfBuffer(model->buffers[i], buffer);
|
||||
} else {
|
||||
binUri = defaultBinFilename + defaultBinFileExt;
|
||||
bool inUse = true;
|
||||
int numUsed = 0;
|
||||
while (inUse) {
|
||||
inUse = false;
|
||||
for (const std::string &usedName : usedUris) {
|
||||
if (binUri.compare(usedName) != 0) continue;
|
||||
inUse = true;
|
||||
binUri = defaultBinFilename + std::to_string(numUsed++) +
|
||||
defaultBinFileExt;
|
||||
break;
|
||||
std::string binSavePath;
|
||||
std::string binUri;
|
||||
if (!model->buffers[i].uri.empty() && !IsDataURI(model->buffers[i].uri)) {
|
||||
binUri = model->buffers[i].uri;
|
||||
} else {
|
||||
binUri = defaultBinFilename + defaultBinFileExt;
|
||||
bool inUse = true;
|
||||
int numUsed = 0;
|
||||
while (inUse) {
|
||||
inUse = false;
|
||||
for (const std::string &usedName : usedUris) {
|
||||
if (binUri.compare(usedName) != 0) continue;
|
||||
inUse = true;
|
||||
binUri = defaultBinFilename + std::to_string(numUsed++) +
|
||||
defaultBinFileExt;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
usedUris.push_back(binUri);
|
||||
binSavePath = JoinPath(baseDir, binUri);
|
||||
if (!SerializeGltfBuffer(model->buffers[i], buffer, binSavePath,
|
||||
binUri)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
usedUris.push_back(binUri);
|
||||
binSavePath = JoinPath(baseDir, binUri);
|
||||
if (!SerializeGltfBuffer(model->buffers[i], buffer, binSavePath,
|
||||
binUri)) {
|
||||
return false;
|
||||
}
|
||||
JsonPushBack(buffers, std::move(buffer));
|
||||
}
|
||||
JsonPushBack(buffers, std::move(buffer));
|
||||
}
|
||||
JsonAddMember(output, "buffers", std::move(buffers));
|
||||
}
|
||||
|
||||
// IMAGES
|
||||
if (model->images.size()) {
|
||||
|
||||
Reference in New Issue
Block a user