raylib
raylib copied to clipboard
[rmodels] OBJ models with multiple materials/meshes not loading correctly
- [x] I tested it on latest raylib version from master branch
- [x] I checked there is no similar issue already reported
- [x] I checked the documentation on the wiki
- [x] My code has no errors or misuse of raylib
Issue description
I have a model made in blender with multiple meshes and materials that load correctly in raylib 4.5, but after updating to 5.0 everything is in one material. I have provided a minimal example with a simple 2 mesh/material model.
Environment
Tried on multiple machines and operating systems
Issue Screenshot
4.5
master/5.0
Code Example
#include "src/raylib.h"
int main(void)
{
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "model loading");
Camera camera = { 0 };
camera.position = (Vector3){ 2.0f, 2.0f, 2.0f };
camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
camera.fovy = 45.0f;
camera.projection = CAMERA_PERSPECTIVE;
Model model = LoadModel("tomato.obj");
Vector3 position = {0};
DisableCursor();
SetTargetFPS(60);
while (!WindowShouldClose())
{
UpdateCamera(&camera, CAMERA_FIRST_PERSON);
BeginDrawing();
ClearBackground(RAYWHITE);
BeginMode3D(camera);
DrawModel(model, (Vector3) {0}, 1.0f, WHITE);
DrawGrid(20, 10.0f);
EndMode3D();
EndDrawing();
}
UnloadModel(model);
CloseWindow();
return 0;
}
@njegg OBJ loading was redesigned due to other issues when mixing different number of materials and meshes. It probably needs another redesign.
Model loading breaks when calling LoadModel():
$ ./truck
INFO: Initializing raylib 5.1-dev
INFO: Platform backend: DESKTOP (GLFW)
INFO: Supported raylib modules:
INFO: > rcore:..... loaded (mandatory)
INFO: > rlgl:...... loaded (mandatory)
INFO: > rshapes:... loaded (optional)
INFO: > rtextures:. loaded (optional)
INFO: > rtext:..... loaded (optional)
INFO: > rmodels:... loaded (optional)
INFO: > raudio:.... loaded (optional)
INFO: DISPLAY: Trying to enable MSAA x4
INFO: DISPLAY: Trying to enable VSYNC
INFO: DISPLAY: Device initialized successfully
INFO: > Display size: 1920 x 1080
INFO: > Screen size: 800 x 450
INFO: > Render size: 800 x 450
INFO: > Viewport offsets: 0, 0
INFO: GLAD: OpenGL extensions loaded successfully
INFO: GL: Supported extensions count: 226
INFO: GL: OpenGL device information:
INFO: > Vendor: Intel
INFO: > Renderer: Mesa Intel(R) HD Graphics 620 (KBL GT2)
INFO: > Version: 4.6 (Core Profile) Mesa 22.3.7
INFO: > GLSL: 4.60
INFO: GL: VAO extension detected, VAO functions loaded successfully
INFO: GL: NPOT textures extension detected, full NPOT textures supported
INFO: GL: DXT compressed textures supported
INFO: GL: ETC2/EAC compressed textures supported
INFO: PLATFORM: DESKTOP (GLFW): Initialized successfully
INFO: TEXTURE: [ID 1] Texture loaded successfully (1x1 | R8G8B8A8 | 1 mipmaps)
INFO: TEXTURE: [ID 1] Default texture loaded successfully
INFO: SHADER: [ID 1] Vertex shader compiled successfully
INFO: SHADER: [ID 2] Fragment shader compiled successfully
INFO: SHADER: [ID 3] Program shader loaded successfully
INFO: SHADER: [ID 3] Default shader loaded successfully
INFO: RLGL: Render batch vertex buffers loaded successfully in RAM (CPU)
INFO: RLGL: Render batch vertex buffers loaded successfully in VRAM (GPU)
INFO: RLGL: Default OpenGL state initialized successfully
INFO: TEXTURE: [ID 2] Texture loaded successfully (128x128 | GRAY_ALPHA | 1 mipmaps)
INFO: FONT: Default font loaded successfully (224 glyphs)
INFO: FILEIO: [./share/truck.obj] Text file loaded successfully
INFO: MODEL: [./share/truck.obj] OBJ data loaded successfully: 5 meshes/8 materials
Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
Backtrace for this error:
#0 0x827820369 in ???
#1 0x82781f495 in ???
#2 0x8227fdb5f in ???
#3 0x8227fd11e in ???
#4 0x7ffffffff8a2 in ???
#5 0x457fb1 in LoadOBJ
at /tmp/raylib-master/src/rmodels.c:3995
#6 0x457808 in LoadModel
at /tmp/raylib-master/src/rmodels.c:1037
#7 0x402e0e in MAIN__
at examples/truck.f90:36
#8 0x4032b6 in main
at examples/truck.f90:12
Le voy a tirar un ojo !
I have the same issue. My OBJ models are exported from Blender triangulated - the else statement here is the issue - the tri count is actually an offset when there are two or more models in the same file:
https://github.com/raysan5/raylib/blob/dc7f81a7b0cbc666812c870841313e0c15a87a0c/src/rmodels.c#L4070-L4076
Here's an example - it crashes the demo here too - https://www.raylib.com/examples/models/loader.html?name=models_loading
My fix is to use the length (despite the comment) warning about quads
else tris = meshes[i].length;
Hello
I also have issues loading a model with 3 materials defined by an obj+mtl files.
Only the first material appearing in mtl file is used for the entire model.
BUT !
If I add this
model.meshMaterial[0]=0;
model.meshMaterial[1]=1;
model.meshMaterial[2]=2;
Then my model displays completely fine, with the right 3 materials ...
I had a similar issue, and the above code by BobSleigh38 didn't work for me with the materials not being set properly to the meshes. What I did was just export in Blender as a .glb file instead of an .obj file (so this isn't really a fix to this issue, rather a workaround).
When I initially did this however, all my meshes were centered at (0,0,0) and with normalized scales, so my fix was just to select all the objects in Blender (A), and apply the transforms by pressing Ctrl+A and selecting All Transforms like so:
Here's my export settings in Blender...
If it helps anyone else, for OBJ model loaded I ended up using this single file library to load OBJ models into raylib: https://github.com/Not-Nik/rlobj
Not sure the author would agree to swapping it out, but for anything more complicated than a single object it's required
@x1nixmzeng
Not sure the author would agree to swapping it out, but for anything more complicated than a single object it's required
The license for rlobj is a bit sketchy. There is a statement about an exception at the repository but there is no such statement in the license document itself. The exception is also for (indirectly-)named individuals, not arbitrary users of raylib. I'm not sure it could even be incorporated here as an external.
@x1nixmzeng, @orcmid is right, the license does not seem to be compatible with raylib.
Is there any progress being done on this?