Raw bitmap as texture

For a mesh viewer I’m writing, I’ll need to read raw 4-bits-per-pixel bitmaps and use them as textures. I’ve been looking through the docs, but I don’t know what I need to do to load a texture like that. Do I need to make a custom file format for the loader object somehow?

I’ll be applying these textures to procedurally-generated geometry, if that makes any difference.

I could convert the texture images beforehand with some other program, but the whole point of the program I’m writing (a mesh viewer for a PSX game) is that it should be able to read and write the textures and geometry in its native format.

I’d appreciate any pointers!

Do you really mean 4 bits per pixel, or do you mean 4 bytes per pixel?

If you mean 4 bytes per pixel, and if the bytes are arranged in the order BGRA, then you can load that data directly onto the texture with tex.setRamImage(). It’s also necessary to set up the other texture properties (number of components, component width, format, etc.) to match exactly.

The easier way (which involves less mucking around in low-level calls) is to create a PNMImage and set the pixels one at a time, then loading that PNMImage into the texture with tex.load(). This is slower, of course, but that may or may not be an issue for you, depending on your application design.


Yep, bits. The texture is stored as a 4-bits-per-pixel bitmap, and then each polygon can apply one of 16 different 16-color palettes to the texture while it’s being rendered.

Since it’s 4 bits (not 4 bytes) and the first way sounds harder, I guess I’ll look into the second way. :slight_smile:

I’m getting ahead of myself here, but will I be able to make changes to the texture easily? Even just being able to write the texture as a PNG for editing and then re-import the PNG, apply it to the mesh (to preview it), and optionally save it back to its native format would be nice. Will I need PIL for that? And if I’m using PIL anyway, does that give me a quicker way to load the native textures to begin with? My previous viewer used PIL, and I wrote a custom file format so PIL could read the native textures. I just don’t know whether Panda3D interfaces easily with PIL.


You can do all that, but it’s easier (and faster) to go through Panda’s native PNMImage class. You can load from and store into a PNMImage, and you can modify a PNMImage easily.