tinypbrt 1.1.1
pbrt-v4 scene file parser for C11 with a header-only C++20 wrapper
Loading...
Searching...
No Matches
examples/cpp/quickstart/main.cpp
// C++ quickstart: load a scene, walk over its shapes and print what each one is made of.
// tinypbrt-cpp-example-quickstart <scene.pbrt>
#include <cstdio>
#include <string>
#include <utility>
namespace {
// helper for std::visit with one lambda per alternative
template<class... Fs>
struct overloaded : Fs... {
using Fs::operator()...;
};
template<class... Fs>
overloaded(Fs...) -> overloaded<Fs...>;
} // namespace
int main(const int argc, const char* const* const argv) {
if (argc < 2) {
std::fprintf(stderr, "usage: %s <scene.pbrt>\n", argv[0]);
return 1;
}
// result<scene>: either the loaded scene or an error code (no exceptions)
auto loaded = tpbrt::load_scene_from_file(argv[1]);
if (!loaded) {
std::fprintf(stderr, "cannot load %s: %s\n", argv[1], tpbrt::to_string(loaded.get_error()).c_str());
return 1;
}
// the scene owns the C memory and frees it in its destructor
const tpbrt::scene scene = std::move(*loaded);
std::printf("film %ux%u, %zu shapes, %zu materials, %zu lights\n", scene.film().x_resolution(), scene.film().y_resolution(),
static_cast<std::size_t>(scene.shapes().size()), static_cast<std::size_t>(scene.materials().size()),
static_cast<std::size_t>(scene.lights().lights_sources().size() + scene.lights().area_lights().size()));
for (const tpbrt::shape shape : scene.shapes()) {
// every alternative is a separate type, so the visitor knows exactly which shape it got
const std::string geometry =
shape
.visit(overloaded {
[](const tpbrt::shape_sphere_params& s) { return "sphere r=" + std::to_string(s.radius()); },
[](const tpbrt::shape_disk_params& d) { return "disk r=" + std::to_string(d.radius()); },
return "mesh with " + std::to_string(m.indices().size() / 3) + " triangles";
},
[&](const auto&) { return std::string(tpbrt::to_string(shape.type())); },
})
.value_or("unknown shape");
// handles are resolved by the lists, a missing element is an error code, not a crash
const auto material = scene.materials().at(shape.material());
const std::string material_name = !material ? "<none>" :
material->name().empty() ? std::string(tpbrt::to_string(material->type())) :
std::string(material->name().to_string_view());
std::printf(" %-28s material: %s%s\n", geometry.c_str(), material_name.c_str(),
scene.lights().area_lights().at(shape.area_light()) ? " (emissive)" : "");
}
return 0;
}
Scene loaded from a pbrt-v4 file or string, owning all its memory (move only, freed in the destructor...
Definition scene.hpp:112
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
result< element_type > at(const area_light_handle handle) const noexcept
Returns area light pointed by given handle.
Definition light.hpp:437
constexpr size_t size() const noexcept
Returns size of this array.
Definition common.hpp:489
constexpr uint_t y_resolution() const noexcept
Returns y_resolution value.
Definition film.hpp:193
constexpr uint_t x_resolution() const noexcept
Returns x_resolution value.
Definition film.hpp:190
constexpr area_lights_list area_lights() const noexcept
Returns area_lights value.
Definition light.hpp:468
constexpr lights_sources_list lights_sources() const noexcept
Returns lights_sources value.
Definition light.hpp:465
result< element_type > at(const material_handle handle) const noexcept
Returns material pointed by given handle.
Definition material.hpp:918
constexpr tpbrt::lights_list lights() const noexcept
Returns lights value.
Definition scene.hpp:85
constexpr tpbrt::film film() const noexcept
Returns film value.
Definition scene.hpp:55
constexpr tpbrt::materials_list materials() const noexcept
Returns materials value.
Definition scene.hpp:79
constexpr tpbrt::shapes_list shapes() const noexcept
Returns shapes value.
Definition scene.hpp:88
C++ wrapper for tpbrt_shape_disk_params_t.
Definition shape.hpp:258
C++ wrapper for tpbrt_shape_sphere_params_t.
Definition shape.hpp:284
C++ wrapper for tpbrt_shape_triangle_mesh_params_t.
Definition shape.hpp:308
C++ wrapper for tpbrt_shape_t.
Definition shape.hpp:382