terrain.cpp

00001 #include "debug.h"
00002 #include "terrain.h"
00003 
00004 using namespace irr;
00005 using namespace core;
00006 using namespace scene;
00007 using namespace video;
00008 
00009 extern Debug* dbg;
00010 
00011 Terrain::Terrain(IrrlichtDevice* d,
00012                  stringc fileName,
00013                  stringc textureFileName_1,
00014                  stringc textureFileName_2)
00015 {
00016     //ctor
00017     dbg->log("create Terrain");
00018     device = d;
00019     terrain = NULL;
00020     collisionTriangles = NULL;
00021 
00022     // load terrain according to fileName extension
00023     if (stringHasExtension(fileName, stringc("bmp")))
00024     {
00025         loadFromHeightmap(fileName, textureFileName_1, textureFileName_2);
00026         fileType = FILE_TYPE_BMP;
00027     }
00028     else if (stringHasExtension(fileName, stringc("3ds")))
00029     {
00030         loadFrom3ds(fileName, textureFileName_1, textureFileName_2);
00031         fileType = FILE_TYPE_3DS;
00032     }
00033     else
00034     {
00035         dbg->log("Terrain: cannot load file. Filetype unknown.");
00036     }
00037 
00038     loadSkyBox();
00039 }
00040 
00041 Terrain::~Terrain()
00042 {
00043     //dtor
00044     if (terrain)
00045     {
00046         terrain->remove();
00047         terrain = NULL;
00048     }
00049     if(collisionTriangles)
00050     {
00051         collisionTriangles->drop();
00052         collisionTriangles = NULL;
00053     }
00054     dbg->log("Terrain deleted");
00055 }
00056 
00057 vector3df Terrain::getCenter()
00058 {
00059     if (terrain)
00060     {
00061         return terrain->getTerrainCenter();
00062     }
00063     return vector3df(-1,-1,-1);
00064 }
00065 
00066 ITriangleSelector* Terrain::getCollisionTriangles()
00067 {
00068     if (! collisionTriangles)
00069     {
00070         if (terrain)
00071         {
00072             s32 LOD = 0;
00073             collisionTriangles = device->getSceneManager()->createTerrainTriangleSelector(terrain, LOD);
00074             terrain->setTriangleSelector(collisionTriangles);
00075         }
00076     }
00077     return collisionTriangles;
00078 }
00079 
00080 void Terrain::loadFromHeightmap(stringc heightMapFileName,
00081                    stringc textureFileName_1,
00082                    stringc textureFileName_2)
00083 {
00084     ISceneManager* s = device->getSceneManager();
00085 
00086     ISceneNode* parent = s->getRootSceneNode();
00087     s32 id = 0;
00088     vector3df pos(0, 0, 0);
00089     vector3df rot(0, 0, 0);
00090     vector3df scale(40, 4.4f, 40);
00091 
00092     terrain = s->addTerrainSceneNode(heightMapFileName.c_str(), parent, id, pos, rot, scale);
00093     if (! terrain)
00094     {
00095         dbg->log("Terrain::load() cannot load", heightMapFileName);
00096         return;
00097     }
00098 
00099     device->getVideoDriver()->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, true);
00100     ITexture* texture_1 = device->getVideoDriver()->getTexture(textureFileName_1.c_str());
00101     if (! texture_1)
00102     {
00103         dbg->log("Terrain::load() cannot load", textureFileName_1);
00104         return;
00105     }
00106     ITexture* texture_2 = device->getVideoDriver()->getTexture(textureFileName_2.c_str());
00107     if (! texture_2)
00108     {
00109         dbg->log("Terrain::load() cannot load", textureFileName_2);
00110         return;
00111     }
00112     device->getVideoDriver()->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, false);
00113     terrain->setMaterialTexture(0, texture_1);
00114     terrain->setMaterialTexture(1, texture_2);
00115     terrain->scaleTexture(1, 20);
00116     //terrain->setMaterialFlag(EMF_WIREFRAME, true);
00117     terrain->setMaterialType(EMT_DETAIL_MAP);
00118     terrain->setMaterialFlag(EMF_LIGHTING, false);
00119 }
00120 
00121 void Terrain::loadFrom3ds(stringc FileName3ds,
00122                           stringc textureFileName_1,
00123                           stringc textureFileName_2)
00124 {
00125     ISceneManager* s = device->getSceneManager();
00126 
00127     // load mesh
00128     IMesh* mesh = s->getMesh(FileName3ds.c_str());
00129     if (! mesh)
00130     {
00131         dbg->log("Terrain::load() cannot load", FileName3ds);
00132         return;
00133     }
00134 
00135     // create terrain from mesh
00136     ISceneNode* parent = s->getRootSceneNode();
00137     s32 id = 0;
00138     s32 minimalPolysPerNode = 128;
00139     terrain = s->addOctTreeSceneNode(mesh, parent, id, minimalPolysPerNode);
00140     if (! terrain)
00141     {
00142         dbg->log("Terrain::load() cannot create OctTree", FileName3ds);
00143         return;
00144     }
00145 
00146     // setup terrain
00147     vector3df pos(0, 0, 0);
00148     vector3df rot(0, 0, 0);
00149     vector3df scale(40, 4.4f, 40);
00150 
00151     // load textures
00152     device->getVideoDriver()->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, true);
00153     ITexture* texture_1 = device->getVideoDriver()->getTexture(textureFileName_1.c_str());
00154     if (! texture_1)
00155     {
00156         dbg->log("Terrain::load() cannot load", textureFileName_1);
00157         return;
00158     }
00159     ITexture* texture_2 = device->getVideoDriver()->getTexture(textureFileName_2.c_str());
00160     if (! texture_2)
00161     {
00162         dbg->log("Terrain::load() cannot load", textureFileName_2);
00163         return;
00164     }
00165 
00166     // set textures
00167     device->getVideoDriver()->setTextureCreationFlag(ETCF_ALWAYS_32_BIT, false);
00168     terrain->setMaterialTexture(0, texture_1);
00169     terrain->setMaterialTexture(1, texture_2);
00170     terrain->scaleTexture(1, 20);
00171     //terrain->setMaterialFlag(EMF_WIREFRAME, true);
00172     terrain->setMaterialType(EMT_DETAIL_MAP);
00173     terrain->setMaterialFlag(EMF_LIGHTING, false);
00174 }
00175 
00176 bool Terrain::stringHasExtension(stringc s, stringc extension)
00177 {
00178     bool result = false;
00179     s32 s_size = s.size();
00180     stringc ext(extension.c_str());
00181     stringc tmp(s.subString(s_size - 3, s_size).c_str());
00182     result = tmp.equals_ignore_case(ext);
00183 
00184     return result;
00185 }
00186 
00187 void Terrain::loadSkyBox()
00188 {
00189         device->getVideoDriver()->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, false);
00190         device->getSceneManager()->addSkyBoxSceneNode(
00191                 device->getVideoDriver()->getTexture("media/terrain/irrlicht2_up.jpg"),
00192                 device->getVideoDriver()->getTexture("media/terrain/irrlicht2_dn.jpg"),
00193                 device->getVideoDriver()->getTexture("media/terrain/irrlicht2_lf.jpg"),
00194                 device->getVideoDriver()->getTexture("media/terrain/irrlicht2_rt.jpg"),
00195                 device->getVideoDriver()->getTexture("media/terrain/irrlicht2_ft.jpg"),
00196                 device->getVideoDriver()->getTexture("media/terrain/irrlicht2_bk.jpg"));
00197         device->getVideoDriver()->setTextureCreationFlag(ETCF_CREATE_MIP_MAPS, true);
00198 }
00199 

Generated on Sun Dec 2 17:09:58 2007 for Swarm by  doxygen 1.4.6-NO