mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 09:17:35 +00:00
3DFileViewer: Port MeshLoader
and its child to the new Core::File
As it was using the `lines()` method of `Core::DeprecatedFile`, this patch also introduce the usage of `BufferedFile` to take advantage of its API: `can_read_line()` and `read_line()`.
This commit is contained in:
parent
83da3c5c3e
commit
b4cea1c72e
4 changed files with 14 additions and 6 deletions
|
@ -18,5 +18,5 @@ public:
|
||||||
MeshLoader() = default;
|
MeshLoader() = default;
|
||||||
virtual ~MeshLoader() = default;
|
virtual ~MeshLoader() = default;
|
||||||
|
|
||||||
virtual ErrorOr<NonnullRefPtr<Mesh>> load(Core::DeprecatedFile& file) = 0;
|
virtual ErrorOr<NonnullRefPtr<Mesh>> load(String const& filename, NonnullOwnPtr<Core::File> file) = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,9 @@
|
||||||
|
|
||||||
#include "WavefrontOBJLoader.h"
|
#include "WavefrontOBJLoader.h"
|
||||||
#include <AK/FixedArray.h>
|
#include <AK/FixedArray.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <LibCore/DeprecatedFile.h>
|
#include <LibCore/DeprecatedFile.h>
|
||||||
|
#include <LibCore/File.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
static inline GLuint get_index_value(StringView& representation)
|
static inline GLuint get_index_value(StringView& representation)
|
||||||
|
@ -16,17 +18,22 @@ static inline GLuint get_index_value(StringView& representation)
|
||||||
return representation.to_uint().value_or(1) - 1;
|
return representation.to_uint().value_or(1) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(Core::DeprecatedFile& file)
|
ErrorOr<NonnullRefPtr<Mesh>> WavefrontOBJLoader::load(String const& filename, NonnullOwnPtr<Core::File> file)
|
||||||
{
|
{
|
||||||
|
auto buffered_file = TRY(Core::BufferedFile::create(move(file)));
|
||||||
|
|
||||||
Vector<Vertex> vertices;
|
Vector<Vertex> vertices;
|
||||||
Vector<Vertex> normals;
|
Vector<Vertex> normals;
|
||||||
Vector<TexCoord> tex_coords;
|
Vector<TexCoord> tex_coords;
|
||||||
Vector<Triangle> triangles;
|
Vector<Triangle> triangles;
|
||||||
|
|
||||||
dbgln("Wavefront: Loading {}...", file.name());
|
dbgln("Wavefront: Loading {}...", filename);
|
||||||
|
|
||||||
// Start reading file line by line
|
// Start reading file line by line
|
||||||
for (auto object_line : file.lines()) {
|
auto buffer = TRY(ByteBuffer::create_uninitialized(PAGE_SIZE));
|
||||||
|
while (TRY(buffered_file->can_read_line())) {
|
||||||
|
auto object_line = TRY(buffered_file->read_line(buffer));
|
||||||
|
|
||||||
// Ignore file comments
|
// Ignore file comments
|
||||||
if (object_line.starts_with('#'))
|
if (object_line.starts_with('#'))
|
||||||
continue;
|
continue;
|
||||||
|
|
|
@ -18,5 +18,5 @@ public:
|
||||||
WavefrontOBJLoader() = default;
|
WavefrontOBJLoader() = default;
|
||||||
~WavefrontOBJLoader() override = default;
|
~WavefrontOBJLoader() override = default;
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<Mesh>> load(Core::DeprecatedFile& file) override;
|
ErrorOr<NonnullRefPtr<Mesh>> load(String const& filename, NonnullOwnPtr<Core::File> file) override;
|
||||||
};
|
};
|
||||||
|
|
|
@ -318,7 +318,8 @@ bool GLContextWidget::load_file(Core::DeprecatedFile& file)
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto new_mesh = m_mesh_loader->load(file);
|
auto new_mesh = m_mesh_loader->load(String::from_deprecated_string(file.filename()).release_value_but_fixme_should_propagate_errors(),
|
||||||
|
Core::File::adopt_fd(file.leak_fd(), Core::File::OpenMode::Read).release_value_but_fixme_should_propagate_errors());
|
||||||
if (new_mesh.is_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);
|
GUI::MessageBox::show(window(), DeprecatedString::formatted("Reading \"{}\" failed: {}", filename, new_mesh.release_error()), "Error"sv, GUI::MessageBox::Type::Error);
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue