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
Notes: sideshowbarker 2024-07-18 04:38:32 +09:00
4 changed files with 14 additions and 19 deletions

View file

@ -18,5 +18,5 @@ public:
MeshLoader() = default; MeshLoader() = default;
virtual ~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; 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> vertices;
Vector<Vertex> normals; Vector<Vertex> normals;
@ -34,8 +34,7 @@ RefPtr<Mesh> WavefrontOBJLoader::load(Core::File& file)
if (object_line.starts_with("vt"sv)) { if (object_line.starts_with("vt"sv)) {
auto tex_coord_line = object_line.split_view(' '); auto tex_coord_line = object_line.split_view(' ');
if (tex_coord_line.size() != 3) { if (tex_coord_line.size() != 3) {
dbgln("Wavefront: Malformed TexCoord line. Aborting."); return Error::from_string_literal("Wavefront: Malformed TexCoord line.");
return nullptr;
} }
tex_coords.append({ static_cast<GLfloat>(atof(DeprecatedString(tex_coord_line.at(1)).characters())), 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)) { if (object_line.starts_with("vn"sv)) {
auto normal_line = object_line.split_view(' '); auto normal_line = object_line.split_view(' ');
if (normal_line.size() != 4) { if (normal_line.size() != 4) {
dbgln("Wavefront: Malformed vertex normal line. Aborting."); return Error::from_string_literal("Wavefront: Malformed vertex normal line.");
return nullptr;
} }
normals.append({ static_cast<GLfloat>(atof(DeprecatedString(normal_line.at(1)).characters())), 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')) { if (object_line.starts_with('v')) {
auto vertex_line = object_line.split_view(' '); auto vertex_line = object_line.split_view(' ');
if (vertex_line.size() != 4) { if (vertex_line.size() != 4) {
dbgln("Wavefront: Malformed vertex line. Aborting."); return Error::from_string_literal("Wavefront: Malformed vertex line.");
return nullptr;
} }
vertices.append({ static_cast<GLfloat>(atof(DeprecatedString(vertex_line.at(1)).characters())), 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 face_line = object_line.substring_view(2).split_view(' ');
auto number_of_vertices = face_line.size(); auto number_of_vertices = face_line.size();
if (number_of_vertices < 3) { if (number_of_vertices < 3) {
dbgln("Wavefront: Malformed face line. Aborting."); return Error::from_string_literal("Wavefront: Malformed face line.");
return nullptr;
} }
auto vertex_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 = FixedArray<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices); auto tex_coord_indices = TRY(FixedArray<GLuint>::try_create(number_of_vertices));
auto normal_indices = FixedArray<GLuint>::must_create_but_fixme_should_propagate_errors(number_of_vertices); auto normal_indices = TRY(FixedArray<GLuint>::try_create(number_of_vertices));
for (size_t i = 0; i < number_of_vertices; ++i) { for (size_t i = 0; i < number_of_vertices; ++i) {
auto vertex_parts = face_line.at(i).split_view('/', SplitBehavior::KeepEmpty); 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()) { if (vertices.is_empty()) {
dbgln("Wavefront: Failed to read any data from 3D file: {}", file.name()); return Error::from_string_literal("Wavefront: Failed to read any data from 3D file");
return nullptr;
} }
dbgln("Wavefront: Done."); dbgln("Wavefront: Done.");

View file

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