Learning books: OpenGL Super treasure Dictionary (Chinese fifth edition) password: FU4W

Book source code: OpenGL Super baodian fifth edition source code password: OYb4

Environment setup: OpenGL learning 01-MAC Setup OpenGL environment

PS: Because later Demo source code will be more and more, it is unlikely to be copied here, I will explain the core source code of Demo, all the source code please go to my Github /OpenGLDemo download, there will be detailed annotations in the source code.

The basic concept

A vector,

A vertex is a position (x,y,z) in the XYZ spatial coordinate system, and this coordinate can also represent a vector (an arrowhead line segment) pointing from the origin of the coordinate to that position.

In OpenGL, the corresponding data types are M3DVector3f(3-dimensional float vector), M3DVector4f(4-dimensional float vector)

The geometric significance of the calculation between vectors is shown below:

Second, the matrix

Matrix is a very powerful mathematical tool, which greatly simplifies the process of solving equations or systems of equations with complex relationships between variables.

In OpenGL, mainly used in coordinate transformation, corresponding data type M3DMatrix33f(3X3 floating point matrix), M3DMatrix44f(4×4 floating point matrix) and so on.

Note: matrix multiplication does not satisfy the commutative law, that is, AB! = BA

Third, transformation

1. View transformation

The view transform is the first transformation applied to the scene to determine the vantage point in the scene, the position of the observer, like placing a camera in the scene and pointing it in a certain direction.

2. Model transformation

The coordinate transformation of the object itself has three basic model transformations: rotation, translation and scaling.

3. Model view transformation

It’s actually a combination of the view transformation and the model transformation, because the view transformation and the model transformation are essentially the same thing, but they’re just distinguished for the programmer’s convenience, because if you move the object 10 meters forward, and the observer moves 10 meters back, the end result is the same.

4. Projection transformation

Projection transformation has orthographic projection and perspective projection:

  • Orthographic (parallel projection) : Objects are drawn to the same size no matter how far away they are.
  • Perspective projection: Distant objects look smaller than close objects of the same size.

5. Viewport transformation

The transformation of the logical window coordinates to the physical window coordinates is called viewport transformation, and usually we don’t need to worry about it, graphics hardware already does it for us.

The source code parsing

First, matrix transformation

Void m3dLoadIdentity44(M3DMatrix44f m); Void m3dTranslationMatrix44(M3DMatrix44f m,float x, float y, floatz); Void m3dRotationMatrix44(M3DMatrix44f m,float angle, float x, float y, floatz); Void m3dScaleMatrix44(m3dScalemmatrix44f m,float xScale, float yScale, floatzScale); Product void m3dMatrixMultiply44(M3DMatrix44f product, const M3DMatrix44f a, const M3DMatrix44f b);Copy the code

08-MOVEBymatrix core source code as follows, all source code download: 08-MOVEBymatrix

M3DMatrix44f mFinalTransform mTranslationMatrix mRotationMatrix M3DMatrix44f mFinalTransform mTranslationMatrix // The translationMatrix (xPos, yPos, 0) represents m3dTranslationMatrix44(mTranslationMatrix, xPos, yPos, 0.0f); M3dDegToRad = Angle -> radians staticfloatZRot = 0.0 f; ZRot + = 5.0 f; M3dRotationMatrix44 (mRotationMatrix, m3dDegToRad(zRot), 0.0f, 0.0f, 1.0f); M3dMatrixMultiply44 (mFinalTransform, mTranslationMatrix, mRotationMatrix);Copy the code

Second, projection matrix

The projection matrix is set with the view body (GLFrustum)

[GLFrustum] {// Just indicate that the following methods are GLFrustum's methods // Set the orthographic matrix parameters, (x, y, z) min and Max void SetOrthographic(GL)float xMin, GLfloat xMax, 
                         GLfloat yMin, GLfloat yMax, 
                         GLfloat zMin, GLfloatzMax); // Set the parameters of the perspective projection matrix, which are: through perspective, aspect ratio, near perspective, and far perspective.float fFov, float fAspect, float fNear, floatfFar); // Get the projection matrix const M3DMatrix44f& GetProjectionMatrix(void); }Copy the code

To facilitate the management of each matrix, GLTools provides a matrix stack GLMatrixStack. The default stack depth is 64, and there are operations such as pushing and removing the stack.

[GLMatrixStack] {/ / simply said the following method is GLMatrixStack / / -- -- -- -- -- -- -- -- matrix loading -- -- -- -- -- -- -- -- -- -- - / / at the top of the stack load cell matrix, Void LoadIdentity(void); // Load any matrix at the top of the stack void LoadMatrix(const M3DMatrix44f m); Void MultMatrix(const M3DMatrix44f mMatrix); void MultMatrix(const M3DMatrix44f mMatrix); // Get the top matrix const M3DMatrix44f& GetMatrix(void); Void PushMatrix(const M3DMatrix44f mMatrix); void PushMatrix(const M3DMatrix44f mMatrix); Void PopMatrix(void); void PopMatrix(void); / / -- -- -- -- -- -- -- -- affine transformation -- -- -- -- -- -- -- -- -- -- -- / / stack to scaling transformation matrix void Scale (GLfloat x, GLfloat y, GLfloatz); // Stack top matrix translation void Translate(GLfloat x, GLfloat y, GLfloatz); Rotate(GL); // Rotate(GL); // Rotate(GL)float angle, GLfloat x, GLfloat y, GLfloat z);
}
Copy the code

To facilitate the management of the model view matrix stack and projection matrix stack, GLTools provides a management pipeline, GLGeometryTransform, to help us keep track of these two matrix stacks and quickly retrieve the top of the model view matrix stack or projection matrix stack.

[GLGeometryTransform] {// simply means that the following methods are methods of GLGeometryTransform // ----------- sets the matrix stack --------- // Separately sets the model view matrix stack void SetModelViewMatrixStack(GLMatrixStack& mModelView); Void SetProjectionMatrixStack(GLMatrixStack& mProjection); Void SetMatrixStacks(GLMatrixStack& mModelView, GLMatrixStack& mProjection); // ----------- to get the top of the stack --------- // to get the top of the stack of the model view matrix const m3DMatrix44f&getModelViewMatrix (void); // Get the top matrix of the projection matrix stack const M3DMatrix44f& GetProjectionMatrix(void); Two matrices / / get the result of the matrix multiplication matrix stack const M3DMatrix44f & GetModelViewProjectionMatrix (void); }Copy the code

10-OrthoGraphic Core source code is as follows, full download: 10-OrthoGraphic

// Set the orthographic projection parameter, (xMin, xMax, yMin, yMax, zMin, zMax) viewFrustum. SetOrthographic (130.0 130.0 f, f, 130.0 f, f 130.0, 130.0 f, 130.0 f); / / get to load the stack projectionMatrix. The orthogonal projection matrix LoadMatrix (viewFrustum. GetProjectionMatrix ()); / / transformation pipeline, the management of 2 stack transformPipeline. SetMatrixStacks (modelViewMatix projectionMatrix);Copy the code

11-Perspective core source code is as follows, full source code download: 11-Perspective

/ / set the perspective projection matrix parameters, respectively, through the Angle of view, aspect ratio, close, distance viewFrustum. SetPerspective (35.0 f,float(width)/float(height), 1.0 f, 1000.0 f); / / load the perspective projection matrix projectionMatrix. LoadMatrix (viewFrustum. GetProjectionMatrix ()); / / by using transformation pipeline, the management of 2 stack transformPipeline. SetMatrixStacks (modelViewMatix projectionMatrix);Copy the code

12-ModelViewProjection core source code is as follows, all source code download: 12-ModelViewProjection

Void RenderScene(void) {// Define a test run time static CStopWatch rotTimer; // Get the interval from the last point in time to the current point in time (in ticks per second, i.e. 1/60s)floatYRot = rotTimer. GetElapsedSeconds () * 60.0 f; / / to empty buffer glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); M3DMatrix44f mTranslate, mRotate, mModelview, mModelViewProjection; // m3dTranslationMatrix44(mTranslate, 0.0f, 0.0f, -2.5f); Rotate m3dRotationMatrix44(m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f); rotate m3dRotationMatrix44(m3dDegToRad(yRot), 0.0f, 1.0f, 0.0f); M3dMatrixMultiply44 (mModelview, mTranslate, mRotate); / / projection matrix + = final position of the object coordinate transformation matrix view m3dMatrixMultiply44 (mModelViewProjection viewFrustum. GetProjectionMatrix (), mModelview); // Draw bracket GLfloatVBlack [] = {0.0f, 0.0f, 0.0f, 1.0f}; shaderManager.UseStockShader(GLT_SHADER_FLAT, mModelViewProjection, vBlack); torusBatch.Draw(); GlutSwapBuffers (); // Because of the dual buffer mode, the background buffer is replaced by the foreground buffer. // Automatically triggers the next render callback to achieve the animation effect glutPostRedisplay(); }Copy the code

All the Demo source code above is on my Github /OpenGLDemo, we can go to download and debug.

Any questions can be put forward in the comments section below, if not written well, you can put forward your opinion, I will reasonably adopt, O(∩_∩) ha ha ~, asking for attention and praise