Reputation: 282895
I'm trying to calculate the zoom percentage from a projection matrix. The following code works as long as the image isn't rotated:
void UpdateZoomPercent()
{
var zoom = _projMatrix.M11 * glControl.Width / 2; // M11 is the top left value
lblZoomPercent.Text = (zoom * 100).ToString("G3") + "%";
}
Essentially it just takes the X scale and then multiplies it out to the viewport. X an Y will always be scaled proportionally, so I don't need to look at Y. My scene is 2D.
How do I factor in rotation?
Upvotes: 1
Views: 297
Reputation: 162184
Consider a matrix of real values forming base of a cartesian coordinate system. Each column represents a base vector. In the case of a isometric zoom, all base vectors have the same length. Let C(M,n) denote the n-th column vector of matrix M, i.e. C(M,n) = M_n,j, then for a isometric zoom it would be len(C(M,0)) = len(C(M,1)) = len(C(M,…)), where len(v) = sqrt(v·v)
In the case of anisometric scaling the length of the base vectors would differ, which is what you can use to detect this situation.
In computer graphics the matrices you encounter are homogenous to allow for a single matrix to represent all possible transformations within the space they represent. Homogenous matrices have N+1 rows and columns, where N is the number of dimensions of the coordinate space represented by them. By convention (at least in all popular computer graphics software) the upper left part (i.e. M_i,j where i and j in 1…N, 1-based index) form the base vectors, while the Nth column and row form the homogenous part.
So in case of OpenGL you'd look at the upper left 3×3 submatrix as coordinate base vectors. And since OpenGL indexes column major order you don't have to reorder what's retrieved from OpenGL.
Upvotes: 2