00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "AnimatedMeshComponent.h"
00018 #include "../../core/GameManager.h"
00019
00020
00021
00022
00023 AnimatedMeshComponent::AnimatedMeshComponent(Entity *parent)
00024 : SceneComponent(parent, true)
00025 {
00026 if(pParent != NULL)
00027 init();
00028 }
00029
00030
00031 AnimatedMeshComponent::AnimatedMeshComponent(Entity *parent, const std::string &fileName,
00032 const vector3df &scale)
00033 : SceneComponent(parent, true)
00034 {
00035 if(pParent != NULL)
00036 {
00037 init(scale);
00038 setMesh(fileName);
00039 }
00040 }
00041
00042
00043 AnimatedMeshComponent::AnimatedMeshComponent(Entity *parent, IAnimatedMesh *mesh,
00044 const vector3df &scale)
00045 : SceneComponent(parent, true)
00046 {
00047 if(pParent != NULL)
00048 {
00049 init(scale);
00050 setMesh(mesh);
00051 }
00052 }
00053
00054
00055 AnimatedMeshComponent::~AnimatedMeshComponent()
00056 {
00057 if(pParent != NULL)
00058 {
00059 mAnimatedMeshSN->remove();
00060 mSceneNode = NULL;
00061 }
00062 }
00063
00064
00065 void AnimatedMeshComponent::init(const vector3df &scale)
00066 {
00067
00068 ISceneManager *pSceneMgr = GameManager::Instance()->getSceneManager();
00069
00070
00071 mAnimatedMeshSN = pSceneMgr->addAnimatedMeshSceneNode(0, 0, pParent->getID(),
00072 pParent->getAbsolutePosition(),
00073 pParent->getAbsoluteRotation(),
00074 scale, true);
00075 mSceneNode = mAnimatedMeshSN;
00076 mShadowVolumeSN = NULL;
00077
00078
00079 mTriSelector = pSceneMgr->createTriangleSelectorFromBoundingBox(mAnimatedMeshSN);
00080 mMetaSelector = pSceneMgr->createMetaTriangleSelector();
00081 }
00082
00083
00084 IAnimatedMeshSceneNode* AnimatedMeshComponent::getAnimatedMeshSceneNode()
00085 {
00086 return mAnimatedMeshSN;
00087 }
00088
00089
00090 void AnimatedMeshComponent::animate()
00091 {
00092 mAnimatedMeshSN->animateJoints();
00093 }
00094
00095
00096 s32 AnimatedMeshComponent::getEndFrame() const
00097 {
00098 return mAnimatedMeshSN->getEndFrame();
00099 }
00100
00101
00102 f32 AnimatedMeshComponent::getFrame() const
00103 {
00104 return mAnimatedMeshSN->getFrameNr();
00105 }
00106
00107
00108 IMesh* AnimatedMeshComponent::getMesh()
00109 {
00110 return mAnimatedMeshSN->getMesh();
00111 }
00112
00113
00114 IShadowVolumeSceneNode* AnimatedMeshComponent::getShadowVolumeSceneNode()
00115 {
00116 return mShadowVolumeSN;
00117 }
00118
00119
00120 s32 AnimatedMeshComponent::getStartFrame() const
00121 {
00122 return mAnimatedMeshSN->getStartFrame();
00123 }
00124
00125
00126 void AnimatedMeshComponent::setAnimationSpeed(f32 speed)
00127 {
00128 mAnimatedMeshSN->setAnimationSpeed(speed);
00129 }
00130
00131
00132 void AnimatedMeshComponent::setCurrentFrame(f32 frame)
00133 {
00134 mAnimatedMeshSN->setCurrentFrame(frame);
00135 }
00136
00137
00138 void AnimatedMeshComponent::setFrameLoop(s32 start, s32 end)
00139 {
00140 mAnimatedMeshSN->setFrameLoop(start, end);
00141 }
00142
00143
00144 void AnimatedMeshComponent::setLoopMode(bool value)
00145 {
00146 mAnimatedMeshSN->setLoopMode(value);
00147 }
00148
00149
00150 void AnimatedMeshComponent::setMD2Animation(const std::string &animationName)
00151 {
00152 mAnimatedMeshSN->setMD2Animation(animationName.c_str());
00153 }
00154
00155
00156 void AnimatedMeshComponent::setMD2Animation(EMD2_ANIMATION_TYPE anim)
00157 {
00158 mAnimatedMeshSN->setMD2Animation(anim);
00159 }
00160
00161
00162 void AnimatedMeshComponent::setMesh(const std::string &fileName)
00163 {
00164
00165 AssetGroup *assets = pParent->getAssetGroup();
00166 IAnimatedMesh *mesh = 0;
00167
00168
00169 if(assets != NULL)
00170 {
00171
00172 assets->disconnectEventSignal(std::string("meshes/") + mMeshFileName, this);
00173
00174
00175 MeshProcessor *processor = static_cast<MeshProcessor*>(assets->getAssetProcessor("meshes"));
00176 mesh = processor->getMesh(fileName);
00177
00178 if(mesh)
00179 {
00180 assets->connectEventSignal(std::string("meshes/") + fileName, this,
00181 &AnimatedMeshComponent::onMesh);
00182 mMeshFileName = fileName;
00183 }
00184 }
00185
00186 else
00187 mesh = GameManager::Instance()->getSceneManager()->getMesh(fileName.c_str());
00188
00189
00190 mAnimatedMeshSN->setMesh(mesh);
00191 }
00192
00193
00194 void AnimatedMeshComponent::setMesh(IAnimatedMesh *mesh)
00195 {
00196
00197 AssetGroup *assets = pParent->getAssetGroup();
00198
00199 if(assets != NULL)
00200 {
00201 assets->disconnectEventSignal(std::string("meshes/") + mMeshFileName, this);
00202 mMeshFileName = "";
00203 }
00204
00205
00206 mAnimatedMeshSN->setMesh(mesh);
00207 }
00208
00209
00210 void AnimatedMeshComponent::setShadowVolumeMesh(const std::string &fileName)
00211 {
00212
00213 if(mShadowVolumeSN)
00214 {
00215
00216 AssetGroup *assets = pParent->getAssetGroup();
00217 IMesh *mesh = 0;
00218
00219
00220 if(assets != NULL)
00221 {
00222
00223 assets->disconnectEventSignal(std::string("meshes/") + mShadowVolumeMeshFileName, this);
00224
00225
00226 MeshProcessor *processor = static_cast<MeshProcessor*>(assets->getAssetProcessor("meshes"));
00227 mesh = processor->getMesh(fileName);
00228
00229 if(mesh)
00230 {
00231 assets->connectEventSignal(std::string("meshes/") + fileName, this,
00232 &AnimatedMeshComponent::onShadowVolumeMesh);
00233 mShadowVolumeMeshFileName = fileName;
00234 }
00235 }
00236
00237 else
00238 mesh = GameManager::Instance()->getSceneManager()->getMesh(fileName.c_str());
00239
00240
00241 mShadowVolumeSN->setShadowMesh(mesh);
00242 }
00243 }
00244
00245
00246 void AnimatedMeshComponent::setShadowVolumeMesh(IMesh *mesh)
00247 {
00248
00249 AssetGroup *assets = pParent->getAssetGroup();
00250
00251 if(assets != NULL)
00252 {
00253 assets->disconnectEventSignal(std::string("meshes/") + mShadowVolumeMeshFileName, this);
00254 mShadowVolumeMeshFileName = "";
00255 }
00256
00257
00258 mShadowVolumeSN->setShadowMesh(mesh);
00259 }
00260
00261
00262 void AnimatedMeshComponent::setShadowVolumeSceneNode(const std::string &fileName, bool zfailmethod,
00263 f32 infinity)
00264 {
00265 if(!mShadowVolumeSN)
00266 {
00267
00268 mShadowVolumeSN = mAnimatedMeshSN->addShadowVolumeSceneNode(0, pParent->getID(),
00269 zfailmethod, infinity);
00270
00271 setShadowVolumeMesh(fileName);
00272 }
00273 }
00274
00275
00276 void AnimatedMeshComponent::setShadowVolumeSceneNode(IMesh *mesh, bool zfailmethod, f32 infinity)
00277 {
00278 if(!mShadowVolumeSN)
00279 {
00280 mShadowVolumeSN = mAnimatedMeshSN->addShadowVolumeSceneNode(mesh, pParent->getID(),
00281 zfailmethod, infinity);
00282 }
00283 }
00284
00285
00286 void AnimatedMeshComponent::setTransitionTime(f32 time)
00287 {
00288 mAnimatedMeshSN->setTransitionTime(time);
00289 }
00290
00291
00292 void AnimatedMeshComponent::onMesh(void *p)
00293 {
00294
00295 IAnimatedMesh *mesh = reinterpret_cast<IAnimatedMesh*>(p);
00296
00297
00298 mAnimatedMeshSN->setMesh(mesh);
00299 }
00300
00301
00302 void AnimatedMeshComponent::onShadowVolumeMesh(void *p)
00303 {
00304
00305 IMesh *mesh = reinterpret_cast<IMesh*>(p);
00306
00307
00308 mShadowVolumeSN->setShadowMesh(mesh);
00309 }
00310
00311
00312 bool AnimatedMeshComponent::parseXML(IXMLReader *file, Entity *entity)
00313 {
00314
00315 AnimatedMeshComponent *component = NULL;
00316
00317
00318 if(file->getNodeType() == io::EXN_ELEMENT)
00319 {
00320 if(stringw("AnimatedMeshComponent") == file->getNodeName())
00321 {
00322
00323 component = new AnimatedMeshComponent(entity);
00324 }
00325 }
00326
00327 if(component == NULL)
00328 return false;
00329
00330
00331 while(file->read())
00332 {
00333 switch(file->getNodeType())
00334 {
00335 case io::EXN_ELEMENT:
00336
00337
00338 if(stringw("animationSpeed") == file->getNodeName())
00339 component->setAnimationSpeed(file->getAttributeValueAsFloat(L"value"));
00340
00341
00342 else if(stringw("frameloop") == file->getNodeName())
00343 {
00344 component->setFrameLoop(file->getAttributeValueAsInt(L"start"),
00345 file->getAttributeValueAsInt(L"end"));
00346 }
00347
00348
00349 else if(stringw("loopMode") == file->getNodeName())
00350 {
00351 stringc value = file->getAttributeValue(L"value");
00352 component->setLoopMode( (value == "true") ? true : false );
00353 }
00354
00355
00356 else if(stringw("MD2Animation") == file->getNodeName())
00357 {
00358 stringc value = file->getAttributeValue(L"value");
00359 component->setMD2Animation(value.c_str());
00360 }
00361
00362
00363 else if(stringw("mesh") == file->getNodeName())
00364 {
00365 stringc fileName = file->getAttributeValue(L"fileName");
00366 component->setMesh(fileName.c_str());
00367 }
00368
00369
00370 else if(stringw("shadowVolumeMeshSceneNode") == file->getNodeName())
00371 {
00372 stringc fileName = file->getAttributeValue(L"fileName");
00373 stringc zfailmethod = file->getAttributeValue(L"zfailmethod");
00374
00375 component->setShadowVolumeSceneNode(fileName.c_str(),
00376 (zfailmethod == "true") ? true : false,
00377 file->getAttributeValueAsFloat(L"infinity"));
00378 }
00379
00380
00381 else if(stringw("transitionTime") == file->getNodeName())
00382 component->setTransitionTime(file->getAttributeValueAsFloat(L"value"));
00383
00384
00385 else SceneComponent::parseBaseXML(file, component);
00386
00387 break;
00388
00389 case io::EXN_ELEMENT_END:
00390
00391
00392 if(stringw("AnimatedMeshComponent") == file->getNodeName())
00393 return true;
00394
00395 break;
00396
00397 default:
00398
00399 break;
00400 }
00401
00402 }
00403
00404
00405 return false;
00406 }
00407
00408