Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[module] Segfault on 3D animation update #4626

Open
ncollie42 opened this issue Dec 21, 2024 · 1 comment
Open

[module] Segfault on 3D animation update #4626

ncollie42 opened this issue Dec 21, 2024 · 1 comment

Comments

@ncollie42
Copy link

Issue description

After raylib 5.5, updating animation causes segfault when using these gltf assests , due to this line of code . Given these models used to animate on previous versions of raylib, we should account for this edge case.

Segfault is due to the fact that not all mesh in the model (IE: hats) are part of the animation. A null check of some kind fixes the issue on my side.

           if (mesh.boneWeights == NULL) continue;
            // Iterates over 4 bones per vertex
            for (int j = 0; j < 4; j++, boneCounter++)
            {
                boneWeight = mesh.boneWeights[boneCounter];
                boneId = mesh.boneIds[boneCounter];
                ....

Environment

Fedora

Issue Screenshot

screenshot is all mesh data for a given model, we can see 1 boneweights to be null for the first mesh.
image

Code Example

Using the animation example, slightly modified.

#include "raylib.h"

int main(void)
{
    // Initialization
    //--------------------------------------------------------------------------------------
    const int screenWidth = 800;
    const int screenHeight = 450;

    InitWindow(screenWidth, screenHeight, "raylib [models] example - model animation");

    // Define the camera to look into our 3d world
    Camera camera = { 0 };
    camera.position = (Vector3){ 10.0f, 10.0f, 10.0f }; // Camera position
    camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };      // Camera looking at point
    camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };          // Camera up vector (rotation towards target)
    camera.fovy = 45.0f;                                // Camera field-of-view Y
    camera.projection = CAMERA_PERSPECTIVE;             // Camera mode type

    path = "path/to/model.glb"
    Model model = LoadModel(path);   // Load the animated model mesh and basic data

    Vector3 position = { 0.0f, 0.0f, 0.0f };            // Set model position

    // Load animation data
    int animsCount = 0;
    ModelAnimation *anims = LoadModelAnimations(path, &animsCount);
    int animFrameCounter = 0;

    DisableCursor();                    // Catch cursor
    SetTargetFPS(60);                   // Set our game to run at 60 frames-per-second
    //--------------------------------------------------------------------------------------

    // Main game loop
    while (!WindowShouldClose())        // Detect window close button or ESC key
    {
        // Update
        //----------------------------------------------------------------------------------
        UpdateCamera(&camera, CAMERA_FIRST_PERSON);

        animFrameCounter++;
        UpdateModelAnimation(model, anims[0], animFrameCounter);
        if (animFrameCounter >= anims[0].frameCount) animFrameCounter = 0;
        //----------------------------------------------------------------------------------

        // Draw
        //----------------------------------------------------------------------------------
        BeginDrawing();

            ClearBackground(RAYWHITE);

            BeginMode3D(camera);

                DrawModelEx(model, position, (Vector3){ 1.0f, 0.0f, 0.0f }, -90.0f, (Vector3){ 1.0f, 1.0f, 1.0f }, WHITE);

                DrawGrid(10, 1.0f);         // Draw a grid

            EndMode3D();


        EndDrawing();
        //----------------------------------------------------------------------------------
    }

    return 0;
}

@JettMonstersGoBoom
Copy link
Contributor

#4627 (comment) resolves this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants