Ogre : How to actually get morph animations to work in the engine

来源:互联网 发布:淘宝优惠助手 编辑:程序博客网 时间:2024/06/10 07:34

Introduction

Ogre features both Morph animation and Pose animation. The difference between these two techniques is explained in the manual(external link). However, what is not explained there, is how to actually get morph animations to work in the engine. This article provides explanation of this.

Code in the engine

Morph animation works like skeletal animation from the in-engine point of view. If there is a morph animation in the mesh, then you can get it to animate like this:

Morph AnimationState usage

//get the animation Ogre::AnimationState* state = entity->getAnimationState("morph"); state->setEnabled(true);  //do this every frame to play the animation float time = 0.1f; state->addTime(time);

An example shader

  

You can let OGRE do software blending of the morph animation, but you can also do it in hardware. The shader will only get morph data from the engine if its AnimationState is enabled, so the code above is needed.

Morph.material

vertex_program Cg_Morph_VP cg {     source Morph.cg     entry_point Morph_VP     profiles vs_1_1      includes_morph_animation true      default_params     {         param_named_auto worldViewProjMatrix worldviewproj_matrix          param_named_auto animationPhase animation_parametric     } }  material Morph {     technique     {         pass         {             vertex_program_ref Cg_Morph_VP             {             }         }     } }

Morph.cg

  

Note that the second position is put in the first free texture coordinate.

void Morph_VP     (         float3 position1    : POSITION,         float2 uv        : TEXCOORD0,         float3 position2    : TEXCOORD1,          out float4 oPosition : POSITION,          uniform float4x4 worldViewProjMatrix,         uniform float animationPhase     ) {     float3 blendedPosition = lerp(position1, position2, animationPhase);     oPosition = mul(worldViewProjMatrix, float4(blendedPosition, 1));

}


注意事项:

        position2是放到TEXCOORD1的位置,如果你自己本身有N个纹理坐标,那么position的位置会被放到末尾,即TEXCOORDN+1


原文链接:http://www.ogre3d.org/tikiwiki/tiki-index.php?page=Morph+animation




0 0
原创粉丝点击