From e1cbdd1d61ffb04ae156fe67923ec5e0871021f1 Mon Sep 17 00:00:00 2001 From: ladevieq Date: Sun, 15 Aug 2021 23:17:06 +0200 Subject: Improve gltf loading --- src/scene.cpp | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) (limited to 'src/scene.cpp') diff --git a/src/scene.cpp b/src/scene.cpp index 73c73e5..15cea11 100644 --- a/src/scene.cpp +++ b/src/scene.cpp @@ -1,6 +1,7 @@ #include "scene.hpp" #include +#include #include "utils.hpp" #include "gltf.hpp" @@ -69,23 +70,42 @@ scene::scene(camera cam, uint32_t width, uint32_t height) :meta(cam, width, height){ + // auto bistro_interior_path = std::filesystem::path("../models/BistroInterior"); + // auto bistro_interior_filename = std::string("BistroInterior.gltf"); + // auto root_node = gltf::load(bistro_interior_path, bistro_interior_filename); + auto sponza_path = std::filesystem::path("../models/sponza"); auto sponza_filename = std::string("Sponza.gltf"); - auto mesh = gltf::load(sponza_path, sponza_filename); + auto root_node = gltf::load(sponza_path, sponza_filename); + + std::queue nodes_to_load {}; + nodes_to_load.push(root_node); + + while(nodes_to_load.size() != 0) { + auto &node = nodes_to_load.front(); - auto node = mesh.nodes[0]; - for (auto& mesh_part: node) { - for (size_t i = 0; i < mesh_part.indices.size(); i += 3) { - auto i1 = mesh_part.indices[i]; - auto i2 = mesh_part.indices[i + 1]; - auto i3 = mesh_part.indices[i + 2]; + if (node.mesh) { + auto &mesh = node.mesh.value(); + for (auto& mesh_part: mesh.parts) { + for (size_t i = 0; i < mesh_part.indices.size(); i += 3) { + auto i1 = mesh_part.indices[i]; + auto i2 = mesh_part.indices[i + 1]; + auto i3 = mesh_part.indices[i + 2]; - auto v1 = vertex { .position = mesh_part.positions[i1] }; - auto v2 = vertex { .position = mesh_part.positions[i2] }; - auto v3 = vertex { .position = mesh_part.positions[i3] }; + auto v1 = vertex { .position = mesh_part.positions[i1] }; + auto v2 = vertex { .position = mesh_part.positions[i2] }; + auto v3 = vertex { .position = mesh_part.positions[i3] }; - triangles.emplace_back(v1, v2, v3); + triangles.emplace_back(v1, v2, v3); + } + } } + + for (auto &node: node.children) { + nodes_to_load.push(node); + } + + nodes_to_load.pop(); } // random_scene(); -- cgit v1.3