NL3D::CLodCharacterShape Class Reference

#include <lod_character_shape.h>


Detailed Description

A very Small Shape with anims encoded as Key Meshes. Used for Lod of skinned meshes NB: normals are not skinned (for anim size consideration).
Author:
Lionel Berenguier

Nevrax France

Date:
2002

Definition at line 130 of file lod_character_shape.h.

Public Member Functions

bool addAnim (const CAnimBuild &animBuild)
void buildMesh (const std::string &name, const CLodCharacterShapeBuild &lodBuild)
 CLodCharacterShape ()
 Constructor.

sint getAnimIdByName (const std::string &name) const
 get the animId from a name. -1 if not found

const CVector3sgetAnimKey (uint animId, TGlobalAnimationTime time, bool wrapMode, CVector &unPackScaleFactor) const
sint getBoneIdByName (const std::string &name) const
 get a bone id, according to its name. -1 if not found

const std::string & getName () const
 Get name of this lod.

const CVectorgetNormals () const
 get a ptr to the triangles indices

uint getNumBones () const
 get the number of bones

uint getNumTriangles () const
 get the number of triangles of this mesh

uint getNumVertices () const
 get the number of vertices of this mesh

const uint32getTriangleArray () const
 get a ptr to the triangles indices

const CUVgetUVs () const
 get a ptr on the UVs.

void serial (NLMISC::IStream &f)
 serial this shape

Vertex per Bone AlphaTesting
This system is used for example to remove weapon of a Lod, using AlphaTesting (ie the polygons of the weapons are still drawn, but with alpha==0 => invisible)

void addBoneAlpha (uint boneId, std::vector< uint8 > &tmpAlphas) const
 Add a bone alpha influence to tmpAlpha.

void startBoneAlpha (std::vector< uint8 > &tmpAlphas) const
 init the process by resize-ing a tmp uint8 vector of getNumVertices() size, and reset to 0


Private Types

typedef TStrIdMap::const_iterator CstItStrIdMap
typedef TStrIdMap::iterator ItStrIdMap
typedef std::map< std::string,
uint32
TStrIdMap
 Map name To Id.


Private Attributes

TStrIdMap _AnimMap
std::vector< CAnim_Anims
 List of animation.

TStrIdMap _BoneMap
std::vector< CBoneInfluence_Bones
 List of bones and vertices they influence.

std::string _Name
std::vector< CVector_Normals
uint32 _NumTriangles
uint32 _NumVertices
std::vector< uint32_TriangleIndices
 numTriangles * 3.

std::vector< CUV_UVs


Member Typedef Documentation

typedef TStrIdMap::const_iterator NL3D::CLodCharacterShape::CstItStrIdMap [private]
 

Definition at line 278 of file lod_character_shape.h.

typedef TStrIdMap::iterator NL3D::CLodCharacterShape::ItStrIdMap [private]
 

Definition at line 277 of file lod_character_shape.h.

typedef std::map<std::string, uint32> NL3D::CLodCharacterShape::TStrIdMap [private]
 

Map name To Id.

Definition at line 276 of file lod_character_shape.h.


Constructor & Destructor Documentation

NL3D::CLodCharacterShape::CLodCharacterShape  ) 
 

Constructor.

Definition at line 330 of file lod_character_shape.cpp.

References _NumTriangles, and _NumVertices.

00331 {
00332         _NumVertices= 0;
00333         _NumTriangles= 0;
00334 }


Member Function Documentation

bool NL3D::CLodCharacterShape::addAnim const CAnimBuild animBuild  ) 
 

Add an animation. many nlAssert to verify array size etc... return false, if same AnimName exist. NB: the entire animation is compressed to CVector3s internally.

Definition at line 411 of file lod_character_shape.cpp.

References _AnimMap, _Anims, _NumVertices, NL3D::CLodCharacterShape::CAnimBuild::AnimLength, NL3D::CLodCharacterShape::CAnim::AnimLength, NLMISC::clamp(), getAnimIdByName(), NL3D::CLodCharacterShape::CAnim::Keys, NL3D::CLodCharacterShape::CAnimBuild::Keys, NLMISC::CVector::maxof(), NL3D::CLodCharacterShape::CAnim::Name, NL3D::CLodCharacterShape::CAnimBuild::Name, nlassert, NL3D::CLodCharacterShape::CAnimBuild::NumKeys, NL3D::CLodCharacterShape::CAnim::NumKeys, NL3D::CLodCharacterShape::CAnim::OOAnimLength, sint16, uint, NL3D::CLodCharacterShape::CAnim::UnPackScaleFactor, v, NL3D::CLodCharacterShape::CVector3s::x, NLMISC::CVector::x, NLMISC::CVectorD::x, NL3D::CLodCharacterShape::CVector3s::y, NLMISC::CVector::y, NLMISC::CVectorD::y, NL3D::CLodCharacterShape::CVector3s::z, NLMISC::CVector::z, and NLMISC::CVectorD::z.

Referenced by NL3D::CLodCharacterBuilder::addAnim().

00412 {
00413         // first, verify don't exist.
00414         if(getAnimIdByName(animBuild.Name)!=-1)
00415                 return false;
00416 
00417         // build basics of the animation
00418         CAnim   dstAnim;
00419         dstAnim.Name= animBuild.Name;
00420         dstAnim.AnimLength= animBuild.AnimLength;
00421         // Possible to have an Anim with just one key. setup an epsilon for animLength if 0.
00422         if(dstAnim.AnimLength<=0)
00423                 dstAnim.AnimLength= 0.001f;
00424         dstAnim.OOAnimLength= 1.0f / animBuild.AnimLength;
00425         dstAnim.NumKeys= animBuild.NumKeys;
00426         // verify size of the array
00427         nlassert(dstAnim.NumKeys>0);
00428         nlassert(dstAnim.NumKeys * _NumVertices == animBuild.Keys.size());
00429         // resize dest array
00430         dstAnim.Keys.resize(animBuild.Keys.size());
00431 
00432 
00433         // Pack animation. 1st pass: compute max size over the animation vertices
00434         uint    i;
00435         // minimum shape size is , say, 1 cm :)
00436         CVector         maxSize(0.01f, 0.01f, 0.01f);
00437         for(i=0;i<animBuild.Keys.size();i++)
00438         {
00439                 // take the maxSize of the abs values
00440                 maxSize.maxof(maxSize, -animBuild.Keys[i]);
00441                 maxSize.maxof(maxSize, animBuild.Keys[i]);
00442         }
00443 
00444         // compute the UnPackScaleFactor ie maxSize, to be multiplied by max Abs value of a sint16
00445         dstAnim.UnPackScaleFactor= maxSize * (1.0f/32767);
00446 
00447         // Pack animation. 2st pass: pack.
00448         CVectorD                packScaleFactor;
00449         packScaleFactor.x= 1.0 / dstAnim.UnPackScaleFactor.x;
00450         packScaleFactor.y= 1.0 / dstAnim.UnPackScaleFactor.y;
00451         packScaleFactor.z= 1.0 / dstAnim.UnPackScaleFactor.z;
00452         // For all key vertices
00453         for(i=0;i<animBuild.Keys.size();i++)
00454         {
00455                 CVector         v= animBuild.Keys[i];
00456                 CVector3s       &dstV= dstAnim.Keys[i];
00457 
00458                 // compress
00459                 v.x= float(v.x*packScaleFactor.x);
00460                 v.y= float(v.y*packScaleFactor.y);
00461                 v.z= float(v.z*packScaleFactor.z);
00462                 // clamp to sint16 limits (for float precision problems).
00463                 clamp(v.x, -32767, 32767);
00464                 clamp(v.y, -32767, 32767);
00465                 clamp(v.z, -32767, 32767);
00466                 // get into the vector3s
00467                 dstV.x= (sint16)floor(v.x);
00468                 dstV.y= (sint16)floor(v.y);
00469                 dstV.z= (sint16)floor(v.z);
00470         }
00471 
00472 
00473         // Add the anim to the array, and add an entry to the map
00474         _Anims.push_back(dstAnim);
00475         _AnimMap.insert(make_pair(dstAnim.Name, _Anims.size()-1));
00476 
00477         return true;
00478 }

void NL3D::CLodCharacterShape::addBoneAlpha uint  boneId,
std::vector< uint8 > &  tmpAlphas
const
 

Add a bone alpha influence to tmpAlpha.

Definition at line 641 of file lod_character_shape.cpp.

References _Bones, NL3D::CLodCharacterShape::CBoneInfluence::InfVertices, uint, and NL3D::CLodCharacterShape::CVertexInf::VertexId.

Referenced by NL3D::CSkeletonModel::computeCLodVertexAlpha().

00642 {
00643         // Yoyo: This is an error to not have the same skeleton that the one stored in the lod shape. But must not crash
00644         if(boneId>=_Bones.size())
00645                 return;
00646         const CBoneInfluence    &bone= _Bones[boneId];
00647 
00648         // for all vertices influenced by this bone, must set the alpha to full
00649         for(uint i=0; i<bone.InfVertices.size(); i++)
00650         {
00651                 const CVertexInf        &vInf= bone.InfVertices[i];
00652                 tmpAlphas[vInf.VertexId]= 255;
00653         }
00654 }

void NL3D::CLodCharacterShape::buildMesh const std::string &  name,
const CLodCharacterShapeBuild lodBuild
 

build the Mesh base information NB: SkinWeights array tells for each vertex what bone to use (for alpha test removing)

Definition at line 337 of file lod_character_shape.cpp.

References _AnimMap, _Anims, _BoneMap, _Bones, _Normals, _NumTriangles, _NumVertices, _TriangleIndices, NL3D::CLodCharacterShapeBuild::BonesNames, NL3D::CLodCharacterShape::CVertexInf::Influence, NL3D_MESH_SKINNING_MAX_MATRIX, nlassert, NL3D::CLodCharacterShapeBuild::Normals, NL3D::CLodCharacterShapeBuild::SkinWeights, NL3D::CLodCharacterShapeBuild::TriangleIndices, uint, NL3D::CLodCharacterShapeBuild::UVs, NL3D::CLodCharacterShape::CVertexInf::VertexId, and NL3D::CLodCharacterShapeBuild::Vertices.

Referenced by NL3D::CLodCharacterBuilder::setShape().

00338 {
00339         uint    numVertices= lodBuild.Vertices.size();
00340         const vector<uint32>                            &triangleIndices= lodBuild.TriangleIndices;
00341         const vector<CMesh::CSkinWeight>        &skinWeights= lodBuild.SkinWeights;
00342         const vector<CUV>                                       &uvs= lodBuild.UVs;
00343         const vector<CVector>                           &normals= lodBuild.Normals;
00344 
00345         nlassert(numVertices>0);
00346         nlassert(triangleIndices.size()>0);
00347         nlassert((triangleIndices.size()%3)==0);
00348         nlassert(skinWeights.size() == numVertices);
00349         nlassert(uvs.size() == numVertices);
00350         nlassert(normals.size() == numVertices);
00351 
00352         // reset data
00353         contReset(_Anims);
00354         contReset(_AnimMap);
00355         contReset(_Bones);
00356         contReset(_BoneMap);
00357         contReset(_TriangleIndices);
00358         contReset(_UVs);
00359         contReset(_Normals);
00360 
00361         // Copy data.
00362         _Name= name;
00363         _NumVertices= numVertices;
00364         _NumTriangles= triangleIndices.size()/3;
00365         _TriangleIndices= triangleIndices;
00366         _UVs= uvs;
00367         _Normals= normals;
00368 
00369         // check indices.
00370         uint    i;
00371         for(i=0;i<triangleIndices.size();i++)
00372         {
00373                 nlassert(triangleIndices[i]<_NumVertices);
00374         }
00375 
00376         // Copy bone names, and compute bone Map
00377         _Bones.resize(lodBuild.BonesNames.size());
00378         for(i=0; i<_Bones.size(); i++)
00379         {
00380                 _Bones[i].Name= lodBuild.BonesNames[i];
00381                 _BoneMap.insert( make_pair(_Bones[i].Name, i) );
00382         }
00383 
00384         // "Normalize" SkinWeights for CLodCharacterShape
00385         for(i=0;i<skinWeights.size();i++)
00386         {
00387                 nlassert(skinWeights[i].Weights[0]>0);
00388                 // for all slots not 0
00389                 for(uint j=0;j<NL3D_MESH_SKINNING_MAX_MATRIX;j++)
00390                 {
00391                         // if this this slot is used.
00392                         if(skinWeights[i].Weights[j]>0)
00393                         {
00394                                 uint boneId= skinWeights[i].MatrixId[j];
00395                                 nlassert(boneId < _Bones.size());
00396                                 // init the vInf data
00397                                 CVertexInf      vInf;
00398                                 vInf.VertexId= i;
00399                                 vInf.Influence= skinWeights[i].Weights[j];
00400                                 // Insert this vertex influence in the bone.
00401                                 _Bones[boneId].InfVertices.push_back(vInf);
00402                         }
00403                         else
00404                                 // stop for this vertex.
00405                                 break;
00406                 }
00407         }
00408 }

sint NL3D::CLodCharacterShape::getAnimIdByName const std::string &  name  )  const
 

get the animId from a name. -1 if not found

Definition at line 541 of file lod_character_shape.cpp.

References _AnimMap, and sint.

Referenced by addAnim(), and NL3D::CSceneUser::getCLodAnimIdByName().

00542 {
00543         CstItStrIdMap   it= _AnimMap.find(name);
00544         if(it == _AnimMap.end())
00545                 return -1;
00546         else
00547                 return it->second;
00548 }

const CLodCharacterShape::CVector3s * NL3D::CLodCharacterShape::getAnimKey uint  animId,
TGlobalAnimationTime  time,
bool  wrapMode,
CVector unPackScaleFactor
const
 

get a ptr to the vertices of the key according to animId and time. NB: the anim Loop if wrapMode is true

Parameters:
unPackScaleFactor return value is the scale factor which to multiply
Returns:
NULL if animId is not valid

Definition at line 572 of file lod_character_shape.cpp.

References _Anims, _NumVertices, NL3D::CLodCharacterShape::CAnim::AnimLength, NLMISC::clamp(), H_AUTO, NL3D::CLodCharacterShape::CAnim::Keys, NL3D::CLodCharacterShape::CAnim::NumKeys, NL3D::CLodCharacterShape::CAnim::OOAnimLength, sint, NL3D::TGlobalAnimationTime, uint, and NL3D::CLodCharacterShape::CAnim::UnPackScaleFactor.

Referenced by NL3D::CLodCharacterManager::addRenderCharacterKey().

00573 {
00574         H_AUTO( NL3D_LodCharacterShape_getAnimKey )
00575 
00576         float   localTime;
00577 
00578         if(animId>=_Anims.size())
00579                 return NULL;
00580 
00581         // get the anim.
00582         const CAnim &anim= _Anims[animId];
00583 
00584         // scale info
00585         unPackScaleFactor= anim.UnPackScaleFactor;
00586 
00587         // Loop mgt.
00588         if(wrapMode)
00589                 localTime= (float)fmod(time, anim.AnimLength);
00590         else
00591                 localTime= (float)time;
00592 
00593         // Clamp to the range.
00594         clamp(localTime, 0, anim.AnimLength);
00595 
00596         // get the key.
00597         sint    keyId= (sint)floor( (localTime*anim.OOAnimLength) * anim.NumKeys );
00598         clamp(keyId, 0, sint(anim.NumKeys-1));
00599 
00600         // return the key.
00601         return &anim.Keys[keyId * _NumVertices];
00602 }

sint NL3D::CLodCharacterShape::getBoneIdByName const std::string &  name  )  const
 

get a bone id, according to its name. -1 if not found

Definition at line 552 of file lod_character_shape.cpp.

References _BoneMap, and sint.

Referenced by NL3D::CSkeletonModel::computeCLodVertexAlpha().

00553 {
00554         CstItStrIdMap   it= _BoneMap.find(name);
00555         if(it == _BoneMap.end())
00556                 return -1;
00557         else
00558                 return it->second;
00559 }

const std::string& NL3D::CLodCharacterShape::getName  )  const [inline]
 

Get name of this lod.

Definition at line 178 of file lod_character_shape.h.

Referenced by NL3D::CLodCharacterManager::compile().

00178 {return _Name;}

const CVector * NL3D::CLodCharacterShape::getNormals  )  const
 

get a ptr to the triangles indices

Definition at line 614 of file lod_character_shape.cpp.

References _Normals, and _NumVertices.

Referenced by NL3D::CLodCharacterManager::addRenderCharacterKey().

00615 {
00616         if(_NumVertices==0)
00617                 return NULL;
00618 
00619         return &_Normals[0];
00620 }

uint NL3D::CLodCharacterShape::getNumBones  )  const [inline]
 

get the number of bones

Definition at line 190 of file lod_character_shape.h.

References _Bones, and uint.

00190 {return _Bones.size();}

uint NL3D::CLodCharacterShape::getNumTriangles  )  const [inline]
 

get the number of triangles of this mesh

Definition at line 187 of file lod_character_shape.h.

References _NumTriangles, and uint.

Referenced by NL3D::CLodCharacterManager::addRenderCharacterKey().

00187 {return _NumTriangles;}

uint NL3D::CLodCharacterShape::getNumVertices  )  const [inline]
 

get the number of vertices of this mesh

Definition at line 184 of file lod_character_shape.h.

References _NumVertices, and uint.

Referenced by NL3D::CLodCharacterBuilder::addAnim(), NL3D::CLodCharacterManager::addRenderCharacterKey(), NL3D::CLodCharacterManager::initInstance(), and startBoneAlpha().

00184 {return _NumVertices;}

const uint32 * NL3D::CLodCharacterShape::getTriangleArray  )  const
 

get a ptr to the triangles indices

Definition at line 563 of file lod_character_shape.cpp.

References _NumTriangles, _TriangleIndices, and uint32.

Referenced by NL3D::CLodCharacterManager::addRenderCharacterKey().

00564 {
00565         if(_NumTriangles)
00566                 return &_TriangleIndices[0];
00567         else
00568                 return NULL;
00569 }

const CUV * NL3D::CLodCharacterShape::getUVs  )  const
 

get a ptr on the UVs.

Definition at line 605 of file lod_character_shape.cpp.

References _NumVertices.

Referenced by NL3D::CLodCharacterManager::initInstance().

00606 {
00607         if(_NumVertices==0)
00608                 return NULL;
00609 
00610         return &_UVs[0];
00611 }

void NL3D::CLodCharacterShape::serial NLMISC::IStream f  ) 
 

serial this shape

Definition at line 505 of file lod_character_shape.cpp.

References _AnimMap, _Anims, _BoneMap, _Bones, _Normals, _NumTriangles, _NumVertices, _TriangleIndices, NLMISC::IStream::serial(), NLMISC::IStream::serialCheck(), NLMISC::IStream::serialCont(), NLMISC::IStream::serialVersion(), sint, and uint32.

00506 {
00507         // NEL_CLODSHAP
00508         f.serialCheck((uint32)'_LEN');
00509         f.serialCheck((uint32)'DOLC');
00510         f.serialCheck((uint32)'PAHS');
00511 
00512         /*
00513         Version 1:
00514                 - UVs and Normals.
00515         */
00516         sint ver= f.serialVersion(1);
00517 
00518         f.serial(_Name);
00519         f.serial(_NumVertices);
00520         f.serial(_NumTriangles);
00521         f.serialCont(_Bones);
00522         f.serialCont(_BoneMap);
00523         f.serialCont(_TriangleIndices);
00524         f.serialCont(_Anims);
00525         f.serialCont(_AnimMap);
00526 
00527         if(ver>=1)
00528         {
00529                 f.serialCont(_UVs);
00530                 f.serialCont(_Normals);
00531         }
00532         else
00533         {
00534                 // Must init dummy UVs/normals
00535                 _UVs.resize(_NumVertices, CUV(0,0));
00536                 _Normals.resize(_NumVertices, CVector::K);
00537         }
00538 }

void NL3D::CLodCharacterShape::startBoneAlpha std::vector< uint8 > &  tmpAlphas  )  const
 

init the process by resize-ing a tmp uint8 vector of getNumVertices() size, and reset to 0

Definition at line 632 of file lod_character_shape.cpp.

References getNumVertices().

Referenced by NL3D::CSkeletonModel::computeCLodVertexAlpha().

00633 {
00634         // clear
00635         tmpAlphas.clear();
00636         // alocate, and fill
00637         tmpAlphas.resize(getNumVertices(), 0);
00638 }


Field Documentation

TStrIdMap NL3D::CLodCharacterShape::_AnimMap [private]
 

Definition at line 300 of file lod_character_shape.h.

Referenced by addAnim(), buildMesh(), getAnimIdByName(), and serial().

std::vector<CAnim> NL3D::CLodCharacterShape::_Anims [private]
 

List of animation.

Definition at line 297 of file lod_character_shape.h.

Referenced by addAnim(), buildMesh(), getAnimKey(), and serial().

TStrIdMap NL3D::CLodCharacterShape::_BoneMap [private]
 

Definition at line 291 of file lod_character_shape.h.

Referenced by buildMesh(), getBoneIdByName(), and serial().

std::vector<CBoneInfluence> NL3D::CLodCharacterShape::_Bones [private]
 

List of bones and vertices they influence.

Definition at line 288 of file lod_character_shape.h.

Referenced by addBoneAlpha(), buildMesh(), getNumBones(), and serial().

std::string NL3D::CLodCharacterShape::_Name [private]
 

Definition at line 281 of file lod_character_shape.h.

std::vector<CVector> NL3D::CLodCharacterShape::_Normals [private]
 

Definition at line 286 of file lod_character_shape.h.

Referenced by buildMesh(), getNormals(), and serial().

uint32 NL3D::CLodCharacterShape::_NumTriangles [private]
 

Definition at line 283 of file lod_character_shape.h.

Referenced by buildMesh(), CLodCharacterShape(), getNumTriangles(), getTriangleArray(), and serial().

uint32 NL3D::CLodCharacterShape::_NumVertices [private]
 

Definition at line 282 of file lod_character_shape.h.

Referenced by addAnim(), buildMesh(), CLodCharacterShape(), getAnimKey(), getNormals(), getNumVertices(), getUVs(), and serial().

std::vector<uint32> NL3D::CLodCharacterShape::_TriangleIndices [private]
 

numTriangles * 3.

Definition at line 294 of file lod_character_shape.h.

Referenced by buildMesh(), getTriangleArray(), and serial().

std::vector<CUV> NL3D::CLodCharacterShape::_UVs [private]
 

Definition at line 285 of file lod_character_shape.h.


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