NL3D::CLodCharacterBuilder Class Reference

#include <lod_character_builder.h>


Detailed Description

A tool class used to build a CLodCharacterShape Use it by first calling setShape(), then add animations to it, and finally get result with getLodShape
Author:
Lionel Berenguier

Nevrax France

Date:
2002

Definition at line 49 of file lod_character_builder.h.

Public Member Functions

void addAnim (const char *animName, CAnimation *animation, float frameRate)
 CLodCharacterBuilder ()
 Constructor.

const CLodCharacterShapegetLodShape () const
 return the lod shape in its current state.

void setShape (const std::string &name, CSkeletonShape *skeletonShape, CLodCharacterShapeBuild *lodBuild)
 ~CLodCharacterBuilder ()

Private Member Functions

void applySkin (CSkeletonModel *skeleton, CVector *dstVertices)

Private Attributes

std::vector< uint_BoneRemap
CLodCharacterShapeBuild_LodBuild
CLodCharacterShape _LodCharacterShape
NLMISC::CSmartPtr< CSkeletonShape_SkeletonShape
CScene_TmpScene


Constructor & Destructor Documentation

NL3D::CLodCharacterBuilder::CLodCharacterBuilder  ) 
 

Constructor.

Definition at line 45 of file lod_character_builder.cpp.

References _LodBuild, _SkeletonShape, and _TmpScene.

00046 {
00047         _SkeletonShape= NULL;
00048         _LodBuild= NULL;
00049         _TmpScene= NULL;
00050 }

NL3D::CLodCharacterBuilder::~CLodCharacterBuilder  ) 
 

Definition at line 52 of file lod_character_builder.cpp.

References _TmpScene, and NL3D::CScene::release().

00053 {
00054         // release the scene
00055         if(_TmpScene)
00056         {
00057                 _TmpScene->release();
00058                 delete _TmpScene;
00059                 _TmpScene= NULL;
00060         }
00061 }


Member Function Documentation

void NL3D::CLodCharacterBuilder::addAnim const char *  animName,
CAnimation animation,
float  frameRate
 

add an animation to the process

Parameters:
animName is the name of the animation, used as a key to receover animations in the CLodCharacterShape built
animation is the animation to bake/over sample. NB: the animation is deleted at the end of addAnim
frameRate is the desired overSampling rate. eg: 20 frame per second.

Definition at line 111 of file lod_character_builder.cpp.

References _LodCharacterShape, _SkeletonShape, _TmpScene, NL3D::CLodCharacterShape::addAnim(), NL3D::CAnimationSet::addAnimation(), NL3D::CLodCharacterShape::CAnimBuild::AnimLength, applySkin(), NL3D::CAnimationSet::build(), NL3D::CSkeletonModel::computeAllBones(), NL3D::CScene::deleteModel(), NL3D::CChannelMixer::eval(), NL3D::CAnimationSet::getAnimationIdByName(), NL3D::CAnimation::getEndTime(), NL3D::CLodCharacterShape::getNumVertices(), NL3D::CLodCharacterShape::CAnimBuild::Keys, min, NL3D::CLodCharacterShape::CAnimBuild::Name, nlassert, NL3D::CLodCharacterShape::CAnimBuild::NumKeys, NL3D::CSkeletonModel::registerToChannelMixer(), NL3D::CChannelMixer::setAnimationSet(), NL3D::CChannelMixer::setSlotAnimation(), NL3D::CChannelMixer::setSlotTime(), uint, and uint64.

00112 {
00113         nlassert(frameRate>0);
00114         nlassert(animation);
00115 
00116         /*      Create a Scene, a skeletonModel, an animation set, and a channel mixer to play the animation
00117                 NB: no render is made and no driver is created. The scene is just here for correct creation of the skeleton
00118                 Yoyo: This is a tricky way, but I found it the easier one...
00119         */
00120 
00121         // Create Components necesssary to play the animation
00122         //==========================
00123 
00124         // create an animationSet, and a channelMixer.
00125         //--------------
00126         // build an animation set with the only one animation. This animation will be deleted with the animationSet
00127         CAnimationSet   *tmpAnimationSet= new CAnimationSet;
00128         tmpAnimationSet->addAnimation(animName, animation);
00129         tmpAnimationSet->build();
00130         // Build a channelMixer.
00131         CChannelMixer   *tmpChannelMixer= new CChannelMixer;
00132         tmpChannelMixer->setAnimationSet(tmpAnimationSet);
00133 
00134 
00135         // create a skeleton Model for animation
00136         //---------------
00137         CSkeletonModel  *skeleton= (CSkeletonModel*)_SkeletonShape->createInstance(*_TmpScene);
00138         // and skeleton it with animation
00139         skeleton->registerToChannelMixer(tmpChannelMixer, "");
00140         // activate the anim
00141         uint animID = tmpAnimationSet->getAnimationIdByName(animName);
00142         nlassert(animID != CAnimationSet::NotFound);
00143         tmpChannelMixer->setSlotAnimation(0, animID);
00144 
00145 
00146         // Build Dst Animation basics.
00147         //--------------
00148         CLodCharacterShape::CAnimBuild  dstAnim;
00149         dstAnim.Name= animName;
00150         dstAnim.AnimLength= animation->getEndTime();
00151         dstAnim.NumKeys= (uint)ceil(dstAnim.AnimLength * frameRate);
00152         dstAnim.NumKeys= max(1U, dstAnim.NumKeys);
00153         // resize array.
00154         dstAnim.Keys.resize(_LodCharacterShape.getNumVertices() * dstAnim.NumKeys);
00155 
00156 
00157         // Bake the animation
00158         //==========================
00159         double  time=0;
00160         double  dt= 1.0/(double)frameRate;
00161         uint64  evalDetaiDate= 0;
00162         for(uint i=0; i<dstAnim.NumKeys; i++, time+= dt)
00163         {
00164                 // clamp the time
00165                 time= min(time, (double)dstAnim.AnimLength);
00166 
00167                 // setup the channelMixer time
00168                 tmpChannelMixer->setSlotTime(0, (float)time);
00169 
00170                 // Eval the channelMixer, both global and detail
00171                 tmpChannelMixer->eval(false);
00172                 tmpChannelMixer->eval(true, evalDetaiDate++);
00173 
00174                 // Use the skeleton model to compute bone skin matrix, supposing an identity skeleton worldMatrix
00175                 skeleton->computeAllBones(CMatrix::Identity);
00176 
00177                 // apply the skinning from the current skeleton state
00178                 applySkin(skeleton, &dstAnim.Keys[i*_LodCharacterShape.getNumVertices()]);
00179         }
00180 
00181 
00182         // Add the animation to the lod
00183         //==========================
00184         _LodCharacterShape.addAnim(dstAnim);
00185 
00186 
00187         // Delete
00188         //==========================
00189         // release the skeleton
00190         _TmpScene->deleteModel(skeleton);
00191         // delete the channelMixer
00192         delete tmpChannelMixer;
00193         // delete the animationSet
00194         delete tmpAnimationSet;
00195 }

void NL3D::CLodCharacterBuilder::applySkin CSkeletonModel skeleton,
CVector dstVertices
[private]
 

Definition at line 199 of file lod_character_builder.cpp.

References _BoneRemap, _LodBuild, NL3D::CSkeletonModel::Bones, NL3D::CMesh::CSkinWeight::MatrixId, NL3D_MESH_SKINNING_MAX_MATRIX, NL3D::CLodCharacterShapeBuild::SkinWeights, uint, NL3D::CLodCharacterShapeBuild::Vertices, and NL3D::CMesh::CSkinWeight::Weights.

Referenced by addAnim().

00200 {
00201         uint    numVerts= _LodBuild->Vertices.size();
00202 
00203         // for all vertices.
00204         for(uint i=0; i<numVerts; i++)
00205         {
00206                 CMesh::CSkinWeight      &skinWgt= _LodBuild->SkinWeights[i];
00207                 CVector                         &srcVert= _LodBuild->Vertices[i];
00208                 CVector                         &dstVert= dstVertices[i];
00209                 dstVert= CVector::Null;
00210                 // parse all Weights, and add influence.
00211                 for(uint j=0; j<NL3D_MESH_SKINNING_MAX_MATRIX; j++)
00212                 {
00213                         float   wgt= skinWgt.Weights[j];
00214 
00215                         if(wgt==0)
00216                         {
00217                                 // this should not happen, at least weight 0 should have an influence.
00218                                 if(j==0)
00219                                         dstVert= srcVert;
00220                                 // no more influence for this vertex.
00221                                 break;
00222                         }
00223                         else
00224                         {
00225                                 // Get the skeleton bone to read.
00226                                 uint    boneId= _BoneRemap[skinWgt.MatrixId[j]];
00227                                 // Get the computed matrix from the skeleton.
00228                                 const   CMatrix &boneMat= skeleton->Bones[boneId].getBoneSkinMatrix();
00229                                 // Add the influence of this bone.
00230                                 dstVert+= (boneMat * srcVert) * wgt;
00231                         }
00232                 }
00233         }
00234 }

const CLodCharacterShape& NL3D::CLodCharacterBuilder::getLodShape  )  const [inline]
 

return the lod shape in its current state.

Definition at line 75 of file lod_character_builder.h.

References _LodCharacterShape.

00075 {return _LodCharacterShape;}

void NL3D::CLodCharacterBuilder::setShape const std::string &  name,
CSkeletonShape skeletonShape,
CLodCharacterShapeBuild lodBuild
 

init process with the Mesh and the SkeletonShape to use. NB: nlWarnings may occurs if don't find bones used by lodBuild, in the skeletonShape. In this case, root bone 0 of the skeleton shape is used.

Parameters:
name is the name to give to the shape
skeletonShape the skeleton used for animation. Ptr is handled by the builder as a smartPtr.
lodBuild the mesh information. Ptr is hold by the builder, but not deleted

Definition at line 64 of file lod_character_builder.cpp.

References _BoneRemap, _LodBuild, _LodCharacterShape, _SkeletonShape, _TmpScene, NL3D::CLodCharacterShape::buildMesh(), NL3D::CScene::initDefaultRoots(), NL3D::CScene::initQuadGridClipManager(), nlassert, nlwarning, sint32, and uint.

00065 {
00066         nlassert(skeletonShape);
00067         nlassert(lodBuild);
00068 
00069         // SmartPtr the skeleton Shape (NB: important because skeletonModel use it)
00070         _SkeletonShape= skeletonShape;
00071         // a std ptr.
00072         _LodBuild= lodBuild;
00073 
00074         // Remap bone, with help of lodBuild and skeleton names.
00075         _BoneRemap.resize(lodBuild->BonesNames.size());
00076         for(uint i=0; i<_BoneRemap.size(); i++)
00077         {
00078                 const std::string       &boneName= lodBuild->BonesNames[i];
00079                 sint32  boneId= _SkeletonShape->getBoneIdByName(boneName);
00080                 // If not found
00081                 if(boneId<0)
00082                 {
00083                         nlwarning("Not found a bone in the skeleton Shape: %s", boneName.c_str());
00084                         // use root bone.
00085                         _BoneRemap[i]= 0;
00086                 }
00087                 else
00088                         // remap
00089                         _BoneRemap[i]= boneId;
00090         }
00091 
00092         // build basics
00093         _LodCharacterShape.buildMesh(name, *_LodBuild);
00094 
00095         // Build a scene, for addAnim purpose
00096         if(!_TmpScene)
00097         {
00098                 _TmpScene= new CScene(false);
00099                 // Must init Statics for scene (because use it in addAnim). NB: never mind if done twice.
00100                 CScene::registerBasics();
00101                 // init default Roots.
00102                 _TmpScene->initDefaultRoots();
00103                 // Don't Set driver/viewport
00104                 // init QuadGridClipManager
00105                 _TmpScene->initQuadGridClipManager ();
00106         }
00107 }


Field Documentation

std::vector<uint> NL3D::CLodCharacterBuilder::_BoneRemap [private]
 

Definition at line 84 of file lod_character_builder.h.

Referenced by applySkin(), and setShape().

CLodCharacterShapeBuild* NL3D::CLodCharacterBuilder::_LodBuild [private]
 

Definition at line 83 of file lod_character_builder.h.

Referenced by applySkin(), CLodCharacterBuilder(), and setShape().

CLodCharacterShape NL3D::CLodCharacterBuilder::_LodCharacterShape [private]
 

Definition at line 80 of file lod_character_builder.h.

Referenced by addAnim(), getLodShape(), and setShape().

NLMISC::CSmartPtr<CSkeletonShape> NL3D::CLodCharacterBuilder::_SkeletonShape [private]
 

Definition at line 82 of file lod_character_builder.h.

Referenced by addAnim(), CLodCharacterBuilder(), and setShape().

CScene* NL3D::CLodCharacterBuilder::_TmpScene [private]
 

Definition at line 87 of file lod_character_builder.h.

Referenced by addAnim(), CLodCharacterBuilder(), setShape(), and ~CLodCharacterBuilder().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 06:49:47 2004 for NeL by doxygen 1.3.6