How to implement the OpenGL zoom function

I have an OpenGL application that implements navigation functions like Orbit, Walk, Pan, Rotate, etc. for navigating a 3D environment. It all works flawlessly and is pretty tricky to configure with gluPerspective and gluLookAt.

glMatrixMode GL_PROJECTION
   glLoadIdentity
   gluPerspective m_ViewAngle, m_AspectRatio, m_ClipDistance_Near, m_ClipDistance_Far          

   glMatrixMode GL_MODELVIEW
   glLoadIdentity
   gluLookAt m_Eye.X, m_Eye.Y, m_Eye.Z, m_Focus.X, m_Focus.Y, m_Focus.Z, m_ViewUP.X, m_ViewUP.Y, m_ViewUP.Z

   glCallList DisplayListIndex

Like the typical ZoomExtents or ZoomToFit command in CAD software from any arbitrary point of view (viewing direction), I would like to be able to scale so that (1) The entire 3D environment is visible, and (2) The 3D environment model fills the entire viewport ( as much as possible given the current viewport size).

I know the bounding box (extents) of the environment (min XYZ, max XYZ). However, I could not get what the Eye and Focus position should be for the ViewAngle and AspectRatio data and environment.

, , gluLookAt. !

+3
2

.

- . ( y, y, x, x) , , , , , .

, , - .

, .

, .

, .

+2

, . - , z. , , . . .

alt text

. , . , . , z . :

 eye := (center.x, center.y, center.z) - depth * (camera.x, camera.y, camera.z)

Bx := Ax * Bz / Az Bz := Bx * Az / Ax.

1.0 1,0 . , 1,0 100,0 20 .

Bz := 1.0 * 100.0 / 10
Bz := 10.0

, 200,0 , Ax 100,0, 1024 , Bx 512pixel.

Az := (Ax / Bx) * Bz
Az := (100.0/512.0) * 10.0
Az := 1.953m

m Bz.

, (0, 0, 1), (1, 0, 0), (0, 0, 0).

eye := center - depth * camera
eye := (0, 0, 0) - 1.953 * (1, 0, 0)
eye := (-1.953, 0, 0)
0

Source: https://habr.com/ru/post/1711217/


All Articles