123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- //-----------------------------------------------------------------------------
- // Copyright (c) 2012 GarageGames, LLC
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to
- // deal in the Software without restriction, including without limitation the
- // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
- // sell copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
- // IN THE SOFTWARE.
- //-----------------------------------------------------------------------------
- #include "platform/platform.h"
- #include "ts/collada/colladaExtensions.h"
- #include "ts/assimp/assimpAppMesh.h"
- #include "ts/assimp/assimpAppNode.h"
- // assimp include files.
- #include <assimp/cimport.h>
- #include <assimp/scene.h>
- #include <assimp/postprocess.h>
- #include <assimp/types.h>
- bool AssimpAppMesh::fixedSizeEnabled = false;
- S32 AssimpAppMesh::fixedSize = 2;
- //------------------------------------------------------------------------------
- AssimpAppMesh::AssimpAppMesh(const struct aiMesh* mesh, AssimpAppNode* node)
- : mMeshData(mesh), appNode(node)
- {
- Con::printf("[ASSIMP] Mesh Created: %s", getName());
- // See if it's a skinned mesh
- mIsSkinMesh = false;
- for (U32 b = 0; b < mesh->mNumBones; b++)
- if (mMeshData->mBones[b]->mNumWeights > 0)
- {
- mIsSkinMesh = true;
- break;
- }
- }
- const char* AssimpAppMesh::getName(bool allowFixed)
- {
- // Some exporters add a 'PIVOT' or unnamed node between the mesh and the
- // actual object node. Detect this and return the object node name instead
- // of the pivot node.
- const char* nodeName = appNode->getName();
- if ( dStrEqual(nodeName, "null") || dStrEndsWith(nodeName, "PIVOT") )
- nodeName = appNode->getParentName();
- // If all geometry is being fixed to the same size, append the size
- // to the name
- return allowFixed && fixedSizeEnabled ? avar("%s %d", nodeName, fixedSize) : nodeName;
- }
- MatrixF AssimpAppMesh::getMeshTransform(F32 time)
- {
- return appNode->getNodeTransform(time);
- }
- void AssimpAppMesh::lockMesh(F32 t, const MatrixF& objOffset)
- {
- // After this function, the following are expected to be populated:
- // points, normals, uvs, primitives, indices
- // There is also colors and uv2s but those don't seem to be required.
- points.reserve(mMeshData->mNumVertices);
- uvs.reserve(mMeshData->mNumVertices);
- normals.reserve(mMeshData->mNumVertices);
- bool flipNormals = ColladaUtils::getOptions().invertNormals;
- bool noUVFound = false;
- for (U32 i = 0; i<mMeshData->mNumVertices; i++)
- {
- // Points and Normals
- aiVector3D pt = mMeshData->mVertices[i];
- aiVector3D nrm;
- if (mMeshData->HasNormals())
- nrm = mMeshData->mNormals[i];
- else
- nrm.Set(0, 0, 0);
- Point3F tmpVert;
- Point3F tmpNormal;
- tmpVert = Point3F(pt.x, pt.y, pt.z);
- tmpNormal = Point3F(nrm.x, nrm.y, nrm.z);
- if (flipNormals)
- tmpNormal *= -1.0f;
- objOffset.mulP(tmpVert);
- points.push_back(tmpVert);
- if (mMeshData->HasTextureCoords(0))
- {
- uvs.push_back(Point2F(mMeshData->mTextureCoords[0][i].x, mMeshData->mTextureCoords[0][i].y));
- }
- else
- {
- // I don't know if there's any solution to this issue.
- // If it's not mapped, it's not mapped.
- noUVFound = true;
- uvs.push_back(Point2F(1, 1));
- }
- // UV2s
- if (mMeshData->HasTextureCoords(1))
- {
- uv2s.push_back(Point2F(mMeshData->mTextureCoords[1][i].x, mMeshData->mTextureCoords[1][i].y));
- }
- // Vertex Colors
- if (mMeshData->HasVertexColors(0))
- {
- LinearColorF vColor(mMeshData->mColors[0][i].r,
- mMeshData->mColors[0][i].g,
- mMeshData->mColors[0][i].b,
- mMeshData->mColors[0][i].a);
- colors.push_back(vColor.toColorI());
- }
- //uvs.push_back(mModel->mVerts[i].texcoord);
- normals.push_back(tmpNormal);
- //edgeVerts.push_back(mModel->mVerts[i].edge);
- }
- U32 numFaces = mMeshData->mNumFaces;
- //primitives.reserve(numFaces);
- //Fetch the number of indices
- U32 indicesCount = 0;
- for (U32 i = 0; i < numFaces; i++)
- {
- indicesCount += mMeshData->mFaces[i].mNumIndices;
- }
- indices.reserve(indicesCount);
- // Create TSMesh primitive
- primitives.increment();
- TSDrawPrimitive& primitive = primitives.last();
- primitive.start = 0;
- primitive.matIndex = (TSDrawPrimitive::Triangles | TSDrawPrimitive::Indexed) | (S32)mMeshData->mMaterialIndex;
- primitive.numElements = indicesCount;
- for ( U32 n = 0; n < mMeshData->mNumFaces; ++n)
- {
- const struct aiFace* face = &mMeshData->mFaces[n];
- if ( face->mNumIndices == 3 )
- {
- U32 indexCount = face->mNumIndices;
- for (U32 ind = 0; ind < indexCount; ind++)
- {
- U32 index = face->mIndices[ind];
- indices.push_back(index);
- }
- }
- else
- {
- Con::printf("[ASSIMP] Non-Triangle Face Found. Indices: %d", face->mNumIndices);
- }
- }
- U32 boneCount = mMeshData->mNumBones;
- bones.setSize(boneCount);
- // Count the total number of weights for all of the bones.
- U32 totalWeights = 0;
- U32 nonZeroWeights = 0;
- for (U32 b = 0; b < boneCount; b++)
- totalWeights += mMeshData->mBones[b]->mNumWeights;
- // Assimp gives weights sorted by bone index. We need them in vertex order.
- Vector<F32> tmpWeight;
- Vector<S32> tmpBoneIndex;
- Vector<S32> tmpVertexIndex;
- tmpWeight.setSize(totalWeights);
- tmpBoneIndex.setSize(totalWeights);
- tmpVertexIndex.setSize(totalWeights);
- for (U32 b = 0; b < boneCount; b++)
- {
- String name = mMeshData->mBones[b]->mName.C_Str();
- aiNode* nodePtr = AssimpAppNode::findChildNodeByName(mMeshData->mBones[b]->mName.C_Str(), appNode->mScene->mRootNode);
- if (!nodePtr)
- bones[b] = new AssimpAppNode(appNode->mScene, appNode->mNode);
- else
- bones[b] = new AssimpAppNode(appNode->mScene, nodePtr);
- MatrixF boneTransform;
- AssimpAppNode::assimpToTorqueMat(mMeshData->mBones[b]->mOffsetMatrix, boneTransform);
- Point3F boneScale = boneTransform.getScale();
- Point3F bonePos = boneTransform.getPosition();
- if (boneScale != Point3F::One && ColladaUtils::getOptions().ignoreNodeScale)
- {
- Point3F scaleMult = Point3F::One / boneScale;
- boneTransform.scale(scaleMult);
- bonePos /= scaleMult;
- }
- bonePos *= ColladaUtils::getOptions().unit * ColladaUtils::getOptions().formatScaleFactor;
- boneTransform.setPosition(bonePos);
- initialTransforms.push_back(boneTransform);
- //Weights
- U32 numWeights = mMeshData->mBones[b]->mNumWeights;
- for (U32 w = 0; w < numWeights; ++w)
- {
- aiVertexWeight* aiWeight = &mMeshData->mBones[b]->mWeights[w];
- if (aiWeight->mWeight > 0.0f)
- {
- tmpWeight[nonZeroWeights] = aiWeight->mWeight;
- tmpVertexIndex[nonZeroWeights] = aiWeight->mVertexId;
- tmpBoneIndex[nonZeroWeights] = b;
- nonZeroWeights++;
- }
- }
- }
- weight.setSize(nonZeroWeights);
- vertexIndex.setSize(nonZeroWeights);
- boneIndex.setSize(nonZeroWeights);
- // Copy the weights to our vectors in vertex order and
- // normalize vertex weights (force weights for each vert to sum to 1)
- U32 nextWeight = 0;
- for (U32 i = 0; i < mMeshData->mNumVertices; ++i)
- {
- U32 vertStart = nextWeight;
- F32 invTotalWeight = 0;
- for (U32 ind = 0; ind < nonZeroWeights; ++ind)
- {
- if (tmpVertexIndex[ind] == i)
- {
- weight[nextWeight] = tmpWeight[ind];
- invTotalWeight += tmpWeight[ind];
- vertexIndex[nextWeight] = tmpVertexIndex[ind];
- boneIndex[nextWeight] = tmpBoneIndex[ind];
- nextWeight++;
- }
- }
- // Now normalize the vertex weights
- if (invTotalWeight > 0.0)
- {
- invTotalWeight = 1.0f / invTotalWeight;
- for (U32 ind = vertStart; ind < nextWeight; ++ind)
- weight[ind] *= invTotalWeight;
- }
- }
- if ( noUVFound )
- Con::warnf("[ASSIMP] No UV Data for mesh.");
- }
- void AssimpAppMesh::lookupSkinData()
- { // This function is intentionally left blank. The skin data - bones, weights and indexes are
- // processed in lockMesh() with the rest of the mesh data.
- }
- F32 AssimpAppMesh::getVisValue(F32 t)
- {
- return 1.0f;
- }
|