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