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…