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