preface

He that will do his work well must first sharpen his tools. At the beginning of learning a language, setting up the environment is very important. The following is the whole process. It is not complicated, but it needs to be careful.

1. Create a MAC App project

2. Add Opengl. framework and GLUT. Framework system libraries

3. Drag libglTools. a and include folder (glew inside)

  • The resource bundle download path: pan.baidu.com/s/1SE5rMDuc… Extraction code: MBCW

  • As shown below, search header Search path and drag the include folder

4. Delete the five files shown in the following figure

5. Create a main. CPP file

Choose c + + files

Deselect create header file [image uploaded…(image.pg-d3536D-1556361060513-0)]

6. Run the test code. If it can run, the environment is successfully built

\#define GL_SILENCE_DEPRECATION 
// Remove the warning. Comment out this line and then compile.

#include "GLShaderManager.h"
/* '#include< glshaderManager. h>' moves the GLTool shader Mananger class. Without shaders, we can't do shaders in OpenGL. The shader manager not only allows us to create and manage shaders, but also provides a set of "storage shaders" that can perform some initial 䄦 basic rendering operations. * /

#include "GLTools.h"
/* '#include< gltools. h>' gltool. h header contains most of the separate C-like GLTool functions */


#include <GLUT/GLUT.h>
* On Mac, '#include< Glut /glut.h>' on Windows and Linux, we use the static library version of FreeGlut and need to add a macro */

// Define a shader manager
GLShaderManager shaderManager;

// A simple batch container is a simple container class for GLTools.
GLBatch triangleBatch;

/* Receives new width & height when the window size changes. * /
void changeSize(int w,int h)
{
    /* x and y parameters represent the coordinates of the lower left corner of the view in the window, while width and height are represented by pixels. Usually x and y are 0 */
    glViewport(0.0, w, h);
    
}

void RenderScene(void)
{
    
    //1. Clear a specific cache or group of caches
    /* A buffer is a block of storage space containing image information. The red, green, blue, and alpha components are often referenced together as color caches or pixel caches. More than one buffer in OpenGL (color cache, depth cache, and template cache) clears the cache to preset values: GL_COLOR_BUFFER_BIT: indicates the currently active buffer for color writing GL_DEPTH_BUFFER_BIT: indicates the depth buffer GL_STENCIL_BUFFER_BIT: indicates the template buffer */
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
    
    
    //2. Set a set of floating-point numbers to represent red
    GLfloat vRed[] = {1.0.0.0.0.0.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();
    
    // When we first set up the openGL window, we specified a double buffer rendering environment. This means that the rendering will be done in the background buffer and then swapped out to the foreground. This prevents the viewer from seeing the rendering process that might be accompanied by flicker from animation frame to animation frame. The buffer switching platform will proceed in a platform-specific manner.
    // Render the background buffer and swap it to the foreground when finished
    glutSwapBuffers();
    
}

void setupRC(a)
{
    // Set the clear screen color (background color)
    glClearColor(0.98 f.0.40 f.0.7 f.1);
    
    
    // No rendering can be done in the OpenGL core framework without shaders. Initialize a render manager.
    // In the previous course, we will use solid pipeline rendering, and we will learn to write shaders in OpenGL
    shaderManager.InitializeStockShaders();
    
    
    // Specify vertices
    // In OpenGL, triangles are a basic 3D metagraphing element.
    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
    };
    
    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
    
}

int main(int argc,char *argv[])
{
    // Set the current working directory for MAC OS X
    /* 'GLTools' function' glSetWorkingDrectory 'sets the current working directory. In fact, this is not necessary in Windows because the working directory is the same directory as the program executable by default. But on Mac OS X, this program changes the current working folder to the '/Resource' folder in the application bundle. The 'GLUT' priority setting does this automatically, but the method is more secure. * /
    gltSetWorkingDirectory(argv[0]);
    
    
    // Initialize GLUT library. This function just legends command arguments and initializes GLUT library
    glutInit(&argc, argv);
    
    /* Initialize the double buffer window, where GLUT_DOUBLE, GLUT_RGBA, GLUT_DEPTH, GLUT_STENCIL indicate the double buffer window, RGBA color mode, depth test, and template buffer, respectively. Double-cached Windows, where a drawing command is actually executed off-screen from the cache and then quickly converted to a window view, are often used to generate animation effects; --GLUT_DEPTH ': flag allocates a depth cache as part of the display, so we can perform a depth test; GLUT_STENCIL: Make sure we also have a template cache available. Depth and template testing will be covered in more detail */
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL);
    
    //GLUT window size, window title
    glutInitWindowSize(800.600);
    glutCreateWindow("Triangle");
    
    /* GLUT internally runs a local message loop that intercepts the appropriate message. Then call the callback function that we registered at different times. We registered two callback functions: 1) a callback for window resizing and 2) a callback that contains OpenGL rendering */
    // Register the remodeling function
    glutReshapeFunc(changeSize);
    // Register the display function
    glutDisplayFunc(RenderScene);
    
    /* Initialize a GLEW library to ensure that the OpenGL API is fully available to the program. Before attempting to do any rendering, check to make sure there are no problems with driver initialization */
    GLenum status = glewInit();
    if(GLEW_OK ! = status) {printf("GLEW Error:%s\n",glewGetErrorString(status));
        return 1;
        
    }
    
    // Set up our render environment
    setupRC();
    
    glutMainLoop();
    
    
    
    
    return  0;
    
}
Copy the code

The running result is shown in the picture below, and a triangle is rendered, which proves that the environment has been built successfully. When writing openGL code in the future, you can copy this basic project, and there is no need to build each time:

OpenGL introduction (a) — OpenGL professional noun analysis

Introduction to OpenGL (2) — Build an OpenGL Mac environment

OpenGL Start (3) — Quickly draw a square

OpenGL introduction (4) – rendering process analysis

OpenGL introduction (five) – metagraph drawing actual combat

OpenGL Introduction (six) – Matrix basic change actual combat

OpenGL introduction (seven) – hidden surface elimination details

OpenGL Start (eight) – Texture coordinate parsing