//-------------------------------------------------------------------------- void VertexAnimationTrack::_applyBaseKeyFrame(const KeyFrame* b) { const VertexPoseKeyFrame* base = static_cast<const VertexPoseKeyFrame*>(b); for (KeyFrameList::iterator i = mKeyFrames.begin(); i != mKeyFrames.end(); ++i) { VertexPoseKeyFrame* kf = static_cast<VertexPoseKeyFrame*>(*i); kf->_applyBaseKeyFrame(base); } }
//--------------------------------------------------------------------- void PlayPen_testPoseAnimationWithoutNormals::setupContent() { mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5)); Vector3 dir(-1, -1, 0.5); dir.normalise(); Light* l = mSceneMgr->createLight("light1"); l->setType(Light::LT_DIRECTIONAL); l->setDirection(dir); MeshPtr mesh = MeshManager::getSingleton().load("cube.mesh", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME); String newName = "testposenonormals.mesh"; mesh = mesh->clone(newName); SubMesh* sm = mesh->getSubMesh(0); // Re-organise geometry since this mesh has no animation and all // vertex elements are packed into one buffer VertexDeclaration* newDecl = sm->vertexData->vertexDeclaration->getAutoOrganisedDeclaration(false, true, false); sm->vertexData->reorganiseBuffers(newDecl); // create 2 poses Pose* pose = mesh->createPose(1, "pose1"); // Pose1 moves vertices 0, 1, 2 and 3 upward Vector3 offset1(0, 50, 0); pose->addVertex(0, offset1); pose->addVertex(1, offset1); pose->addVertex(2, offset1); pose->addVertex(3, offset1); pose = mesh->createPose(1, "pose2"); // Pose2 moves vertices 3, 4, and 5 to the right // Note 3 gets affected by both Vector3 offset2(100, 0, 0); pose->addVertex(3, offset2); pose->addVertex(4, offset2); pose->addVertex(5, offset2); Animation* anim = mesh->createAnimation("poseanim", 20.0f); VertexAnimationTrack* vt = anim->createVertexTrack(1, sm->vertexData, VAT_POSE); // Frame 0 - no effect VertexPoseKeyFrame* kf = vt->createVertexPoseKeyFrame(0); // Frame 1 - bring in pose 1 (index 0) kf = vt->createVertexPoseKeyFrame(3); kf->addPoseReference(0, 1.0f); // Frame 2 - remove all kf = vt->createVertexPoseKeyFrame(6); // Frame 3 - bring in pose 2 (index 1) kf = vt->createVertexPoseKeyFrame(9); kf->addPoseReference(1, 1.0f); // Frame 4 - remove all kf = vt->createVertexPoseKeyFrame(12); // Frame 5 - bring in pose 1 at 50%, pose 2 at 100% kf = vt->createVertexPoseKeyFrame(15); kf->addPoseReference(0, 0.5f); kf->addPoseReference(1, 1.0f); // Frame 6 - bring in pose 1 at 100%, pose 2 at 50% kf = vt->createVertexPoseKeyFrame(18); kf->addPoseReference(0, 1.0f); kf->addPoseReference(1, 0.5f); // Frame 7 - reset kf = vt->createVertexPoseKeyFrame(20); // Export the mesh DataStreamPtr stream = Root::getSingleton().createFileStream(newName, ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true); MeshSerializer ser; ser.exportMesh(mesh.get(), stream); stream->close(); // Unload old mesh to force reload MeshManager::getSingleton().remove(mesh->getHandle()); mesh->unload(); mesh.setNull(); Entity* e; AnimationState* animState; // software pose e = mSceneMgr->createEntity("test2", newName); mSceneMgr->getRootSceneNode()->createChildSceneNode(Vector3(150,0,0))->attachObject(e); animState = e->getAnimationState("poseanim"); animState->setEnabled(true); animState->setWeight(1.0f); mAnimStateList.push_back(animState); // test hardware pose e = mSceneMgr->createEntity("test", newName); mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(e); e->setMaterialName("Examples/HardwarePoseAnimation"); animState = e->getAnimationState("poseanim"); animState->setEnabled(true); animState->setWeight(1.0f); mAnimStateList.push_back(animState); mCamera->setNearClipDistance(0.5); Plane plane; plane.normal = Vector3::UNIT_Y; plane.d = 200; MeshManager::getSingleton().createPlane("Myplane", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane, 1500,1500,10,10,true,1,5,5,Vector3::UNIT_Z); Entity* pPlaneEnt = mSceneMgr->createEntity( "plane", "Myplane" ); pPlaneEnt->setMaterialName("2 - Default"); pPlaneEnt->setCastShadows(false); mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(pPlaneEnt); mCamera->setPosition(0,-200,-300); mCamera->lookAt(0,0,0); }
//-------------------------------------------------------------------------- void VertexAnimationTrack::getInterpolatedKeyFrame(const TimeIndex& timeIndex, KeyFrame* kf) const { // Only relevant for pose animation if (mAnimationType == VAT_POSE) { // Get keyframes KeyFrame *kf1, *kf2; Real t = getKeyFramesAtTime(timeIndex, &kf1, &kf2); VertexPoseKeyFrame* vkfOut = static_cast<VertexPoseKeyFrame*>(kf); VertexPoseKeyFrame* vkf1 = static_cast<VertexPoseKeyFrame*>(kf1); VertexPoseKeyFrame* vkf2 = static_cast<VertexPoseKeyFrame*>(kf2); // For each pose reference in key 1, we need to locate the entry in // key 2 and interpolate the influence const VertexPoseKeyFrame::PoseRefList& poseList1 = vkf1->getPoseReferences(); const VertexPoseKeyFrame::PoseRefList& poseList2 = vkf2->getPoseReferences(); for (VertexPoseKeyFrame::PoseRefList::const_iterator p1 = poseList1.begin(); p1 != poseList1.end(); ++p1) { Real startInfluence = p1->influence; Real endInfluence = 0; // Search for entry in keyframe 2 list (if not there, will be 0) for (VertexPoseKeyFrame::PoseRefList::const_iterator p2 = poseList2.begin(); p2 != poseList2.end(); ++p2) { if (p1->poseIndex == p2->poseIndex) { endInfluence = p2->influence; break; } } // Interpolate influence Real influence = startInfluence + t*(endInfluence - startInfluence); vkfOut->addPoseReference(p1->poseIndex, influence); } // Now deal with any poses in key 2 which are not in key 1 for (VertexPoseKeyFrame::PoseRefList::const_iterator p2 = poseList2.begin(); p2 != poseList2.end(); ++p2) { bool found = false; for (VertexPoseKeyFrame::PoseRefList::const_iterator p1 = poseList1.begin(); p1 != poseList1.end(); ++p1) { if (p1->poseIndex == p2->poseIndex) { found = true; break; } } if (!found) { // Need to apply this pose too, scaled from 0 start Real influence = t * p2->influence; vkfOut->addPoseReference(p2->poseIndex, influence); } } // key 2 iteration } }
SkeletonPtr MergeSkeleton::bake() { MeshCombiner::getSingleton().log( "Baking: New Skeleton started" ); SkeletonPtr sp = SkeletonManager::getSingleton().create( "mergeSkeleton", ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true ); for( std::vector< Ogre::SkeletonPtr >::iterator it = m_Skeletons.begin(); it != m_Skeletons.end(); ++it ) { if( it == m_Skeletons.begin() ) { MeshCombiner::getSingleton().log( "Baking: using " + (*it)->getName() + " as the base skeleton" ); MeshCombiner::getSingleton().log( "Baking: adding bones" ); Skeleton::BoneIterator bit = (*it)->getBoneIterator(); while( bit.hasMoreElements() ) { Bone* bone = bit.getNext(); Bone* newbone = sp->createBone( bone->getName(), bone->getHandle() ); newbone->setScale( bone->getScale() ); newbone->setOrientation( bone->getOrientation() ); newbone->setPosition( bone->getPosition() ); } MeshCombiner::getSingleton().log( "Baking: building bone hierarchy" ); // bone hierarchy bit = (*it)->getBoneIterator(); while( bit.hasMoreElements() ) { Bone* bone = bit.getNext(); Node* pnode = bone->getParent(); if( pnode != NULL ) { Bone* pbone = static_cast<Bone*>( pnode ); sp->getBone( pbone->getHandle() )->addChild( sp->getBone( bone->getHandle() ) ); } } } MeshCombiner::getSingleton().log( "Baking: adding animations for " + (*it)->getName() ); // insert all animations for (unsigned short a=0; a < (*it)->getNumAnimations(); ++a ) { Animation* anim = (*it)->getAnimation( a ); Animation* newanim = sp->createAnimation( anim->getName(), anim->getLength() ); if( anim->getNumNodeTracks() > 0 ) MeshCombiner::getSingleton().log( "Baking: adding node tracks" ); for( unsigned short na=0; na < anim->getNumNodeTracks(); ++na ) { if( anim->hasNodeTrack( na ) ) { NodeAnimationTrack* nat = anim->getNodeTrack( na ); NodeAnimationTrack* newnat = newanim->createNodeTrack( na ); // all key frames for( unsigned short nf=0; nf < nat->getNumKeyFrames(); ++nf ) { TransformKeyFrame* tkf = nat->getNodeKeyFrame( nf ); TransformKeyFrame* newtkf = newnat->createNodeKeyFrame( tkf->getTime() ); newtkf->setRotation( tkf->getRotation() ); newtkf->setTranslate( tkf->getTranslate() ); newtkf->setScale( tkf->getScale() ); } newnat->setAssociatedNode( sp->getBone( nat->getHandle() ) ); } } if( anim->getNumNumericTracks() > 0 ) MeshCombiner::getSingleton().log( "Baking: adding numeric tracks" ); for( unsigned short na=0; na < anim->getNumNumericTracks(); ++na ) { if( anim->hasNumericTrack( na ) ) { NumericAnimationTrack* nat = anim->getNumericTrack( na ); NumericAnimationTrack* newnat = newanim->createNumericTrack( na ); // all key frames for( unsigned short nf=0; nf < nat->getNumKeyFrames(); ++nf ) { NumericKeyFrame* nkf = nat->getNumericKeyFrame( nf ); NumericKeyFrame* newnkf = newnat->createNumericKeyFrame( nkf->getTime() ); newnkf->setValue( nkf->getValue() ); } } } if( anim->getNumVertexTracks() > 0 ) MeshCombiner::getSingleton().log( "Baking: adding vertex tracks" ); for( unsigned short va=0; va < anim->getNumVertexTracks(); ++va ) { if( anim->hasVertexTrack( va ) ) { VertexAnimationTrack* vat = anim->getVertexTrack( va ); VertexAnimationTrack* newvat = newanim->createVertexTrack( va, vat->getAnimationType() ); // all key frames for( unsigned short nf=0; nf < vat->getNumKeyFrames(); ++nf ) { // all morphs VertexMorphKeyFrame* vmkf = vat->getVertexMorphKeyFrame( nf ); if( vmkf != NULL ) { VertexMorphKeyFrame* newvmkf = newvat->createVertexMorphKeyFrame( vmkf->getTime() ); // @todo vertex buffer copying correct?? HardwareVertexBufferSharedPtr buf = vmkf->getVertexBuffer(); HardwareVertexBufferSharedPtr newbuf = HardwareBufferManager::getSingleton().createVertexBuffer( buf->getVertexSize(), buf->getNumVertices(), buf->getUsage(), buf->hasShadowBuffer() ); newbuf->copyData( *buf.getPointer(), 0, 0, buf->getSizeInBytes() ); } // all poses VertexPoseKeyFrame* vpkf = vat->getVertexPoseKeyFrame( nf ); if( vpkf != NULL ) { VertexPoseKeyFrame* newvpkf = newvat->createVertexPoseKeyFrame( vpkf->getTime() ); VertexPoseKeyFrame::PoseRefIterator pit = vpkf->getPoseReferenceIterator(); while( pit.hasMoreElements() ) { VertexPoseKeyFrame::PoseRef pr = pit.getNext(); newvpkf->addPoseReference( pr.poseIndex, pr.influence ); } } } } } } } return sp; }