Can I get pixel dimensions of a displayed polygon?


I’m trying to develop a dialog system based on speech bubbles. These bubbles are to be generated as textures for TextNodes. Now, I’d like to generate these textures on the fly to fit the text. Maybe pre-generating them would be more efficient, but this is definitely more educational.

If I can find out how many pixels the TextNode’s card will take up on screen, I can generate an appropriate texture that looks crisp. But is that possible and, if so, how do I do it?


Sounds like your texture cards are in 3D space, and I’m assuming that they are billboards always facing the camera.

To calculate the pixel dimensions of a card of size lxw:

I’m a C++ programmer so I’ll just write generic math…

fovx,fovy = camera field of view in radians

D = distance from camera to card, world units

Spx, Spy = Screen pixel width and height, use GraphicsOutput getXSize and getYsize; you can get GraphicsOutput from the WindowFramework

Swx, Swy = Screen width and height in world units, D units from camera

Cwx, Cwy = card width and height in world units

Cpx, Cpy = card width and height in pixels

Swx = 2Dtan(fovx/2.)

Swy = 2Dtan(fovy/2.)

Cpx = Spx * Cwx/Swx

Cpy = Spy * Cwy/Swy

Maybe Panda has a way of doing this, but I don’t know

You could use getRelativeVector to translate the bounds into the coordinate system of base.pixel2d.

That said, I think TextNode contains methods to tell you exactly how big the generated text is. You’d need to investigate further, though, I wouldn’t know.

Thanks, guys. I’m working on an implementation. I’m still new to this and I’ve only got a little bit of time each day, so I’ll get back to you and post something when I’ve got it working.