A, transformation,
1. Transformations in OpenGL
2. View transformation
- The view transform is the first transformation applied to the scene to determine the vantage point in the scene, by default, at the origin (0,0,0) in perspective projection, and to view along the negative z-axis (” look inside “the display).
- When the observer point is at the origin (0,0,0), as in perspective projection.
- The view transform puts the observer wherever you want. And allows the scene to be viewed in any direction. Determining a view transformation is like placing an observer in the scene and pointing it in one direction;
- In the grand scheme of things, view transformations must be applied before any other model transformations can be applied. This is done because, for the visual coordinate system, the view transformation moves the coordinate system of the current work; Subsequent changes will be based on the newly adjusted coordinate system.
3. Model transformation
Model transformation: Used to manipulate the model with a specific transformation within it. These transformations move the object to the desired position by rotating, scaling, translating, and so on.
- Note: model transformations do not satisfy the commutative law. It means that rotation and translation is not the same as translation and rotation.
- There are two ways of looking at model transformations: moving the observer and moving the object
API: translation
Void m3dTranslationMatrix44(M3DMatrix44f m, float Y, float Z) void m3dTranslationMatrix44(M3DMatrix44f m, float Y, float Z)Copy the code
rotating
/ / parameters: So here we have radians (we need to convert the Angle to radians), going around X/Y/Z, going around who to write 1, M3dRotationMatrix44 (m3dDegToRad(45.0), floata x, float y, float z);Copy the code
The zoom
// Parameters: result, scaled around X/Y/Z. Void m3dScaleMatrix44(M3DMatrix44f m, Floata xScale, float yScale, float zScale);Copy the code
Comprehensive transformation
// Result, first transformation, second transformation. Void m3dMatrixMultiply44(M3DMatrix44f product, const M3DMatrix44f a, const M3DMatrix44f b);Copy the code
Matrix stack
1. Basic API introduction
type
GLMatrixStack::GLMatrixStack(int iStackDepth = 64);
Copy the code
Load a cell matrix at the top of the stack
void GLMatrixStack::LoadIdentity(void);
Copy the code
Load any matrix at the top of the stack
/ / parameters: 4 * 4 matrix void GLMatrixStack: : LoadMatrix (const M3DMatrix44f m);Copy the code
The matrix times the matrix at the top of the stack and the result is stored at the top of the stack
void GLMatrixStack::MultMatrix(const M3DMatrix44f);
Copy the code
Gets the GetMatrix function at the top of the matrix stack
// To accommodate the use of GLShaderMananger, or to get a copy of the top matrix const M3DMatrix44f & GLMatrixStack::GetMatrix(void); void GLMatrixStack::GetMatrix(M3DMatrix44f mMatrix);Copy the code
2. Stack pressing and unloading and process analysis
The pressure of stack
/ / the current matrix onto the stack (stack) matrix copy a to stack void GLMatrixStack: : PushMatrix (void);Copy the code
Push a matrix
Void PushMatrix(const M3DMatrix44f mMatrix);Copy the code
- PushMatrix can pass not only a matrix, but also GLFame objects
Push a GLFame object
// Push the GLFame object into the matrix object void PushMatrix(GLFame & Frame);Copy the code
Out of the stack
Void GLMatrixStack::PopMatrix(void); void GLMatrixStack::PopMatrix(void);Copy the code
Process analysis:
- The LoadIdentity is pushed onto the top of the stack with a cell matrix
- If PushMatrix is empty, it will copy the element matrix at the top of the stack and push it to the top
- If you PushMatrix a matrix, you push that matrix directly to the top of the stack
- For subsequent changes, the MultMatrix is used to multiply the element matrix at the top of the stack
Refer to the previous article: observing the way the MV matrix stack relates
3. Radiation transformation
Transforms the current matrix stack directly
//Rotate //Rotate Function the Angle argument is passed in degrees instead of radians void MatrixStack::Rotate(GLfloat Angle,GLfloat x,GLfloat y,GLfloat z); Void MatrixStack::Translate(GLfloat x,GLfloat y,GLfloat z); Void MatrixStack::Scale(GLfloat x,GLfloat y,GLfloat z);Copy the code
For example:
modelViewMatrix.PushMatrix(); Scale(1.0f, -1.0f, 1.0f); / / along the Y axis offset the modelViewMatrix. Translate (0.0 0.0 f, 0.8 f, f); modelViewMatrix.PopMatrix();Copy the code
4. GLFrame API
Operate on a GLFrame object, such as an observer object
class GLFrame
{
protected:
M3DVector3f vOrigin; // Where am I?
M3DVector3f vForward; // Where am I going?
M3DVector3f vUp; // Which way is up?
}
cameraFrame.SetOrigin(float x, float y, float z)
cameraFrame.SetUpVector(float x, float y, float z)
cameraFrame.SetForwardVector(float x, float y, float z)
Copy the code
/ / will press the top of the stack into the void any matrix GLMatrixStack: : LoadMatrix (GLFrame & frame); // Matrix times the matrix at the top of the matrix stack. Multiplication results are stored in the top of the stack void GLMatrixStack: : MultMatrix (GLFrame & frame); / / pressed the matrix of the current stack void GLMatrixStack: : PushMatrix (GLFrame & frame);Copy the code