Related Contents:

  1. OpenGL– An introduction to the graphics API
  2. OpenGL– Related terms explained
  3. OpenGL– Environment configuration
  4. OpenGL- Case 1- Draw a triangle
  5. OpenGL– Case 2 — Draw a square
  6. OpenGL– Image rip
  7. OpenGL–3D mathematics related (Vectors and Matrices)
  8. OpenGL– Matrix transformations and matrix stacks

Now that we’ve learned how to configure the OpenGL environment, let’s play 2 short cases about OpenGL. (Just like learning any other language, you shouldn’t start with a “Hello world!! “Play.” So let’s start by drawing a triangle using OpenGL

The preparatory work

In the previous environment configuration, we imported three files “GLTools”, “glew” and “libglTools. a” into the project. This case will use the contents of these resource files.

Utility class

1. GLShaderManager

The shader manager, in the core framework of OpenGL, does not provide us with any built-in rendering pipeline. Before submitting an image for rendering, we must implement a shader. GLShaderManager is a shader management class implemented by C++ that manages a set of “storage shaders” that fit our case.

/ / header files
#include "GLShaderManager.h"
/ / renamed
GLShaderManager shaderManager;
/ / initialization
shaderManager.InitializeStockShaders();
Copy the code

2. GLTools

OpenGL a static tool library, contains the OpenGL tool function library, utility library and some other commonly used functions. Convenient for us to call the API.

/ / header files
#include "GLTools.h"
/ / contains
// Universal includes
#include <stdio.h>
#include <math.h>
#include "math3d.h"
#include "GLBatch.h"
#include "GLTriangleBatch.h"
/ / and glew
Copy the code

3. GLUT

This library is like a toolkit for OpenGL, there is no way to create Windows directly from OpenGL on the Mac. GLUT provides apis for creating Windows, popup menus, window management, and more. In addition, GLUT development has been interrupted on Windows and Linux systems, using a function library called FreeGLUT.

* * Glut sub-API. */ extern void APIENTRY glutInit(int *argcp, char **argv) OPENGL_DEPRECATED(10_0, 10_9); */ extern int APIENTRY glutCreateWindow(const char *title) OPENGL_DEPRECATED(10_0, 10 _9); * * * The func callback function is automatically called when the program is running. */ extern void APIENTRY glutDisplayFunc(void (*func)(void)) OPENGL_DEPRECATED(10_0, 10_9); */ extern void APIENTRY glutDisplayFunc(*func)(void) OPENGL_DEPRECATED(10_0, 10_9); */ extern void APIENTRY glutReshapeFunc(void (*func)(int width, int height)) OPENGL_DEPRECATED(10_0, 10_9);Copy the code

Function and implementation

  • ChangeSize Custom function. Register as a remodeling function with glutReshaperFunc(function name). This function is called to adjust the window size/viewport size when the screen size changes/or the window is created for the first time
void ChangeSize(int w,int h) {
  // glViewport (GLint x, GLint y, GLsizei width, GLsizei height)
  // x,y, in pixels, specifies the lower-left corner of the window
  // width,height represents the width and height of the viewport rectangle, and redraws the window according to the real-time changes of the window
    glViewport(0.0, w, h);
}
Copy the code
  • RenderScene custom function. Register as a display render function with 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
//
GLBatch triangleBatch; / / batch
GLShaderManager shaderManager; // Shader manager
//
void RenderScene(void) {
    // Clear a specific buffer or group of buffers
 	  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    // Set a set of float numbers to represent red (RGBA)
    GLfloat vRed[] = {1.0 f.0.0 f.0.0 f.1.0 f};
    // Passes to the storage shader, the GLT_SHADER_IDENTITY shader, which simply renders the geometry on the screen using the specified color and default Cartesian coordinates
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY,vRed);
    // Submit the shader
    triangleBatch.Draw();
    // Will render in the background buffer and then swap to the foreground at the end
  	//(Extension: double buffering, vSYNC, triple buffering)
    glutSwapBuffers();
}
Copy the code
  • SetupRC custom function, set you need to render graphics related vertex data, color data and other data equipment work.
void SetupRC(a) {
    // Reset the color of the back with the color represented by the parameter
    glClearColor(0.0 f.0.0 f.1.0 f.1.0 f);
    // Initialize the shader manager
    shaderManager.InitializeStockShaders();
    // Set the triangle, where the array vVert contains cartesian pairs of x,y, and z for all 3 vertices.
    GLfloat vVerts[] = {
        0.5 f.0.0 f.0.0 f.0.5 f.0.0 f.0.0 f.0.0 f.0.5 f.0.0 f};// batch processing
    triangleBatch.Begin(GL_TRIANGLES,3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
}
Copy the code
  • OpenGL is procedural programming, so you will find that using OpenGL to process graphics/images is a chained form, and the image processing framework based on OpenGL encapsulation is also chained programming
int main(int argc,char* argv[]) {
    // Set the current working directory
    gltSetWorkingDirectory(argv[0]);
    // Initialize the GLUT library
    glutInit(&argc, argv);
    /* Initialize the double buffering window with the GLUT_DOUBLE, GLUT_RGBA, GLUT_DEPTH, and GLUT_STENCIL symbols indicating the double buffering window, RGBA color mode, depth test, and template buffer */
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL);
    //GLUT window size, title window
    glutInitWindowSize(800.600);
    glutCreateWindow("Triangle");
    // Register callback functions (ChangeSize and RenderScene are the custom functions we implemented above)
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    // There are no problems with driver initialization.
    GLenum err = glewInit();
    if(GLEW_OK ! = err) {fprintf(stderr."glew error:%s\n",glewGetErrorString(err));
        return 1;
    }
    / / call SetupRC
    SetupRC();
    // Enter the GLUT event processing loop
    glutMainLoop();
    return 0;
}
Copy the code

The execution result

Ps: Some people may implement the wrong triangle shape, and then change the size of the box is normal. This is allegedly due to the fact that the API we are using is old and the xcode version is new, so don’t worry about the details.

Demo address: Github