BulletHeightfieldShape problems with latest panda builds

It seems that latest modifications made in panda from 2015-03-15 (commit 2212052) on, give some issues to BulletHeightfieldShape.
This code show them:

from pandac.PandaModules import loadPrcFileData, GeoMipTerrain, PNMImage, \
                TextureStage, Vec3, NodePath, BitMask32, Point3
from panda3d.bullet import BulletSphereShape, BulletRigidBodyNode, \
                BulletWorld, ZUp, BulletDebugNode, BulletHeightfieldShape
from direct.showbase.ShowBase import ShowBase
from direct.actor.Actor import Actor

loadPrcFileData("", "show-frame-rate-meter #t")
loadPrcFileData("", "win-size 1024 768")

if __name__ == '__main__':
    app = ShowBase()
    app.cam.setPos(0.0, -1000, 200.0)
    app.cam.lookAt(0.0, -8.0, 0.0)
    
    #scaling parameters
    heightScale = 100.0
    widthScale = 3.0

    #update terrain
    def update_terrain(task):
        global globalClock, bulletWorld
        #get time step
        timeStep = globalClock.getDt()
        #simulate
        bulletWorld.doPhysics(timeStep, 1)
        return task.cont
    
    app.taskMgr.add(update_terrain, "update terrain")
    
    #GeoMipTerrain
    heightField = PNMImage("heightfield.png")
    terrain = GeoMipTerrain("terrain")
    terrain.setHeightfield(heightField)
    #terrain properties
    terrain.setBlockSize(64)
    terrain.setBruteforce(True)
    #GeoMipTerrain texturing
    textureImage = app.loader.loadTexture("terrain.png")
    terrain.getRoot().setTexScale(TextureStage.getDefault(), 1.0, 1.0)
    terrain.getRoot().setTexture(TextureStage.getDefault(), textureImage, 1)
    #Generate the terrain
    terrain.generate();
    #adjust position
    terrain.getRoot().setPosHpr((-heightField.getXSize() / 2.0,
                                 -heightField.getYSize() / 2.0, 
                                 -1 / 2.0),
                                Vec3.zero())

    #Physics
    bulletWorld = BulletWorld()
    bulletWorld.setGravity(0.0, 0.0, -9.81)
    #Debug
    debugNode = BulletDebugNode("Debug")
    debugNode.showWireframe(True)
    debugNode.showConstraints(True)
    debugNode.showBoundingBoxes(False)
    debugNode.showNormals(False)
    debugNP = app.render.attachNewNode(debugNode)
    debugNP.hide()
    bulletWorld.setDebugNode(debugNode)
    def toggleDebug():
        global debugNP
        if debugNP.isHidden():
            debugNP.show()
        else:
            debugNP.hide()
    
    app.accept("d", toggleDebug)
    
    #BulletHeightfieldShape
    #Create RigidBodyNode
    bulletNode = BulletRigidBodyNode("terrainBodyNode")
    #create and add a Collision Shape
    collisionShape = BulletHeightfieldShape(PNMImage("heightfield.png"), 1.0, ZUp)
    bulletNode.addShape(collisionShape)
    #set physical parameters
    bulletNode.setFriction(0.8)
    bulletNode.setRestitution(0.1)
    bulletNode.setMass(0.0);
    bulletNode.setKinematic(False)
    bulletNode.setStatic(True)
    bulletNode.setDeactivationEnabled(True)
    bulletNode.setActive(False)
    #attach to Bullet World
    bulletWorld.attach(bulletNode)
    #create a node path for the rigid body
    bulletHeightfieldNP = NodePath(bulletNode)
    #set collide mask
    bulletHeightfieldNP.setCollideMask(BitMask32.allOn())

    #reparent GeoMipTerrain::root to bulletHeightfieldNP
    terrain.getRoot().reparentTo(bulletHeightfieldNP)
    #scaling is applied to bulletHeightfield
    bulletHeightfieldNP.setScale(
            Vec3(widthScale, widthScale, heightScale))

    #reparent bulletHeightfieldNP to render
    bulletHeightfieldNP.reparentTo(app.render)
    
    #Load the Actor model
    actor = Actor("models/panda")
    actor.setScale(4)
    minP = Point3()
    maxP = Point3()
    actor.calcTightBounds(minP, maxP)
    delta = maxP - minP
    actorDims = Vec3(abs(delta.getX()), abs(delta.getY()), 
                     abs(delta.getZ()))
    actorRadius = max(max(actorDims.getX(), actorDims.getY()),
                      actorDims.getZ()) / 2.0
    #
    actorBody = BulletRigidBodyNode("actorBody")
    actorBody.addShape(BulletSphereShape(actorRadius))
    actorBody.setMass(10.0)
    actorBody.setKinematic(False)
    actorBody.setStatic(False)
    actorBody.setDeactivationEnabled(True)
    actorBody.setActive(True)
    bulletWorld.attach(actorBody)
    #attach to scene
    actorBodyNP = NodePath(actorBody)
    actorBodyNP.setCollideMask(BitMask32.allOn())
    actorBodyNP.setPos(0, 0, 250.0)
    actor.reparentTo(actorBodyNP)
    actor.setZ(-actorRadius / 2.0)
    actorBodyNP.reparentTo(app.render)
    #
    app.run()

It works fine with panda previous to that commit. After that, scaling has been modified (heightScale in the code should be reduced from 100.0 to about 1.0 or less to give quite the same terrain appearance), but the main problem is that bullet body with BulletHeightfieldShape doesn’t collide anymore.
I suspect these issues are related to PNMImage and other code modified in that commit and after.
I attached the files used by above code.




Whoops! Thanks for reporting. Turned out there was a scaling issue when loading 16-bit PNG files - they were still being divided by 255 instead of 65535.

Fixed now, in ba6529811d14105f5f20d23d6fb42680268692ef - development builds will soon be available here.

I do want to point out that your heightfield PNG is configured to sRGB, with a gamma value of 0.454545, which will give unexpected results now that PNMImage does proper gamma correction. Is this an export from a terrain editor tool? If so, I highly recommend that you file a bug report with them such that it produces PNG files with a gamma of 1.0, to preserve linearity.