automatic zoom to fit irregular 3D model to screen

Hi All,
For a 3D model, since I can rotate it on the screen, it may look irregular, how can I make it automatically fit to the screen?

My idea is to find a scale to use glScale to do this, but how to get the scale?

Maybe I have to measure the height and width of the current shape on the screen, how should I do it?

Thanks for your help.

I would transform all vertices to window space (gluProject or look at my glhlib library).
If all the vertices are not inside, translate backwards and recalculate and check to see if the vertices are inside.

If vertices are inside the window, translate forward.

You can use 8 points to define a bounding cube for your geometry. Then, transform these 8 points by your current modelview matrix.
You now can easily calculate the width and height of the transformed bounding cube.

Next, find the world space extents of your window. Assuming you’re using 1 viewport for the entire window, transform your viewport extents into worldspace (gluUnproject).

Now you have the world space extents (x,y) of your visible worldspace.

now do a glScale(x,y,z) where

x = world_space_extents_x / bounding_cube_x
y = world_space_extents_y / bounding_cube_y

Alternatively, you can use window space coordinates as V-man suggested. Instead, translate your 8 bounding cube vertices to window space (gluProject).

The rest of what I said applies only now you can use your viewport dimensions directly (since they are in window space).

Hi Aeluned,
Thanks for your reply.
The way you suggest works well when the model is always displayed as a whole, but when only some parts of the model are shown, that way may not work well, right?

Hi V-man,
Thanks for your reply.
Currently, I’m using realbasic with the openGL module, your library is for C, so …
Seems that I still need keep searching for another way…