简体   繁体   中英

OpenGL: Fit scene to screen

I have a 3D scene with a perspective projection.

I want to fit the scene to the screen based on a bounding box ( min and max ).

I have centered my scene like this:

glm::vec3 center = (min + max) / 2.0f;
rootNode->translate(-center.x, -center.y, -center.z);

Now I need a scale factor to scale my rootNode to fit the screen. How do I do this?

(this: 8.070 How can I automatically calculate a view that displays my entire model? (I know the bounding sphere and up vector.) does not help because its based on a orthogonal projection)

The reason this question is harder with a perspective projection than it is with an orthogonal projection is that the min and max you need are not constant with a perspective projection.

With a perspective projection the distance between either edge of the visible region increases as you move away from the camera.

With a perspective projection you typically have a field of view angle, theta , a camera position, and a "looking at" vector, v . At any distance, d from the camera's position (in the direction of v ) you can imagine a plane whose normal is v . The region of this plane that your camera can "see" has width:

2 * d * tan(theta / 2).

In a simple fixed camera setup you might have your camera at the origin and looking down the z-axis, and then the distance d for any point will just be the point's z coordinate.

Note also that you may have different horizontal and vertical field of view angles. If you have set a vertical field of view angle "fovy" and an aspect ratio (viewport width / viewport height) then your horizontal field of view angle is your vertical field of view angle times the aspect ratio.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM