#OpenGL fixed pipeline shaders include:
- Unit shader (GLT_SHADER_IDENTITY)
- Flat shader (GLT_SHADER_FLAT)
- Coloring shader (GLT_SHADER_SHADED)
- Default light shader (GLT_SHADER_DEFAULT_LIGHT)
- Point light shader (GLT_SHADER_POINT_LIGHT_DIEF)
- Texture replacement matrix shader (GLT_SHADER_TEXTURE_REPLACE)
- GLT_SHADER_TEXTURE_MODULATE texture adjustment shader
- Texture shader (GLT_SHADER_TEXTURE_POINT_LIGHT_DIEF)
The role of model view projection matrix is to transform vertices from the local coordinate system to the canonical cube. In a word, model view projection matrix = model matrix view matrix projection matrix; Model matrix: converts vertices from local coordinates to world coordinates; View matrix: Converts vertices from world coordinates to view coordinates; Projection matrix: Converts vertices from the view coordinate system to the gauge cube (that is, the screen);
Let’s get familiar with a few header files:
// contains most of the separate c-like functions in GLTool#include "GLTools.h" // Matrix tools class, you can use GLMatrixStack to load cell matrix/matrix/matrix multiply/stack/unstack/scale/pan/rotate#include "GLMatrixStack.h" // Matrix utility class, representing position. By setting vOrigin, vForward, and vUp#include "GLFrame.h"// Matrix tool class, used to quickly set forward/through projection matrix, complete coordinates from 3D -> 2D mapping#include "GLFrustum.h"// Batch class, helper class, can transfer vertex/light/texture/color data to shaders#include "GLBatch.h"// Transform pipeline class: used to transfer view matrix/projection matrix/projection transformation matrix in code quickly#include "GLGeometryTransform.h"/ / math library#include <math.h>
Copy the code
### Flat shader to achieve a pyramid effect shader of many kinds, here is not one example, the usage is the process is the same, just according to different needs to choose different shaders and different algorithms.
##### defines some variables:
GLShaderManager shaderManager; // GLMatrixStack modelViewMatrix; // model view matrix stack GLMatrixStack projectionMatrix; // Perspective matrix stack GLFrame cameraFrame; // Observer position GLFrame objectFrame; // The position of the object itself GLFrustum viewFrustum; // GLBatch triangleBatch; // batch class GLGeometryTransform transformPipeline; // geometry transform pipeline GLfloatVGreen [] = {0.0f, 1.0f, 0.0f, 1.0f}; // Pyramid color GLfloatVBlack [] = {0.0f, 0.0f, 0.0f, 1.0f}; // The color of the dividing line of the pyramidCopy the code
Specific project source see the attachment Demo, here only write the key code ##### initialization preparation
void SetupRC() {// Clear the screen and set the background color glClearColor(0.3f,0.3f,0.3f,1.0f); / / initialize shader manager shaderManager. InitializeStockShaders (); // start depth test glEnable(GL_DEPTH_TEST); / / set transform pipeline to use the two matrices transformPipeline. SetMatrixStacks (modelViewMatrix projectionMatrix); Camerafame.MoveForward(-15); camerafame (-15); camerafame (-30) // Define points to represent the pyramid's apex, GLfloatVPyramid [12] [3] = {2.0 f to 0.0 f to 2.0 f to 2.0 f to 0.0 f to 2.0 f to 0.0 f to 4.0 f to 0.0 f to 2.0 f to 0.0 f to 2.0 f to 2.0 f to 0.0 f to 2.0 f to 0.0 f, 4.0 f, f, 0.0 to 2.0 f, f 0.0, 2.0 f to 2.0 f, 0.0 f, f 2.0, 0.0 f, f 4.0, 0.0 f to 2.0 f, f 0.0, 2.0 f to 2.0 f, 0.0 f to 2.0 f, f 0.0, 4.0, f 0.0 f}; // Triangles.Begin(GL_TRIANGLES,12); triangleBatch.CopyVertexData3f(vPyramid); triangleBatch.End(); }Copy the code
# # # # # changeSize callback
Void ChangeSize(int w,int h) {glViewport(0,0, w, h); / * create the projection matrix, and add it to the projection matrix stack * parameter description: vertical field of view Angle, window width and the height of the aspect ratio, nearly cutting surface distance, cutting surface distance * / viewFrustum. SetPerspective (40.0 f,float(w) / float(h), 1.0 f, 500.0 f); projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix()); / / load the unit matrix, unit matrix can not write modelViewMatrix. LoadIdentity (); }Copy the code
##### special keys to control the rotation of the pyramid
Void SpecialKeys(int key,int x,int y) {if(key == GLUT_KEY_UP) // Rotate around a specified X,Y,Z axis. ObjectFrame. RotateWorld (m3dDegToRad (5.0 f), 1.0 f, 0.0 f, 0.0 f);ifRotateWorld(m3dDegToRad(5.0f), 1.0f, 0.0f, 0.0f);ifRotateWorld(m3dDegToRad(-5.0f), 0.0f, 1.0f, 0.0f);ifRotateWorld(m3dDegToRad(5.0f), 0.0f, 1.0f, 0.0f); glutPostRedisplay(); }Copy the code
#####RenderScene Start rendering scene
Void RenderScene (void) {/ / clear one or perform a specific set of buffer glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); / / the current transformation matrix (matrix) onto the stack glPushMatrix purpose is to let all transform independent push each other with corresponding modelViewMatrix. Pop PushMatrix (); // Get the observer's matrix stack M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); / / pressure stack: the view matrix, can also direct matrix multiplication, if is a push, will need another pop/behind/modelViewMatrix PushMatrix (mCamera); modelViewMatrix.MultMatrix(mCamera); M3DMatrix44f mObjectFrame; // Get the matrix objectframe.getMatrix (mObjectFrame); modelViewMatrix.MultMatrix(mObjectFrame); / / surface shader shaderManager. UseStockShader (GLT_SHADER_FLAT, transformPipeline GetModelViewProjectionMatrix (), vGreen); DrawGoldTowerBatch(&triangleBatch); DrawGoldTowerBatch(&triangleBatch); modelViewMatrix.PopMatrix(); GlutSwapBuffers (); }Copy the code
##### Start drawing a pyramid
Void DrawGoldTowerBatch (GLBatch * pBatch) {/ * -- -- -- -- -- -- -- -- -- -- -- -- painted green parts -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * / / * GLShaderManager in Uniform value - surface shader Parameter 1: Plane shader parameter 2: run to specify a 4 * 4 transformation matrix for geometric transformation. Color value * / shaderManager. UseStockShader (GLT_SHADER_FLAT, transformPipeline GetModelViewProjectionMatrix (), vGreen); pBatch->Draw(); / * -- -- -- -- -- -- -- -- -- -- - border part -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - * / / * glEnable (GLenum mode); Used to enable various functions. Note: the function is determined by the parameters of the parameter list: http://blog.csdn.net/augusdi/article/details/23747081 GlEnable () cannot be written between glBegin() and glEnd() GL_POLYGON_OFFSET_LINE GL_POLYGON_OFFSET_LINE GL_LINE_SMOOTH Zigzag GL_BLEND for filter line points enables color blending. For example, GL_DEPTH_TEST enables the depth test to automatically hide hidden graphics according to the distance of coordinates (material glDisable(GLenum mode); GlPolygonOffset (-1.0f, -1.0f); glPolygonOffset(-1.0f, -1.0f); glPolygonOffset(-1.0f, -1.0f); glEnable(GL_POLYGON_OFFSET_LINE); GlEnable (GL_LINE_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the front or back of the polygon to wireframe mode by calling glPolygonMode. GlPolygonMode (GL_FRONT_AND_BACK, GL_LINE); // Set line width glLineWidth(2.5f); /* Uniform values in GLShaderManager -- Plane shader arguments 1: plane shader arguments 2: Operation of geometry transform to specify a 4 * 4 transformation matrix -- transformPipeline. GetModelViewProjectionMatrix () to obtain the GetMatrix function on top of the stack can gain matrix value parameter 3: Color (black). * / shaderManager UseStockShader (GLT_SHADER_FLAT, transformPipeline GetModelViewProjectionMatrix (), vBlack); pBatch->Draw(); //// Set the front or back of the polygon to full fill mode by calling glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glDisable(GL_POLYGON_OFFSET_LINE); GlLineWidth (1.0 f); glDisable(GL_BLEND); glDisable(GL_LINE_SMOOTH); }Copy the code
The running effect is as follows:
The demo link: download.csdn.net/download/zc…
OpenGL matrix stack handling