1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 21:27:34 +00:00

3DFileViewer: Remove DeprecatedString usage in object loader

This change also improves error handling if the given .obj file
contains malformed floating point numbers.
This commit is contained in:
Tim Ledbetter 2023-09-13 17:53:45 +01:00 committed by Andreas Kling
parent 4eb52351b4
commit 1beadba0bb

View file

@ -10,13 +10,21 @@
#include <AK/FixedArray.h> #include <AK/FixedArray.h>
#include <AK/String.h> #include <AK/String.h>
#include <LibCore/File.h> #include <LibCore/File.h>
#include <stdlib.h>
static inline GLuint get_index_value(StringView& representation) static inline GLuint get_index_value(StringView& representation)
{ {
return representation.to_uint().value_or(1) - 1; return representation.to_uint().value_or(1) - 1;
} }
static ErrorOr<GLfloat> parse_float(StringView string)
{
auto maybe_float = string.to_float(TrimWhitespace::No);
if (!maybe_float.has_value())
return Error::from_string_literal("Wavefront: Expected floating point value when parsing TexCoord line");
return maybe_float.release_value();
}
ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(String const& filename, NonnullOwnPtr<Core::File> file) ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(String const& filename, NonnullOwnPtr<Core::File> file)
{ {
auto buffered_file = TRY(Core::InputBufferedFile::create(move(file))); auto buffered_file = TRY(Core::InputBufferedFile::create(move(file)));
@ -43,8 +51,8 @@ ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(String const& filename, No
return Error::from_string_literal("Wavefront: Malformed TexCoord line."); return Error::from_string_literal("Wavefront: Malformed TexCoord line.");
} }
tex_coords.append({ static_cast<GLfloat>(atof(DeprecatedString(tex_coord_line.at(1)).characters())), tex_coords.append({ TRY(parse_float(tex_coord_line.at(1))),
static_cast<GLfloat>(atof(DeprecatedString(tex_coord_line.at(2)).characters())) }); TRY(parse_float(tex_coord_line.at(2))) });
continue; continue;
} }
@ -55,9 +63,9 @@ ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(String const& filename, No
return Error::from_string_literal("Wavefront: Malformed vertex normal line."); return Error::from_string_literal("Wavefront: Malformed vertex normal line.");
} }
normals.append({ static_cast<GLfloat>(atof(DeprecatedString(normal_line.at(1)).characters())), normals.append({ TRY(parse_float(normal_line.at(1))),
static_cast<GLfloat>(atof(DeprecatedString(normal_line.at(2)).characters())), TRY(parse_float(normal_line.at(2))),
static_cast<GLfloat>(atof(DeprecatedString(normal_line.at(3)).characters())) }); TRY(parse_float(normal_line.at(3))) });
continue; continue;
} }
@ -69,9 +77,9 @@ ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(String const& filename, No
return Error::from_string_literal("Wavefront: Malformed vertex line."); return Error::from_string_literal("Wavefront: Malformed vertex line.");
} }
vertices.append({ static_cast<GLfloat>(atof(DeprecatedString(vertex_line.at(1)).characters())), vertices.append({ TRY(parse_float((vertex_line.at(1)))),
static_cast<GLfloat>(atof(DeprecatedString(vertex_line.at(2)).characters())), TRY(parse_float((vertex_line.at(2)))),
static_cast<GLfloat>(atof(DeprecatedString(vertex_line.at(3)).characters())) }); TRY(parse_float((vertex_line.at(3)))) });
continue; continue;
} }