## Zoom out to show all content

Apprentice
Posts: 11
Joined: 2005.09
Post: #1
I am having a hard time trying to figure out how to solve my problem, which technically should be very easy.

What I have is any sized grid of tiles (say 15 high, an 20 wide, with a tile size of 20 units). What I am trying to do is determine how far to zoom the camera out so that all the tiles are displayed on screen (which is not a fixed size).

In this particular case glOrtho will not fit the situation as the camera is needed to follow a trackball, that part works fine.

Any help would be highly appreciated. I have a feeling that the answer is going to make me feel dumb for not thinking of it.
Luminary
Posts: 5,139
Joined: 2002.04
Post: #2
the fovy angle you pass to gluPerspective determines a right-angled triangle, with the right corner at the center of the trackball. Once you've determined the radius of the circle you want to be visible in the view, you can use that radius as the opposite side of the right triangle and a bit of simple trigonometry to find the length of the adjacent side, A.K.A the distance of the camera.

I'm sure someone will draw a diagram if you can't follow that ^_^
Apprentice
Posts: 11
Joined: 2005.09
Post: #3
I must not be thinking properly, or I am just doing it wrong. But here is what I have from your suggestion.

I want to find the radius, which I have as 200 (20 tiles * 20 tile width * 0.5) and a length for the first side of 150 (15 tiles * 20 tile width * 0.5).

When I find the long side of the triangle I am getting 250, which still does not show the full grid. When I zoom out manually it appears that 260 is the number I want to have as a result of my calculation.

Would the size of glViewport effect the calculation?
Luminary
Posts: 5,139
Joined: 2002.04
Post: #4
No, glViewport doesn't affect the calculation.

Your radius seems off to me, I expected to see a sqrt(width * width + height * height) in there, but I don't know quite what you're rendering.

The radius and opposite side length are the same thing...
Apprentice
Posts: 11
Joined: 2005.09
Post: #5
Sorry, I left that part out.

I was doing sqrt((200 * 200) + (150 * 150)), which is where I was getting the 250 value from.

This does display all the content for the width, but still does not display all the content for the height. That is where I got the 261 value from, with that zoom I can display all the content. But that changes of course if I change the size of the grid or tile size.
Sage
Posts: 1,199
Joined: 2004.10
Post: #6
You might not be taking into account the ratio of width to height of your frustum. I'd use the lesser of the two...
Apprentice
Posts: 11
Joined: 2005.09
Post: #7
Which number would I apply the ratio to?