mirror of
https://git.mirrors.martin98.com/https://github.com/syoyo/tinygltf.git
synced 2025-08-16 17:46:00 +08:00
commit
70d8b7a42b
9
examples/gltfutil/CMakeLists.txt
Normal file
9
examples/gltfutil/CMakeLists.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.6)
|
||||||
|
project(gltfutil)
|
||||||
|
|
||||||
|
set(CMAKE_CXX_STANDARD 11)
|
||||||
|
|
||||||
|
include_directories(../../)
|
||||||
|
|
||||||
|
file(GLOB gltfutil_sources *.cc *.h)
|
||||||
|
add_executable(gltfutil ${gltfutil_sources})
|
60
examples/gltfutil/gltfuilconfig.h
Normal file
60
examples/gltfutil/gltfuilconfig.h
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <array>
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "texture_dumper.h"
|
||||||
|
|
||||||
|
namespace gltfutil {
|
||||||
|
|
||||||
|
enum class ui_mode { cli, interactive };
|
||||||
|
enum class cli_action { not_set, help, dump };
|
||||||
|
enum class FileType { Ascii, Binary, Unknown };
|
||||||
|
|
||||||
|
/// Probe inside the file, or check the extension to determine if we have to
|
||||||
|
/// load a text file, or a binary file
|
||||||
|
FileType detectType(const std::string& path) {
|
||||||
|
// Quickly open the file as binary and chekc if there's the gltf binary magic
|
||||||
|
// number
|
||||||
|
{
|
||||||
|
auto probe = std::ifstream(path, std::ios_base::binary);
|
||||||
|
if (!probe) throw std::runtime_error("Could not open " + path);
|
||||||
|
|
||||||
|
std::array<char, 5> buffer;
|
||||||
|
for (size_t i{0}; i < 4; ++i) probe >> buffer[i];
|
||||||
|
buffer[4] = 0;
|
||||||
|
|
||||||
|
if (std::string("glTF") == std::string(buffer.data())) {
|
||||||
|
std::cout
|
||||||
|
<< "Detected binary file thanks to the magic number at the start!\n";
|
||||||
|
return FileType::Binary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we don't have any better, check the file extension.
|
||||||
|
auto extension = path.substr(path.find_last_of('.') + 1);
|
||||||
|
std::transform(std::begin(extension), std::end(extension),
|
||||||
|
std::begin(extension),
|
||||||
|
[](char c) { return char(::tolower(int(c))); });
|
||||||
|
if (extension == "gltf") return FileType::Ascii;
|
||||||
|
if (extension == "glb") return FileType::Binary;
|
||||||
|
|
||||||
|
return FileType::Unknown;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct configuration {
|
||||||
|
std::string input_path, output_dir;
|
||||||
|
ui_mode mode;
|
||||||
|
cli_action action = cli_action::not_set;
|
||||||
|
texture_dumper::texture_output_format requested_format =
|
||||||
|
texture_dumper::texture_output_format::not_specified;
|
||||||
|
|
||||||
|
bool has_output_dir;
|
||||||
|
bool is_valid() {
|
||||||
|
// TODO impl check
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace gltfutil
|
123
examples/gltfutil/main.cc
Normal file
123
examples/gltfutil/main.cc
Normal file
@ -0,0 +1,123 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include "gltfuilconfig.h"
|
||||||
|
#include "texture_dumper.h"
|
||||||
|
|
||||||
|
#define TINYGLTF_IMPLEMENTATION
|
||||||
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
|
#include <tiny_gltf.h>
|
||||||
|
|
||||||
|
#define STB_IMAGE_WRITE_IMPLEMENTATION
|
||||||
|
#include "stb_image_write.h"
|
||||||
|
|
||||||
|
namespace gltfutil {
|
||||||
|
int usage(int ret = 0) {
|
||||||
|
using std::cout;
|
||||||
|
cout << "gltfutil: tool for manipulating gltf files\n"
|
||||||
|
<< " usage information:\n\n"
|
||||||
|
<< "\t gltfutil (-d|-h|) (-f [png|bmp|tga]) [path to .gltf/glb] (-o "
|
||||||
|
"[path to output directory])\n\n"
|
||||||
|
//<< "\t\t -i: start in interactive mode\n"
|
||||||
|
<< "\t\t -d: dump enclosed content (image assets)\n"
|
||||||
|
<< "\t\t -f: file format for image output"
|
||||||
|
<< "\t\t -o: ouptput directory path"
|
||||||
|
<< "\t\t -h: print this help\n";
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
int arg_error() {
|
||||||
|
(void)usage();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int parse_args(int argc, char** argv) {
|
||||||
|
gltfutil::configuration config;
|
||||||
|
|
||||||
|
for (size_t i = 1; i < size_t(argc); ++i) {
|
||||||
|
char* arg = argv[i];
|
||||||
|
if (arg[0] == '-') switch (arg[1]) {
|
||||||
|
case 'h':
|
||||||
|
return usage(0);
|
||||||
|
break;
|
||||||
|
case 'd':
|
||||||
|
config.mode = ui_mode::cli;
|
||||||
|
config.action = cli_action::dump;
|
||||||
|
break;
|
||||||
|
case 'i':
|
||||||
|
config.mode = ui_mode::interactive;
|
||||||
|
break;
|
||||||
|
case 'o':
|
||||||
|
i++;
|
||||||
|
config.output_dir = argv[i];
|
||||||
|
break;
|
||||||
|
case 'f':
|
||||||
|
i++;
|
||||||
|
config.requested_format =
|
||||||
|
texture_dumper::get_fromat_from_string(argv[i]);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return arg_error();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// set the input path
|
||||||
|
config.input_path = argv[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (config.is_valid()) {
|
||||||
|
tinygltf::TinyGLTF loader;
|
||||||
|
tinygltf::Model model;
|
||||||
|
std::string error;
|
||||||
|
bool state;
|
||||||
|
switch (detectType(config.input_path)) {
|
||||||
|
case FileType::Ascii:
|
||||||
|
state = loader.LoadASCIIFromFile(&model, &error, config.input_path);
|
||||||
|
break;
|
||||||
|
case FileType::Binary:
|
||||||
|
state = loader.LoadBinaryFromFile(&model, &error, config.input_path);
|
||||||
|
break;
|
||||||
|
case FileType::Unknown:
|
||||||
|
default:
|
||||||
|
return arg_error();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cerr << "state is " << state << '\n';
|
||||||
|
|
||||||
|
if (config.mode == ui_mode::interactive) {
|
||||||
|
// interactive usage now;
|
||||||
|
// TODO impl
|
||||||
|
} else {
|
||||||
|
switch (config.action) {
|
||||||
|
case cli_action::help:
|
||||||
|
return usage();
|
||||||
|
break;
|
||||||
|
|
||||||
|
case cli_action::dump: {
|
||||||
|
texture_dumper dumper(model);
|
||||||
|
if (config.requested_format !=
|
||||||
|
texture_dumper::texture_output_format::not_specified)
|
||||||
|
dumper.set_output_format(config.requested_format);
|
||||||
|
|
||||||
|
if (config.output_dir.empty())
|
||||||
|
dumper.dump_to_folder();
|
||||||
|
else
|
||||||
|
dumper.dump_to_folder(config.output_dir);
|
||||||
|
|
||||||
|
} break;
|
||||||
|
default:
|
||||||
|
return arg_error();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return arg_error();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
} // namespace gltfutil
|
||||||
|
|
||||||
|
int main(int argc, char* argv[]) {
|
||||||
|
if (argc == 1) return gltfutil::usage();
|
||||||
|
if (argc > 1) return gltfutil::parse_args(argc, argv);
|
||||||
|
}
|
1830
examples/gltfutil/stb_image_write.h
Normal file
1830
examples/gltfutil/stb_image_write.h
Normal file
File diff suppressed because it is too large
Load Diff
67
examples/gltfutil/texture_dumper.cc
Normal file
67
examples/gltfutil/texture_dumper.cc
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "stb_image_write.h"
|
||||||
|
#include "texture_dumper.h"
|
||||||
|
|
||||||
|
#include <tiny_gltf.h>
|
||||||
|
|
||||||
|
using namespace gltfutil;
|
||||||
|
using namespace tinygltf;
|
||||||
|
using std::cout;
|
||||||
|
|
||||||
|
texture_dumper::texture_dumper(const Model& input)
|
||||||
|
: model(input), configured_format(texture_output_format::png) {
|
||||||
|
cout << "Texture dumper\n";
|
||||||
|
}
|
||||||
|
|
||||||
|
void texture_dumper::dump_to_folder(const std::string& path) {
|
||||||
|
cout << "dumping to folder " << path << '\n';
|
||||||
|
cout << "model file has " << model.textures.size() << " textures.\n";
|
||||||
|
size_t index = 0;
|
||||||
|
|
||||||
|
for (const auto& texture : model.textures) {
|
||||||
|
index++;
|
||||||
|
const auto& image = model.images[texture.source];
|
||||||
|
cout << "image name is: \"" << image.name << "\"\n";
|
||||||
|
cout << "image size is: " << image.width << 'x' << image.height << '\n';
|
||||||
|
cout << "pixel channel count :" << image.component << '\n';
|
||||||
|
std::string name = image.name.empty() ? std::to_string(index) : image.name;
|
||||||
|
|
||||||
|
switch (configured_format) {
|
||||||
|
case texture_output_format::png:
|
||||||
|
name = path + "/" + name + ".png";
|
||||||
|
std::cout << "Image will be written to " << name << '\n';
|
||||||
|
stbi_write_png(name.c_str(), image.width, image.height, image.component,
|
||||||
|
image.image.data(), 0);
|
||||||
|
break;
|
||||||
|
case texture_output_format::bmp:
|
||||||
|
std::cout << "Image will be written to " << name << '\n';
|
||||||
|
name = path + "/" + name + ".bmp";
|
||||||
|
stbi_write_bmp(name.c_str(), image.width, image.height, image.component,
|
||||||
|
image.image.data());
|
||||||
|
break;
|
||||||
|
case texture_output_format::tga:
|
||||||
|
std::cout << "Image will be written to " << name << '\n';
|
||||||
|
name = path + "/" + name + ".tga";
|
||||||
|
stbi_write_tga(name.c_str(), image.width, image.height, image.component,
|
||||||
|
image.image.data());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void texture_dumper::set_output_format(texture_output_format format) {
|
||||||
|
configured_format = format;
|
||||||
|
}
|
||||||
|
|
||||||
|
texture_dumper::texture_output_format texture_dumper::get_fromat_from_string(
|
||||||
|
const std::string& str) {
|
||||||
|
std::string type = str;
|
||||||
|
std::transform(str.begin(), str.end(), type.begin(), ::tolower);
|
||||||
|
|
||||||
|
if (type == "png") return texture_output_format::png;
|
||||||
|
if (type == "bmp") return texture_output_format::bmp;
|
||||||
|
if (type == "tga") return texture_output_format::tga;
|
||||||
|
|
||||||
|
return texture_output_format::not_specified;
|
||||||
|
}
|
23
examples/gltfutil/texture_dumper.h
Normal file
23
examples/gltfutil/texture_dumper.h
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <tiny_gltf.h>
|
||||||
|
|
||||||
|
namespace gltfutil {
|
||||||
|
class texture_dumper {
|
||||||
|
public:
|
||||||
|
enum class texture_output_format { png, bmp, tga, not_specified };
|
||||||
|
|
||||||
|
private:
|
||||||
|
const tinygltf::Model& model;
|
||||||
|
texture_output_format configured_format;
|
||||||
|
|
||||||
|
public:
|
||||||
|
texture_dumper(const tinygltf::Model& inputModel);
|
||||||
|
void dump_to_folder(const std::string& path = "./");
|
||||||
|
void set_output_format(texture_output_format format);
|
||||||
|
|
||||||
|
static texture_output_format get_fromat_from_string(const std::string& str);
|
||||||
|
};
|
||||||
|
} // namespace gltfutil
|
Loading…
x
Reference in New Issue
Block a user