@@ -52,6 +52,7 @@ void Model::load_textures() {
5252 texture.component , 1 , " glTF2 model texture" );
5353 } else {
5454 spdlog::error (" Can't load texture with {} channels!" , texture.component );
55+ spdlog::warn (" Generating error texture as a replacement." );
5556
5657 // Generate an error texture (checkboard pattern of pink and black squares).
5758 m_textures.emplace_back (m_device, wrapper::CpuTexture ());
@@ -119,10 +120,8 @@ void Model::load_node(const tinygltf::Node &start_node, ModelNode *parent, std::
119120 // If the node contains mesh data, we load vertices and indices from the buffers.
120121 // In glTF this is done via accessors and buffer views.
121122 if (start_node.mesh > -1 ) {
122- const tinygltf::Mesh mesh = m_model.meshes [start_node.mesh ];
123-
124123 // Iterate through all primitives of this node's mesh.
125- for (const auto &primitive : mesh.primitives ) {
124+ for (const auto &primitive : m_model. meshes [start_node. mesh ] .primitives ) {
126125 const auto attr = primitive.attributes ;
127126
128127 // Load Vertices.
@@ -134,42 +133,51 @@ void Model::load_node(const tinygltf::Node &start_node, ModelNode *parent, std::
134133
135134 // Get buffer data for vertex normals.
136135 if (attr.find (" POSITION" ) != attr.end ()) {
137- const tinygltf::Accessor &accessor = m_model.accessors [attr.find (" POSITION" )->second ];
138- const tinygltf::BufferView &view = m_model.bufferViews [accessor.bufferView ];
136+ const auto &accessor = m_model.accessors [attr.find (" POSITION" )->second ];
137+ const auto &view = m_model.bufferViews [accessor.bufferView ];
139138 position_buffer = reinterpret_cast <const float *>( // NOLINT
140139 &(m_model.buffers [view.buffer ].data [accessor.byteOffset + view.byteOffset ]));
141140 vertex_count = accessor.count ;
142141 }
143142
144143 // Get buffer data for vertex normals.
145144 if (attr.find (" NORMAL" ) != attr.end ()) {
146- const tinygltf::Accessor &accessor = m_model.accessors [attr.find (" NORMAL" )->second ];
147- const tinygltf::BufferView &view = m_model.bufferViews [accessor.bufferView ];
145+ const auto &accessor = m_model.accessors [attr.find (" NORMAL" )->second ];
146+ const auto &view = m_model.bufferViews [accessor.bufferView ];
148147 normals_buffer = reinterpret_cast <const float *>( // NOLINT
149148 &(m_model.buffers [view.buffer ].data [accessor.byteOffset + view.byteOffset ]));
150149 }
151150
152151 // Get buffer data for vertex texture coordinates.
153- // glTF supports multiple sets, we only load the first one.
152+ // glTF supports multiple sets, we currently only load the first one.
154153 if (attr.find (" TEXCOORD_0" ) != attr.end ()) {
155- const tinygltf::Accessor &accessor = m_model.accessors [attr.find (" TEXCOORD_0" )->second ];
156- const tinygltf::BufferView &view = m_model.bufferViews [accessor.bufferView ];
154+ const auto &accessor = m_model.accessors [attr.find (" TEXCOORD_0" )->second ];
155+ const auto &view = m_model.bufferViews [accessor.bufferView ];
157156 texture_coordinate_buffer = reinterpret_cast <const float *>( // NOLINT
158157 &(m_model.buffers [view.buffer ].data [accessor.byteOffset + view.byteOffset ]));
159158 }
160159
161160 // Append data to model's vertex buffer
162161 for (std::size_t vertex_number = 0 ; vertex_number < vertex_count; vertex_number++) {
163162 ModelVertex new_vertex{};
163+
164164 // TODO: Remove pointer arithmetic or add NOLINT
165165 new_vertex.pos = glm::vec4 (glm::make_vec3 (&position_buffer[vertex_number * 3 ]), 1 .0f );
166- // TODO: Remove pointer arithmetic or add NOLINT
167- new_vertex.normal = glm::normalize (glm::vec3 (
166+
167+ if (normals_buffer != nullptr ) {
168+ // TODO: Remove pointer arithmetic or add NOLINT
169+ new_vertex.normal = glm::normalize (glm::make_vec3 (&normals_buffer[vertex_number * 3 ]));
170+ } else {
171+ new_vertex.normal = glm::normalize (glm::vec3 (0 .0f ));
172+ }
173+
174+ if (texture_coordinate_buffer != nullptr ) {
168175 // TODO: Remove pointer arithmetic or add NOLINT
169- normals_buffer != nullptr ? glm::make_vec3 (&normals_buffer[vertex_number * 3 ]) : glm::vec3 (0 .0f )));
170- new_vertex.uv = texture_coordinate_buffer != nullptr
171- ? glm::make_vec2 (&texture_coordinate_buffer[vertex_number * 2 ])
172- : glm::vec3 (0 .0f );
176+ new_vertex.uv = glm::make_vec2 (&texture_coordinate_buffer[vertex_number * 2 ]);
177+ } else {
178+ new_vertex.uv = glm::vec3 (0 .0f );
179+ }
180+
173181 new_vertex.color = glm::vec3 (1 .0f );
174182
175183 vertices.push_back (new_vertex);
@@ -180,9 +188,7 @@ void Model::load_node(const tinygltf::Node &start_node, ModelNode *parent, std::
180188 const auto &buffer_view = m_model.bufferViews [accessor.bufferView ];
181189 const auto &buffer = m_model.buffers [buffer_view.buffer ];
182190
183- auto first_index = static_cast <std::uint32_t >(indices.size ());
184191 auto vertex_start = static_cast <std::uint32_t >(vertices.size ());
185- auto index_count = static_cast <std::uint32_t >(accessor.count );
186192
187193 // glTF2 supports different component types of indices.
188194 switch (accessor.componentType ) {
@@ -225,8 +231,8 @@ void Model::load_node(const tinygltf::Node &start_node, ModelNode *parent, std::
225231 }
226232
227233 ModelPrimitive new_primitive{};
228- new_primitive.first_index = first_index ;
229- new_primitive.index_count = index_count ;
234+ new_primitive.first_index = static_cast <std:: uint32_t >(indices. size ()) ;
235+ new_primitive.index_count = static_cast <std:: uint32_t >(accessor. count ) ;
230236 new_primitive.material_index = primitive.material ;
231237 new_node.mesh .push_back (new_primitive);
232238 }
@@ -245,10 +251,9 @@ void Model::load_nodes() {
245251 // Preallocate memory for the model model.
246252 m_scenes.reserve (m_model.scenes .size ());
247253
248- std::size_t scene_index = 0 ;
249-
250- for (const auto &scene : m_model.scenes ) {
251- for (const auto &node : scene.nodes ) {
254+ for (std::size_t scene_index = 0 ; scene_index < m_model.scenes .size (); scene_index++) {
255+ for (const auto &node : m_model.scenes [scene_index].nodes ) {
256+ // Load child nodes recursively.
252257 load_node (m_model.nodes [node], nullptr , m_scenes[scene_index].vertices , m_scenes[scene_index].indices );
253258 }
254259 scene_index++;
0 commit comments