const Vec3 Positionable::position() const { Vec4 position = m_transformationMatrix * Vec4(0,0,0,1); Vec3 resultPosition(position.x, position.y, position.z); return resultPosition; }
void Model::draw(unsigned int animation, double timeIn) { if (scene->mNumAnimations <= animation) return; aiAnimation* currentAnimation = scene->mAnimations[animation]; //Step 1: Interpolate position, rotation, scale in the all channels and add to transformation for (unsigned int i = 0; i < currentAnimation->mNumChannels; i++) { aiNodeAnim* currentChannel = currentAnimation->mChannels[i]; aiVector3D currentPosition = interpolatePosition(currentChannel, timeIn); aiQuaternion currentRotation = interpolateRotation(currentChannel, timeIn); aiVector3D currentScale = interpolateScale(currentChannel, timeIn); aiMatrix4x4& transformation = aiMatrix4x4(currentScale, currentRotation, currentPosition); aiNode* currentNode = scene->mRootNode->FindNode(currentChannel->mNodeName); currentNode->mTransformation = transformation; } for (unsigned int k = 0; k < scene->mNumMeshes; k++) { //Step 2: Bone transformation, change transformation of the bone according to animation aiMesh* currentMesh = scene->mMeshes[k]; std::vector<aiMatrix4x4> boneTransformations = std::vector<aiMatrix4x4>(currentMesh->mNumBones); for (unsigned int i = 0; i < currentMesh->mNumBones; i++) { aiBone* currentBone = currentMesh->mBones[i]; aiNode* currentNode = scene->mRootNode->FindNode(currentBone->mName); boneTransformations[i] = parentMultiplication(currentNode); boneTransformations[i] *= currentMesh->mBones[i]->mOffsetMatrix; } //Step 3: Skinning std::vector<aiVector3D> resultPosition(currentMesh->mNumVertices); for (size_t k = 0; k < currentMesh->mNumBones; k++) { const aiBone* currentBone = currentMesh->mBones[k]; const aiMatrix4x4& positionMatrix = boneTransformations[k]; for (size_t j = 0; j < currentBone->mNumWeights; j++) { const aiVertexWeight& weight = currentBone->mWeights[j]; size_t vertexId = weight.mVertexId; const aiVector3D& srcPosition = currentMesh->mVertices[vertexId]; resultPosition[vertexId] += weight.mWeight * (positionMatrix * srcPosition); } } //Step 4: Draw the final model // For every face size_t cv = 0, ctc = 0; glColor3d(1.0, 1.0, 1.0); // Set the face color to white for (int cf = 0; cf < currentMesh->mNumFaces; cf++) { const aiFace& face = currentMesh->mFaces[cf]; // For all vertices in face (Final drawing) if (wireframe) glBegin(GL_LINE_LOOP); else glBegin(GL_TRIANGLES); for (int cfi = 0; cfi < 3; cfi++) { double x = resultPosition[face.mIndices[cfi]].x; double y = resultPosition[face.mIndices[cfi]].y; double z = resultPosition[face.mIndices[cfi]].z; glVertex3d(x, y, z); } glEnd(); } } }