Rendering Images
In order to render an image, it is necessary to first create a flat piece of geometry consisting of two triangles (usually called a quad or a card) and then apply the desired image as a texture. Panda3D provides convenience methods for creating such a card automatically.
OnscreenImage
Just like OnscreenText, you can use OnscreenImage as a quick way to put an image onscreen. Use an OnscreenImage whenever you want a quick way to display an ordinary image without a lot of fancy requirements.
from direct.gui.OnscreenImage import OnscreenImage
imageObject = OnscreenImage(image='myImage.jpg', pos=(-0.5, 0, 0.02))
If you want, you can change the image into another one using setImage():
imageObject.setImage('myImage2.jpg')
When you want to take the image away, use:
imageObject.destroy()
A full list of arguments that can be passed to the constructor is available
on the OnscreenText
page of the API
reference.
Generating a card
CardMaker
is a convenience class that can be used to generate a card
with arbitrary dimensions. It can be used for rendering an image in the 3D or 2D
scene. The image should be loaded as a texture and then applied to the generated
card:
cm = CardMaker('card')
card = render2d.attachNewNode(cm.generate())
tex = loader.loadTexture('maps/noise.rgb')
card.setTexture(tex)
This will generate a card that causes the image to be stretched to cover the
entire screen. To preserve the aspect ratio of the image, it is necessary to
instead parent it to “aspect2d”, as well as use either
NodePath.setScale()
or CardMaker.set_frame()
to adjust the
card dimensions to match the aspect ratio of the image.
See the CardMaker
class in the API reference for a full list of
methods to configure the generated card.
Transparency
To enable transparency in images, you must tell Panda3D to enable a transparency mode on the object, otherwise the transparent parts of the image will show up as black. This can be done using the following code:
from panda3d.core import TransparencyAttrib
image = OnscreenImage(image='myImage.png', pos=(0, 0, 0))
image.setTransparency(TransparencyAttrib.MAlpha)
See the section on Transparency and Blending for some caveats about rendering objects with transparency.