Fit the 3d model inside the window

I want to display models of different sizes embedded in a view so that the entire model is visible inside the screen.
What is the best way to do this? I tried to scale (using glScale) the model using this formula

scaleFactor = ( screenSize / (maxModelSize * constant) )

Where size is height or width, whichever is greater.
Constant 1 / (length of one screen pixel in OpenGL units)
There are two problems with this:
1. After doing some transformations, I want to return to this initial scale (the model scales according to the window) using Identity. At present, the challenging identity will bring the model to its original size (up to the “fixation” scale).
2. "Constant" - this is what I got as a result of trial and error, I think the wrong method for me. I also suspect that it is not constant and depends on the screen resolution, and God knows what else.

+5
source share
1 answer

8.070:

:

. : ( (c.x, c.y, c.z) ) ( "diam" ).

zNear . - , 1.0. , ,

zNear = 1.0; zFar = zNear + diam; 

( ):

GLdouble left = c.x - diam; 
GLdouble right = c.x + diam;
GLdouble bottom c.y - diam; 
GLdouble top = c.y + diam; 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glOrtho(left, right, bottom, top, zNear, zFar); 
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); 

, ( , = 1,0). , , , , , glOrtho():

GLdouble aspect = (GLdouble) windowWidth / windowHeight; 
if ( aspect < 1.0 ) { 
    // window taller than wide 
    bottom /= aspect; 
    top /= aspect; 
} else { 
    left *= aspect; 
    right *= aspect;
} 

. (. ..), .

ModelView :

GluLookAt (0., 0., 2.*diam, c.x, c.y, c.z, 0.0, 1.0, 0.0);
+9

All Articles