00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "OctTreeComponent.h"
00019 #include "../../core/GameManager.h"
00020
00021
00022
00023
00024 OctTreeComponent::OctTreeComponent(Entity *parent, s32 minPolysPerNode)
00025 : MeshComponent(parent, true)
00026 {
00027 init(minPolysPerNode);
00028 }
00029
00030
00031 OctTreeComponent::OctTreeComponent(Entity *parent, const std::string &fileName, s32 minPolysPerNode)
00032 : MeshComponent(parent, true)
00033 {
00034 init(minPolysPerNode);
00035 setMesh(fileName);
00036 }
00037
00038
00039 OctTreeComponent::OctTreeComponent(Entity *parent, IMesh *mesh, s32 minPolysPerNode)
00040 : MeshComponent(parent, true)
00041 {
00042 init(minPolysPerNode);
00043 setMesh(mesh);
00044 }
00045
00046
00047 OctTreeComponent::OctTreeComponent(Entity *parent, IAnimatedMesh *mesh, s32 minPolysPerNode)
00048 : MeshComponent(parent, true)
00049 {
00050 init(minPolysPerNode);
00051 setMesh(mesh);
00052 }
00053
00054
00055 OctTreeComponent::~OctTreeComponent()
00056 {
00057 }
00058
00059
00060 void OctTreeComponent::init(s32 minPolysPerNode)
00061 {
00062
00063 ISceneManager *pSceneMgr = GameManager::Instance()->getSceneManager();
00064
00065
00066 mMeshSN = pSceneMgr->addOctTreeSceneNode((IMesh*)0, 0, pParent->getID(), minPolysPerNode, true);
00067 mSceneNode = mMeshSN;
00068 mSceneNode->setPosition(pParent->getAbsolutePosition());
00069 mSceneNode->setRotation(pParent->getAbsoluteRotation());
00070
00071
00072 mMinPolysPerNode = minPolysPerNode;
00073
00074
00075 mLocalMetaSelector = pSceneMgr->createMetaTriangleSelector();
00076 mTriSelector = mLocalMetaSelector;
00077 mMetaSelector = pSceneMgr->createMetaTriangleSelector();
00078 }
00079
00080
00081 void OctTreeComponent::setMesh(const std::string &fileName)
00082 {
00083
00084 AssetGroup *assets = pParent->getAssetGroup();
00085 IMesh *mesh = 0;
00086
00087
00088 if(assets != NULL)
00089 {
00090
00091 assets->disconnectEventSignal(std::string("meshes/") + mMeshFileName, this);
00092
00093
00094 MeshProcessor *processor = static_cast<MeshProcessor*>(assets->getAssetProcessor("meshes"));
00095 mesh = processor->getMesh(fileName);
00096
00097 if(mesh)
00098 {
00099 assets->connectEventSignal(std::string("meshes/") + fileName, this,
00100 &OctTreeComponent::onMesh);
00101 mMeshFileName = fileName;
00102 }
00103 }
00104
00105 else
00106 mesh = GameManager::Instance()->getSceneManager()->getMesh(fileName.c_str());
00107
00108
00109 mLocalMetaSelector->removeTriangleSelector(mOctTreeSelector);
00110
00111
00112 mMeshSN->setMesh(mesh);
00113
00114
00115 mOctTreeSelector = GameManager::Instance()->getSceneManager()->
00116 createTriangleSelector(mesh, mMeshSN);
00117 mLocalMetaSelector->addTriangleSelector(mOctTreeSelector);
00118 }
00119
00120
00121 void OctTreeComponent::setMesh(IMesh *mesh)
00122 {
00123
00124 AssetGroup *assets = pParent->getAssetGroup();
00125
00126 if(assets != NULL)
00127 {
00128 assets->disconnectEventSignal(std::string("meshes/") + mMeshFileName, this);
00129 mMeshFileName = "";
00130 }
00131
00132
00133 mLocalMetaSelector->removeTriangleSelector(mOctTreeSelector);
00134
00135
00136 mMeshSN->setMesh(mesh);
00137
00138
00139 mOctTreeSelector = GameManager::Instance()->getSceneManager()->
00140 createOctTreeTriangleSelector(mesh, mMeshSN, mMinPolysPerNode);
00141 mLocalMetaSelector->addTriangleSelector(mOctTreeSelector);
00142 }
00143
00144
00145 void OctTreeComponent::onMesh(void *p)
00146 {
00147
00148 IMesh *mesh = reinterpret_cast<IMesh*>(p);
00149
00150
00151 mLocalMetaSelector->removeTriangleSelector(mOctTreeSelector);
00152
00153
00154 mMeshSN->setMesh(mesh);
00155
00156
00157 mOctTreeSelector = GameManager::Instance()->getSceneManager()->
00158 createOctTreeTriangleSelector(mesh, mMeshSN, mMinPolysPerNode);
00159 mLocalMetaSelector->addTriangleSelector(mOctTreeSelector);
00160 }
00161
00162
00163 bool OctTreeComponent::parseXML(IXMLReader *file, Entity *entity)
00164 {
00165
00166 OctTreeComponent *component = NULL;
00167
00168
00169 if(file->getNodeType() == io::EXN_ELEMENT)
00170 {
00171 if(stringw("OctTreeComponent") == file->getNodeName())
00172 {
00173
00174 s32 minPolysPerNode = file->getAttributeValueAsInt(L"minPolysPerNode");
00175
00176
00177 component = new OctTreeComponent(entity, minPolysPerNode);
00178 }
00179 }
00180
00181 if(component == NULL)
00182 return false;
00183
00184
00185 while(file->read())
00186 {
00187 switch(file->getNodeType())
00188 {
00189
00190 if(!MeshComponent::parseBaseXML(file, component))
00191 SceneComponent::parseBaseXML(file, component);
00192
00193 break;
00194
00195 case io::EXN_ELEMENT_END:
00196
00197
00198 if(stringw("OctTreeComponent") == file->getNodeName())
00199 return true;
00200
00201 break;
00202
00203 default:
00204
00205 break;
00206 }
00207
00208 }
00209
00210
00211 return false;
00212 }
00213
00214