3DFileViewer: Properly propagate errors from WavefrontOBJLoader

Fixes 3 FIXMEs.
This commit is contained in:
Maciej 2022-12-04 14:52:41 +01:00 committed by Linus Groh
parent a3e82eaad3
commit 6e4f886999
4 changed files with 14 additions and 19 deletions

View File

@ -18,5 +18,5 @@ public:
MeshLoader() = default;
virtual ~MeshLoader() = default;
virtual RefPtr<Mesh> load(Core::File& file) = 0;
virtual ErrorOr<NonnullRefPtr<Mesh>> load(Core::File& file) = 0;
};

View File

@ -16,7 +16,7 @@ static inline GLuint get_index_value(StringView& representation)
return representation.to_uint().value_or(1) - 1;
}
RefPtr<Mesh> WavefrontOBJLoader::load(Core::File& file)
ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(Core::File& file)
{
Vector<Vertex> vertices;
Vector<Vertex> normals;
@ -34,8 +34,7 @@ RefPtr<Mesh> WavefrontOBJLoader::load(Core::File& file)
if (object_line.starts_with("vt"sv)) {
auto tex_coord_line = object_line.split_view(' ');
if (tex_coord_line.size() != 3) {
dbgln("Wavefront: Malformed TexCoord line. Aborting.");
return nullptr;
return Error::from_string_literal("Wavefront: Malformed TexCoord line.");
}
tex_coords.append({ static_cast<GLfloat>(atof(DeprecatedString(tex_coord_line.at(1)).characters())),
@ -47,8 +46,7 @@ RefPtr<Mesh> WavefrontOBJLoader::load(Core::File& file)
if (object_line.starts_with("vn"sv)) {
auto normal_line = object_line.split_view(' ');
if (normal_line.size() != 4) {
dbgln("Wavefront: Malformed vertex normal line. Aborting.");
return nullptr;
return Error::from_string_literal("Wavefront: Malformed vertex normal line.");
}
normals.append({ static_cast<GLfloat>(atof(DeprecatedString(normal_line.at(1)).characters())),
@ -62,8 +60,7 @@ RefPtr<Mesh> WavefrontOBJLoader::load(Core::File& file)
if (object_line.starts_with('v')) {
auto vertex_line = object_line.split_view(' ');
if (vertex_line.size() != 4) {
dbgln("Wavefront: Malformed vertex line. Aborting.");
return nullptr;
return Error::from_string_literal("Wavefront: Malformed vertex line.");
}
vertices.append({ static_cast<GLfloat>(atof(DeprecatedString(vertex_line.at(1)).characters())),
@ -78,13 +75,12 @@ RefPtr<Mesh> WavefrontOBJLoader::load(Core::File& file)
auto face_line = object_line.substring_view(2).split_view(' ');
auto number_of_vertices = face_line.size();
if (number_of_vertices < 3) {
dbgln("Wavefront: Malformed face line. Aborting.");
return nullptr;
return Error::from_string_literal("Wavefront: Malformed face line.");
}
auto vertex_indices = FixedArray<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices);
auto tex_coord_indices = FixedArray<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices);
auto normal_indices = FixedArray<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices);
auto vertex_indices = TRY(FixedArray<GLuint>::try_create(number_of_vertices));
auto tex_coord_indices = TRY(FixedArray<GLuint>::try_create(number_of_vertices));
auto normal_indices = TRY(FixedArray<GLuint>::try_create(number_of_vertices));
for (size_t i = 0; i < number_of_vertices; ++i) {
auto vertex_parts = face_line.at(i).split_view('/', SplitBehavior::KeepEmpty);
@ -111,8 +107,7 @@ RefPtr<Mesh> WavefrontOBJLoader::load(Core::File& file)
}
if (vertices.is_empty()) {
dbgln("Wavefront: Failed to read any data from 3D file: {}", file.name());
return nullptr;
return Error::from_string_literal("Wavefront: Failed to read any data from 3D file");
}
dbgln("Wavefront: Done.");

View File

@ -18,5 +18,5 @@ public:
WavefrontOBJLoader() = default;
~WavefrontOBJLoader() override = default;
RefPtr<Mesh> load(Core::File& file) override;
ErrorOr<NonnullRefPtr<Mesh>> load(Core::File& file) override;
};

View File

@ -320,8 +320,8 @@ bool GLContextWidget::load_file(Core::File& file)
}
auto new_mesh = m_mesh_loader->load(file);
if (new_mesh.is_null()) {
GUI::MessageBox::show(window(), DeprecatedString::formatted("Reading \"{}\" failed.", filename), "Error"sv, GUI::MessageBox::Type::Error);
if (new_mesh.is_error()) {
GUI::MessageBox::show(window(), DeprecatedString::formatted("Reading \"{}\" failed: {}", filename, new_mesh.release_error()), "Error"sv, GUI::MessageBox::Type::Error);
return false;
}
@ -358,7 +358,7 @@ bool GLContextWidget::load_file(Core::File& file)
dbgln("3DFileViewer: Couldn't load texture for {}", filename);
}
m_mesh = new_mesh;
m_mesh = new_mesh.release_value();
dbgln("3DFileViewer: mesh has {} triangles.", m_mesh->triangle_count());
return true;