I. Examples and related functions

Use OpenGL to draw a square, and use the keyboard keys to control the square up and down around the movement. The effect is as follows:The following functions are required for this example:

  • int main(int argc,char *argv[]) The main function:

Just like the main function that we’re developing, it’s the entrance to the APP, and in OPenGL it’s the entrance to the APP. OpenGL is procedural programming. So what you’ll see with OpenGL is that it’s all chained. And the image processing framework based on OpenGL encapsulation is also chain programming.

  • void changeSize(int w ,int h):ChangeSize function:

A custom function registered as a remodeling function through glutReshaperFunc. This function is called to resize the window/viewport when the screen size changes/or the window is created for the first time.

  • void RenderScene(void) RenderScene function:

A custom function registered as a display rendering function through glutDisplayFunc(function name). This function is called when the screen changes/or the developer renders actively, and is used to implement the data -> rendering process.

  • void setupRC(void) SetupRC function:

Custom function to set the vertex data/color data of the graph you need to render.

  • void SpecialKeys(int key, int x, int y) SpecialKey function:

Custom function that receives the response of the left, right, up, down keys on the keyboard, calculates according to the keys and triggers the redraw API.

Here is the flow chart of the whole case:

Two, the code

The main () ` function:

The entry to the start of a program, in this case mainly to do the preparatory work of the program:

  1. Initialize OpenGL, set the current working directory, initializeGLUTLibrary, etc.;
  2. Set the window size and title to display relevant content
  3. Register various listener callback functions (mainly the following in this case:changeSize Callbacks for window size changes;RenderScene Draw/redraw callback;SpecialKeys Keyboard callback after special key is clicked)
  4. Call setupRC() and turn it onglutMainLoop (Similar to iOSRunLoop).
Int main(int argc,char *argv[]) {// Set the current working directory, for MAC OS X gltSetWorkingDirectory(argv[0]); GlutInit (&argc, argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL); GlutInitWindowSize (800, 600); glutCreateWindow("Triangle"); GlutReshapeFunc (changeSize) ¶ If the window size changes, the changeSize() callback is triggered // Register the display function; When changeSize fires, or glutPostRedisplay() fires //RenderScene() callback glutDisplayFunc(RenderScene); // Register special functions (keyboard keystroke listening), triggered when the keyboard is clicked; glutSpecialFunc(SpecialKeys); GLenum status = glewInit(); if (GLEW_OK ! = status) { printf("GLEW Error:%s\n",glewGetErrorString(status)); return 1; } // set our render environment setupRC(); glutMainLoop(); return 0; }Copy the code

` setupRC () function:

SetupRC mainly does some initialization, which can be understood as UI development configuration in iOS when initializing UIView

  1. Set the window background color
  2. Initialize stores the colorsshaderManager
  3. Sets graph vertex data
  4. usingGLBatch The triangle batch class passes data to the shader

SetupRC can only be called manually triggered in the main function.

Void setupRC() {// Set the clear color (background color) glClearColor(0.98f, 0.40f, 0.7f, 1); / / initialize shader manager shaderManager. InitializeStockShaders (); GL_TRIANGLE_FAN = GL_TRIANGLE_FAN = GL_TRIANGLE_FAN Trianglebatch.begin (GL_TRIANGLE_FAN, 4); / / what's data triangleBatch CopyVertexData3f (vVerts); // Complete the triangleBatch.end (); }Copy the code

` changeSize () function:

ChangeSize does two things:

  1. Set up theOpenGLviewport
  2. Set up theOpenGLProjection mode, etc.

There are two main changeSize trigger modes: manual trigger (creating a window) and callback trigger (changing the viewport position).

Void changeSize(int w, int h) {/* changeSize(int w, int h) {/* changeSize(int w, int h); }Copy the code

` RenderScene () function:

RenderScene deals with what’s on the screen:

  1. Clean the cache (color, depth, template cache, etc.)
  2. Use to store the colors
  3. Draw graphics.

You can think of it as drawInRect under UIView:; There are two main ways to trigger the RenderScene: an automatic trigger (after a changeSize call) and a callback trigger (after a glutPostRedisplay redraw call).

void RenderScene(void) { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); / / set A color {R, G, B, B} GLfloat vRed [] = {0.0 1.0 f, f, f 0.0, 0.0} f; // M3DMatrix44f mTransfromMatrix; // m3dTranslationMatrix44(mTransfromMatrix, xPos, yPos, 0.0f); / / results submitted to the matrix to fixed shader (flat surface shader) draw shaderManager. UseStockShader (GLT_SHADER_FLAT mTransfromMatrix, vRed); triangleBatch.Draw(); GlutSwapBuffers (); }Copy the code

It’s worth noting that we’re moving the view through a matrix, and we’re also computing points separately, but that’s extremely inefficient with a lot of vertices. So the proper use of matrices is very important for us to use OpenGL.

` SpecialKeys () function:

SpecialKeys is a callback that we added in main() to listen to a keyboard button click. This callback returns a key of type int that we can use to determine which key on the keyboard we clicked. This method is the core method to control the effect of view movement on the screen by pressing buttons, so it mainly captures up, down, left and right, and does some data processing.

Void SpecialKeys(int key, int x, int y){GLfloat stepSize = 0.025f; if (key == GLUT_KEY_UP) { yPos += stepSize; } if (key == GLUT_KEY_DOWN) { yPos -= stepSize; } if (key == GLUT_KEY_LEFT) { xPos -= stepSize; } if (key == GLUT_KEY_RIGHT) { xPos += stepSize; } if (xPos < (-1.0f + blockSize)) {xPos = -1.0f + blockSize; } if (xPos > (1.0f-blocksize)) {xPos = 1.0f-blocksize; } if (yPos < (-1.0f + blockSize)) {yPos = -1.0f + blockSize; } if (yPos > (1.0f-blocksize)) {yPos = 1.0f-blocksize; } // This is an API that triggers a redraw, similar to calling setNeedsDisplay() glutPostRedisplay(); }Copy the code

Collision detection in the code is actually a display boundary problem in case we move the square view out of our display view area.

Remember to like it if you think it’s good! I heard that the people who read the praise will meet the test, meet the prize will be in. ღ(´ · ᴗ · ‘)