How can I scale a random 3d model to fit in an opengl viewport? I am able to center the model in the middle of the view port. How do I scale it to fit it in the viewport. The model could be an airplane, a cone, an 3d object or any other random model.
Appreciate any help.
You’ll need the following information:
r: the radius of the object’s bounding spherez: the distance from the object to the camerafovy: the vertical field of view (let’s say in degrees) of the camera, as you might have passed it togluPerspectiveMake a little sketch of the situation, find the right triangle in there, and deduce the maximum radius of a sphere that would fit exactly. Given the above parameters, you should find
r_max = z * sin(fovy*M_PI/180 / 2).From that, the scale factor is
r_max / r.All this assumes that the viewport is wider than it is high; if it’s not, you should derive
fovxfirst, and use that instead offovy.