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