#include <algorithm>
#include <cstdio>
#include <ranges>
#include <string>
namespace {
if (const auto rgb = spectrum->as_rgb()) {
return "rgb(" + std::to_string(rgb->
r()) +
", " + std::to_string(rgb->
g()) +
", " +
std::to_string(rgb->
b()) +
")";
}
if (const auto builtin = spectrum->as_builtin()) {
}
if (const auto constant = spectrum->as_constant()) { return "constant " + std::to_string(*constant); }
}
const auto texture = textures.
at(handle);
return texture ? "texture \"" + std::string(texture->name().to_string_view()) + "\"" : "<missing texture>";
}
}
int main(const int argc, const char* const* const argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: %s <scene.pbrt> [material name]\n", argv[0]);
return 1;
}
if (!scene) {
std::fprintf(stderr,
"cannot load %s: %s\n", argv[1],
tpbrt::to_string(scene.get_error()).c_str());
return 1;
}
auto named = scene->materials() | std::views::filter([](
const tpbrt::material m) {
return !m.
name().empty(); });
std::printf("%zu materials, %zu named:\n", static_cast<std::size_t>(scene->materials().size()),
static_cast<std::size_t>(std::ranges::distance(named)));
std::printf(" %-8.*s %s\n", static_cast<int>(material.name().size()), material.name().data(),
}
const std::string name = argc > 2 ? argv[2] : "gold";
const auto material = scene->materials().at(name);
if (!material) {
std::printf(
"no material \"%s\": %s\n", name.c_str(),
tpbrt::to_string(material.get_error()).c_str());
return 0;
}
std::printf(
"\n\"%s\" is %s\n", name.c_str(),
tpbrt::to_string(material->type()).data());
if (const auto diffuse = material->as_diffuse()) {
std::printf(" reflectance: %s\n", describe(scene->textures(), diffuse->reflectance()).c_str());
}
if (const auto conductor = material->as_conductor()) {
if (const auto eta_k = conductor->as_eta_k()) {
std::printf(" eta: %s\n k: %s\n", describe(scene->textures(), eta_k->eta()).c_str(),
describe(scene->textures(), eta_k->k()).c_str());
}
if (const auto reflectance = conductor->as_reflectance()) {
std::printf(" reflectance: %s\n", describe(scene->textures(), *reflectance).c_str());
}
std::printf(" roughness: %s\n", describe(scene->textures(), conductor->roughness_params().roughness()).c_str());
}
const auto users = std::ranges::count_if(scene->shapes(), [&](
const tpbrt::shape shape) {
const auto used = scene->materials().at(shape.material());
return used && used->name() == name;
});
std::printf(" used by %zu shape(s)\n", static_cast<std::size_t>(users));
return 0;
}
constexpr std::string_view to_string(const accelerator_type type) noexcept
Converts accelerator_type enum value to string.
Definition accelerator.hpp:39
result< scene > load_scene_from_file(const std::string &filepath)
Loads scene from given file.
Definition scene.hpp:198
C++ wrapper for tpbrt_material_t.
Definition material.hpp:762
constexpr string name() const noexcept
Returns name value.
Definition material.hpp:770
C++ wrapper for tpbrt_shape_t.
Definition shape.hpp:382
C++ wrapper for tpbrt_texture_handle_t.
Definition texture.hpp:53
constexpr std::optional< spectrum > as_spectrum() const noexcept
Returns texture handle value as spectrum.
Definition texture.hpp:79
C++ wrapper for tpbrt_textures_list_t.
Definition texture.hpp:933
result< element_type > at(const texture_handle handle) const noexcept
Returns texture pointed by given handle.
Definition texture.hpp:944
constexpr float_t g() const noexcept
Returns g value.
Definition math.hpp:146
constexpr float_t b() const noexcept
Returns b value.
Definition math.hpp:152
constexpr float_t r() const noexcept
Returns r value.
Definition math.hpp:140