I’ve been visiting this problem off and on for at least a year and have never found a very good solution.

I want to find the correct transformation (at run-time) that will position my model in the middle of the window such that the extents of the model are touching the left and right edges of the window and the top (and perhaps bottom) of the window. The user has the option to specify the location from which the coordinate system is viewed.

For example, if I draw the x, y, and z axis of this coordinate system, the top of the z axis touches the top of the window, and the x and y axiis rotate about this z axis and their max values touch the left and right edges of the window.

This coordinate system is right handed with Z up, a typical “data” coordinate system, not the “graphics” coordinate system with Z going to or away from the viewer.

Now the whole problem is that the domain of this coordinate system is not known until run time. I use glOrtho(xMin,xMax,yMin,…) and (after rotation) my window is not “big enough” to show the whole coordinate system.

I have tried all kinds of loops involving successive “growing” of the domain along each of the dimensions, finding where and how much to “grow” an axis by using glProject until the resultant window coordinates are within the drawing window. This has never worked the way that I’d like.

Has anyone ever solved this particular type of problem?

There are an arbitrary number of transforms
which will accomplish this, because as you
move away from the viewer, you can scale your
thing up to fill the window.

If you’re using glOrtho or gluOrtho2D and do
not get the exact size you’re asking for,
there is a bug in your driver or GL
implementation, assuming you’ve set the
viewport correctly to match the native window
size.

If you want to use a perspective projection,
you can use gluUnProject to find the world
coordinates of some specific window
coordinate; getting these values for the
bottom left and top right of your screen and
feeding them back to GL should get you
something which exactly covers the screen at
the depth which you specified.

I think you have given me the avenue I need to try with your comment about gluUnProject

I can find the minimum and maximum window coordinates that will be required to fit my desired coordinate system through glProject (these coordinates will likely be outside the viewport defined).

I believe that what I need is to “scale” my coordinate system by the ratio of this calculated window size to the implemented window size. What is not at all clear is where (along which axis or axies) to apply this scaling.

I believe I’ve found something that’s working ok. So I think I’ll table the question.

I continue to just do “brute” force, successively adding a small amount to the domain in all three dimensions until glOrtho produces a transform that ensures all six corners of the cube about that space are in the window.

I think the reason I was getting bad results was because of the arguments I’d pass to glOrtho for near and far (the last 2 arguments) I could never get those two just right so that all cases would work.

Finally, out of desperation, I just decided to try -DBL_MAX and DBL_MAX respectively for both and it works!

I believe this problem is solved, thanks for the help.