#include "heightfield.hpp" #include #include #include #include #include namespace { template auto makeHeights(const T* heights, float sqrtVerts) -> std::enable_if_t::value, std::vector> { return {}; } template auto makeHeights(const T* heights, float sqrtVerts) -> std::enable_if_t::value, std::vector> { return std::vector(heights, heights + static_cast(sqrtVerts * sqrtVerts)); } template auto getHeights(const T* floatHeights, const std::vector&) -> std::enable_if_t::value, const btScalar*> { return floatHeights; } template auto getHeights(const T*, const std::vector& btScalarHeights) -> std::enable_if_t::value, const btScalar*> { return btScalarHeights.data(); } } namespace MWPhysics { HeightField::HeightField(const float* heights, int x, int y, float triSize, float sqrtVerts, float minH, float maxH, const osg::Object* holdObject) : mHeights(makeHeights(heights, sqrtVerts)) { mShape = new btHeightfieldTerrainShape( sqrtVerts, sqrtVerts, getHeights(heights, mHeights), 1, minH, maxH, 2, PHY_FLOAT, false ); mShape->setUseDiamondSubdivision(true); mShape->setLocalScaling(btVector3(triSize, triSize, 1)); btTransform transform(btQuaternion::getIdentity(), btVector3((x+0.5f) * triSize * (sqrtVerts-1), (y+0.5f) * triSize * (sqrtVerts-1), (maxH+minH)*0.5f)); mCollisionObject = new btCollisionObject; mCollisionObject->setCollisionShape(mShape); mCollisionObject->setWorldTransform(transform); mHoldObject = holdObject; } HeightField::~HeightField() { delete mCollisionObject; delete mShape; } btCollisionObject* HeightField::getCollisionObject() { return mCollisionObject; } const btCollisionObject* HeightField::getCollisionObject() const { return mCollisionObject; } const btHeightfieldTerrainShape* HeightField::getShape() const { return mShape; } }