'OpenGL. zooming to fit scene in window

I'm using perspective projection and a gluLookAt as view matrix I'm trying to implement the function of zooming to fit scene to the window size using the min & max BoundBox. I calculate the min-max coordinates of the bound box, find the middle(center) of scene and set it as a target in the lookAt function. First, I implemented the function by multiplying the vertex by the world matrix, everything works well, but with this approach, the scene does not increase to full screen size when viewed(camera position) from the side or from above, since the bund box is often rectangular, not a cube. Accordingly, as I thought, in order for the scene to fit into the screen completely, regardless of the placement of the camera, you need to multiply the vertices not only by the world matrix, but also by the view matrix, but I immediately noticed an bug, the target is now almost always outside the real scene BoundBox and always in different places. Can someone please explain what is the problem or where am I doing wrong?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source