Skip to content

Commit f4701fc

Browse files
committed
[*] Small fixes
1 parent 3b69206 commit f4701fc

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

include/inexor/vulkan-renderer/gltf2/gltf2_model.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ struct ModelScene {
3838
};
3939

4040
/// @brief A struct for glTF2 model nodes.
41+
/// @warning Since glTF2 models can be very huge, we could run into out of stack problems when calling the destructors
42+
/// of the tree's nodes.
4143
struct ModelNode {
4244
ModelNode *parent{nullptr};
4345
std::vector<ModelNode> children;
@@ -76,20 +78,18 @@ class Model {
7678
// TODO: load pbr (physically based rendering) settings
7779
// TODO: load multiple texture coordinate sets
7880
// TODO: try to .reserve() memory for vectors and use emplace_back()
81+
// TODO: should we move node loading into a separate class?
7982

8083
public:
8184
/// @brief Extract the model data from a model file.
8285
/// @paran device The device wrapper.
8386
/// @param file The glTF2 model file.
8487
Model(const wrapper::Device &device, tinygltf::Model &model);
85-
8688
Model(const Model &) = delete;
8789
Model(Model &&) = delete;
8890

8991
Model &operator=(const Model &) = delete;
9092
Model &operator=(Model &&) = delete;
91-
92-
~Model();
9393
};
9494

9595
} // namespace inexor::vulkan_renderer::gltf2

src/vulkan-renderer/gltf2/gltf2_model.cpp

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)