dwmkerr / sharpgl

Use OpenGL in .NET applications. SharpGL wraps all modern OpenGL features and offers a powerful scene graph to aid development.
MIT License
768 stars 300 forks source link

Loading .obj files with textures does not work? #200

Open Kermalis opened 3 years ago

Kermalis commented 3 years ago

I'm trying to follow your samples but it seems basic functionality is not working.

When you do either SerializationEngine.Instance.LoadScene(path) or new ObjFileFormat().LoadData(path), there's a crash. This is because LoadData() makes a brand new Scene with no gl context, meaning the Scene's gl context is null. Then when loading textures, it crashes because it calls Texture.Create() with a null gl context.

Am I missing something? I feel like everyone in the planet would be making this issue if it was actually broken. There's no way for me to pass in a gl context. Also, I can't downgrade to 2.4.4 (before this crash would've existed) because your Serialization nuget package does not go before 3.1.0

trenttobler commented 2 years ago

I ran into this issue too as I was trying to port the library and samples to .net 6. I got the "WinForms Sample/DuckySample" to load by modifying the ObjFileFormat to include a property to provide the GlContext, assigning it when creating the ObjFileFormat, and calling scene.CreateInContext(..) before loading the format.

I don't think it was just you. This seems a pretty clear bug, possibly a design issue by the mandate that the GL context be initialized before being able to utilize the load model data (which was more what I was looking for - something that could parse and open Wavefront OBJ files, rather than a full OpenGL engine.)

progmars commented 1 year ago

This got me as well, spent quite a few minutes investigating file paths, formats and whatnot, and finally found the faulty place: https://github.com/dwmkerr/sharpgl/blob/9a18e2624f488635ee87d4ee5a75aa2e6df09cdd/source/SharpGL/Core/SharpGL.Serialization/Wavefront/ObjFileFormat.cs#L254

Fortunately, it was pretty easy to create a custom ObjFileFormat with context.