如何在Ogre3d和PhysX上使用16位高度图

时间:2009-06-26 04:51:09

标签: c++ physics ogre3d

我正在使用Ogre3D和PhysX。

当我从8位高度的地图加载地形时,它在Visual Debugger上看起来很正常。

看第一张图片: http://img44.imageshack.us/gal.php?g=44927650.jpg

但是当我将高度图保存为16位图像时,我得到你在第二张图像上看到的内容。

这是代码,它可以正常使用8位PNG:

    mSceneMgr->setWorldGeometry("terrain.cfg" );
    mTSM=static_cast<TerrainSceneManager*>(sceneMgr);

    TerrainOptions mTerrainOptions = mTSM->getOptions();

    //load heihgtmap
    Image mImage;
    mImage.load("isl_h_ph.png", ResourceGroupManager::getSingleton().getWorldResourceGroupName()); //load image

    //write image buffer to pOrigSrc 
    const uchar* pOrigSrc = mImage.getData();
    const uchar* pSrc;
    // image size to mPageSize
    size_t   mPageSize  = mTerrainOptions.pageSize;

    NxActorDesc ActorDesc;

    //set number of segments
    heightFieldDesc = new NxHeightFieldDesc;
    heightFieldDesc->nbColumns      = mPageSize;
    heightFieldDesc->nbRows         = mPageSize;
    heightFieldDesc->verticalExtent   = -1000; 
    heightFieldDesc->convexEdgeThreshold    = 0;

    heightFieldDesc->samples      = new NxU32[mPageSize*mPageSize];   //constructor for every sample?
    heightFieldDesc->sampleStride   = sizeof(NxU32);                    //some sample step = number of samples

    pSrc = pOrigSrc;
    char* currentByte = (char*)heightFieldDesc->samples;     //current sample mb?
    LogManager::getSingletonPtr()->logMessage("+++Heightmap---");
    for (NxU32 row = 0; row < mPageSize; row++)
    {
        for (NxU32 column = 0; column < mPageSize; column++)   //cycle around samples
        {
            pSrc = pOrigSrc + column*mPageSize +row;

            //NxReal s = NxReal(row) / NxReal(mPageSize);
            //NxReal t = NxReal(column) / NxReal(mPageSize);

            NxI16 height = (NxI32)(*pSrc++);
        NxU32 matrixOffset = (row % gMatrixSize) * gMatrixSize + (column % gMatrixSize);

            //LogManager::getSingletonPtr()->logMessage(Ogre::StringConverter::toString(height));
            NxHeightFieldSample* currentSample = (NxHeightFieldSample*)currentByte;
            currentSample->height = height;
        currentSample->materialIndex0 = gMatrix[matrixOffset][1];
        currentSample->materialIndex1 = gMatrix[matrixOffset][2];
        currentSample->tessFlag = gMatrix[matrixOffset][0];
            currentByte += heightFieldDesc->sampleStride;
        }
    }

    heightField = mScene->getPhysicsSDK().createHeightField(*heightFieldDesc);

    NxHeightFieldShapeDesc heightFieldShapeDesc;
    heightFieldShapeDesc.heightField   = heightField;
    heightFieldShapeDesc.shapeFlags      = NX_SF_FEATURE_INDICES | NX_SF_VISUALIZATION;
    heightFieldShapeDesc.group   = 1;
    heightFieldShapeDesc.heightScale   = 18.8f;//1 в Physx = 255 в огре
    heightFieldShapeDesc.rowScale      = mTerrainOptions.scale.x;
    heightFieldShapeDesc.columnScale   = mTerrainOptions.scale.z;
    heightFieldShapeDesc.meshFlags   = NX_MESH_SMOOTH_SPHERE_COLLISIONS;
    heightFieldShapeDesc.materialIndexHighBits = 0;
    heightFieldShapeDesc.holeMaterial = 2;
    ActorDesc.shapes.pushBack(&heightFieldShapeDesc);

我需要更改什么才能让16位或更高的图像加载工作?

P.S。:抱歉英语不好

2 个答案:

答案 0 :(得分:1)

你的pOrigSrc是一个8位的uchar,所以当你这样做时你得不到正确的偏移量:

pSrc = pOrigSrc + column*mPageSize +row;

您可以通过在循环之前首先抓取图像的步幅来解决此问题,如下所示:

int imageStride = mImage.getBPP() / 8;

然后将计算出的偏移乘以步幅,如下所示:

pSrc = pOrigSrc + (column*mPageSize +row)*imageStride;

这应该允许您使用8位和16位高度贴图。 PhysX仅支持16位高度贴图,因此您不能高于此值。

答案 1 :(得分:0)

您的代码中的所有字符现在都需要短路,并且您需要一次遍历文件2个字节,而不是1.因为8位是一个字节(char或uchar的大小),16 bits是两个字节,短或无符号短的大小。