2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
|
2022-01-10 20:13:43 +00:00
|
|
|
Copyright (c) 2006-2022, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2017-05-09 17:57:36 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the
|
2015-05-19 03:48:29 +00:00
|
|
|
following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* Neither the name of the assimp team, nor the names of its
|
|
|
|
contributors may be used to endorse or promote products
|
|
|
|
derived from this software without specific prior
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
----------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file SkeletonMeshBuilder.cpp
|
2015-05-19 03:52:10 +00:00
|
|
|
* @brief Implementation of a little class to construct a dummy mesh for a skeleton
|
2015-05-19 03:48:29 +00:00
|
|
|
*/
|
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/SkeletonMeshBuilder.h>
|
2020-06-23 19:05:42 +00:00
|
|
|
#include <assimp/scene.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// The constructor processes the given scene and adds a mesh there.
|
2020-06-23 19:05:42 +00:00
|
|
|
SkeletonMeshBuilder::SkeletonMeshBuilder(aiScene *pScene, aiNode *root, bool bKnobsOnly) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// nothing to do if there's mesh data already present at the scene
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pScene->mNumMeshes > 0 || pScene->mRootNode == nullptr) {
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
if (!root) {
|
2015-05-19 03:57:13 +00:00
|
|
|
root = pScene->mRootNode;
|
2020-06-23 19:05:42 +00:00
|
|
|
}
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
mKnobsOnly = bKnobsOnly;
|
|
|
|
|
|
|
|
// build some faces around each node
|
2020-06-23 19:05:42 +00:00
|
|
|
CreateGeometry(root);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// create a mesh to hold all the generated faces
|
|
|
|
pScene->mNumMeshes = 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
pScene->mMeshes = new aiMesh *[1];
|
2015-05-19 03:57:13 +00:00
|
|
|
pScene->mMeshes[0] = CreateMesh();
|
|
|
|
// and install it at the root node
|
|
|
|
root->mNumMeshes = 1;
|
|
|
|
root->mMeshes = new unsigned int[1];
|
|
|
|
root->mMeshes[0] = 0;
|
|
|
|
|
|
|
|
// create a dummy material for the mesh
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pScene->mNumMaterials == 0) {
|
|
|
|
pScene->mNumMaterials = 1;
|
|
|
|
pScene->mMaterials = new aiMaterial *[1];
|
|
|
|
pScene->mMaterials[0] = CreateMaterial();
|
2016-01-17 14:18:12 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2015-05-19 03:52:10 +00:00
|
|
|
// Recursively builds a simple mesh representation for the given node
|
2020-06-23 19:05:42 +00:00
|
|
|
void SkeletonMeshBuilder::CreateGeometry(const aiNode *pNode) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// add a joint entry for the node.
|
2016-11-27 02:17:02 +00:00
|
|
|
const unsigned int vertexStartIndex = static_cast<unsigned int>(mVertices.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// now build the geometry.
|
2020-06-23 19:05:42 +00:00
|
|
|
if (pNode->mNumChildren > 0 && !mKnobsOnly) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// If the node has children, we build little pointers to each of them
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < pNode->mNumChildren; a++) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// find a suitable coordinate system
|
2020-06-23 19:05:42 +00:00
|
|
|
const aiMatrix4x4 &childTransform = pNode->mChildren[a]->mTransformation;
|
|
|
|
aiVector3D childpos(childTransform.a4, childTransform.b4, childTransform.c4);
|
2016-07-15 05:38:29 +00:00
|
|
|
ai_real distanceToChild = childpos.Length();
|
2020-06-23 19:05:42 +00:00
|
|
|
if (distanceToChild < 0.0001)
|
2015-05-19 03:57:13 +00:00
|
|
|
continue;
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D up = aiVector3D(childpos).Normalize();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D orth(1.0, 0.0, 0.0);
|
|
|
|
if (std::fabs(orth * up) > 0.99)
|
|
|
|
orth.Set(0.0, 1.0, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
aiVector3D front = (up ^ orth).Normalize();
|
|
|
|
aiVector3D side = (front ^ up).Normalize();
|
|
|
|
|
2016-11-27 02:17:02 +00:00
|
|
|
unsigned int localVertexStart = static_cast<unsigned int>(mVertices.size());
|
2020-06-23 19:05:42 +00:00
|
|
|
mVertices.push_back(-front * distanceToChild * (ai_real)0.1);
|
|
|
|
mVertices.push_back(childpos);
|
|
|
|
mVertices.push_back(-side * distanceToChild * (ai_real)0.1);
|
|
|
|
mVertices.push_back(-side * distanceToChild * (ai_real)0.1);
|
|
|
|
mVertices.push_back(childpos);
|
|
|
|
mVertices.push_back(front * distanceToChild * (ai_real)0.1);
|
|
|
|
mVertices.push_back(front * distanceToChild * (ai_real)0.1);
|
|
|
|
mVertices.push_back(childpos);
|
|
|
|
mVertices.push_back(side * distanceToChild * (ai_real)0.1);
|
|
|
|
mVertices.push_back(side * distanceToChild * (ai_real)0.1);
|
|
|
|
mVertices.push_back(childpos);
|
|
|
|
mVertices.push_back(-front * distanceToChild * (ai_real)0.1);
|
|
|
|
|
|
|
|
mFaces.push_back(Face(localVertexStart + 0, localVertexStart + 1, localVertexStart + 2));
|
|
|
|
mFaces.push_back(Face(localVertexStart + 3, localVertexStart + 4, localVertexStart + 5));
|
|
|
|
mFaces.push_back(Face(localVertexStart + 6, localVertexStart + 7, localVertexStart + 8));
|
|
|
|
mFaces.push_back(Face(localVertexStart + 9, localVertexStart + 10, localVertexStart + 11));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2020-06-23 19:05:42 +00:00
|
|
|
} else {
|
2015-05-19 03:57:13 +00:00
|
|
|
// if the node has no children, it's an end node. Put a little knob there instead
|
2020-06-23 19:05:42 +00:00
|
|
|
aiVector3D ownpos(pNode->mTransformation.a4, pNode->mTransformation.b4, pNode->mTransformation.c4);
|
|
|
|
ai_real sizeEstimate = ownpos.Length() * ai_real(0.18);
|
|
|
|
|
|
|
|
mVertices.push_back(aiVector3D(-sizeEstimate, 0.0, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, -sizeEstimate));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(sizeEstimate, 0.0, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, -sizeEstimate));
|
|
|
|
mVertices.push_back(aiVector3D(sizeEstimate, 0.0, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, -sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, -sizeEstimate));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, -sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(-sizeEstimate, 0.0, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, -sizeEstimate));
|
|
|
|
|
|
|
|
mVertices.push_back(aiVector3D(-sizeEstimate, 0.0, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, sizeEstimate));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, sizeEstimate));
|
|
|
|
mVertices.push_back(aiVector3D(sizeEstimate, 0.0, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(sizeEstimate, 0.0, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, sizeEstimate));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, -sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, -sizeEstimate, 0.0));
|
|
|
|
mVertices.push_back(aiVector3D(0.0, 0.0, sizeEstimate));
|
|
|
|
mVertices.push_back(aiVector3D(-sizeEstimate, 0.0, 0.0));
|
|
|
|
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 0, vertexStartIndex + 1, vertexStartIndex + 2));
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 3, vertexStartIndex + 4, vertexStartIndex + 5));
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 6, vertexStartIndex + 7, vertexStartIndex + 8));
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 9, vertexStartIndex + 10, vertexStartIndex + 11));
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 12, vertexStartIndex + 13, vertexStartIndex + 14));
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 15, vertexStartIndex + 16, vertexStartIndex + 17));
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 18, vertexStartIndex + 19, vertexStartIndex + 20));
|
|
|
|
mFaces.push_back(Face(vertexStartIndex + 21, vertexStartIndex + 22, vertexStartIndex + 23));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
2016-11-27 02:17:02 +00:00
|
|
|
unsigned int numVertices = static_cast<unsigned int>(mVertices.size() - vertexStartIndex);
|
2020-06-23 19:05:42 +00:00
|
|
|
if (numVertices > 0) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// create a bone affecting all the newly created vertices
|
2020-06-23 19:05:42 +00:00
|
|
|
aiBone *bone = new aiBone;
|
|
|
|
mBones.push_back(bone);
|
2015-05-19 03:57:13 +00:00
|
|
|
bone->mName = pNode->mName;
|
|
|
|
|
|
|
|
// calculate the bone offset matrix by concatenating the inverse transformations of all parents
|
2020-06-23 19:05:42 +00:00
|
|
|
bone->mOffsetMatrix = aiMatrix4x4(pNode->mTransformation).Inverse();
|
|
|
|
for (aiNode *parent = pNode->mParent; parent != nullptr; parent = parent->mParent)
|
|
|
|
bone->mOffsetMatrix = aiMatrix4x4(parent->mTransformation).Inverse() * bone->mOffsetMatrix;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// add all the vertices to the bone's influences
|
|
|
|
bone->mNumWeights = numVertices;
|
|
|
|
bone->mWeights = new aiVertexWeight[numVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < numVertices; a++)
|
|
|
|
bone->mWeights[a] = aiVertexWeight(vertexStartIndex + a, 1.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// HACK: (thom) transform all vertices to the bone's local space. Should be done before adding
|
|
|
|
// them to the array, but I'm tired now and I'm annoyed.
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMatrix4x4 boneToMeshTransform = aiMatrix4x4(bone->mOffsetMatrix).Inverse();
|
|
|
|
for (unsigned int a = vertexStartIndex; a < mVertices.size(); a++)
|
2015-05-19 03:57:13 +00:00
|
|
|
mVertices[a] = boneToMeshTransform * mVertices[a];
|
|
|
|
}
|
|
|
|
|
|
|
|
// and finally recurse into the children list
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < pNode->mNumChildren; a++)
|
|
|
|
CreateGeometry(pNode->mChildren[a]);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Creates the mesh from the internally accumulated stuff and returns it.
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMesh *SkeletonMeshBuilder::CreateMesh() {
|
|
|
|
aiMesh *mesh = new aiMesh();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// add points
|
2016-11-27 02:17:02 +00:00
|
|
|
mesh->mNumVertices = static_cast<unsigned int>(mVertices.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->mVertices = new aiVector3D[mesh->mNumVertices];
|
2020-06-23 19:05:42 +00:00
|
|
|
std::copy(mVertices.begin(), mVertices.end(), mesh->mVertices);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
mesh->mNormals = new aiVector3D[mesh->mNumVertices];
|
|
|
|
|
|
|
|
// add faces
|
2016-11-27 02:17:02 +00:00
|
|
|
mesh->mNumFaces = static_cast<unsigned int>(mFaces.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
mesh->mFaces = new aiFace[mesh->mNumFaces];
|
2020-06-23 19:05:42 +00:00
|
|
|
for (unsigned int a = 0; a < mesh->mNumFaces; a++) {
|
|
|
|
const Face &inface = mFaces[a];
|
|
|
|
aiFace &outface = mesh->mFaces[a];
|
2015-05-19 03:57:13 +00:00
|
|
|
outface.mNumIndices = 3;
|
|
|
|
outface.mIndices = new unsigned int[3];
|
|
|
|
outface.mIndices[0] = inface.mIndices[0];
|
|
|
|
outface.mIndices[1] = inface.mIndices[1];
|
|
|
|
outface.mIndices[2] = inface.mIndices[2];
|
|
|
|
|
|
|
|
// Compute per-face normals ... we don't want the bones to be smoothed ... they're built to visualize
|
|
|
|
// the skeleton, so it's good if there's a visual difference to the rest of the geometry
|
|
|
|
aiVector3D nor = ((mVertices[inface.mIndices[2]] - mVertices[inface.mIndices[0]]) ^
|
2020-06-23 19:05:42 +00:00
|
|
|
(mVertices[inface.mIndices[1]] - mVertices[inface.mIndices[0]]));
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2016-07-15 05:38:29 +00:00
|
|
|
if (nor.Length() < 1e-5) /* ensure that FindInvalidData won't remove us ...*/
|
2020-06-23 19:05:42 +00:00
|
|
|
nor = aiVector3D(1.0, 0.0, 0.0);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
for (unsigned int n = 0; n < 3; ++n)
|
|
|
|
mesh->mNormals[inface.mIndices[n]] = nor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// add the bones
|
2016-11-27 02:17:02 +00:00
|
|
|
mesh->mNumBones = static_cast<unsigned int>(mBones.size());
|
2020-06-23 19:05:42 +00:00
|
|
|
mesh->mBones = new aiBone *[mesh->mNumBones];
|
|
|
|
std::copy(mBones.begin(), mBones.end(), mesh->mBones);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// default
|
|
|
|
mesh->mMaterialIndex = 0;
|
|
|
|
|
|
|
|
return mesh;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Creates a dummy material and returns it.
|
2020-06-23 19:05:42 +00:00
|
|
|
aiMaterial *SkeletonMeshBuilder::CreateMaterial() {
|
|
|
|
aiMaterial *matHelper = new aiMaterial;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Name
|
2020-06-23 19:05:42 +00:00
|
|
|
aiString matName(std::string("SkeletonMaterial"));
|
|
|
|
matHelper->AddProperty(&matName, AI_MATKEY_NAME);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// Prevent backface culling
|
|
|
|
const int no_cull = 1;
|
2020-06-23 19:05:42 +00:00
|
|
|
matHelper->AddProperty(&no_cull, 1, AI_MATKEY_TWOSIDED);
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
return matHelper;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|