OpenGL developers need to learn to first person that fixed line rendering process and train of thought, for after learning programmable pipeline and write GLSL shader program to build the foundation, the difference between fixed line and programmable pipeline difference only lies in the shader program, other business logic, good, explain, began to dry on the concept part

1, OpenGL fixed pipeline under a variety of storage shader introduction 2, OpenGL diagram element 3, OpenGL point/line/triangle 4, OpenGL tool class GLBatch useCopy the code

OpenGL fixed pipeline storage shader introduction


Fixed pipeline, we need to use a class GLShaderManager(shader manager), let’s learn GLShaderManager initialization

// define the global variable GLShaderManager shaderManager; / / in the OpenGL environment configuration is completed to initialize shaderManager. InitializeStockShaders ();Copy the code

After GLShaderManager initialization, let’s take a look at storage shaders in a fixed pipeline. When you use storage shaders in a fixed pipeline, you don’t need to know which work is done by vertex shaders, which work is done by chip shaders, which channel to use for passing values, You just need to learn how to use the API to pass in the required input parameters, and it does the rest. It already encapsulates the functionality.

So just to be clear, in OpenGL any fixed pipe that we use, we use this one function, different shader types, just different parameters for the shaders

shaderManager.UseStockShader(<#GLT_STOCK_SHADER nShaderID, ... # >)

Copy the code

Let’s start with shaders:

1. Unit shaders

Usage scenario: Draw a graph in the default OpenGL coordinate system (-1, 1). All the fragments of the graph are filled with one color

API function:

GLShaderManager::UserStockShader(GLT_SHADER_IDENTITY,GLfloatvColor[4]); Parameter 1: Store shader type - unit shader GLT_SHADER_IDENTITY Parameter 2: one-dimensional color array with subscript corresponding to RGBACopy the code

2. Flat shader

Use scenarios: When drawing graphs, transformations can be applied, such as affine transformations (projection/model) changes

API function:

GLShaderManager::UserStockShader(GLT_SHADER_FLAT,GLfloat mvp[16],GLfloatvColor[4]); Parameter 1: storage shader type - flat shader GLT_SHADER_FLAT Parameter 2: variable 4*4 matrix parameter 3: one-dimensional color array, subscript corresponding to RGBACopy the code

3. Coloring shaders

Use scenarios: When drawing graphics, you can apply changes (affine transformations, such as projection/model changes) to insert colors smoothly between vertices called smooth shading.

API function:

GLShaderManager::UserStockShader(GLT_SHADER_SHADED,GLfloatmvp[16]); Parameter 1: store shader type - coloring shader GLT_SHADER_SHADED parameter 2: a 4*4 matrix that allows variationCopy the code

4. Default light shader

Use scenarios: When drawing a graph, you can apply variations (affine transformations, such as projections/model variations). This shader creates shadows and lighting effects (parallel lighting) on the graph.

API function:

GLShaderManager::UserStockShader(GLT_SHADER_DEFAULT_LIGHT,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloatvColor[4]); Parameter 1: Storage shader type - default light shader GLT_SHADER_DEFAULT_LIGHT Parameter 2: model 4*4 matrix parameter 3: projection 4*4 matrix parameter 4: color value (note: the color to draw this graph, not the light color)Copy the code

5, point light shader

Use scenarios: When drawing a graph, you can apply variations (affine transformations, such as projections/model variations). This shader creates shadows and lighting effects (parallel lighting) on the graph. It is very similar to the default light shader, except that the position of the light may be specific.

API function:

GLShaderManager::UserStockShader(GLT_SHADER_POINT_LIGHT_DIEF,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloatvColor[4]); Parameter 1: Storage shader type - Point shader GLT_SHADER_POINT_LIGHT_DIEF Parameter 2: model 4*4 matrix parameter 3: projection 4*4 matrix parameter 4: position of point light source (x,y,z) because light source does not require W (singular coordinates, commonly known as scaling factor) parameter 5: diffuse color valueCopy the code

6. Texture replacement matrix shader

Usage scenario: When drawing a graph, you can use the shader change (projection/model change) to project a matrix through a given model view. Color filling is done using texture units, where the color value of each pixel point is taken from the texture.

API function:

GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_REPLACE,GLfloatmvMatrix[16],GLint nTextureUnit); Parameter 1: Store shader type - Texture replacement matrix shader GLT_SHADER_TEXTURE_REPLACE Parameter 2: model 4*4 matrix parameter 3: texture unitCopy the code

7. Texture adjustment shaders

Usage scenario: When drawing a graph, you can use the shader change (projection/model change) 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 colors the color with the texture before filling the fragment.

API function:

GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_MODULATE,GLfloat mvMatrix[16],GLfloatvColor[4],GLint nTextureUnit); Parameter 1: Store shader type - Texture adjustment shader GLT_SHADER_TEXTURE_MODULATE Parameter 2: model 4*4 matrix parameter 3: color value parameter 4: texture unitCopy the code

8. Texture light shader

Usage scenario: When drawing a graph, you can use the shader change (projection/model change) to project a matrix through a given model view. The shader adjusts (multiplies) a texture with diffuse lighting calculations.

API function:

GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF,G Lfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloatvBaseColor[4],GLint nTextureUnit); Parameter 1: Store shader type - Texture light shader GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF Parameter 2: model 4*4 matrix parameter 3: projection 4*4 matrix parameter 4: point light location parameter 5: color value (base color of geometry) parameter 6: texture unitCopy the code

OpenGL basic primients


The following table describes the seven basic primitives commonly used in OpenGL

The most popular of these is a triangle, GL_TRIANGLES, because triangles can theoretically be used to draw anything.

Here are some examples of these 6 types of diagrams (note :GL_POINTS are not drawn)

3, OpenGL point/line/triangle and triangle surround mode


1, the simplest and most commonly used method glPointSize

glPointSize(<#GLfloat size#>);Parameter 1: indicates the point size, for example, glPointSize(4.0f);Copy the code

2. Set the size range of points and the interval between points, and obtain the size range of points and the minimum step size

GLfloatSizes [2] = {8.0 2.0 f, f}; GLfloatStep = 1.0 f; glGetFloatv(GL_POINT_SIZE_RANGE, sizes); glGetFloatv(GL_POINT_SIZE_GRANULARITY, &step);Copy the code

3. Set point size by using program point size mode, which allows you to programmatically set point size in a vertex shader or geometry shader. The shader has a built-in variable gl_PointSize and can be written directly in the shader source code.

glEnable(GL_PROGRAM_POINT_SIZE);

gl_PointSize = 5.0

Copy the code

4. Set the width of line segment

glLineWidth(<#GLfloat width#>)Parameter 1: line width for example: glLineWidth(3.f);Copy the code

5. The most popular choice for OpenGL rasterization is triangles. Just three vertices can form a triangle. The type (shape) of a triangle depends on how the three vertices are connected and how their coordinates are. So, say GL_TRIANGLES in that primitive diagram

6. The way triangles surround

Refer to GL_TRIANGLES in the OpenGL diagram above

When drawing the first triangle, the lines are joined from A0->A1->A2->A0 to form a closed triangle. Triangle A0A1A2 is going counterclockwise along the vertices, and triangle A3A4A5 is going clockwise along the vertices of color. The way this order is combined with direction to specify vertices is called wrapping.

By default, OpenGL considers polygons with counterclockwise loops to be faces. So that means that triangle A0A1A2 is heads, and triangle A3A4A5 is tails.

7. Triangle belt and the advantages of triangle belt

For many surfaces or shapes, we will need to draw several connected triangles. We can save a lot of time by using the GL_TRIANGLE_STRIP primitives to draw strings of connected triangles. See GL_TRIANGLE_STRIP in the OpenGL metagraph above

Advantages of triangle belt:

Advantages of triangle belt: 1. After the first three vertices are specified for the first triangle, only one vertex needs to be specified for each subsequent triangle. When a large number of triangles need to be drawn, using this method can save a lot of program code and data storage space 2, provide computing performance and save bandwidth. Fewer vertices means faster data transfer from memory to graphics cards, and fewer vertex shaders need to process them.Copy the code

Triangular fan

For many surfaces or shapes, we will need to draw several connected triangles. In this case, we can use GL_TRIANGLE_FAN primiprimiers to draw a set of triangles connected around a central point, as shown in the OpenGL primiprimiers above

OpenGL tool class GLBatch common API use


GLBatch, commonly known as the batch class, is a simple container class included in GLTools. Common apis are as follows:

1. Set primitives, vertices and texture coordinates (texture coordinates are optional)

void GLBatch::Begain(GLeunm primitive,GLuint nVerts,GLuint nTexttureUnints = 0); Parameter 1: pixel parameter 2: number of vertices Parameter 3: one or two texture coordinates (optional)Copy the code

2, copy vertex data (a one-dimensional array consisting of 3 x, Y,z vertices)

void GLBatch::CopyVerterxData3f(GLfloat*vVerts); Parameter 1:3-component vertex array, representing (x,y,z)Copy the code

3. Copy the surface normal data

void GLBatch::CopyNormalDataf(GLfloat*vNorms); Parameter 1: normalCopy the code

4. Copy the color data

void GLBatch::CopyColorData4f(GLfloat*vColors); Parameter 1: RGBA color value arrayCopy the code

5. Copy texture coordinate data

void GLBatch::CopyTexCoordData2f(GLFloat *vTextCoords, GLuint uiTextureLayer);

Copy the code

6. End the data replication

void GLBatch::End(void);

Copy the code

7. Draw graphs

void GLBatch::Draw(void);

Copy the code

In the next section, we will use the fixed storage shader to do a variety of primitive composition implementation, that’s all for today