mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-08-13 20:35:58 +08:00
Format source.
This commit is contained in:
parent
0a3b09f33e
commit
9a478238c7
@ -1,13 +1,13 @@
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
|
||||
#define TINYGLTF_LOADER_IMPLEMENTATION
|
||||
#define STB_IMAGE_IMPLEMENTATION
|
||||
#include "../../tiny_gltf_loader.h"
|
||||
|
||||
static std::string GetFilePathExtension(const std::string &filename) {
|
||||
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 "";
|
||||
@ -40,14 +40,14 @@ static std::string EncodeType(int ty) {
|
||||
return "**UNKNOWN**";
|
||||
}
|
||||
|
||||
|
||||
// http://www.adp-gmbh.ch/cpp/common/base64.html
|
||||
static const std::string base64_chars =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
|
||||
std::string base64_encode(unsigned char const* bytes_to_encode,
|
||||
unsigned int in_len) {
|
||||
std::string ret;
|
||||
int i = 0;
|
||||
int j = 0;
|
||||
@ -58,49 +58,49 @@ std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_
|
||||
char_array_3[i++] = *(bytes_to_encode++);
|
||||
if (i == 3) {
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[1] =
|
||||
((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] =
|
||||
((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for(i = 0; (i <4) ; i++)
|
||||
ret += base64_chars[char_array_4[i]];
|
||||
for (i = 0; (i < 4); i++) ret += base64_chars[char_array_4[i]];
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (i)
|
||||
{
|
||||
for(j = i; j < 3; j++)
|
||||
char_array_3[j] = '\0';
|
||||
if (i) {
|
||||
for (j = i; j < 3; j++) char_array_3[j] = '\0';
|
||||
|
||||
char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
|
||||
char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[1] =
|
||||
((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
|
||||
char_array_4[2] =
|
||||
((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
|
||||
char_array_4[3] = char_array_3[2] & 0x3f;
|
||||
|
||||
for (j = 0; (j < i + 1); j++)
|
||||
ret += base64_chars[char_array_4[j]];
|
||||
|
||||
while((i++ < 3))
|
||||
ret += '=';
|
||||
for (j = 0; (j < i + 1); j++) ret += base64_chars[char_array_4[j]];
|
||||
|
||||
while ((i++ < 3)) ret += '=';
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
bool EncodeBuffers(picojson::object* o, const std::map<std::string, tinygltf::Buffer>& buffers)
|
||||
{
|
||||
bool EncodeBuffers(picojson::object* o,
|
||||
const std::map<std::string, tinygltf::Buffer>& buffers) {
|
||||
std::map<std::string, tinygltf::Buffer>::const_iterator it(buffers.begin());
|
||||
std::map<std::string, tinygltf::Buffer>::const_iterator itEnd(buffers.end());
|
||||
for (; it != itEnd; it++) {
|
||||
// @todo { Support external file resource. }
|
||||
picojson::object buf;
|
||||
std::string b64_data = base64_encode(it->second.data.data(), it->second.data.size());
|
||||
std::string b64_data =
|
||||
base64_encode(it->second.data.data(), it->second.data.size());
|
||||
buf["type"] = picojson::value("arraybuffer");
|
||||
buf["uri"] = picojson::value(std::string("data:application/octet-stream;base64,") + b64_data);
|
||||
buf["byteLength"] = picojson::value(static_cast<double>(it->second.data.size()));
|
||||
buf["uri"] = picojson::value(
|
||||
std::string("data:application/octet-stream;base64,") + b64_data);
|
||||
buf["byteLength"] =
|
||||
picojson::value(static_cast<double>(it->second.data.size()));
|
||||
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
@ -108,28 +108,30 @@ bool EncodeBuffers(picojson::object* o, const std::map<std::string, tinygltf::Bu
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeBufferViews(picojson::object* o, const std::map<std::string, tinygltf::BufferView>& bufferViews)
|
||||
{
|
||||
std::map<std::string, tinygltf::BufferView>::const_iterator it(
|
||||
bufferViews.begin());
|
||||
std::map<std::string, tinygltf::BufferView>::const_iterator itEnd(
|
||||
bufferViews.end());
|
||||
bool EncodeBufferViews(
|
||||
picojson::object* o,
|
||||
const std::map<std::string, tinygltf::BufferView>& bufferViews) {
|
||||
std::map<std::string, tinygltf::BufferView>::const_iterator it(
|
||||
bufferViews.begin());
|
||||
std::map<std::string, tinygltf::BufferView>::const_iterator itEnd(
|
||||
bufferViews.end());
|
||||
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object buf;
|
||||
buf["buffer"] = picojson::value(it->second.buffer);
|
||||
buf["byteLength"] = picojson::value(static_cast<double>(it->second.byteLength));
|
||||
buf["byteOffset"] = picojson::value(static_cast<double>(it->second.byteOffset));
|
||||
buf["target"] = picojson::value(static_cast<double>(it->second.target));
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object buf;
|
||||
buf["buffer"] = picojson::value(it->second.buffer);
|
||||
buf["byteLength"] =
|
||||
picojson::value(static_cast<double>(it->second.byteLength));
|
||||
buf["byteOffset"] =
|
||||
picojson::value(static_cast<double>(it->second.byteOffset));
|
||||
buf["target"] = picojson::value(static_cast<double>(it->second.target));
|
||||
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
|
||||
return true;
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
|
||||
bool EncodeFloatArray(picojson::array *arr, const std::vector<double>& values)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeFloatArray(picojson::array* arr, const std::vector<double>& values) {
|
||||
for (size_t i = 0; i < values.size(); i++) {
|
||||
arr->push_back(picojson::value(values[i]));
|
||||
}
|
||||
@ -137,8 +139,8 @@ bool EncodeFloatArray(picojson::array *arr, const std::vector<double>& values)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeStringArray(picojson::array *arr, const std::vector<std::string>& values)
|
||||
{
|
||||
bool EncodeStringArray(picojson::array* arr,
|
||||
const std::vector<std::string>& values) {
|
||||
for (size_t i = 0; i < values.size(); i++) {
|
||||
arr->push_back(picojson::value(values[i]));
|
||||
}
|
||||
@ -146,7 +148,7 @@ bool EncodeStringArray(picojson::array *arr, const std::vector<std::string>& val
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeNode(picojson::object *o, const tinygltf::Node &node) {
|
||||
bool EncodeNode(picojson::object* o, const tinygltf::Node& node) {
|
||||
(*o)["name"] = picojson::value(node.name);
|
||||
(*o)["camera"] = picojson::value(node.camera);
|
||||
|
||||
@ -187,76 +189,82 @@ bool EncodeNode(picojson::object *o, const tinygltf::Node &node) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeNodes(picojson::object* o, const std::map<std::string, tinygltf::Node> &nodes)
|
||||
{
|
||||
std::map<std::string, tinygltf::Node>::const_iterator it(nodes.begin());
|
||||
std::map<std::string, tinygltf::Node>::const_iterator itEnd(nodes.end());
|
||||
bool EncodeNodes(picojson::object* o,
|
||||
const std::map<std::string, tinygltf::Node>& nodes) {
|
||||
std::map<std::string, tinygltf::Node>::const_iterator it(nodes.begin());
|
||||
std::map<std::string, tinygltf::Node>::const_iterator itEnd(nodes.end());
|
||||
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object node;
|
||||
EncodeNode(&node, it->second);
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object node;
|
||||
EncodeNode(&node, it->second);
|
||||
|
||||
(*o)[it->first] = picojson::value(node);
|
||||
}
|
||||
|
||||
return true;
|
||||
(*o)[it->first] = picojson::value(node);
|
||||
}
|
||||
|
||||
bool EncodeScenes(picojson::object* o, const std::map<std::string, std::vector<std::string> >& scenes)
|
||||
{
|
||||
std::map<std::string, std::vector<std::string> >::const_iterator it(
|
||||
scenes.begin());
|
||||
std::map<std::string, std::vector<std::string> >::const_iterator itEnd(
|
||||
scenes.end());
|
||||
|
||||
for (; it != itEnd; it++) {
|
||||
|
||||
picojson::object buf;
|
||||
picojson::array arr;
|
||||
for (size_t i = 0; i < it->second.size(); i++) {
|
||||
arr.push_back(picojson::value(it->second[i]));
|
||||
}
|
||||
|
||||
buf["nodes"] = picojson::value(arr);
|
||||
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeAccessors(picojson::object* o, const std::map<std::string, tinygltf::Accessor>& accessors)
|
||||
{
|
||||
std::map<std::string, tinygltf::Accessor>::const_iterator it(accessors.begin());
|
||||
std::map<std::string, tinygltf::Accessor>::const_iterator itEnd(
|
||||
accessors.end());
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object buf;
|
||||
buf["bufferView"] = picojson::value(it->second.bufferView);
|
||||
buf["byteOffset"] = picojson::value(static_cast<double>(it->second.byteOffset));
|
||||
buf["byteStride"] = picojson::value(static_cast<double>(it->second.byteStride));
|
||||
buf["componentType"] = picojson::value(static_cast<double>(it->second.componentType));
|
||||
buf["count"] = picojson::value(static_cast<double>(it->second.count));
|
||||
buf["type"] = picojson::value(EncodeType(it->second.type));
|
||||
|
||||
if (!it->second.minValues.empty()) {
|
||||
picojson::array arr;
|
||||
EncodeFloatArray(&arr, it->second.minValues);
|
||||
buf["min"] = picojson::value(arr);
|
||||
}
|
||||
if (!it->second.maxValues.empty()) {
|
||||
picojson::array arr;
|
||||
EncodeFloatArray(&arr, it->second.maxValues);
|
||||
buf["max"] = picojson::value(arr);
|
||||
}
|
||||
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeScenes(
|
||||
picojson::object* o,
|
||||
const std::map<std::string, std::vector<std::string> >& scenes) {
|
||||
std::map<std::string, std::vector<std::string> >::const_iterator it(
|
||||
scenes.begin());
|
||||
std::map<std::string, std::vector<std::string> >::const_iterator itEnd(
|
||||
scenes.end());
|
||||
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object buf;
|
||||
picojson::array arr;
|
||||
for (size_t i = 0; i < it->second.size(); i++) {
|
||||
arr.push_back(picojson::value(it->second[i]));
|
||||
}
|
||||
|
||||
buf["nodes"] = picojson::value(arr);
|
||||
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
|
||||
bool EncodePrimitive(picojson::object* o, const tinygltf::Primitive &primitive) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeAccessors(
|
||||
picojson::object* o,
|
||||
const std::map<std::string, tinygltf::Accessor>& accessors) {
|
||||
std::map<std::string, tinygltf::Accessor>::const_iterator it(
|
||||
accessors.begin());
|
||||
std::map<std::string, tinygltf::Accessor>::const_iterator itEnd(
|
||||
accessors.end());
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object buf;
|
||||
buf["bufferView"] = picojson::value(it->second.bufferView);
|
||||
buf["byteOffset"] =
|
||||
picojson::value(static_cast<double>(it->second.byteOffset));
|
||||
buf["byteStride"] =
|
||||
picojson::value(static_cast<double>(it->second.byteStride));
|
||||
buf["componentType"] =
|
||||
picojson::value(static_cast<double>(it->second.componentType));
|
||||
buf["count"] = picojson::value(static_cast<double>(it->second.count));
|
||||
buf["type"] = picojson::value(EncodeType(it->second.type));
|
||||
|
||||
if (!it->second.minValues.empty()) {
|
||||
picojson::array arr;
|
||||
EncodeFloatArray(&arr, it->second.minValues);
|
||||
buf["min"] = picojson::value(arr);
|
||||
}
|
||||
if (!it->second.maxValues.empty()) {
|
||||
picojson::array arr;
|
||||
EncodeFloatArray(&arr, it->second.maxValues);
|
||||
buf["max"] = picojson::value(arr);
|
||||
}
|
||||
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodePrimitive(picojson::object* o,
|
||||
const tinygltf::Primitive& primitive) {
|
||||
(*o)["material"] = picojson::value(primitive.material);
|
||||
(*o)["indices"] = picojson::value(primitive.indices);
|
||||
(*o)["mode"] = picojson::value(static_cast<double>(primitive.mode));
|
||||
@ -277,30 +285,30 @@ bool EncodePrimitive(picojson::object* o, const tinygltf::Primitive &primitive)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EncodeMeshes(picojson::object* o, const std::map<std::string, tinygltf::Mesh>& meshes)
|
||||
{
|
||||
std::map<std::string, tinygltf::Mesh>::const_iterator it(meshes.begin());
|
||||
std::map<std::string, tinygltf::Mesh>::const_iterator itEnd(meshes.end());
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object buf;
|
||||
bool EncodeMeshes(picojson::object* o,
|
||||
const std::map<std::string, tinygltf::Mesh>& meshes) {
|
||||
std::map<std::string, tinygltf::Mesh>::const_iterator it(meshes.begin());
|
||||
std::map<std::string, tinygltf::Mesh>::const_iterator itEnd(meshes.end());
|
||||
for (; it != itEnd; it++) {
|
||||
picojson::object buf;
|
||||
|
||||
buf["name"] = picojson::value(it->second.name);
|
||||
buf["name"] = picojson::value(it->second.name);
|
||||
|
||||
picojson::array arr;
|
||||
for (size_t i = 0; i < it->second.primitives.size(); i++) {
|
||||
picojson::object primitive;
|
||||
EncodePrimitive(&primitive, it->second.primitives[i]);
|
||||
arr.push_back(picojson::value(primitive));
|
||||
}
|
||||
buf["primitives"] = picojson::value(arr);
|
||||
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
picojson::array arr;
|
||||
for (size_t i = 0; i < it->second.primitives.size(); i++) {
|
||||
picojson::object primitive;
|
||||
EncodePrimitive(&primitive, it->second.primitives[i]);
|
||||
arr.push_back(picojson::value(primitive));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
buf["primitives"] = picojson::value(arr);
|
||||
|
||||
bool SaveGLTF(const std::string& output_filename, const tinygltf::Scene& scene)
|
||||
{
|
||||
(*o)[it->first] = picojson::value(buf);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool SaveGLTF(const std::string& output_filename,
|
||||
const tinygltf::Scene& scene) {
|
||||
picojson::object root;
|
||||
|
||||
{
|
||||
@ -358,7 +366,6 @@ bool SaveGLTF(const std::string& output_filename, const tinygltf::Scene& scene)
|
||||
root["scenes"] = picojson::value(scenes);
|
||||
}
|
||||
|
||||
|
||||
// @todo {}
|
||||
picojson::object shaders;
|
||||
picojson::object programs;
|
||||
@ -388,11 +395,7 @@ bool SaveGLTF(const std::string& output_filename, const tinygltf::Scene& scene)
|
||||
|
||||
// ----------------------------------------------------------------
|
||||
|
||||
int
|
||||
main(
|
||||
int argc,
|
||||
char **argv)
|
||||
{
|
||||
int main(int argc, char** argv) {
|
||||
if (argc < 3) {
|
||||
printf("Needs input.gltf output.gltf\n");
|
||||
exit(1);
|
||||
@ -425,5 +428,4 @@ main(
|
||||
ret = SaveGLTF(argv[2], scene);
|
||||
|
||||
return ret ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user