Sizing a view frustum around a quad for 2D GUI in 3D
I've been thinking of ways to handle placing my GUI interface in a 3D perspective projection, so that I can have 3D rotation effects on my "2D" interface elements. The problem I've been having is that it's been difficult positioning elements and making certain they all fit in view at once.
The idea I've had so far is to define a 2D quad in space to represent a "screen" surface, and layout the GUI on top of the quad in the same way I'd layout the gui in 2D. Then I'd move the camera to the center of the quad, point the camera at it and project the camera's position a certain distance along the quad's surface normal until the entire quad fits into the viewing frustum. The problem with that is I'm not sure how to find the minimum distance to project the camera positio开发者_如何学Cn.
Anyone know an equation/algorithm for finding this?
You could simply make a loop and increase the distance and measure until it fits... but I think with some basic trig you could find the exact distance required to make the image appear a certain size.
You want it to fill a square of a certain size. If you imagine a square of that size, with the camera sitting at some distance (d) away and centered within the image, then you would take half the width of the image you want (w), and half the viewing angle of the camera (a), and use the sine to find the distance - you know the angle, and the size of the opposite side.
So the formula is basically this: d = sin(a) * w
精彩评论