vector
What is a vector?
In OpenGL, we always use vectors to represent directional quantities, such as a line segment with an arrow from the origin to an arbitrary point (x,y,z) in a 3D coordinate system. It has two important values: direction and quantity (also known as length).
Scalars are just quantities, magnitudes
A unit vector
A vector of length 1 is a unit vector
So how do we figure out the length of a vector?
But if a vector is not a unit vector, you can scale it by 1 by normalization.
By dividing a nonzero vector by its magnitude (the length of the vector), you get a unit vector with the same direction
(x/|xyz|, y/|xyz|, z/|xyz|)
Define the vector
In OpenGL’s Math3D library
- M3DVector3f represents a three-dimensional vector (x,y,z)
- M3DVector4f represents a four-dimensional vector (x,y,z,w),xyz is scaled by the value of w
See the // three dimensional vector/four dimensional vector declaration typedeffloat M3DVector3f[3];
typedef floatM3DVector4f[4]; // Declare a three-dimensional vector M3DVector3f: type vVector: variable name M3DVector3fvVector; M3DVector4f v3dvector4f vVertex = {0,0,1,1}; // declare a three-component vertices array, for example, generate a triangle //M3DVector3f vVerts[]={-0.5f,0.0f,0.0f, 0.0f,0.5f,0.0f,0.0f, 0.5f,0.0f};Copy the code
What can a vector do?
Dot product
When you want to figure out the Angle between two three-dimensional vectors, the unit vector, it’s called the dot product. The result returns a value in the range of [=1,1], which is the cosine of the Angle between the two unit vectors.
In the Math3D library, such functions are included to compute the dot product
- float m3dDotProduct3(const M3DVector3f u,const M3DVector3f v); // Get the dot product between two unit vectors
- float m3dGetAngleBetweenVector3(const M3DVector3f u,const M3DVector3f v); // A more advanced function that returns the radian value of the included Angle
cross-product
When you want to get a vector that is perpendicular to the plane defined by the two vectors, this operation is called the cross product, and the result will be a new vector that will be perpendicular to the plane defined by the two vectors.
In math3D library, compute the function of the cross product
void m3dCrossProduct3(M3DVector3f result,constM3D Vector3f u,const M3DVector3f v);
Attention!!!!!!
- The two vectors that take the cross product don’t have to be unit vectors
- Depending on the order of calculation, the result is also different
V1 cross V2 — V2 cross V1, they’re both going to be perpendicular to V1 and V2, but they’re going to be vectors pointing in the opposite direction.
matrix
If I have a point in space, and I rotate it around any position by a certain Angle in any direction, the new position of that point, instead of just adding and subtracting vertices x,y, and z, is computed by a matrix. Because the x of the new position depends not only on the original x coordinate and other rotation parameters, but also on the y and z coordinate values.
In the reference code for defining vectors above, we see that OpenGL tends to use one-dimensional arrays more, because OpenGL uses the convention of column first matrix sorting
Unit matrix
When you multiply a vector times a unit matrix, that’s the same thing as multiplying this vector times 1, it doesn’t change anything.
- Everything on the main diagonal is 1
- Vector times element matrix is equal to vector times 1
- The number of columns is equal to the number of rows of the matrix, otherwise it does not comply with the rules of the matrix
Matrix multiplication
- From a linear algebra point of view
In the dimension of linear algebra, for writing purposes, coordinate calculations are carried out from left to right, as follows:
The transformed vertex vector = V_local * M_model * M_view * M_pro; The transformed vertex vector = vertex * model matrix * view matrix * projection matrix;Copy the code
And since the vertex vectors are row vectors, to satisfy the rule of multiplying matrices, you have to put the matrix on the right, the right product.
- From the perspective of OpenGL
In OpenGL, the matrix is defined as column priority, so vertices are represented as column vectors. The same calculation method will be changed, as follows:
Transform vertex vector = M_pro * M_view * M_model * V_local; Vertex vector after transformation = projection matrix * view matrix * model matrix * vertex;Copy the code
In this case, it’s a left multiplication
- From the matrix stack matrix multiplication source analysis
1. First store the top stack matrix in mTemp. 2. The results are kept at the top of the stackCopy the code
Recommendation:
Understand vectors, matrices in OpenGL