Esempio n. 1
0
void OgreApplication::SetupAnimation(Ogre::String object_name){

	/* Retrieve scene manager and root scene node */
    Ogre::SceneManager* scene_manager = ogre_root_->getSceneManager("MySceneManager");
	Ogre::SceneNode* root_scene_node = scene_manager->getRootSceneNode();

	/* Set up animation */
	Ogre::Real duration = Ogre::Math::TWO_PI;
	Ogre::Real num_steps = 36;
	Ogre::Real step = duration/num_steps;
	Ogre::Animation* animation = scene_manager->createAnimation("Animation", duration);
	animation->setInterpolationMode(Ogre::Animation::IM_LINEAR);
	Ogre::Node *object_scene_node = root_scene_node->getChild(object_name);
	Ogre::NodeAnimationTrack* track = animation->createNodeTrack(0, object_scene_node);

	/* Set up frames for animation */
	Ogre::TransformKeyFrame* key;
	Ogre::Quaternion quat;
	for (int i = 0; i < num_steps; i++){
		Ogre::Real current = ((float) i) * step;
		key = track->createNodeKeyFrame(current);
		quat.FromAngleAxis(Ogre::Radian(-current), Ogre::Vector3(0, 1, 0));
		key->setRotation(quat);
		key->setScale(Ogre::Vector3(0.5, 0.5, 0.5));
	}

	/* Create animation state */
	animation_state_ = scene_manager->createAnimationState("Animation");
	animation_state_->setEnabled(true);
	animation_state_->setLoop(true);

	/* Turn on animating flag */
	animating_ = true;
}
void SkeletonSerializerEx::readAnimationTrack(
	Ogre::DataStreamPtr& stream, Ogre::Animation* anim, Ogre::Skeleton* pSkel)
{
	// unsigned short boneIndex     : Index of bone to apply to
	unsigned short boneHandle;
	readShorts(stream, &boneHandle, 1);

	// Find bone
	Ogre::Bone *targetBone = pSkel->getBone(boneHandle);

	// Create track
	Ogre::NodeAnimationTrack* pTrack = anim->createNodeTrack(boneHandle, targetBone);

	// Keep looking for nested keyframes
	if (!stream->eof())
	{
		unsigned short streamID = readChunk(stream);
		while((streamID == Ogre::SKELETON_ANIMATION_TRACK_KEYFRAME || streamID == 0x4120 )
				&& !stream->eof())
		{
			if (streamID == 0x4120)
			{			
				unsigned short len;
				unsigned short flags;
				readShorts(stream, &len, 1);
				readShorts(stream, &flags, 1);

				float time;
				for (int i = 0; i < len; i += 1)
				{
					readFloats(stream, &time, 1);
					Ogre::TransformKeyFrame *kf = pTrack->createNodeKeyFrame(time);

					Ogre::Quaternion rot = Ogre::Quaternion::IDENTITY;
					if (flags & 1)
					{
						readObject(stream, rot);
					}
					kf->setRotation(rot);

					Ogre::Vector3 trans = Ogre::Vector3::ZERO;
					if (flags & 2)
					{
						readObject(stream, trans);
					}
					kf->setTranslate(trans);

					// 为正确解析天龙八部模型的骨骼动画              
					Ogre::Vector3 scale = Ogre::Vector3::UNIT_SCALE;
					if (flags & 4)
					{
						readObject(stream, scale);
					}
					kf->setScale(scale);
				}
			}
			else
				readKeyFrame(stream, pTrack, pSkel);

			if (!stream->eof())
			{
				// Get next stream
				streamID = readChunk(stream);
			}
		}
		if (!stream->eof())
		{
			// Backpedal back to start of this stream if we've found a non-keyframe
			stream->skip(-STREAM_OVERHEAD_SIZE);
		}
	}
}
Esempio n. 3
0
/* Function used to load an AnimationState to a sceneNode.
 * Params:
 * 	@scnManager		the SceneManager
 * 	@node			The SceneNode to load the AnimationStates
 * 	@elem			The TiXmlElement where is the animation
 * Returns:
 * 	anim			On success
 * 	0				On error
 */
bool Util::getAnimation(Ogre::SceneManager *scnManager,
		Ogre::SceneNode *node,
		TiXmlElement *elem,
		std::list<Ogre::AnimationState *> &animList)
{
	ASSERT(scnManager);
	ASSERT(node);
	ASSERT(elem);

	if(Ogre::String(elem->Value()) != "animations") {
		debug("Invalid animation xml: %s \n", elem->Value());
		return false;
	}
	animList.clear();

	TiXmlElement *pElement = elem->FirstChildElement("animation");
	if(!pElement){
		debug("No animations found\n");
		return false;
	}
	while(pElement){
		TiXmlElement *actualElement = pElement;
		Ogre::String nombreanimacion = actualElement->Attribute("name");
		Ogre::String activada = actualElement->Attribute("enable");
		Ogre::String loop = actualElement->Attribute("loop");
		Ogre::String modointerpolacion = actualElement->Attribute("interpolationMode");
		Ogre::String modointerpolacionrotacion = actualElement->Attribute("rotationInterpolationMode");
		Ogre::Real longitud= Ogre::StringConverter::parseReal(actualElement->Attribute("length"));

		Ogre::SceneManager *sceneMgr = scnManager;
		Ogre::Animation *animrueda = sceneMgr->createAnimation(nombreanimacion,longitud);

		if (modointerpolacion == "spline") {
			animrueda->setInterpolationMode(Ogre::Animation::IM_SPLINE);
		} else //linear
		{
			animrueda->setInterpolationMode(Ogre::Animation::IM_LINEAR);
		}

		if (modointerpolacionrotacion == "spherical") {
			animrueda->setRotationInterpolationMode(Ogre::Animation::RIM_SPHERICAL);
		} else //linear
		{
			animrueda->setRotationInterpolationMode(Ogre::Animation::RIM_LINEAR);
		}

		Ogre::NodeAnimationTrack *track = animrueda->createNodeTrack(
				animrueda->getNumNodeTracks() + 1, node);

		actualElement = actualElement->FirstChildElement();
		do {
			Ogre::Real tiempo = Ogre::StringConverter::parseReal(
					actualElement->Attribute("time"));
			Ogre::TransformKeyFrame *kf = track->createNodeKeyFrame(tiempo);

			kf->setTranslate(
					parseVector3(actualElement->FirstChildElement("translation")));
			kf->setRotation(
					parseQuaternion(
							actualElement->FirstChildElement("rotation")));
			kf->setScale(
					parseVector3(
							actualElement->FirstChildElement("scale")));

		} while (actualElement = actualElement->NextSiblingElement());

		// Create the animation and put it in the list
		Ogre::AnimationState *as = scnManager->createAnimationState(nombreanimacion);
		as->setEnabled(false);
		as->setLoop(false);
		ASSERT(as);
		animList.push_back(as);
		pElement = pElement->NextSiblingElement("animation");
	}


	return true;
}