Storage shader
Storage shaders are managed by GLTools’s C++ class, GLShaderManager, for basic rendering needs.
shaderManager.InitializeStockShaders(); GLShaderManager:: UserStockShader(xxx, xxxx, ...) ; Unified approach to creating shaders, by passing in different parameters, you can create different shadersCopy the code
1. Identity shader
GLShaderManager:: UserStockShader(GLT_SHADER_IDENTITY, GLfloatvColor[4]); Parameter 1 GLT_SHADER_IDENTITY: Store shader type - unit shader parameter 2 GLfloatVColor [4] : color valueCopy the code
- Use the default Cartesian coordinate system (range -1.0 to 1.0 on all axes).
- Apply the same color to all fragments.
- The geometry is solid and unrendered.
- Use only one attribute
GLT_ATTRIBUTE_VERTEX
.
2. Flat shaders
GLShaderManager:: UserStockShader(GLT_SHADER_FLAT,GLfloat mvp[16], GLfloatvColor[4]); Parameter 1 GLT_SHADER_FLAT: Storage shader type - Flat shader parameter 2 GLfloatMVP [16]: allowable variable 4*4 matrix parameter 3 GLfloatVColor [4] : color valueCopy the code
- Make one for geometric transformation
4x4
Transformation matrix. It is usually left multiplied by the model view matrix and projection matrix, known as the “model view projection matrix”. - Use only one attribute
GLT_ATTRIBUTE_VERTEX
.
3. Shaded shader
GLShaderManager:: UserStockShader(GLT_SHADER_SHADED,GLfloatmvp[16]); Parameter 1 GLT_SHADER_FLAT: Storage shader type - Flat shader parameter 2 GLfloatMVP [16]: a 4 by 4 matrix that allows variationCopy the code
- When drawing graphics, transformations (model/projection changes) can be applied, and color values are smoothly inserted between vertices called smooth shading.
- use
GLT_ATTRIBUTE_VERTEX
andGLT_ATTRIBUTE_COLOR
.
4. Default light shader
GLShaderManager:: UserStockShader(GLT_SHADER_DEFAULT_LIGHT, GLfloat mvMatrix[16], GLfloat pMatrix[16], GLfloatvColor[4]); Parameter 1: Storage shader type - Default light shader Parameter 2: model 4*4 matrix parameter 3: projection 4*4 matrix parameter 4: color valueCopy the code
- An effect similar to that produced by monodiffused light from the observer’s position, causing the object to have the effect of shadow and illumination.
- use
GLT_ATTRIBUTE_VERTEX
andGLT_ATTRIBUTE_NORMAL
5. Point light shader
GLShaderManager::UserStockShader(GLT_SHADER_POINT_LIGHT_DIEF,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloatvColor[4]); Parameter 1: Storage shader type - Point light shader Parameter 2: model 4*4 matrix parameter 3: projection 4*4 matrix parameter 4: point light location parameter 5: diffuse color valueCopy the code
- Similar to the default light shader, but the light can be specific.
- use
GLT_ATTRIBUTE_VERTEX
andGLT_ATTRIBUTE_NORMAL
6. Texture replacement shaders
GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_REPLACE,GLfloatmvpMatrix[16],GLint nTextureUnit); Parameter 1: Store shader type - Texture replacement shader Parameter 2: model view projection matrix parameter 3: texture unitCopy the code
- Transforms the geometry using the specified texture. The fragment color is obtained directly from the texture sample
- use
GLT_ATTRIBUTE_VERTEX
andGLT_ATTRIBUTE_NORMAL
7. Texture adjustment shaders
GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_MODULATE,GLfloat mvpMatrix[16],GLfloatvColor, GLint nTextureUnit); Parameter 1: Store shader type - Texture adjustment shader Parameter 2: model view projection matrix parameter 3: Basic color parameter 4: texture unitCopy the code
- Base color multiplied by one taken from the texture unit
nTextureUnit
The texture. - use
GLT_ATTRIBUTE_VERTEX
andGLT_ATTRIBUTE_TEXTURE0
8. Texture light shader
GLShaderManager::UserStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF,GLfloat mvMatrix[16],GLfloat pMatrix[16],GLfloat vLightPos[3],GLfloatvBaseColor[4],GLint nTextureUnit); Parameter 1: Store shader type - Texture light shader parameter 2: model view 4*4 matrix parameter 3: projection 4*4 matrix parameter 4: point light position parameter 5: color value (basic color of geometry) parameter 6: texture unitCopy the code
- Computationally adjust a texture with diffuse illumination
- The position of the ray is given
- use
GLT_ATTRIBUTE_VERTEX
,GLT_ATTRIBUTE_TEXTURE0
andGLT_ATTRIBUTE_NORMAL
.
GLBatch
The GLTools library contains a simple container class, GLBatch. This class can be used as a container for simple batches of the seven primiples listed below, and it knows how to render primiples using any storage shader supported by GLShaderManager. The purpose of this class is to accommodate a list of vertices and render them as a batch of primitives of a particular type.
GLBatch
Batch.begin (GL_TRIANGLE_FAN, 4); // Specify the primitive type, number of vertices, and (optionally) one or two texture coordinates. // Copy at least one array of three (x, y, z) vertices. batch.CopyVertexData3f(vVerts); //end to indicate that data replication is complete. batch.End(); / / the last when we select the appropriate storage shader can invoke the Draw function to apply colours to a drawing shaderManager. UseStockShader (GLT_SHADER_IDENTITY vRedColor); batch.Draw();Copy the code
GLTriangleBatch
After GLBatch, we can also use the triangle batch class GLTriangleBatch. Features are as follows:
- A container for triangles
- Each vertex can have a surface normal for lighting calculations and texture coordinates
- Organize triangles in a more efficient way (indexed vertices arrays) and actually store polygons on a graphics card (using fixed-point buffer objects).
Related apis used:
- Create a sphere
gltMakeSphere(GLTriangleBatch& sphereBatch, GLfloatfRadius, GLint iSlices, GLint iStacks); Parameter 1: sphereBatch, triangle batch class object parameter 2: fRadius, sphere radius parameter 3: iSlices, the number of triangle strips stacked from the bottom of the sphere to the top; 4: iStacks, the logarithm of triangles arranged around a circle of spheres suggests that the number of fragments of a sphere with good symmetry is twice the number of stacks, that is, iStacks = 2 * iSlices; The spheres are drawn around the z-axis so that + Z is the vertex of the sphere and -z is the bottom of the sphere.Copy the code
gltMakeTorus(GLTriangleBatch& torusBatch, GLfloat majorRadius, GLfloatminorRadius, GLint numMajor, GLint numMinor); Parameter 1: torusBatch, triangle batch class object parameter 2: majorRadius, the radius from the center of the doughnut to the outer edge parameter 3: minorRadius, the radius from the center of the doughnut to the inner edge parameter 4: numMajor, the number of triangles along the main radius parameter 5: NumMinor, the number of triangles along the smaller radius insideCopy the code
void gltMakeCylinder(GLTriangleBatch& cylinderBatch, GLfloat baseRadius, GLfloat topRadius, GLfloatfLength, GLint numSlices, GLint numStacks); Parameter 1: cylinderBatch, triangle batch class object parameter 2: baseRadius, baseRadius parameter 3: topRadius, head radius parameter 4: fLength, circular length parameter 5: numSlices, number of triangle pairs around the z-axis parameter 6: NumStacks, the number of triangles that stack the bottom of a cylinder into the top ringCopy the code
0
void gltMakeDisk(GLTriangleBatch& diskBatch, GLfloat innerRadius, GLfloatouterRadius, GLint nSlices, GLint nStacks); Parameter 1:diskBatch, triangle batch class object parameter 2:innerRadius, innerRadius parameter 3:outerRadius, outerRadius parameter 4:nSlices, number of triangle pairs around the Z axis of the disk 5:nStacks, number of triangles from the outer network to the inner circle of the diskCopy the code
0
GLFrustum
Viewport class, used to create a projection matrix.
// orthographic void SetOrthographic(GLfloat xMin, GLfloat xMax, GLfloat yMin, GLfloat yMax, GLfloat zMin, GLfloatZMax) / / perspective projection CLFrustum: : SetPerspective (float fFov , float fAspect ,float fNear ,floatfFar); Parameter: fFov: vertical field of view Angle fAspect: aspect ratio of window width to height fNear: distance from near clipping surface fFar: Distance from far clipping surface ASPECT ratio = Width (W)/ Height (h)Copy the code
Matrix stack GLMatrixStack
The GLMatrixStack class is a helper class provided by GLTools to help developers create and manipulate matrix multiplications. GLMatrixStack is a stack structure that provides stack-related methods for developers to call.
Related apis are as follows:
GLMatrixStack: : GLMatrixStack (int iStackDepth = 64) it is important to note: 1. GLMatrixStack::void LoadIdentity(void); GLMatrixStack::void LoadMatrix(const) Void MultMatrix(const M3DMatrix44f mMatrix) multiply a matrix by the matrix at the top of the stack, The result of the multiplication is then stored at the top of the stack without increasing the total number of matrix stacks const m3dMatrix44f&glMatrixStack ::GetMatrix(void) gets the top matrixCopy the code
Push and push
The real value of matrices is in storing a state by pushing the stack and then restoring that state by pushing the stack.
Void PushMatrix(const M3DMatrix44f mMatrix) +1 void PushMatrix(const M3DMatrix44f mMatrix) Void PopMatrix(void PopMatrix) +1 void PopMatrix(void PopMatrix) +1 void PopMatrix(void PopMatrix) +1 void PopMatrix(void PopMatrixCopy the code
Affine transformation
The GLMatrixStack class also provides support for rotation, panning, and scaling
void Scale(GLfloat x, GLfloat y, GLfloat z)
void Translate(GLfloat x, GLfloat y, GLfloat z)
void Rotate(GLfloat angle, GLfloat x, GLfloat y, GLfloat z)
Copy the code
The internal implementation is to create the appropriate matrix and then multiply that matrix by the element at the top of the matrix stack, essentially adding a transformation to the current matrix.
Transform pipeline GLGeometryTransform
During development, we typically build a matrix stack for the model view matrix and the projection matrix, and often need to retrieve the two matrices and multiply them to get the model view projection matrix. GLGeometryTransform helps developers keep track of both matrix stacks and quickly retrieve the top of the model view projection matrix stack or the top of the normal matrix stack.
Common apis are as follows:
Void SetModelViewMatrixStack(GLMatrixStack& mModelView) sets the model view matrix stack Void SetMatrixStacks(GLMatrixStack& mModelView, Stack GLMatrixStack & mProjection) set up the model view matrix and projection matrix stack M3DMatrix44f & GetModelViewProjectionMatrix (void) Multiply and return the top stack matrix of the projection matrix stack and the top stack matrix of the model view matrix stackCopy the code