automatically rescale objects


When I load objects from a file… I’d like to scale them to fit the screen… how could I do that?


Different solutions possible here
Track minimum and maximum x, y, and z values, (that is, calculate a bounding box)

Then either setup a standard unit cube viewing frustum like this
gluPerspective(90.0, 1.0, 1.0, 3.0);
glTranslatef(0.0f, 0.0f, -2.0f);
glTranslatef(-0.5f * (xMax - xMin), -0.5f * (yMax - yMin), -0.5f * (zMax - zMin)); // Center your object in the origin.

  • and scale all coordinates by fScale:
    fSize = max((xmax-xmin), (ymax-ymin), (zmax-zmin));
    fScale = 1.0f / fSize;

  • or issue
    glEnable(GL_NORMALIZE); // If you have lighting enabled!
    glScalef(fScale, fScale, fScale);

  • or setup a projection with the fSize as bounding box: (For piece of mind this time with glOrtho )
    fSizeHalf = 0.5f * fSize;
    glOrtho(-fSizeHalf, fSizeHalf, -fSizeHalf, fSizeHalf, 0.0, fsize);
    glTranslatef(0.0f, 0.0f, -fSizeHalf);

(And here is the excuse if anything goes wrong: I just hacked this into the reply box and didn’t copy from a real project, so don’t blame me for typos. )

How to track the minimum and the maximum? @Is there an automatc way of doing that or should I just track the min and max values of x, y , z?

Thanks a lot

Do-it-yourself code while loading the objects coordinates. OpenGL is too low-level to know of objects, resp. bounding boxes itself.