Skip to content

Commit

Permalink
bone parent info
Browse files Browse the repository at this point in the history
  • Loading branch information
Daivuk committed Jul 6, 2024
1 parent 1c3fbfc commit f8da16c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions include/onut/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace onut
OTextureRef pTexture;
uint32_t elementCount;
std::vector<Matrix> bones;
std::vector<int> bone_parents;
std::unordered_map<std::string, int> boneMapping;
int vertexFlags = 0;
int vertexSize = 6;
Expand Down
37 changes: 37 additions & 0 deletions src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,26 @@ namespace onut
out = start + (end - start) * (float)factor;
}

static const aiNode* findParent(const aiNode* node, const aiString& childName)
{
for (unsigned int i = 0; i < node->mNumChildren; ++i)
{
const aiNode* child = node->mChildren[i];
if (child->mName == childName)
{
return node;
}

const aiNode* foundParent = findParent(child, childName);
if (foundParent)
{
return foundParent;
}
}

return nullptr;
}

// Reference: http://ogldev.atspace.co.uk/www/tutorial38/tutorial38.html
static void bakeAnimBones(double t,
aiAnimation* pAssAnim,
Expand Down Expand Up @@ -525,6 +545,23 @@ namespace onut
pVertex[weightOff + 1] = weight;
}
}

// Find bone's parents using the root node for the entire scene... not great assimp, not great.
pMesh->bone_parents.resize(pMesh->bones.size());
for (int i = 0; i < (int)pAssMesh->mNumBones; ++i)
{
pMesh->bone_parents[i] = -1;
auto pAssBone = pAssMesh->mBones[i];
const aiNode* pParentNode = findParent(pScene->mRootNode, pAssBone->mName);
if (pParentNode)
{
auto it = pMesh->boneMapping.find(pParentNode->mName.C_Str());
if (it != pMesh->boneMapping.end())
{
pMesh->bone_parents[i] = it->second;
}
}
}
}

pMesh->pVertexBuffer = OVertexBuffer::createStatic(vertices, pAssMesh->mNumVertices * sizeof(float) * vertexSize);
Expand Down

0 comments on commit f8da16c

Please sign in to comment.