Saturday, June 10, 2006

Texturing & Pygame components

I have created a new component being able to display textures on a plane in 3D. This was actually quite easy, after loading an image I simply draw a plane as usual and additionally specify texture coordinates for each vertex. I have not implemented intersection tests for planes yet but for now it is sufficient to use a thin bounding box. So the object can be moved as usual.

The next obvious step for me was to handle pygame components. I wanted to be able to use them without any need for modification, so I modified Display3D to be compatible with PygameDisplay. The method overridePygameDisplay() is used to replace the PygameDisplay singleton by an instance of Display3D. This way components request surfaces using PygameDisplay without knowing that they are actually interacting with Display3D.

Since I used surfaces as source for the textures anyway, I just had to set up a texture for each pygame component and draw it on a plane. The 2D coordinates get converted to 3D coordinates on the far Plane. To have the pygame comps on top of everything, depth testing is deactivated before drawing them. For mouse handling, I just copied code from Pygamedisplay.

An annoying limitation in OpenGL is that texture dimensions need to be a power of two. Otherwise texturing is really slow. I currently solve this by simply magnifying the dimensions of the created textures to the next power of 2.

The example picture shows a rotating TexPlane showing the kamaelia cat, a moving cube, 3 buttons as well as a Ticker. The example only works with newer pygame component which send REDRAW messages.

0 Comments:

Post a Comment

<< Home