1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 12:47:35 +00:00

GLTeapot: Adds additional error checking when loading files

- If the 3D file contains no vertices then an error is raised
- If the file is not an OBJ file then an error is raised
This commit is contained in:
Erik Biederstadt 2021-05-17 15:45:15 -06:00 committed by Linus Groh
parent 01a5ffdae0
commit d9dc42fab5
2 changed files with 19 additions and 2 deletions

View file

@ -66,6 +66,11 @@ RefPtr<Mesh> WavefrontOBJLoader::load(const String& fname)
} }
} }
if (vertices.is_empty()) {
dbgln("Wavefront: Failed to read any data from 3D file: {}", fname);
return nullptr;
}
dbgln("Wavefront: Done."); dbgln("Wavefront: Done.");
return adopt_ref(*new Mesh(vertices, triangles)); return adopt_ref(*new Mesh(vertices, triangles));
} }

View file

@ -102,7 +102,8 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadMatrixf((float*)matrix.elements()); glLoadMatrixf((float*)matrix.elements());
m_mesh->draw(); if (!m_mesh.is_null())
m_mesh->draw();
m_context->present(); m_context->present();
update(); update();
@ -111,7 +112,12 @@ void GLContextWidget::timer_event(Core::TimerEvent&)
bool GLContextWidget::load(const String& fname) bool GLContextWidget::load(const String& fname)
{ {
m_mesh = m_mesh_loader->load(fname); m_mesh = m_mesh_loader->load(fname);
return !m_mesh.is_null(); if (!m_mesh.is_null()) {
dbgln("GLTeapot: mesh has {} triangles.", m_mesh->triangle_count());
return true;
}
return false;
} }
int main(int argc, char** argv) int main(int argc, char** argv)
@ -159,6 +165,12 @@ int main(int argc, char** argv)
return; return;
auto file = Core::File::construct(open_path.value()); auto file = Core::File::construct(open_path.value());
if (!file->filename().ends_with(".obj")) {
GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: invalid file type", open_path.value()), "Error", GUI::MessageBox::Type::Error);
return;
}
if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) { if (!file->open(Core::OpenMode::ReadOnly) && file->error() != ENOENT) {
GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", open_path.value(), strerror(errno)), "Error", GUI::MessageBox::Type::Error); GUI::MessageBox::show(window, String::formatted("Opening \"{}\" failed: {}", open_path.value(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
return; return;