00001
00008 #include "SO3Scene.h"
00009 #include "../ScolPack/SO3Scol.h"
00010 #include "../SO3Animation/SO3Anim.h"
00011 #include "../SO3Animation/SO3SequenceAnimation.h"
00012 #include "../SO3Material/SO3Compositor.h"
00013 #include "../SO3Material/SO3Material.h"
00014 #include "../SO3Material/SO3Texture.h"
00015 #include "../SO3Material/SO3DynamicCubeMap.h"
00016 #include "../SO3Material/SO3DynamicReflectionMap.h"
00017 #include "../SO3PhysicGraph/SO3Body.h"
00018 #include "../SO3PhysicGraph/SO3MaterialPair.h"
00019 #include "../SO3PhysicGraph/SO3MaterialID.h"
00020 #include "../SO3Renderer/SO3Root.h"
00021 #include "../SO3SceneGraph/SO3Bone.h"
00022 #include "../SO3SceneGraph/SO3EntityFap.h"
00023 #include "../SO3SceneGraph/SO3Camera.h"
00024 #include "../SO3SceneGraph/SO3Light.h"
00025 #include "../SO3SceneGraph/SO3NodeScol.h"
00026 #include "../SO3SceneGraph/SO3ParticleSystem.h"
00027 #include "../SO3SceneGraph/SO3Skeleton.h"
00028 #include "../SO3SceneGraph/SO3Environment.h"
00029
00030
00031 #include <scol.h>
00032
00033 extern WindowHandle hwndScol;
00034 extern int SCENE_PRE_RENDER_EVENT;
00035 extern int SCENE_POST_RENDER_EVENT;
00036 extern int ANIMATION_END_EVENT;
00037
00038 extern int SO3_BODY_LEAVE_WORLD_MSG;
00039 extern int getMaterialOverlapStartedCallback(mmachine m, int id, MCOLL * result);
00040 extern int getMaterialContactCallback(mmachine m, int id, MCOLL * result);
00041 extern int getMaterialOverlapEndedCallback(mmachine m, int id, MCOLL * result);
00042 extern SMaterialPair* getMaterialPairByWorld(SPhysicWorld* world, int ID1, int ID2);
00043
00044
00045 namespace SO3
00046 {
00047
00048 SO3MainFrameListener::SO3MainFrameListener(SScene* scene) : FrameListener()
00049 {
00050 m_scene = scene;
00051 }
00052
00053 SO3MainFrameListener::~SO3MainFrameListener(void)
00054 {
00055
00056 }
00057
00058 bool SO3MainFrameListener::frameStarted(const Ogre::FrameEvent &evt)
00059 {
00060 int time = static_cast<int>(evt.timeSinceLastFrame * 1000);
00061 SendMessage(hwndScol, SCENE_PRE_RENDER_EVENT, (int)m_scene, (LPARAM)time);
00062
00063 SPhysicWorld* physic_world = m_scene->GetPhysicsWorld();
00064
00065
00066 if (physic_world->GetPhysicWorld() != 0)
00067
00068 if (physic_world->GetResetPhysicWorld())
00069 {
00070 physic_world->physicContactOverlapStartMap.clear();
00071 physic_world->physicContactMap.clear();
00072 physic_world->bodyPairMap.clear();
00073
00074 physic_world->GetPhysicWorld()->invalidateCache();
00075 physic_world->SetResetPhysicWorld(false);
00076 }
00077
00078 if (physic_world->GetPhysicWorldEnable())
00079 {
00080 physic_world->GetPhysicWorld()->update(evt.timeSinceLastFrame);
00081
00082 if (physic_world->GetPhysicDebugEnable())
00083 physic_world->GetPhysicDebugger()->showDebugInformation();
00084
00085
00086
00087 const PhysicContactMap lOverlapStart = physic_world->physicContactOverlapStartMap;
00088 PhysicContactMap::const_iterator iOverlapStartMap = lOverlapStart.begin();
00089 while (iOverlapStartMap != lOverlapStart.end())
00090 {
00091 MCOLL* mCollResult = iOverlapStartMap->second;
00092 getMaterialOverlapStartedCallback(mm, (int)mCollResult->matPair, mCollResult);
00093
00094
00095 delete(mCollResult);
00096
00097 iOverlapStartMap++;
00098 }
00099 physic_world->physicContactOverlapStartMap.clear();
00100
00101
00102 const PhysicContactMap lContacts = physic_world->physicContactMap;
00103 PhysicContactMap::const_iterator iContactMap = lContacts.begin();
00104 while (iContactMap != lContacts.end())
00105 {
00106 MCOLL* mCollResult = iContactMap->second;
00107 getMaterialContactCallback(mm, (int)mCollResult->matPair, mCollResult);
00108
00109
00110 delete(mCollResult);
00111
00112 iContactMap++;
00113 }
00114 physic_world->physicContactMap.clear();
00115
00116 const BodyPairMap bodyPairList = physic_world->bodyPairMap;
00117
00118 BodyPairMap::const_iterator iBodyPairMap = bodyPairList.begin();
00119 while (iBodyPairMap != bodyPairList.end())
00120 {
00121
00122 bool bodiesInContact = false;
00123 SBody** bpair = iBodyPairMap->second;
00124 SBody* fbody0 = bpair[0];
00125 SBody* fbody1 = bpair[1];
00126 if ((fbody0 != NULL) && (fbody1 != NULL))
00127 {
00128 OgreNewt::Body* nbody0 = fbody0->getOgreNewtBodyPointer();
00129 OgreNewt::Body* nbody1 = fbody1->getOgreNewtBodyPointer();
00130
00131 if ((nbody0 != NULL) && (nbody1 != NULL))
00132 {
00133 NewtonBody* b0 = nbody0->getNewtonBody();
00134 NewtonBody* b1 = nbody1->getNewtonBody();
00135 NewtonJoint* contactJoint = NewtonBodyGetFirstContactJoint(b0);
00136
00137 while((contactJoint) && (!bodiesInContact))
00138 {
00139
00140 NewtonBody* b0Bis = NewtonJointGetBody0(contactJoint);
00141 NewtonBody* b1Bis = NewtonJointGetBody1(contactJoint);
00142
00143
00144 if(((b0Bis == b0)&&(b1Bis == b1)) || ((b0Bis == b1)&&(b1Bis == b0)))
00145 bodiesInContact = true;
00146 else
00147 contactJoint = NewtonBodyGetNextContactJoint(b0, contactJoint);
00148 }
00149
00150
00151 if(!bodiesInContact)
00152 {
00153
00154 SMaterialPair* matPair = 0;
00155 const SMaterialPairMap materialPairList = physic_world->listOfPhysicsMaterialPair;
00156 SMaterialPairMap::const_iterator iMaterialPairList = materialPairList.begin();
00157 while((iMaterialPairList != materialPairList.end()) && (!matPair))
00158 {
00159 if(((*iMaterialPairList)->getID1() == fbody0->GetMaterialID()->getID() && (*iMaterialPairList)->getID2() == fbody1->GetMaterialID()->getID())
00160 ||((*iMaterialPairList)->getID2() == fbody0->GetMaterialID()->getID() && (*iMaterialPairList)->getID1() == fbody1->GetMaterialID()->getID()))
00161 matPair = *iMaterialPairList;
00162
00163 iMaterialPairList++;
00164 }
00165
00166 if(matPair != 0)
00167 {
00168
00169 MCOLL* mCollResult = new MCOLL();
00170 mCollResult->node0 = fbody0->GetParentNode()->GetName();
00171 mCollResult->node1 = fbody1->GetParentNode()->GetName();
00172 mCollResult->curScene = m_scene;
00173
00174
00175 if (matPair->GetOverlapEndedCallbackActivated())
00176 getMaterialOverlapEndedCallback(mm, (int)matPair, mCollResult);
00177
00178
00179 delete(mCollResult);
00180 }
00181
00182
00183 free(bpair);
00184 physic_world->bodyPairMap.erase(iBodyPairMap->first);
00185 }
00186 }
00187 }
00188
00189 iBodyPairMap++;
00190 }
00191 }
00192 return true;
00193 }
00194
00195 bool SO3MainFrameListener::frameEnded(const Ogre::FrameEvent &evt)
00196 {
00197 int time = static_cast<int>(evt.timeSinceLastFrame * 1000);
00198 SendMessage(hwndScol, SCENE_POST_RENDER_EVENT, (int)m_scene, (LPARAM)time);
00199 return true;
00200 }
00201
00202
00203 SO3ParticleFrameListener::SO3ParticleFrameListener(SScene* scene) : FrameListener()
00204 {
00205 m_scene = scene;
00206 }
00207
00208 SO3ParticleFrameListener::~SO3ParticleFrameListener(void)
00209 {
00210 }
00211
00212 bool SO3ParticleFrameListener::frameStarted(const Ogre::FrameEvent &evt)
00213 {
00214 return true;
00215 }
00216
00217 bool SO3ParticleFrameListener::frameEnded(const Ogre::FrameEvent &evt)
00218 {
00219 return true;
00220 }
00221
00222 SScene::SScene(SRoot* parent, std::string sceneName, int scolChannel) : SData(sceneName), channel(scolChannel)
00223 {
00224 scolRoot = parent;
00225 O3SceneManager = Ogre::Root::getSingleton().createSceneManager(Ogre::ST_GENERIC, sceneName);
00226
00227 animationCounter = 0;
00228 skyBoxMat.setNull();
00229 skyDomeMat.setNull();
00230 skyPlaneMat.setNull();
00231 hasSkyBox = false;
00232 skyBoxDrawFirst = false;
00233 hasSkyDome = false;
00234 skyDomeDrawFirst = false;
00235 hasSkyPlane = false;
00236 skyPlaneDrawFirst = false;
00237 shadowCameraSetup = SO3_DEFAULT_SHADOW_CAMERA;
00238 materialCaster.setNull();
00239 materialReceiver.setNull();
00240
00241 O3MeshManager = Ogre::MeshManager::getSingletonPtr();
00242 O3MaterialManager = Ogre::MaterialManager::getSingletonPtr();
00243 O3TextureManager = Ogre::TextureManager::getSingletonPtr();
00244 O3HighLevelGpuProgramManager = Ogre::HighLevelGpuProgramManager::getSingletonPtr();
00245 O3SkeletonManager = Ogre::SkeletonManager::getSingletonPtr();
00246 O3GpuProgramManager = Ogre::GpuProgramManager::getSingletonPtr();
00247 O3ParticleSystemManager = Ogre::ParticleSystemManager::getSingletonPtr();
00248
00249 mainListener = new SO3MainFrameListener(this);
00250 Ogre::Root::getSingleton().addFrameListener(mainListener);
00251
00252 particleListener = new SO3ParticleFrameListener(this);
00253 Ogre::Root::getSingleton().addFrameListener(particleListener);
00254
00255 physicWorld = new SPhysicWorld(this);
00256
00257
00258 rootNode = new SNode(this, O3SceneManager->getRootSceneNode()->getName(), true);
00259
00260
00261 environment = new SEnvironment(this, sceneName);
00262 }
00263
00264 SScene::SScene() : SData(""), channel(0)
00265 {
00266
00267 }
00268
00269 void SScene::clearOgreScene()
00270 {
00272
00273
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295 SAFE_DELETE(environment);
00296
00297 const SNodeMap nodeListCopy = GetNodeList();
00298 SNodeMap::const_iterator iNodeList = nodeListCopy.begin();
00299 while (iNodeList != nodeListCopy.end())
00300 {
00301 if (iNodeList->second != 0)
00302 DeleteNode(iNodeList->second);
00303
00304 iNodeList++;
00305 }
00306
00307
00308 const BodyPairMap bodyListCopy = physicWorld->bodyPairMap;
00309 BodyPairMap::const_iterator iBodyMap = bodyListCopy.begin();
00310 while (iBodyMap != bodyListCopy.end())
00311 {
00312 free(iBodyMap->second);
00313 physicWorld->bodyPairMap.erase(iBodyMap->first);
00314
00315 iBodyMap++;
00316 }
00317
00318
00319 const SAnimMap seqAnimCopy = animationList;
00320 SAnimMap::const_iterator iSeqAnim = seqAnimCopy.begin();
00321 while (iSeqAnim != seqAnimCopy.end())
00322 {
00323 DeleteAnimation(iSeqAnim->second);
00324 iSeqAnim++;
00325 }
00326
00328
00330 physicWorld->SetPhysicWorldEnable(false);
00331
00333
00335 Ogre::ShadowTextureManager::getSingleton().clear();
00336 O3SceneManager->setShadowTechnique(Ogre::SHADOWTYPE_NONE);
00337
00338
00339 O3SceneManager->destroyAllEntities();
00340 O3SceneManager->destroyAllLights();
00341 O3SceneManager->destroyAllParticleSystems();
00342
00343
00344 O3SceneManager->destroyAllAnimations();
00345 O3SceneManager->destroyAllAnimationStates();
00346 O3SceneManager->destroyAllBillboardChains();
00347 O3SceneManager->destroyAllBillboardSets();
00348 O3ParticleSystemManager->removeAllTemplates(true);
00349 O3SceneManager->destroyAllRibbonTrails();
00350
00351 O3SkeletonManager->unloadAll(false);
00352 O3SkeletonManager->removeAll();
00353
00355
00357 O3SceneManager->clearScene();
00358
00360
00362 O3SceneManager->destroyAllInstancedGeometry();
00363 Ogre::SceneManager::MovableObjectIterator itMO = O3SceneManager->getMovableObjectIterator("ManualObject");
00364 while(itMO.hasMoreElements())
00365 {
00366 Ogre::ManualObject * curObj = static_cast<Ogre::ManualObject*>(itMO.getNext());
00367 if(strcmp(curObj->getName().c_str(),"SkyBox"))
00368 O3SceneManager->destroyManualObject(curObj->getName());
00369 }
00370 this->O3SceneManager->destroyAllStaticGeometry();
00371 if(hasSkyBox)
00372 O3SceneManager->setSkyBox(false, skyBoxMat->getName());
00373 if(this->hasSkyDome)
00374 O3SceneManager->setSkyDome(false, skyDomeMat->getName());
00375 if(this->hasSkyPlane)
00376 O3SceneManager->setSkyPlane(false, skyPlane, skyPlaneMat->getName());
00377
00379
00380
00382 std::vector<Ogre::TexturePtr> listTextureToDelete;
00383 Ogre::ResourceManager::ResourceMapIterator itT = Ogre::TextureManager::getSingleton().getResourceIterator();
00384 while(itT.hasMoreElements())
00385 {
00386 Ogre::TexturePtr tex = static_cast<Ogre::TexturePtr>(itT.getNext());
00387 if((tex->getName() != "SO3DebugFontTexture")
00388 &&(tex->getName() != "BlueHighway-10Texture")
00389 &&(tex->getGroup() != "Internal")
00390 &&(tex->getGroup().substr(0, std::string(SO3_INTERNAL_RESOURCE_GROUP).length()) != SO3_INTERNAL_RESOURCE_GROUP)
00391 &&(tex->getGroup() != SO3_INTERNAL_HYDRAX_RESOURCE_GROUP)
00392 &&(tex->getGroup() != SO3_INTERNAL_SKYX_RESOURCE_GROUP))
00393 {
00394
00395 listTextureToDelete.push_back(tex);
00396 }
00397 }
00398
00399 std::vector<Ogre::TexturePtr>::iterator iListTextureToDelete = listTextureToDelete.begin();
00400 while(iListTextureToDelete != listTextureToDelete.end())
00401 {
00402 O3TextureManager->unload((*iListTextureToDelete)->getName());
00403 O3TextureManager->remove((*iListTextureToDelete)->getName());
00404 iListTextureToDelete++;
00405 }
00406 listTextureToDelete.clear();
00407
00409
00411 O3SceneManager->setFog(Ogre::FOG_NONE, Ogre::ColourValue::White, 0, 0, 0);
00412
00413
00414 environment = new SEnvironment(this, name);
00415 }
00416
00417 SScene::~SScene()
00418 {
00419
00420
00421
00422
00423
00424
00425 if (mainListener != NULL)
00426 {
00427 Ogre::Root::getSingleton().removeFrameListener(mainListener);
00428 SAFE_DELETE(mainListener);
00429 }
00430
00431 if (particleListener != NULL)
00432 {
00433 Ogre::Root::getSingleton().removeFrameListener(particleListener);
00434 SAFE_DELETE(particleListener);
00435 }
00436
00437 SAFE_DELETE(environment);
00438 clearOgreScene();
00439
00440 SAFE_DELETE(physicWorld);
00441 SAFE_DELETE(rootNode);
00442
00443 nodeList.clear();
00444 listOfMaterial.clear();
00445 listOfTexture.clear();
00446
00447 Ogre::Root::getSingleton().destroySceneManager(O3SceneManager);
00448 }
00449
00450 void SScene::AddNode(SNode* existingNode)
00451 {
00452 string nodeName = existingNode->GetName();
00453 SNodeMap::iterator iNodeSearched = nodeList.find(nodeName);
00454 if (iNodeSearched == nodeList.end())
00455 {
00456 nodeList.insert(SNodeMap::value_type(nodeName, existingNode));
00457 }
00458 else
00459 {
00460
00461 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Can not add node named \""+ nodeName +"\", an element with the same name already exist!", "SScene::AddNode");
00462 }
00463 }
00464
00465 void SScene::RemoveNode(SNode* existingNode)
00466 {
00467 RemoveNode(existingNode->GetName());
00468 }
00469
00470 void SScene::RemoveNode(std::string nodeName)
00471 {
00472 SNodeMap::iterator iNodeSearched = nodeList.find(nodeName);
00473 if (iNodeSearched != nodeList.end())
00474 {
00475 nodeList.erase(iNodeSearched);
00476 }
00477 else
00478 {
00479
00480 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can not remove Node named \""+ nodeName +"\", element not found!", "SScene::RemoveNode");
00481 }
00482 }
00483
00484 Ogre::SceneManager* SScene::GetOgreScenePointer()
00485 {
00486 return O3SceneManager;
00487 }
00488
00489 SNode* SScene::GetRootNode()
00490 {
00491 return rootNode;
00492 }
00493
00494 SNode* SScene::GetNode(std::string nodeName)
00495 {
00496 SNodeMap::iterator iNodeSearched = nodeList.find(nodeName);
00497 if (iNodeSearched != nodeList.end())
00498 return iNodeSearched->second;
00499 else
00500 return 0;
00501 }
00502
00503 const SNodeMap& SScene::GetNodeList() const
00504 {
00505 return nodeList;
00506 }
00507
00508 SNode* SScene::CreateNode(std::string newNodeName)
00509 {
00510 SNode* newNode = new SNode(this, newNodeName);
00511 AddNode(newNode);
00512 return newNode;
00513 }
00514
00515 void SScene::DeleteNode(SNode* existingNode)
00516 {
00517 if (existingNode != rootNode)
00518 {
00519 RemoveNode(existingNode);
00520 SAFE_DELETE(existingNode);
00521 }
00522 }
00523
00524 SCamera* SScene::CreateCamera(std::string newCameraName)
00525 {
00526 SCamera* newCamera = new SCamera(this, newCameraName);
00527 AddNode(newCamera);
00528 return newCamera;
00529 }
00530
00531 void SScene::DeleteCamera(SCamera* existingCamera)
00532 {
00533 DeleteNode(existingCamera);
00534 }
00535
00536 SEntity* SScene::CreateEntity(std::string groupName, std::string newEntityName, std::string meshName)
00537 {
00538 SEntity* newEntity = new SEntity(this, groupName, newEntityName, meshName);
00539 AddNode(newEntity);
00540 return newEntity;
00541 }
00542
00543 SEntityFap* SScene::CreateEntityFap(std::string newEntityFapName, std::string meshName, std::string pathStdFDP, std::string pathStdIndices)
00544 {
00545
00546 SEntityFap* newEntityFap = new SEntityFap(this, newEntityFapName, meshName, pathStdFDP, pathStdIndices);
00547 AddNode(newEntityFap);
00548 return newEntityFap;
00549 }
00550
00551 SEntity* SScene::CreateEntity(std::string newEntityName, SEntity::EntityPrefabType prefabType)
00552 {
00553 SEntity* newEntity = new SEntity(this, newEntityName, prefabType);
00554 AddNode(newEntity);
00555 return newEntity;
00556 }
00557
00558 SEntity* SScene::CreatePlane(std::string groupName, std::string newEntityName, SPointFloat sizev, SPointInt seg, SPointFloat uv)
00559 {
00560 SEntity* newEntity = new SEntity(this, groupName, newEntityName, sizev, seg, uv);
00561 AddNode(newEntity);
00562 return newEntity;
00563 }
00564
00565 void SScene::DeleteEntity(SEntity* existingEntity)
00566 {
00567 DeleteNode(existingEntity);
00568 }
00569
00570 void SScene::ReloadEntities()
00571 {
00572 SNodeMap::iterator iNodeit = nodeList.begin();
00573 while (iNodeit != nodeList.end())
00574 {
00575 SNode* tnode = iNodeit->second;
00576 if (tnode->GetNodeType() == SNode::ENTITY_TYPE_ID)
00577 static_cast<SEntity*> (tnode)->Reload();
00578
00579 iNodeit++;
00580 }
00581 }
00582
00583 SSkeleton* SScene::CreateSkeleton(std::string newSkeletonName, SEntity* entity)
00584 {
00585 SSkeleton* newSkeleton = new SSkeleton(this, newSkeletonName, entity);
00586 AddNode(newSkeleton);
00587
00588
00589 newSkeleton->Initialise();
00590 return newSkeleton;
00591 }
00592
00593 void SScene::DeleteSkeleton(SSkeleton* existingSkeleton)
00594 {
00595 DeleteNode(existingSkeleton);
00596 }
00597
00598 SBone* SScene::CreateBone(std::string newBoneName, SSkeleton* skeleton, unsigned short id)
00599 {
00600 SBone* newBone = new SBone(this, newBoneName, skeleton, id);
00601 AddNode(newBone);
00602 return newBone;
00603 }
00604
00605 void SScene::DeleteBone(SBone* existingBone)
00606 {
00607 DeleteNode(existingBone);
00608 }
00609
00610 SParticleSystem* SScene::CreateParticleSystem(std::string newParticleSystemName, std::string particleSystemTemplate)
00611 {
00612 SParticleSystem* newParticleSystem = new SParticleSystem(this, newParticleSystemName, particleSystemTemplate);
00613 AddNode(newParticleSystem);
00614 return newParticleSystem;
00615 }
00616
00617 void SScene::DeleteParticleSystem(SParticleSystem* existingParticleSystem)
00618 {
00619 DeleteNode(existingParticleSystem);
00620 }
00621
00622 SLight* SScene::CreateLight(std::string newLightName)
00623 {
00624 SLight* newLight = new SLight(this, newLightName);
00625 AddNode(newLight);
00626 return newLight;
00627 }
00628
00629 void SScene::DeleteLight(SLight* existingLight)
00630 {
00631 DeleteNode(existingLight);
00632 }
00633
00634 SDynamicCubeMap* SScene::CreateDynamicCubeMap(std::string newDynamicCubeMapName)
00635 {
00636 SDynamicCubeMap* newDynamicCubeMap = new SDynamicCubeMap(this, newDynamicCubeMapName);
00637 AddNode(newDynamicCubeMap);
00638 return newDynamicCubeMap;
00639 }
00640
00641 void SScene::DeleteDynamicCubeMap(SDynamicCubeMap* existingDynamicCubeMap)
00642 {
00643 DeleteNode(existingDynamicCubeMap);
00644 }
00645
00646 SDynamicReflectionMap* SScene::CreateDynamicReflectionMap(SViewPort* viewport, std::string newDynamicReflectionMapName)
00647 {
00648 SDynamicReflectionMap* newDynamicReflectionMap = new SDynamicReflectionMap(this, viewport, newDynamicReflectionMapName);
00649 AddNode(newDynamicReflectionMap);
00650 return newDynamicReflectionMap;
00651 }
00652
00653 void SScene::DeleteDynamicReflectionMap(SDynamicReflectionMap* existingDynamicReflectionMap)
00654 {
00655 DeleteNode(existingDynamicReflectionMap);
00656 }
00657
00658 SMaterial* SScene::CreateMaterial(std::string groupname, std::string matname)
00659 {
00660
00661 if (GetMaterial (groupname, matname))
00662 return 0;
00663
00664 SMaterialMap* materialMap;
00665 SGroupMaterialMap::iterator iGroupSearched = listOfMaterial.find(groupname);
00666 if(iGroupSearched != listOfMaterial.end())
00667 {
00668 materialMap = iGroupSearched->second;
00669 }
00670 else
00671 {
00672 materialMap = new SMaterialMap();
00673 listOfMaterial.insert(SGroupMaterialMap::value_type(groupname, materialMap));
00674 }
00675
00676 SMaterial* material = new SMaterial(this, groupname, matname);
00677 materialMap->insert(SMaterialMap::value_type(matname, material));
00678 return material;
00679 }
00680
00681 void SScene::DeleteMaterial(SMaterial* material)
00682 {
00683
00684 SGroupMaterialMap::iterator iGroupSearched = listOfMaterial.find(material->GetGroupName());
00685 if(iGroupSearched != listOfMaterial.end())
00686 {
00687 SMaterialMap* materialMap = static_cast<SMaterialMap*> (iGroupSearched->second);
00688 SMaterialMap::iterator iMaterialSearched = materialMap->find(material->GetName());
00689 if(iMaterialSearched != materialMap->end())
00690 materialMap->erase(iMaterialSearched);
00691
00692 if(materialMap->empty())
00693 {
00694 listOfMaterial.erase(iGroupSearched);
00695 delete (materialMap);
00696 }
00697 }
00698 delete(material);
00699 }
00700
00701 SMaterial* SScene::GetMaterial(std::string groupName, std::string materialName)
00702 {
00703 SGroupMaterialMap::iterator iGroupSearched = listOfMaterial.find(groupName);
00704 if(iGroupSearched != listOfMaterial.end())
00705 {
00706 SMaterialMap* materialMap = iGroupSearched->second;
00707 SMaterialMap::iterator iMaterialSearched = materialMap->find(materialName);
00708 if(iMaterialSearched != materialMap->end())
00709 {
00710 return iMaterialSearched->second;
00711 }
00712 return 0;
00713 }
00714 return 0;
00715 }
00716
00717 STexture* SScene::CreateTexture(std::string groupname, std::string texname, std::string path, int w, int h)
00718 {
00719
00720 if (GetTexture (groupname, texname))
00721 return 0;
00722
00723 STextureMap* textureMap;
00724
00725 SGroupTextureMap::iterator iGroupSearched = listOfTexture.find(groupname);
00726 if(iGroupSearched != listOfTexture.end())
00727 {
00728 textureMap = iGroupSearched->second;
00729 }
00730 else
00731 {
00732 textureMap = new STextureMap();
00733 listOfTexture.insert(SGroupTextureMap::value_type(groupname, textureMap));
00734 }
00735
00736 STexture* texture = new STexture(this, groupname, texname, path, w, h);
00737 textureMap->insert(STextureMap::value_type(texname, texture));
00738 return texture;
00739 }
00740
00741 void SScene::DeleteTexture(STexture* texture)
00742 {
00743 SGroupTextureMap::iterator iGroupSearched = listOfTexture.find(texture->GetGroupName());
00744 if(iGroupSearched != listOfTexture.end())
00745 {
00746 STextureMap* textureMap = static_cast<STextureMap*> (iGroupSearched->second);
00747 STextureMap::iterator iTextureSearched = textureMap->find(texture->GetName());
00748 if(iTextureSearched != textureMap->end())
00749 textureMap->erase(iTextureSearched);
00750
00751 if(textureMap->empty())
00752 {
00753 listOfTexture.erase(iGroupSearched);
00754 delete (textureMap);
00755 }
00756 }
00757
00758 if (!texture->getOgreTexturePointer().isNull())
00759 {
00760 O3TextureManager->unload(texture->getOgreTexturePointer()->getHandle());
00761 O3TextureManager->remove(texture->getOgreTexturePointer()->getHandle());
00762 }
00763 delete(texture);
00764 }
00765
00766 STexture* SScene::GetTexture(std::string groupName, std::string texName)
00767 {
00768 SGroupTextureMap::iterator iGroupSearched = listOfTexture.find(groupName);
00769 if(iGroupSearched != listOfTexture.end())
00770 {
00771 STextureMap* textureMap = static_cast<STextureMap*> (iGroupSearched->second);
00772 STextureMap::iterator iTextureSearched = textureMap->find(texName);
00773 if(iTextureSearched != textureMap->end())
00774 {
00775 return iTextureSearched->second;
00776 }
00777 return 0;
00778 }
00779 return 0;
00780 }
00781
00782 SRoot* SScene::GetParent()
00783 {
00784 return scolRoot;
00785 }
00786
00787 SPhysicWorld* SScene::GetPhysicsWorld()
00788 {
00789 return physicWorld;
00790 }
00791
00792 SSequenceAnimation* SScene::CreateAnimationSequence(std::string animationName)
00793 {
00794 SSequenceAnimation* animation = new SSequenceAnimation(this, animationName, animationCounter++);
00795 AddAnimation(animation);
00796 return animation;
00797 }
00798
00799 void SScene::DeleteAnimation(SAnim* existingAnimation)
00800 {
00801
00802 assert(existingAnimation != 0);
00803 if(GetAnimation(existingAnimation->GetName()) != existingAnimation)
00804 OGRE_EXCEPT(Ogre::Exception::ERR_INVALIDPARAMS, "Cannot destroy an animation that is not handled by this scene", "SScene::DeleteAnimation");
00805
00806 RemoveAnimation(existingAnimation);
00807 SAFE_DELETE(existingAnimation);
00808 }
00809
00810 unsigned short SScene::GetNumAnimations()
00811 {
00812 return animationList.size();
00813 }
00814
00815 SAnim* SScene::GetAnimation(std::string animationName)
00816 {
00817 SAnimMap::iterator iAnimationSearched = animationList.find(animationName);
00818 if (iAnimationSearched != animationList.end())
00819 return iAnimationSearched->second;
00820 else
00821 return 0;
00822 }
00823
00824 SAnimMap SScene::GetAnimations() const
00825 {
00826 return animationList;
00827 }
00828
00829 void SScene::AddAnimation(SAnim* existingAnimation)
00830 {
00831 string animationName = existingAnimation->GetName();
00832 SAnimMap::iterator iAnimationSearched = animationList.find(animationName);
00833 if (iAnimationSearched == animationList.end())
00834 {
00835 animationList.insert(SAnimMap::value_type(animationName, existingAnimation));
00836 }
00837 else
00838 {
00839
00840 OGRE_EXCEPT(Ogre::Exception::ERR_DUPLICATE_ITEM, "Can not add animation named \""+ animationName +"\", an element with the same name already exist!", "SScene::AddAnimation");
00841 }
00842 }
00843
00844 void SScene::RemoveAnimation(SAnim* existingAnimation)
00845 {
00846 RemoveAnimation(existingAnimation->GetName());
00847 }
00848
00849 void SScene::RemoveAnimation(std::string animationName)
00850 {
00851 SAnimMap::iterator iAnimationSearched = animationList.find(animationName);
00852 if (iAnimationSearched != animationList.end())
00853 {
00854 SAnim* findedAnimation = iAnimationSearched->second;
00855 animationList.erase(iAnimationSearched);
00856 }
00857 else
00858 {
00859
00860 OGRE_EXCEPT(Ogre::Exception::ERR_ITEM_NOT_FOUND, "Can not remove Animation named \""+ animationName +"\", element not found!", "SScene::RemoveAnimation");
00861 }
00862 }
00863
00864 SEnvironment* SScene::GetEnvironment() const
00865 {
00866 return environment;
00867 }
00868
00869 }