This article uses two methods to implement the zoom-in and zoom-out function in OpenGL:
Method 1. Change the distance between the "Viewpoint" and the model [change the model viewpoint transformation matrix]
There are three problems to solve in this method:
① Smooth transition during Scaling
② When a model is scaled up or down to a certain extent, the scaling speed is slowed down to prevent the viewpoint from quickly approaching the model or disappearing into the scene.
③ Prevent the viewpoint from entering the Model
Solution:
Use the arc tangent function: Y = 0.5 * arctan (0.1 * x) + 0.25 * pi. The function image is as follows:
When the model is enlarged, X -- and Y decrease.
When the model is reduced, X ++ and Y increase.
Y minus the hour, that is, the ∠ APM becomes smaller, and the viewpoint a moves to the M point (because y> 0, a will never reach the M point)
When Y increases, that is, when the ∠ APM increases, the viewpoint a is far away from the M point.
The distance between the viewpoint and the center of an object is composed of three parts: Am + Mn + NO. This ensures that the object does not cross the near-cut cross section no matter what the model is in. (question ③)
1 void seteyeandradius ()
2 {
3 double angle = 0.5 * atan (0.1 * x) + 0.25 * PI;
4 // AM = tan (angle) Mn = 0.1 NO = SQRT (3.0)
5 radius = tan (angle) + SQRT (3.0) + 0.1;
6
7 // vector OA vector pointing from the focal point to the viewpoint
8 vector3dd A (eye-target );
9
10 // unitization
11 a. normailize ();
12
13 eye = A * radius + target;
14}
Method 2: Change the fovy angle of the projection transformation [change the projection transformation matrix]
The smaller the fovy, the larger the model. The larger the fovy, the smaller the model.
Use the arc tangent function: Y = arctan (0.1 * x) + 0.5 * pi. Function images include:
When the model is enlarged, X -- and Y decrease (fovy decrease)
When the model is reduced, X ++ and Y increase (fovy increase)
1 void setFovy()
2 {
3 camFovy = 180.0/PI*(atan(0.1*x)+0.5*PI);
4 }
For model browsing, the second method is more concise and easy to understand. You do not need to consider the situation where the viewpoint will enter the model.
As follows:
Source image:
Zoom in:
Zoom out: