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