views:

153

answers:

1

I'm working on opengl project. I set up perspective projection and render a transformed rectangle (rotated, scaled)

How i can calculate rectangle's bounding box (rectangle position,size)

Thank you

+1  A: 

You'd run the rectangle through the same matrices that OpenGL does to transform the 3D points into 2D screen-space ones. Get your input vectors, multiply them by any that you want to apply to the object, ModelView matrix, Projection matrix, then you have screen-space coords. Then check whether the resulting coordinates are on-screen, then you can calculate the minimum/maximum X and Y coordinates, and you have your bounding rectangle.

See also here (9.100), if you've got the GLU utility library functions available:

http://www.opengl.org/resources/faq/technical/transformations.htm

Hope that helps.

Kieren Johnstone
To get a correct screen bounding box also 3d clipping is needed (think to the camera flying over the rectangle). The correct geneal way is to use four planes, but if the camera is not going to be too close to the rectangle then just a single z=epsilon plane is enough.
6502
Sounds good, would suggest making that into an answer :)
Kieren Johnstone
Can you provide any source code or an example please ?
Northern