4. Primitive parsing

OpenGL pipeline rendering process

Data can be passed from a client to a server in three formats: Texture Data, imaginative and Attributes. Each form is just a way of passing data and does not limit the type of data being transferredThe Vertex Shader accepts Texture Data, Attributes, and Uniforms, but the Vertex Shader only handles the Attributes, Textures Data and Uniforms can be passed to the vertex shader and pixel shader, but Attributes can only be passed to the vertex shader, not to the pixel shader

Uniforms, Attributes

The values are typically used to convey colors, vertex data, texture coordinates, illumination normals, Uniforms are typically used to pass around rotation matrices (used by vertex shaders to rotate vertex data) and color conversion matrices (used by slice shaders to convert YUV color values to RGB color values).

Note that Attributes variables do not support integers, booleans, structures, or attribute arrays and cannot be declared in fragment shaders.

Orthographic projection

Perspective projection

Fixed pipeline (store shader)

With storage shaders, we can easily implement our requirements without having to do too much customization, just choose the right storage shader according to the requirements. If you use a fixed pipeline, you first initialize the shader manager.

GLShaderManager shaderManager;
shaderManager.InitializeStockShaders();
Copy the code

Storage shader

Unit shader GLShaderManager: : UserStockShader (GLT_SHADER_IDENTITY, GLfloat vColor [4]); Parameter 1: Storage shader type – Unit shader Parameter 2: Color Usage Scenario: Draw graphs in the default OpenGL coordinate system (-1,1). All segments of the graph are filled with one color

Surface shader GLShaderManager: : UserStockShader (GLT_SHADER_FLAT GLfloat MVP [16], GLfloat vColor [4]); Parameter 1: Storage shader type – Flat shader Parameter 2: 4×4 matrix parameter 3: Color Usage Scenario: Transformations can be applied when drawing graphics (model/projection changes)

Coloring shader GLShaderManager: : UserStockShader (GLT_SHADER_SHADED GLfloat MVP [16]); Parameter 1: Store shader type – Color shader Parameter 2: allow variable 4×4 matrix Usage scenario: When drawing graphics, transformations can be applied (model/projection changes), and colors will be smoothly inserted between vertices called smooth shader

The default light shader GLShaderManager: : UserStockShader (GLT_SHADER_DEFAULT_LIGHT, GLfloat mvMatrix [16], GLfloat pMatrix [16], GLfloat vColor[4]); Parameter 1: Store shader type – Default light shader Parameter 2: model 4×4 matrix parameter 3: projection 4×4 matrix parameter 4: Color (geometry color) Usage Scenario: When drawing a graph, transformations (model/projection changes) can be applied. This shader causes shadows and lighting effects on the graph to be drawn

Point light shader GLShaderManager: : UserStockShader (GLT_SHADER_POINT_LIGHT_DIFF, GLfloat mvMatrix [16], GLfloat pMatrix [16], GLfloat vLightPos[3],GLfloat vColor[4]); Parameter 1: storage shader types – point light shader parameters 2: model parameter 3:4 x4 matrix projection 4 x4 matrix parameters of 4:5 point source location parameters: diffuse color value using scene: when drawing graphics, can be applied to transform (model/projection changes), the shader drawn graphics causes shadows and lighting effects. It is very similar to the default light shader, except that the light position may be specific.

Texture substitution matrix shader GLShaderManager: : UserStockShader (GLT_SHADER_TEXTURE_REPLACE, GLfloat mvMatrix [16], GLint nTextureUnit); Parameter 1: Storage shader type – Texture replacement matrix Shader Parameter 2: model 4×4 matrix parameter 3: Texture unit Usage Scenario: When drawing graphics, transformations (model/projection changes) can be applied, which shaders project matrices through a given model view. Use the texture unit for color filling. The color of each pixel is taken from the texture

Texture adjustment shader GLShaderManager: : UserStockShader (GLT_SHADER_TEXTURE_MODULATE, GLfloat mvMatrix [16], GLfloat vColor [4], GLint nTextureUnit); Parameter 1: Store shader – Texture adjustment shader Parameter 2: model 4×4 matrix parameter 3: color value parameter 4: Texture unit Usage Scenario: When drawing graphics, transformations (model/projection changes) can be applied to this shader to project a matrix through a given model view. The shader multiplies a base color by a texture taken from the texture unit nTextureUnit and mixes the color with the texture before filling the fragment.

Light texture shaders GLShaderManager: : UserStockShader (GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF, GLfloat mvMatrix [16], GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloat vBaseColor[4],GLint nTextureUnit); Parameter 1: Store shader type – Texture light shader parameter 2: model 4×4 matrix parameter 3: projection 4×4 matrix parameter 4: point light source position parameter 5: color value (basic color of geometry) parameter 6: Texture unit Usage Scenario: When drawing graphics, transformation (model/projection change) can be applied. This shader projects a matrix through a given model view. The shader adjusts (multiplies) a texture with diffuse lighting calculations.

Basic primitives

Figure yuan describe
GL_POINT Each vertex is a separate point on the screen
GL_LINES Each pair of vertices defines a line segment
GL_LINE_STRIP Draw lines from the first vertex through each subsequent vertex
GL_LINE_LOOP Start from the first vertex through each subsequent vertex, and then connect the last vertex to the first vertex to draw a ring
GL_TRIANGLES A triangle is defined for every three vertices
GL_TRIANGLE_STRIP A group of triangles that share vertices on a strip
GL_TRIANGLE_FAN A group of triangles fan-shaped around the first point and sharing adjacent vertices

OpenGL Points/lines/triangles

GlPointSize (4.0f) glLineWidth(2.5f) OpenGL default counterclockwise polygon is front side, we can also use the code to configure: GlFrontFace (GL_CW) circled clockwise is front glFrontFace(GL_CCW) circled counterclockwise is front Why triangles are the most popular primitives in OpenGL? For GL_TRIANGLE_STRIP and GL_TRIANGLE_FAN, the first three vertices specify one triangle, and the next triangle only needs to specify a top and bottom. When a large number of triangles need to be drawn, using this method can save a lot of program code and data storage space. Second, it improves performance and saves bandwidth. Fewer vertices mean faster data transfer from memory to graphics cards, and fewer vertex shaders need to be processed.

GLBatch

GLBatch is a simple container class included with GLTools

  • Start filling the array

void GLBatch::Begin(GLenum primitive, GLuint nVerts, GLuint nTextureUnits = 0); Parameter 1: pixel parameter 2: number of vertices Parameter 3: one or two texture coordinates (optional)

  • Copy vertex data (an array of 3 x, Y,z vertices)

void GLBatch::CopyVerterxData3f(GLfloat *vVerts);

  • Copy surface normal data

void GLBatch::CopyNormalDataf(GLfloat *vNorms);

  • Copy color data

void GLBatch::CopyColorData4f(GLfoat *vColors);

  • Copy texture coordinate data

void GLBatch::CopyTexCoordData2f(GLfloat *vTex, GLuint uiTextureLayer)

  • End Data Replication

void GLBatch::End(void)

  • Drawing graphics

void GLBatch::Draw(void)

Draw rotatable points/lines/rings/pyramids/umbrellas/columns

The main function is nothing special, but notice that the normal and special keys do not go through the same callback function, rightdata GLMatrixStackThe change pipeline uses a matrix stack, and the constructor allows you to specify the maximum stack depth, which is 64 by default. The matrix heap is initialized to contain the identity matrix in the stack.

Initialize theGLMatrixStack::GLMatrixStack(int iStackDepth = 64);

Load the identity matrix at the top of the stackvoid GLMatrixStack::LoadIndentiy(void);

Load the matrix M at the top of the stackvoid GLMatrixStack::LoadMatrix(const M3DMatrix44f m);


GLFrame reference frame, which can represent the position and direction of any object in the world coordinate system. There are two coordinate systems involved: the invariable world coordinate system, and the object coordinate system for itself (namely, the drawing coordinate system). Can be used to generate model view matrices to generate position movements.

GLFrame changes are inverse changes. For example:

GLFrame objectFrame
objectFrame.MoveRight(1)
Copy the code

If you look at the code and it moves to the right by 1, what it’s really doing is it’s moving to the left by 1 on the canvas


GLFrustumProjection mode Is used to set the projection mode and obtain the projection matrix


GLGeometryTransformA pipe for geometric transformation


Render Environment Settings

Enable depth testingglEnable(GL_DEPTH_TEST);

Set up the matrix stack, matrix and projection matrix management model transformPipeline. SetMatrixStacks (modelViewMatrix projectionMatrix);

Camerafame.MoveForward(-15.0f); The observer moves back 15

Restore function

Perspective projection SettingsViewFrustum. SetPerspective (50, float (w)/float (h), 1.0 f, 500.0 f);

Spacebar click listen (spacebar value: 32)

Up, down, left and right buttons monitor

Reference frame rotationObjectFrame. RotateWorld (m3dDegToRad (10.0 f), 1.0, 0.0, 0.0); (m3dDegToRad: Angle turns radians)

Rendering function

Pressure out of the stack is to make each rendering are independent of each other, avoid the Settings affect the subsequent Settings, because OpenGL is a state machine, record every operation, so to pop down a copy of the current status, to operate with this copy, copy out the stack after operation, now in the state machine is still the state before the operation

Run a screenshot

See Github for code resources