Setting TFloat Texture from Numpy Array

Hi

Does anyone have experience setting up a 2D texture that uses TFloat for its component-type? I am doing a Cg shader using texture-fetching to create a displacement map and would like to not have to convert my height-values to shorts…

So my question is, is there a way to make something like the following code snippet work?

self.texture = Texture(‘height_field’)
self.texture.setup2dTexture(Row,Col,Texture.TFloat,Texture.FRed)
p = PTAUchar.emptyArray(0)
p.setData(Z.tostring())
self.texture.setRamImage(CPTAUchar§)

In the example above, htArray is a (Row x Col) sized array of floats. When I try to run it I get an error saying that setRamImage only works with CPTAuchar, which is reasonable, but then is there an alternative to setRamImage for floats? Speed is somewhat of an issue as the source array is quite large (~1024x1024). So I guess writing to a file and then doing a texture.load is one option, but this seems like it will be slow. Any ideas?

I haven’t actually used the TFloat image, but I think the code snippet you post should work. Does it not?

David

No the above code snippet fails for me with the following error:

self.texture.setRamImage(CPTAUchar(p))

AssertionError: compression != CM_off || image.size() == do_get_expected_ram_image_size() at line 830 of panda/src/gobj/texture.cxx

This just means that the buffer you gave it wasn’t the length it expected; that is, it wasn’t x * y * num_components * component_width. Check the parameters on your texture to make sure they are consistent with the buffer you are trying to give it.

David

I was forgetting to explicitly cast my numpy array to a 32-byte float, and so it was defaulting to a 64-byte float array which was twice as long as needed.

Thanks, David!

Murphy