I modified the TerrainDemo example to load my own raw values. I am trying to generate raw sine wave values and generate the sine wavy terrain from these values. Here are the modified sections :
An extra case called case eFile: was added to getRawHeightfieldData
Code: Select all
static byte_t *
getRawHeightfieldData
(
eTerrainModel model,
PHY_ScalarType type,
btScalar& minHeight,
btScalar& maxHeight
)
{
// std::cerr << "\nRegenerating terrain\n";
// std::cerr << " model = " << model << "\n";
// std::cerr << " type = " << type << "\n";
long nElements = ((long) s_gridSize) * s_gridSize;
// std::cerr << " nElements = " << nElements << "\n";
int bytesPerElement = getByteSize(type);
// std::cerr << " bytesPerElement = " << bytesPerElement << "\n";
btAssert(bytesPerElement > 0 && "bad bytes per element");
long nBytes = nElements * bytesPerElement;
// std::cerr << " nBytes = " << nBytes << "\n";
byte_t * raw = new byte_t[nBytes];
btAssert(raw && "out of memory");
// reseed randomization every 30 seconds
// srand(time(NULL) / 30);
// populate based on model
switch (model) {
case eRadial:
setRadial(raw, bytesPerElement, type);
break;
case eFractal:
for (int i = 0; i < nBytes; i++)
{
raw[i] = 0;
}
setFractal(raw, bytesPerElement, type, s_gridSize - 1);
break;
case eFile:
for (int i = 0; i < nBytes; i++)
{
raw[i] = 20.f + 20.f*sin(float(i));
}
break;
default:
btAssert(!"bad model type");
}
if (0) {
// inside if(0) so it keeps compiling but isn't
// exercised and doesn't cause warnings
// std::cerr << "final grid:\n";
dumpGrid(raw, bytesPerElement, type, s_gridSize - 1);
}
// find min/max
for (int i = 0; i < s_gridSize; ++i) {
for (int j = 0; j < s_gridSize; ++j) {
float z = getGridHeight(raw, i, j, type);
printf("i=%d, j=%d, z=%f\n" , i, j, z);
// update min/max
if (!i && !j) {
minHeight = z;
maxHeight = z;
} else {
if (z < minHeight) {
minHeight = z;
}
if (z > maxHeight) {
maxHeight = z;
}
}
}
}
if (maxHeight < -minHeight) {
maxHeight = -minHeight;
}
if (minHeight > -maxHeight) {
minHeight = -maxHeight;
}
// std::cerr << " minHeight = " << minHeight << "\n";
// std::cerr << " maxHeight = " << maxHeight << "\n";
return raw;
}
Code: Select all
void TerrainDemo::initialize(void)
{
// std::cerr << "initializing...\n";
// set up basic state
m_upAxis = 1; // start with Y-axis as "up"
m_type = PHY_FLOAT;
m_model = eFile;//eFractal;
m_isDynamic = true;
// set up the physics world
m_collisionConfiguration = new btDefaultCollisionConfiguration();
m_dispatcher = new btCollisionDispatcher(m_collisionConfiguration);
btVector3 worldMin(-1000,-1000,-1000);
btVector3 worldMax(1000,1000,1000);
m_overlappingPairCache = new btAxisSweep3(worldMin,worldMax);
m_constraintSolver = new btSequentialImpulseConstraintSolver();
m_dynamicsWorld = new btDiscreteDynamicsWorld(m_dispatcher,m_overlappingPairCache,m_constraintSolver,m_collisionConfiguration);
// initialize axis- or type-dependent physics from here
this->resetPhysics();
}
Code: Select all
static const int s_gridSize = 8 + 1; // must be (2^N) + 1
Code: Select all
/// called whenever key terrain attribute is changed
void TerrainDemo::resetPhysics(void)
{
// remove old heightfield
clearWorld();
// reset gravity to point in appropriate direction
m_dynamicsWorld->setGravity(getUpVector(m_upAxis, 0.0, -s_gravity));
// get new heightfield of appropriate type
m_rawHeightfieldData =
getRawHeightfieldData(m_model, m_type, m_minHeight, m_maxHeight);
btAssert(m_rawHeightfieldData && "failed to create raw heightfield");
bool flipQuadEdges = false;
btHeightfieldTerrainShape * heightfieldShape =
new btHeightfieldTerrainShape(s_gridSize, s_gridSize,
m_rawHeightfieldData,
s_gridHeightScale,
m_minHeight, m_maxHeight,
m_upAxis, m_type, flipQuadEdges);
btAssert(heightfieldShape && "null heightfield");
// scale the shape
btVector3 localScaling = getUpVector(m_upAxis, s_gridSpacing, 1.0);
heightfieldShape->setLocalScaling(localScaling);
// stash this shape away
m_collisionShapes.push_back(heightfieldShape);
// set origin to middle of heightfield
btTransform tr;
tr.setIdentity();
tr.setOrigin(btVector3(0,-20,0));
// create ground object
float mass = 0.0;
localCreateRigidBody(mass, tr, heightfieldShape);
}
But this gives me only a flat terrain. It seems that something is wrong in the values I set in raw[] as this section in getRawHeightfieldData() always gives z=0 and consequently m_minHeight and m_maxHeight are both 0 in resetPhysics()
Code: Select all
// find min/max
for (int i = 0; i < s_gridSize; ++i) {
for (int j = 0; j < s_gridSize; ++j) {
float z = getGridHeight(raw, i, j, type);
printf("i=%d, j=%d, z=%f\n" , i, j, z);
// update min/max
if (!i && !j) {
minHeight = z;
maxHeight = z;
} else {
if (z < minHeight) {
minHeight = z;
}
if (z > maxHeight) {
maxHeight = z;
}
}
}
}
Thanks