OpenGL rendering architecture parsing
Vertex shader
Chip shader

Use storage shaders

To use storage shaders, we must recognize a utility class, GLTools. This is a C++ utility class that has a GLShaderManager to manage all the storage shaders that meet the basic requirements for normal rendering. GLShaderManager must be initialized before use:

shaderManager.InitializeStockShaders();
Copy the code

attribute

OpenGL supports up to 16 different types of parameters that can be set for each vertex. These parameters are numbered from 0 to 15, and can be associated with any specified variable in the vertex shader, which stores the shader for each variable using consistent internal variable naming rules and the same attribute values. It lists these attributes:

identifier describe
GLT _ ATTIBUTE _ VERTEX Vertex positions of 3 components (x, y, z)
GLT _ ATTIBUTE _ COLOR 4 components (R, G, B, A) color values
GLT _ ATTIBUTE _ NORMAL 3-component (x, y, z) surface normals
GLT _ ATTIBUTE _ TEXTURE0 First pair of 2-component (s, t) texture coordinates
GLT _ ATTIBUTE _ TEXTURE1 The second pair of 2-component (S, t) texture coordinates

Uniform values

I’m sure you’re familiar with this value, which we mentioned in the last article. To render the geometry, we need to submit a property matrix for the object, but first bind it to the shader program we want to use and supply the Uniform values of the program. The GLShaderManager class does this for us. The UseStockShader function selects a store shader and provides a Uniform value for that shader. This can be done in a single call:

GLShaderManager::UseStockShader(GLenum shader, ... ...). ;Copy the code

This function extracts the correct arguments from the stack based on the shader we choose, which are the Uniform values required by the particular shader.

Store shader classification

1. Unit shader/unit shader

The unit shader simply uses the default Cartesian coordinate system. All segments are in the same color and the geometry is solid and unrendered. This shader uses only one property, GLT_ATTIBUTE_VERTEX. The vColor parameter contains the requested color.

/ / a: shader property / / parameter 2: the color of the need to GLShaderManager: : UseStockShader (GLT_SHADER_IDENTITY, GLfloat vColor[4]);
Copy the code

2. Flat shader

The flat shader extends the uniform shader to allow a 4 * 4 transformation matrix to be specified for geometric transformations. Typically this is a left-multiplied model view matrix and projection matrix, also known as “model view projection matrix”. This shader simply uses a single property, GLT_ATTIBUTE_VERTEX.

/ / a: shader property / / parameter 2: / / allow the change of 4 * 4 matrix parameters 3: color GLShaderManager: : UseStockShader (GLT_SHADER_FLAT, GLfloat mvp[16], GLfloat vColor[4]);
Copy the code

3, Shaded shader

The only Uniform value of this shader is the transformation matrix applied to the geometry. Both GLT_ATTIBUTE_VERTEX and GLT_ATTIBUTE_COLOR are used in this shader. The color values are smoothly inserted between vertices (this is called smooth shading).

/ / a: shader property / / parameter 2: allow the change of 4 * 4 matrix GLShaderManager: : UseStockShader (GLT_SHADER_SHADED, GLfloat mvp[16]);
Copy the code

4. Default light shader

In plain English, this shader is the effect produced by emitting a parallel light from the direction of the observer. Causes the object to have a shadow and light effect. Uniform values such as the model view matrix, the projection matrix, and the color value as the base color are required. The required attributes are GLT_ATTIBUTE_VERTEX and GLT_ATTIBUTE_NORMAL.

/ / a: shader property / / parameter 2: / / three parameters: model view matrix projection matrix / / four parameters: the color value GLShaderManager: : UseStockShader (GLT_DEFAULT_LIGHT, GLfloat mvMatrix[16], GLfloat pMatrix[16], GLfloat vColor[4]);
Copy the code

5, point light shader

This shader is similar to the default light shader, but the light position of the point light shader is specific. This shader accepts four Uniform values, namely, the model view matrix, the projection matrix, the position of the light source in the view coordinate system, and the basic diffuse color of the object. The required attributes are GLT_ATTIBUTE_VERTEX and GLT_ATTIBUTE_NORMAL.

// Parameter one: shader properties // Parameter two: model view matrix // Parameter three: projection matrix // Parameter four: viewpoint coordinates light source position // parameter five: Color value GLShaderManager: : UseStockShader (GLT_SHADER_POINT_LIGHT_DIFF, GLfloat mvMatrix[16], GLfloat pMatrix[16],GLfloat vLightPos[3], GLfloat vColor[4]);
Copy the code

6. Texture replacement matrix

The shader transforms the geometry using textures bound to the texture unit specified by nTextureUnit by projecting a matrix from the given model view. The fragment color is obtained directly from the texture sample. The required attributes are GLT_ATTIBUTE_VERTEX and GLT_ATTIBUTE_NORMAL.

/ / a: shader property 2: / / parameter projection matrix / / parameter 3: need to multiply texture GLShaderManager: : UseStockShader (GLT_SHADER_TEXTURE_REPLACE, GLfloat mvpMatrix[16], GLint nTextureUnit);
Copy the code

7. Texture adjustment shaders

This shader multiplies a base color by a texture taken from the TextureUnit of the TextureUnit. The required properties are GLT_ATTIBUTE_VERTEX and GLT_ATTIBUTE_TEXTURE0.

/ / a: shader property 2: / / parameter projection matrix / / three parameters: color / / four parameters: need to multiply the texture GLShaderManager: : UseStockShader (GLT_SHADER_TEXTURE_MODULATE, GLfloat mvpMatrix[16], GLfloat vColor, GLint nTextureUnit);
Copy the code

8. Texture light shader

This shader adjusts (multiplies) a texture through a diffuse lighting calculation, where the light is given in the visual space. The shader accepts five Uniform values: the model view matrix, the projection matrix, the position of the light source in the visual space, the base color of the geometry, and the texture unit to be used. The required attributes are GLT_ATTIBUTE_VERTEX, GLT_ATTIBUTE_TEXTURE0, and GLT_ATTIBUTE_NORMAL.

// Parameter 1: shader properties // Parameter 2: projection matrix // Parameter 3: position of light source in view space // Parameter 4: basic color of geometry // Parameter 5: Going to use the texture unit GLShaderManager: : UseStockShader (GLT_SHADER_TEXTURE_POINT_LIGHT_DEFF, GLfloat mvMatrix, GLfloat mvpMatrix[16],GLfloat vLightPos[3],GLfloat vBaseColor[4], GLint nTextureUnit);
Copy the code