# Calculate scale to fill viewport

How can I calculate the scale and offset to use when I want my 3D-perspective scene to exactly touch the viewport’s edges?

You’ll have to be more specific. The viewport is the area of the window being rendered to. You can render any number of things to exactly touch the edges. Do you mean you want to render a quad to the screen aligned with the camera that goes to the edge of the camera’s frustum on each side?

What do you want to happen when things in your scene start moving around, or when you rotate the entire scene? Do you want the scene to be re-scaled and re-centered in real time so that it fills the viewport exactly? Or, are you thinking of a static scene?

Re:You’ll have to be more specific. ----
The viewport is the area of the window being rendered to. --OK
You can render any number of things to exactly touch the edges. --OK
Do you mean you want to render a quad to the screen aligned with the camera that goes to the edge of the camera’s frustum on each side? – NO, I want to render any number of polygons at various locations with various angles --I can do that, -BUT- it is too small, so I have to increase the scale, -BUT- then it it is too big, clipping at the viewport edges, --SO-- how can I calculate the scale so that it will be as large as possible without clipping? (I know all the vertex points and all the locations and all the angles)

Re: What do you want to happen when things in your scene start moving around, or when you rotate the entire scene? Do you want the scene to be re-scaled and re-centered in real time so that it fills the viewport exactly? —YES—

The calculation should be as quick as possible, but I can live with whatever time it takes.

My scene is an assembly of objects. It is normally static. The user can then mouse-down and drag the scene about the x and z axes. During this turn/tilt, the offset and scale will not change. Then, when the mouse goes up, the scene must recenter and rescale to reach the edges (top,bottom or right,left) of the viewport.

If you want the scene to exactly touch the edge of the screen is quite difficult but if you accept an approximation you can compute the bounding box of your scene aligned if the camera.
Then the center of the bounding box will be the center of the scene.
Then you need to compute the “zoom”.
To zoom the scene without altering the prospective you can put the camera nearer to the scene.
If the bounding box is aligned with the camera the computation is quite easy.
The right/near edge of the box must touch the frustrum. So you have only to compute the contact point between the edge and the frustrum plane.

If you want a more precise computation you have to choose a center of the scene (the medium point? or (max-min)/2?) Then you have to check for every point witch give the farthest camera point.

ps: If you move the camera, remember to adjust the clipping planes.

This can be done, but you’re going to have do something tedious like Rosario is suggesting. How about doing it manually? Use shift+mouse drags to recenter the scene, and the mouse wheel to zoom in or out? This would be easier to code.