Rendering process and store shaders in OpenGL ES Primer trip --OpenGL

In the previous coordinate system of OpenGL last part, we have to understand the shader rendering process. Here's to learn about basic graphics rendering pipeline.

OpenGL graphics rendering pipeline foundation

In OpenGL, whether using programmable pipeline, or the use of fixed lines, its rendering pipeline process is the same. First we look at the rendering pipeline flow:

1. The client - server

Pipeline is divided into two parts, the upper part of the Client is a client, the lower half is the Server service ends. The client is not here we say in the development of the usual iOS client, Android client. Here the client terminal refers to the code stored in the CPU, such as the OpenGL API and C / C ++ code. Here's Server-side call the GPU chip, and then continue from the client data (color) passed to the server for execution.

Professional interpreted as : the client is stored in the CPU memory, and executes the application, or YES in the main memory of the system driver. Drivers will combine rendering commands and data sent to the server YES. The client and server on the function is asynchronous. They are independent piece of software or hardware block. Client does not continue command block and a data block together conveyed to the buffer, the buffer will be sent to the server and then YES.

2. shader

Using the target mark is written in GLSL program that looks very similar to the C language The language comes in handy. To be discolored from the source code is compiled and linked together. Finally ready with discolored program. Four kinds of shaders: vertex shader (necessary), subdivision shader (optional), geometry shader (optional), fragment shader (necessary). Vertex shader and fragment shader program is an essential part of every OpenGL, following highlights it.

Vertex shader : START input process data from the client, to apply it with the transform, for other types of mathematical operations to calculate the effect of care, displacement of colors in value, etc., when rendering a few vertices, the vertex shader executes several times, and each point is performed on the same time.

Fragment shader : This is the last portion of the pipeline for processing individual fragments generated after rasterization OpenGL, which in this stage, a remote computing depth values and color, and then passed to the tile test pipeline and mixing module . If the vertex shader determines what should be the position of a primitive of the screen, then the fragment shader is to determine the color of the fragment.

3. The three kinds of transfer channels to the OpenGL rendering data shader

Attributes property : used to represent a vertex data, and this data may be constantly changing, such as: color data, vertex data, texture coordinates, normal light and the like; and the attribute may be a floating point, integer, Boolean, etc. Wait;

In fact, the vertex position itself is a property, and that property is always stored internally in the form of four-dimensional vectors, even if we do not Using all four components. ⼀ vertices may be stored position (x, y, z), the four components occupy three.

Uniform : Uniform variable is passed to the external variable vertex shader program and fragment shaders. uniform incoming data is a relatively uniform, for primitive (primitives: OpenGL primitives in only a predefined set of vertices joined together nothing manner.) batch process, rather than each vertex change once. For example: transformation matrix, illumination parameters and color space conversion information in vertex shaders.

Uniform variable restriction can actually be used many times, provided a single color value applied to the entire surface, you can also set a time value. Modify it at each vertex animation rendering some type.

Texture data Texture Data : Texture most common decorative effect our object models, such that the surface has a pattern. But in OpenGL, not only the effect of the texture on a graphics performance, it can also be used to store large amounts of data, a typical example is the use of the texture store topographical information.

Attributes property data can be directly transferred to the vertex shader, not directly transmitted to the fragment shader can be transferred indirectly to the fragment shader by vertex shader (Attributes on the design in the design would be used only to vertex shader) . Uniform data can be directly transferred to the vertex shader and fragment shader. Texture Data texture data also may be transferred directly to the vertex shader and fragment shader (vertex significantly discolored, a still picture membered the discolored device both can perform while sampling and filtering the texture data.) However, under normal circumstances, texture data into vertex shader unit is not necessary. (However, according to a scene of view, if there is a scenario is the need to enlarge the texture coordinates, then this can be done is a vertex shader may be done fragment shader.)

4. stored shaders

In previous versions of OpenGL 3.0, OpenGL comprises a fixed line, it can process the geometry data without using the pixel shader environment. Fixed line removed from the version 3.1 kernel mode from the start, so we have to use the shader to complete the work. Modern OpenGL rendering pipeline relies heavily on shader to handle incoming data, we usually use GLSL (OpenGL Shading Language) to write shader programs, GLSL syntax similar to the C language, GLSL compiler after running on the GPU side. Here we have first to introduce shaders

Shader 4.1 storage

Store shader may be a C ++ class GLShaderManager GLTools management function UseStockShader GLShaderManager in will choose a memory shader and provide this Uniform values ​​shader, this function is completed thing is further processed to the vertex data, such as setting vertex color, intensity settings vertex lighting is provided vertex texture like.

Use memory shader 4.2

(1) GLShaderManager initialization

GLShaderManager  shaderManager;
shaderManager.InitializeStockShaders();
复制代码

(2) GLShaderManager properties

Discolored internal variable is stored for each color is variable ⼀ use ⼀ consistent naming rules and attributes same groove. Under uniform value (3) GLShanderManager generally the case, the geometry rendering, we need to submit to an object attribute matrix, therefore especially must first bind the shaders we want to make use of Use, and provide uniform value of the program. But GLShanderManager class temporarily to work for us, the use of useStockShader function, select a shader and memory offer the value Uniform storage shaders.

GLShaderManager::UserStockShader(GLeunm shader...);
复制代码

4.3 shader unit (the Identity)

GLShaderManager::UserStockShader(GLT_ATTRIBUTE_VERTEX,GLfloat vColor[4]);
复制代码

The control unit discolored : simply use the default Cartesian coordinate system (a coordinate range (-1.0, 1.0)). All segments are of the same color application, to get accustomed to what pattern and real Center Weighted unrendered. Needs to be set is stored discolored ⼀ attributes: GLT_ATTRIBUTE_VERTEX (vertex component) vColor [4]: indicates the color to be set

4.4 shader plane (Flat)

GLShaderManager::UserStockShader(GLT_SHADER_FLAT,GLfloat mvp[16],GLfloat vColor[4]);
复制代码

Shader plane : the plane of the unified shader shader rendering, allowing to specify the transform matrix is a 4x4 transformation geometry, often called "model-view projection matrix"; in drawing graphics, may be applied (Model / projective transformation Variety). Parameters GLT_SHADER_FLAT: a plan shader parameter GLfloat mvp [16]: 4x4 matrix representation allows variation parameter GLfloat vColor [4]: indicates the color to be set

Colored shader 4.5 (Shaded)

GLShaderManager::UserStockShader(GLT_SHADER_SHADED,GLfloat mvp[16]);
复制代码

Color shader : get accustomed transformation matrix applied in any pattern. To set stored discolored's GLT_ATTRIBUTE_VERTEX (vertex component) and GLT_ATTRIBUTE_COLOR (components of colors in) 2 properties. Of colors in value to be smoothly inserted (called smooth shading) between vertices parameters GLT_SHADER_SHADED: shader color parameter indicates GLfloat mvp [16]: indicates the color to be set

4.6 default shader light (Light)

GLShaderManager::UserStockShader(GLT_SHADER_DEFAULT_LIGHT,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vColor[4]);
复制代码

Default shader source : Default is discolored with a light source, will generate graphics rendering and shading effects of care. To set stored discolored's GLT_ATTRIBUTE_VERTEX (vertex component) and GLT_ATTRIBUTE_NORMAL (surface normal) may be thought of as the default light source the sun parameters GLT_SHADER_DEFAULT_LIGHT: Shader parameter indicates default GLfloat mvMatrix [16]: 4x4 model-view matrix parameters GLfloat pMatrix [16 ]: 4x4 projection matrix parameters GLfloat vColor [4]: color value

4.7 the point light source is discolored (Light_Dife)

GLShaderManager::UserStockShader(GLT_SHADER_DEFAULT_LIGHT_DIEF,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloat vColor[4]);
复制代码

Shader point source : the point light source and a default discolored significantly discolored is very similar, except that: the light source position is specified. The same pattern will draw a shadow and lighting effects, also need to set the stored discolored's GLT_ATTRIBUTE_VERTEX (vertex component) and GLT_ATTRIBUTE_NORMAL (Table ⾯ normal). Parameters GLT_SHADER_DEFAULT_LIGHT_DIEF: indicates the point light source with the discolored color parameter GLfloat mvMatrix [16]: model-view matrix 4x4 Parameter GLfloat pMatrix [16]: the projection matrix 4x4 parameter GLfloat vLightPos [3]: a viewpoint coordinate light source position parameter GLfloat vColor [4]: Man values of colors in reflected

4.8 texture shader substitution matrix (the Replace)

GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_REPLACE,GLfloat mvMatrix[16],GLint nTextureUnit);
复制代码

Discolored by the given model view projections matrix, bind to the texture Use nTextureUnit (texture elements) specified texture unit changes the geometry. Release segment color: direct access direct from the texture sample. To set stored discolored's GLT_ATTRIBUTE_VERTEX (vertex component) and GLT_ATTRIBUTE_NORMAL (surface normal) parameters GLT_SHADER_TEXTURE_REPLACE: substitution matrix texture shader parameter GLfloat mvMatrix [16]: 4x4 model-view matrix parameters GLint nTextureUnit: texture unit

4.9 adjust the texture is discolored

GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_MODULATE,G
Lfloat mvMatrix[16],GLfloat vColor[4],GLint nTextureUnit);
复制代码

The texture is adjusted (multiplied) by the diffuse reflection illumination computer. The position of the light in the visual space is given. To set stored discolored's GLT_ATTRIBUTE_VERTEX (vertex component) and GLT_ATTRIBUTE_TEXTURE0 (texture coordinates), GLT_ATTRIBUTE_NORMAL (⾯ normal table) parameters GLT_SHADER_TEXTURE_MODULATE: the texture adjustment parameters discolored GLfloat mvMatrix [16]: 4x4 model-view matrix parameters GLfloat vColor [4 ]: diffuse reflectance value of the parameter's color GLint nTextureUnit: texture unit

The source texture is discolored 4.10

GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloat vBaseColor[4],GLint nTextureUnit);
复制代码

This texture shader set remains adjusted (multiplied) by the diffuse reflection illumination computer. The position of the light in the visual space is given. To set stored discolored's GLT_ATTRIBUTE_VERTEX (vertex component) and GLT_ATTRIBUTE_TEXTURE0 (texture coordinates), GLT_ATTRIBUTE_NORMAL (⾯ normal table) parameters GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF: indicates the source texture parameter discolored GLfloat mvMatrix [16]: 4x4 matrix model parameters GLfloat pMatrix [16 ]: 4x4 projection matrix parameters GLfloat vLightPos [3]: visual spatial light midpoint position parameter GLfloat vBaseColor [4]: ​​color value (geometric basic colors) parameters GLint nTextureUnit: texture unit

Reproduced in: https: //juejin.im/post/5d0adfff6fb9a07ea567f420

Guess you like

Origin blog.csdn.net/weixin_34233679/article/details/93182335