Auto Scaling Images to the Window

Hi everyone,

I’m pretty new to Panda, so I imagine I’ll have plenty of dumb questions, but I searched around and couldn’t find anything for this one…seems simple enough, though.

I’m using OnscreenImage to display an image, but I want it to fill the entire window…is there some way to set the scale to be the window size (I could do this when the image is first created, but if the window is resized, is there any way to always have the image scale with the window)?

Yes, try to construct it with either parent=aspect2d or parent=render2d, don’t really remember which. If you don’t specify the position and scale, I’m pretty sure it automatically fills the screen.

aspect2d is the default parent. For me, if I parent an OnscreenImage to aspect2d (or don’t specify a parent at all) it gets scaled to the size of the window, but without distorting the image, so if the aspect ratio of the image is different to that of the window the image might be shorter or longer than the window in one dimension.

If I parent it to render2d then it gets scaled to fit the window, in both dimensions, even if that means stretching the image.

I would like to know the easiest way to load an image from file and display it in 2D in the same size and proportions as the image itself, pixel for pixel. So far, I have to do a manual translation to achieve this.

Ah, thanks; the render2d property did the trick!

Actually what I said above is not true. aspect2d distorts the image, render2d does not. (Assuming the height and width of the image are powers of 2.) Trying to display an image with its original size and proportions using OnscreenImage is either really difficult or impossible. I think egg-texture-cards is rather the way to do it.

impossible ?

def loadImageRealScale(name):
    np=OnscreenImage(name)
    np.setScale(float(np.getTexture().getYSize())/base.win.getYSize())
    return np

That code stretches the image, making it taller and thinner than it should be. I think it’s awkward, if not impossible.

oh, so your image is rectangular.

def loadImageRealScale(name):
    iH=PNMImageHeader()
    iH.readHeader(Filename(name))
    yS=float(iH.getYSize())
    np=OnscreenImage(name)
    np.setScale( Vec3(iH.getXSize(),1,yS)/base.win.getYSize() )
    return np

Vec3(iH.getXSize(),1,yS)/base.win.getYSize() originates from :
Vec3(iH.getXSize()/yS,1,1)*yS/base.win.getYSize()

Yep, that seems to do it :slight_smile: