OpenGL rendering architecture
1, architecture,
- Client Refers to the OpenGL API method called in iOS. This part runs in the CPU
- Sever server, this refers to the underlying rendering processing of OpenGL. This part runs on the GPU.
- API has been called by the client to pass the relevant data of graphics rendering to the vertex shader and chip shader in the server through the channel, and then it is handed over to GPU for processing
- The server receives the transferred data through the channel with the client, and renders it to the corresponding shader, and displays the results on the screen
2. Data transmission mode
- The attributes:
1. Used to deliver frequently changing data. For example: color data, vertex coordinates, texture coordinates, lighting normals, etc. 2. The slice shader cannot be passed in directly, but can be passed in indirectly through the vertex shader
- Uniform:
1. Used to deliver data that does not change often. For example: uniform batch data, pass one thing at a time (transformation matrix) 2, can pass directly to the vertex shader, can also pass directly to the chip shader
- Texture data:
Just like UNIFORM, we can pass vertex shaders and slice shaders directly. Since vertex shaders are mainly used to process vertex data, it doesn’t make sense for us to pass texture data. And the texture processing logic is mainly in the chip shader
Ii. Projection method and API use
Orthographic projection
2. API usage:
/ / in turn into X, Y, Z axis of the minimum and maximum range value GLFrumstum: : SetOrthographic (GLfloat xMin, GLfloat xMax, GLfloat yMin, GLfloat yMax, GLfloat zMin, GLfloat zMax)Copy the code
2. Perspective projection
1, use scene: display 3D graphics effect, and far small near large effect
/ * parameters: FFov: vertical field of view Angle fAspect: aspect ratio of window width to height, High aspect ratio = width (w)/(h) fNear: nearly cut surface distance fFar: cutting surface distance * / CLFrustum: : SetPerspective (float fFov, float fAspect ,float fNear ,float fFar);Copy the code
Storage shader classification
Initialize the
// initialization of GLShaderManager GLShaderManager; shaderManager.InitializeStockShaders();Copy the code
1. Unit shaders
GLT_SHADER_IDENTITY
/* Use scenario: Draw graphs in the default OpenGL coordinate system (-1,1). All fragments of the figure are filled with one color. Parameter 1: Storage shader type - unit shader GLT_SHADER_IDENTITY Parameter 2: Color * / GLShaderManager: : UserStockShader (GLT_SHADER_IDENTITY, GLfloat vColor [4]);Copy the code
2. Flat shader
GLT_SHADER_FLAT
Parameter 1: Storage shader type - flat shader GLT_SHADER_FLAT Parameter 2:4*4 matrix parameter 3: Color * / GLShaderManager: : UserStockShader (GLT_SHADER_FLAT GLfloat MVP [16], GLfloat vColor [4]);Copy the code
3. Coloring shaders
GLT_SHADER_SHADED
/* Use scenarios: When drawing graphics, transformations can be applied (model/projection changes), and colors will be smoothly inserted between vertices called smooth shading. Parameter 1: Store shader type - coloring shader GLT_SHADER_SHADED parameter 2: shaded on Allow the change of 4 * 4 matrix * / GLShaderManager: : UserStockShader (GLT_SHADER_SHADED GLfloat MVP [16]);Copy the code
4. Default light shader
GLT_SHADER_DEFAULT_LIGHT
/* Use scenarios: When drawing graphics, you can apply transformations (model/projection changes). This shader will create shadows and light effects on the drawn graphics. 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 * / GLShaderManager: : UserStockShader (GLT_SHADER_DEFAULT_LIGHT, GLfloat mvMatrix [16], GLfloat pMatrix [16], GLfloat vColor[4]);Copy the code
5, point light shader
GLT_SHADER_POINT_LIGHT_DIEF
/* Use scenarios: When drawing graphics, you can apply transformations (model/projection changes). This shader will create shadows and light effects on the drawn graphics. It is very similar to the default light shader, except that the light position may be specific. Parameter 1: Storage shader type - Point light shader GLT_SHADER_POINT_LIGHT_DIEF Parameter 2: model 4*4 matrix parameter 3: projection 4*4 matrix parameter 4: point light position parameter 5: Color value * / GLShaderManager: : UserStockShader (GLT_SHADER_POINT_LIGHT_DIEF, GLfloat mvMatrix [16], GLfloat pMatrix [16], GLfloat vLightPos[3],GLfloat vColor[4]);Copy the code
6. Texture replacement matrix shader
GLT_SHADER_TEXTURE_REPLACE
/* Use scenario: When drawing a graph, you can apply the transform (model/projection change) shader to project a matrix through a given modal view. Use the texture unit for color filling. Where the color of each pixel is obtained from the texture parameter 1: Store shader type - texture replacement matrix shader GLT_SHADER_TEXTURE_REPLACE Parameter 2: model 4*4 matrix parameter 3: Texture unit * / GLShaderManager: : UserStockShader (GLT_SHADER_TEXTURE_REPLACE, GLfloat mvMatrix [16], GLint nTextureUnit);Copy the code
7. Texture adjustment shaders
GLT_SHADER_TEXTURE_MODULATE
/* Use scenario: When drawing a graph, you can apply the transform (model/projection change) shader to project a matrix through a given modal view. The shader multiplies a base color by a texture taken from the texture unit nTextureUnit. Blend the color with the texture before filling the fragment. 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 unit * / GLShaderManager: : UserStockShader (GLT_SHADER_TEXTURE_MODULATE, GLfloat mvMatrix [16], GLfloat vColor [4], GLint nTextureUnit);Copy the code
8. Texture light shader
GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF
/* Use scenario: When drawing a graph, you can apply the transform (model/projection change) shader to project a matrix through a given modular logic educational view. 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 source position parameter 5: color value (basic color of geometric figure) Parameter 6: Texture unit * / GLShaderManager: : UserStockShader (GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF, G Lfloat mvMatrix [16], GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloat vBaseColor[4],GLint nTextureUnit);Copy the code
There are also programmable pipeline shaders: vertex shaders, slice shaders, and subdivision shaders
4. Common primitives connection mode
1, classification,
2. Dots and lines
GlPointSize (4.0f); GLfloat sizes[2] = {2.0f,4.0f}; //2. GLfloat step = 1.0 f; GlGetFloatv (GL_POINT_SIZE_RANGE,sizes); glGetFloatv(GL_POINT_GRAULARITY ,&step); GlEnable (GL_PROGRAM_POINT_SIZE); //4. //5. This mode allows us to programmatically set point sizes in vertex shaders or geometry shaders. Gl_PointSize = 5.0 //6; gl_PointSize = 5.0 //6; Set line width glLineWidth(2.5f)Copy the code
3. Triangles
The most popular form of rasterization is triangles. Three vertices make a triangle. Not all triangles are equilateral. There are heads and tails.
When I draw the first triangle, the line goes from V0 minus V1 to V2. And then finally back to a closed triangle of V0. This is going clockwise along the vertices. The way this order is combined with direction to specify vertices is called wrapping. In OpenGL, a counterclockwise circling polygon is a face. Reverse clockwise is reverse
/* GL_CW: tells OpenGL that the clockwise polygon is front; GL_CCW: tells OpenGL to wrap a counterclockwise polygon on the front. Default is GL_CW */ glFrontFace(GL_CW);Copy the code
4. Triangle belt
For many surfaces or shapes, we will need to draw several connected triangles. This allows us to save a lot of time by using the GL_TRIANGLE_STRIP primitives to draw a series of connected triangles
Advantages: 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.
5. Triangle fan
For many surfaces or shapes, we will need to draw several connected triangles. This is where we can use the GL_TRIANGLE_FAN element to draw a set of triangles connected around a central point
OpenGL tool class GLBatch
GLBatch, is a simple container class included in GLTools
/* Parameter 1: pixel parameter 2: number of vertices parameter 3: */ void GLBatch::Begain(GLeunm Primitive, GLuint nVerts, GLuint nTexttureUnints = 0) / / copy the vertex data (a weight from 3 x, y, z of vertex arrays) void GLBatch: : CopyVerterxData3f (GLfloat * vVerts); Void GLBatch::CopyNormalDataf(GLfloat *vNorms); Void GLBatch::CopyColorData4f(GLfloat *vColors); / / copy texture coordinates data void GLBatch: : CopyTexCoordData2f (GLFloat * vTextCoords, GLuint uiTextureLayer); // End data replication void GLBatch::End(void); Void GLBatch::Draw(void);Copy the code