Code: Select all
IrrlichtDevice* device = m_gameInstance->getIrrlicht()->getDevice();
IImage* image = device->getVideoDriver()->createImageFromFile(heightmap);
if(!this->loadHeightMap(this->FileSystem->createAndOpenFile(heightmap),SColor(255,255,255,255),20))
std::cout<<"failed to load height map"<<endl;
this->setMaterialTexture(0,texture);
vector3df extent = getBoundingBox().getExtent();
const core::aabbox3df bb = getBoundingBox();
f32 minHeight = 10000000.f, maxHeight = -1000000.f;
int gridSize = image->getDimension().Height;
float * heightVal = new f32[gridSize*gridSize];
const f32 stepWidthX = extent.X / (f32)gridSize;
const f32 stepWidthZ = extent.Z / (f32)gridSize;
u32 runVal = 0;
for (f32 z = bb.MinEdge.Z; z < bb.MaxEdge.Z; z+=stepWidthZ)
{
for (f32 x = bb.MinEdge.X; x < bb.MaxEdge.X; x+=stepWidthX)
{
const f32 curVal = this->getHeight(x, z);
heightVal[runVal++] = curVal;
if (curVal > maxHeight)
maxHeight = curVal;
if (curVal < minHeight)
minHeight = curVal;
}
}
shape = new btHeightfieldTerrainShape(gridSize, gridSize, heightVal, 1.f, minHeight, maxHeight, 1, PHY_FLOAT, true);
shape->setLocalScaling(btVector3(scale.X,1.f,scale.Z));
vector3df irrTrans = getTerrainCenter();
irrTrans.Y *=scale.Y;
btVector3 trans(irrTrans.X, irrTrans.Y, irrTrans.Z);
btQuaternion quat = GamePhysics::eulerToQuat(rotation);
shape->setMargin(.01);
btTransform bulletTransform(quat,trans);
shape->setMargin(.01);
btDefaultMotionState *MotionState = new btDefaultMotionState(bulletTransform);
btVector3 LocalInertia;
shape->calculateLocalInertia(0, LocalInertia);
btTerr = new btRigidBody(0, MotionState, shape, LocalInertia);
btTerr->setUserPointer((void*)fizzObj);
fizzObj = new CPhysicsObject(this, btTerr, ginst);
m_gameInstance->getPhysics()->getWorld()->addRigidBody(btTerr);
image->drop();