Code: Select all
bool CTiledHeightmap::LoadTerrain(irr::video::IVideoDriver* &driver,scene::ISceneManager* &smgr,float track_scale){
m_fTrackScale = track_scale;
video::ITexture* detail = driver->getTexture("terrain/detail.bmp");
//Open up the big heighmap and big texture
video::IImage* heightMap = smgr->getVideoDriver()->createImageFromFile("terrain/island/terrain/heightmap.bmp");
video::IImage* bigTex = smgr->getVideoDriver()->createImageFromFile("terrain/island/terrain/tex.bmp");
s32 data[129*129];
//Split it up into 8x8 tiles
for(int tile=0;tile<64;tile++){
//Sub will be a 129x129 tile of the heightmap
video::IImage* sub = driver->createImageFromData(video::ECF_A8R8G8B8,core::dimension2di(129,129),&data);
//Create a 256x256 texture and copy the appropriate portion of the big texture to it.
video::ITexture *txt = driver->addTexture(core::dimension2d<s32>(256,256), "texture", video::ECF_A8R8G8B8);
video::SColor *p = (video::SColor*)txt->lock();
for(u32 j=0; j<txt->getSize().Height; j++)
for(u32 i=0; i<txt->getSize().Width; i++)
p[j * txt->getSize().Width + i] = bigTex->getPixel(256*(tile%8) + i,256*(tile/8)+j);
txt->unlock();
txt->regenerateMipMapLevels();
//Now copy the correct portion of the large heightmap to the tile
sub->lock();
for(int i=0;i < 129;i++)
for(int j=0; j < 129 ; j++)
sub->setPixel(i,j,heightMap->getPixel(i + (128 * (tile %8)),j + (128 * (tile/8))));
sub->unlock();
//I split the engine a little bit so you can make a terrainscenenode from an image
node[tile] = smgr->addTerrainSceneNode(sub,0,-1,core::vector3df(0.0,0.0,0.0),core::vector3df(0.0,0.0,0.0),core::vector3df(1.0,1.0,1.0),video::SColor(255,255,255,255),5,scene::ETPS_17,5,false);
//Add the texture
node[tile]->setMaterialFlag(video::EMF_LIGHTING, false);
node[tile]->setMaterialTexture(0,txt);
//node[tile]->setMaterialTexture(1,detail);
node[tile]->setMaterialType(video::EMT_DETAIL_MAP);
node[tile]->setScale(core::vector3df(track_scale,track_scale,track_scale));
node[tile]->setPosition(core::vector3df(128.0f * (tile%8)*track_scale,0.0f,128.0f * (tile/8)*track_scale));
node[tile]->scaleTexture(1.0,64.0);
}
//This function takes the adjacent tiles and stitches the edges together.
//Theoretically this should be unnecessary but the smoothing algorithm can cause slight differences at the edges.
for(int i=0; i<7;i++)
for(int j=0; j<7; j++)
{
StitchHeightmap(node[i*8 + j],node[i*8 + j + 1],0);
StitchHeightmap(node[i*8 + j],node[(i+1)*8 + j],1);
}
return true;
}