OpenGL rendering architecture

It mainly consists of two modules:

  • Client: Common App code and OpengL-related call API code, which is executed by CPU.
  • Server: OpenGL underlying shader rendering process, which is executed by GPU.

The architecture analysis

  • In the client, the method in OpenGL API is called by iOS code, and the data related to graphics rendering is transmitted to the vertex shader and chip shader in the Server through the channel, and then processed by GPU.
  • The Server receives the passed data through the channel with the client, and renders it to the corresponding shader, and renders the final result to the screen.

Data transfer mode

  • Attributes
  • Uniform
  • Texture Data
The name of the channel instructions Passable shader
Attributes Frequently changing data: vertex coordinates, texture coordinates, projection matrices, model matrices, etc Vertex shader
Uniform Data that do not change very often Vertex shader/slice shader
Texture Data The texture data Vertex shader/slice shader

Attributes

  • The Attributes channel can only pass data directly to the vertex shader, not directly to the slice shader, but indirectly to the slice shader through the vertex shader.
  • Attributes typically pass data that changes frequently, such as colors, vertices, and so on;
  • Attribute passes parameters like color data, vertex coordinates, texture coordinates, lighting normals, and so on.

Uniform

  • Uniform pass can be passed to both vertex shaders and slice shaders;
  • Uniform data is usually passed in batches that do not change very often.

Texture Data

  • Texture Data, like Unoform, can pass Data to vertex and slice shaders;
  • Since vertex shaders are mainly for vertex data, it doesn’t make much sense for us to pass texture data. The logic of texture processing is mainly carried out in the chip shader.

OpenGL fixed pipeline under common storage shader types and use

Storage shaders in the OpenGL fixed pipeline are not editable and can only be called. The editable shaders in OpenGL are vertex shaders and slice shaders.

Cell shader

Method of use: GLShaderManager: : UserStockShader (GLT_SHADER_IDENTITY, GLfloat vColor [4]);

  • Parameter 1: Storage shader type – cell shader;
  • Parameter 2: Color.

Use scenario: draw the graph in the default OpenGL coordinate system (-1,1). All segments of the graph are filled with one color. Unable to scale, translate, etc.

Flat shader

Method of use: GLShaderManager: : UserStockShader (GLT_SHADER_FLAT GLfloat MVP [16], GLfloat vColor [4]);

  • Parameter 1: Storage shader type – flat shader;
  • Parameter 2: a 4*4 matrix that allows variation;
  • Parameter 3: Color.

Usage scenarios: Transformations (model/projection changes) can be applied when drawing graphics. On the basis of the cell shader, matrix transformation such as scaling and translation can be carried out.

Coloring shader

Method of use: GLShaderManager: : UserStockShader (GLT_SHADER_SHADED GLfloat MVP [16]);

  • Parameter 1: Storage shader type – coloring shader;
  • Parameter 2: a 4*4 matrix that allows variation;

Use scenarios: When drawing graphics, you can apply transformations (model/projection changes) where colors are smoothly inserted between vertices, called smooth shaders.

Default light shader

Usage: GLShaderManager::UserStockShader(GLT_SHADER_DEFAULT_LIGHT,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vColor[4]);

  • Parameter 1: Storage shader type – default light shader;
  • Parameter 2: model 4*4 matrix;
  • Parameter 3: projection 4*4 matrix;
  • Parameter 4: color value.

Use scenarios: When drawing graphics, transformations can be applied (model/projection changes). This shader causes the graphics to have the effect of shadows and lighting.

Point light shader

Usage: GLShaderManager::UserStockShader(GLT_SHADER_POINT_LIGHT_DIEF,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloat vColor[4]);

  • Parameter 1: Storage shader type – point light shader;
  • Parameter 2: model 4*4 matrix;
  • Parameter 3: projection 4*4 matrix;
  • Parameter 4: position of point light source;
  • Parameter 5: color value.

Use scenarios: When drawing graphics, transformations can be applied (model/projection changes). This shader causes the graphics to have the effect of shadows and lighting. It is very similar to the default light shader, except that the light position may be specific.

Texture replacement matrix shader

Method of use: GLShaderManager: : UserStockShader (GLT_SHADER_TEXTURE_REPLACE, GLfloat mvMatrix [16], GLint nTextureUnit);

  • Parameter 1: Store shader type – Texture replacement matrix shader;
  • Parameter 2: model 4*4 matrix;
  • Parameter 3: Texture unit.

Use scenarios: When drawing graphics, transformations (model/projection changes) can be applied. This shader projects a matrix through a given model view and fills it with color using texture units, where the color of each pixel is taken from the texture.

Texture adjustment shader

Usage: GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_MODULATE,GLfloat mvMatrix[16],GLfloat vColor[4],GLint nTextureUnit);

  • Parameter 1: Store shader type – Texture adjustment shader;
  • Parameter 2: model 4*4 matrix;
  • Parameter 3: color value.
  • Parameter 3: Texture unit.

Use scenarios: When drawing graphics, transformations (model/projection changes) can be applied. This shader projects a matrix through a given model view. The shader multiplies a base color by a texture taken from a texture unit, nTextureUnit, and blends the color with the texture before filling the fragment. That is, blending textures and colors.

Texture light shader

Usage: GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF,G Lfloat 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 (base color of geometry);
  • Parameter 6: Texture unit.

Use scenarios: When drawing graphics, transformations (model/projection changes) can be applied. This shader adjusts a texture by a diffuse lighting calculation (multiplied by) using a given model view matrix and projection matrix.

OpenGL Basic primitives connection mode

Common metalinkage methods:

Figure yuan Describe the velocity
GL_POINTS Each vertex is a separate point on the screen
GL_LINES Each pair of vertices defines a line segment
GL_LINE_STRIP A line drawn from the first vertex through each successive vertex
GL_LINE_LOOP Same as GL_LINE_STRIP, but the last vertex is joined to the first.
GL_TRIANGLES Define a new triangle for every three vertices
GL_TRIANGLE_STRIP When drawing n vertices, two vertices, N-1 and n-2, are reused to form a triangle
GL_TRIANGLE_FAN When drawing n vertices, two vertices, 0 and n-1, are reused to form a triangle

Graphical representation of metalinkage:

Triangle band and triangle fan

Using GL_TRIANGLE_STRIP(triangle strip) and triangle fan (GL_TRIANGLE_FAN), you can draw a series of adjacent triangles and avoid duplicate vertices by multiplexing vertices. It saves a lot of program code and data storage space, making the program faster.