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

TrenchBroom crashes when loading a model with animation #4522

Open
HaileyEira opened this issue Mar 12, 2024 · 5 comments
Open

TrenchBroom crashes when loading a model with animation #4522

HaileyEira opened this issue Mar 12, 2024 · 5 comments
Labels
Prio:2 Medium priority: Non crash bugs that impede the user, features that add new functionality. Type:Bug Errors and problems
Milestone

Comments

@HaileyEira
Copy link

System Information

TrenchBroom Win64 v2024.1 on Windows 11

Expected Behavior

Load entity and add into .map like in the previous version of TrenchBroom.

Steps to Reproduce

Include the model in the .fgd, start TrenchBroom, navigate to the Entity tab or open a .map file that references the model, and TrenchBroom will crash.

The issue appears to be related to loading bones for animation. TrenchBroom crashes in AssimpParser.cpp with the error "vector subscript out of range," https://github.com/TrenchBroom/TrenchBroom/blob/master/common/src/IO/AssimpParser.cpp#L578

The model worked in the previous version of TrenchBroom. Its format is correct, and it loads correctly in other applications.

Crash Info

Crash files are attached.

TrenchBroom_SleepwalkR_Crash.zip

@kduske kduske added Type:Bug Errors and problems Prio:2 Medium priority: Non crash bugs that impede the user, features that add new functionality. labels Mar 12, 2024
@kduske kduske added this to the 2024.2 milestone Mar 12, 2024
@kduske
Copy link
Collaborator

kduske commented Mar 12, 2024

@LogicAndTrick can you take a look at this?

@LogicAndTrick
Copy link
Contributor

Looks like it could be an upstream issue, Assimp's built-in viewer also cannot load this model:
image

Work around would be to use a model without animations, such as this version which doesn't crash:
https://github.com/eGax/TrenchBroom_xtras_plus/blob/main/games_wip/MarbleBlast/assets/models/hazards/trapdoor.fbx

However this particular crash can be prevented by checking the range when loading the animation:

diff --git a/common/src/IO/AssimpParser.cpp b/common/src/IO/AssimpParser.cpp
index 642519e67..96287989f 100644
--- a/common/src/IO/AssimpParser.cpp
+++ b/common/src/IO/AssimpParser.cpp
@@ -575,7 +575,10 @@ std::vector<Assets::EntityModelVertex> computeMeshVertices(
     {
       for (unsigned int j = 0; j < bone.mNumWeights; ++j)
       {
-        weightsPerVertex[bone.mWeights[j].mVertexId].push_back(
+        const auto vertexIndex = bone.mWeights[j].mVertexId;
+        if (vertexIndex < 0 || vertexIndex >= mesh.mNumVertices)
+          continue;
+        weightsPerVertex[vertexIndex].push_back(
           {*index, bone.mWeights[j].mWeight, bone});
       }
     }

@HaileyEira
Copy link
Author

Thank you. I'll use the workaround for now. 🩵

@eGax
Copy link
Contributor

eGax commented Mar 12, 2024

Explains why mine wasn't crashing. Thank you @LogicAndTrick for the info.

@HumanGamer
Copy link

However this particular crash can be prevented by checking the range when loading the animation:

diff --git a/common/src/IO/AssimpParser.cpp b/common/src/IO/AssimpParser.cpp
index 642519e67..96287989f 100644
--- a/common/src/IO/AssimpParser.cpp
+++ b/common/src/IO/AssimpParser.cpp
@@ -575,7 +575,10 @@ std::vector<Assets::EntityModelVertex> computeMeshVertices(
     {
       for (unsigned int j = 0; j < bone.mNumWeights; ++j)
       {
-        weightsPerVertex[bone.mWeights[j].mVertexId].push_back(
+        const auto vertexIndex = bone.mWeights[j].mVertexId;
+        if (vertexIndex < 0 || vertexIndex >= mesh.mNumVertices)
+          continue;
+        weightsPerVertex[vertexIndex].push_back(
           {*index, bone.mWeights[j].mWeight, bone});
       }
     }

Tested this myself and it causes the model to look wrong. You need to break out, not continue, and if you do break out you also need to fail the if check here https://github.com/TrenchBroom/TrenchBroom/blob/master/common/src/IO/AssimpParser.cpp#L598

That will work correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Prio:2 Medium priority: Non crash bugs that impede the user, features that add new functionality. Type:Bug Errors and problems
Projects
None yet
Development

No branches or pull requests

5 participants