Skip to content

Commit

Permalink
Add textractor executable to dump model textures to file
Browse files Browse the repository at this point in the history
  • Loading branch information
Nelarius committed Nov 8, 2023
1 parent 6338087 commit 1706265
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Expand Up @@ -89,3 +89,13 @@ add_custom_command(
$<TARGET_FILE_DIR:tests>/Duck.glb
COMMENT "Copying $<TARGET_FILE_DIR:tests>/Duck.glb"
)

add_executable(textractor src/textractor/main.cpp)
target_link_libraries(textractor PRIVATE common glm::glm)
add_custom_command(
TARGET textractor POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_SOURCE_DIR}/assets/Duck.glb
$<TARGET_FILE_DIR:textractor>/Duck.glb
COMMENT "Copying $<TARGET_FILE_DIR:textractor>/Duck.glb"
)
42 changes: 42 additions & 0 deletions src/textractor/main.cpp
@@ -0,0 +1,42 @@
#include <common/gltf_model.hpp>
#include <common/texture.hpp>

#include <cassert>
#include <fstream>

int main()
{
const pt::GltfModel model("Duck.glb");

const auto baseColorTextures = model.baseColorTextures();
for (std::size_t i = 0; i < baseColorTextures.size(); ++i)
{
const pt::Texture& texture = baseColorTextures[i];
const auto dimensions = texture.dimensions();
const auto pixels = texture.pixels();

const std::string filename = "base_color_texture_" + std::to_string(i) + ".ppm";
std::ofstream file(filename, std::ios::out | std::ios::binary);
assert(file.is_open());
// Outputs binary PPM image format, https://netpbm.sourceforge.net/doc/ppm.html
file << "P6\n";
file << dimensions.width << ' ' << dimensions.height << '\n';
file << "255\n";
for (std::uint32_t i = 0; i < dimensions.height; ++i)
{
for (std::uint32_t j = 0; j < dimensions.width; ++j)
{
const std::size_t idx = i * dimensions.width + j;
const auto pixel = pixels[idx];

const auto r = static_cast<std::uint8_t>(pixel.r * 255.0f);
const auto g = static_cast<std::uint8_t>(pixel.g * 255.0f);
const auto b = static_cast<std::uint8_t>(pixel.b * 255.0f);

file << r << g << b;
}
}
}

return 0;
}

0 comments on commit 1706265

Please sign in to comment.