OpenGL projection
Projection method | Usage scenarios | Method statement |
---|---|---|
Orthographic projection | Display 2D graphics, all of the same size | GLFrustum::SetOrthographic(GLfloat xMin, GLfloat xMax, GLfloat yMin, GLfloat yMax, GLfloat zMin, GLfloat zMax) |
3 d projection | Display 3D graphics, far small and near large | GLFrustum::SetPerspective(float fFov , float fAspect ,float fNear ,float fFar) Parameters: FFov: Vertical field of view Angle FAspect: the aspect ratio of the width of a window to its height FNear: indicates the distance near the clipping surface FFar: distance from far clipping surface Aspect ratio = Width (W)/ Height (h) |
Fixed shader classification
The name of the | Enumerated type | methods | parameter | Usage scenarios |
---|---|---|---|---|
Cell shader | GLT_SHADER_IDENTITY |
GLShaderManager::UserStockShader(GLT_SHADER_IDENTITY,GLfloat vColor[4]); |
Parameter 1: Storage shader type – unit shader Parameter 2: Color |
Draw the defaultOpenGL The graph in frame (-1, 1), all the segments of the graph are filled with one color |
Flat shader | GLT_SHADER_FLAT |
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 |
When drawing, you can apply transformations (model/projection transformations) |
Coloring shader | GLT_SHADER_SHADED |
GLShaderManager::UserStockShader(GLT_SHADER_SHADED,GLfloat mvp[16]); |
Parameter 1: Storage shader type – Coloring shader Parameter 2: a 4*4 matrix that allows variation |
When drawing, a transformation (model/projection transform) can be applied to smooth the insertion of colors between vertices called smooth shading |
Default light shader | GLT_SHADER_DEFAULT_LIGHT |
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 |
When drawing, you can apply a transform (model/projection transform), a shader that creates a shadow and light effect on the drawing |
Point light shader | GLT_SHADER_POINT_LIGHT_DIEF |
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 |
When drawing, you can apply a transform (model/projection transform). This shader creates a shadow and light effect on the drawing, similar to the default light shader, except that you can set the position of the light source |
Texture replacement matrix shader | GLT_SHADER_TEXTURE_REPLACE |
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 |
When drawing, transformations can be applied (model/projection transformations). This shader can be filled with texture units by projecting a matrix to the model view, where the color of each pixel is taken from the texture |
Texture adjustment shader | GLT_SHADER_TEXTURE_MODULATE |
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 4: Texture unit |
When drawing, transformations can be applied (model/projection transformations). This shader projects a matrix through the given model view. The shader multiplies a base color by a unit taken from the texturenTextureUnit Blend the color with the texture to fill the fragment |
Texture light shader | GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF |
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: position of point light source Parameter 5: Color value (base color of geometry) Parameter 6: Texture unit |
When drawing, transformations can be applied (model/projection transformations). This shader can project a matrix to the model view, and the shader adjusts a texture by a diffuse lighting calculation (multiplied). |
Common primitives connection modes
Figure yuan | The name of the | describe |
---|---|---|
GL_POINTS |
point | Each vertex is a separate point on the screen |
GL_LINES |
line | Each pair of vertices defines a line segment |
GL_LINE_STRIP |
line | A line drawn from the first vertex through subsequent vertices |
GL_LINE_LOOP |
Line ring | andGL_LINE_LOOP Same thing, but the last vertex is connected to the first vertex |
GL_TRIANGLES |
triangle | A triangle is defined for every three vertices |
GL_TRIANGLE_STRIP |
Triangle strip | Share one waiting for (strip A group of triangles with vertices above |
GL_TRIANGLE_FAN |
Triangle ring | A group of triangles fan-shaped around a dot sharing adjacent vertices |
Basic primitives:
Triangular wrap
OpenGL
By default, the one with counterclockwise direction is the front- You can change the front and back in code
glFrontFace(GL_CW); GL_CW: tells OpenGL that the clockwise polygon is the front side; GL_CCW: tells OpenGL that polygons circling counterclockwise are facesCopy the code
The advantage of the triangle belt
- After specifying the first triangle with the first three vertices, you only need to specify one vertex 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
- Provides program performance, saves bandwidth, and fewer vertices means data is transferred from memory to vertex shaders faster and less processing is done by vertex shaders
Utility class GLBatch
/* Parameter 1: primitive parameter 2: number of vertices parameter 3: one or two texture coordinates (optional) */ void GLBatch::Begain(GLeunm primitive,GLuint nVerts,GLuint nTexttureUnints = 0); / / copy the vertex data (a component quantity from 3 x, y, z of vertex arrays) void GLBatch: : CopyVerterxData3f (GLfloat*vVerts); Void GLBatch::CopyNormalDataf(GLfloat*vNorms); // Copy color data 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