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