Skip to content

Commit 1706265

Browse files
committed
Add textractor executable to dump model textures to file
1 parent 6338087 commit 1706265

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,13 @@ add_custom_command(
8989
$<TARGET_FILE_DIR:tests>/Duck.glb
9090
COMMENT "Copying $<TARGET_FILE_DIR:tests>/Duck.glb"
9191
)
92+
93+
add_executable(textractor src/textractor/main.cpp)
94+
target_link_libraries(textractor PRIVATE common glm::glm)
95+
add_custom_command(
96+
TARGET textractor POST_BUILD
97+
COMMAND ${CMAKE_COMMAND} -E copy
98+
${CMAKE_SOURCE_DIR}/assets/Duck.glb
99+
$<TARGET_FILE_DIR:textractor>/Duck.glb
100+
COMMENT "Copying $<TARGET_FILE_DIR:textractor>/Duck.glb"
101+
)

src/textractor/main.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <common/gltf_model.hpp>
2+
#include <common/texture.hpp>
3+
4+
#include <cassert>
5+
#include <fstream>
6+
7+
int main()
8+
{
9+
const pt::GltfModel model("Duck.glb");
10+
11+
const auto baseColorTextures = model.baseColorTextures();
12+
for (std::size_t i = 0; i < baseColorTextures.size(); ++i)
13+
{
14+
const pt::Texture& texture = baseColorTextures[i];
15+
const auto dimensions = texture.dimensions();
16+
const auto pixels = texture.pixels();
17+
18+
const std::string filename = "base_color_texture_" + std::to_string(i) + ".ppm";
19+
std::ofstream file(filename, std::ios::out | std::ios::binary);
20+
assert(file.is_open());
21+
// Outputs binary PPM image format, https://netpbm.sourceforge.net/doc/ppm.html
22+
file << "P6\n";
23+
file << dimensions.width << ' ' << dimensions.height << '\n';
24+
file << "255\n";
25+
for (std::uint32_t i = 0; i < dimensions.height; ++i)
26+
{
27+
for (std::uint32_t j = 0; j < dimensions.width; ++j)
28+
{
29+
const std::size_t idx = i * dimensions.width + j;
30+
const auto pixel = pixels[idx];
31+
32+
const auto r = static_cast<std::uint8_t>(pixel.r * 255.0f);
33+
const auto g = static_cast<std::uint8_t>(pixel.g * 255.0f);
34+
const auto b = static_cast<std::uint8_t>(pixel.b * 255.0f);
35+
36+
file << r << g << b;
37+
}
38+
}
39+
}
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)