# Prepare resources

  • CLTools
  • glew
  • libGLTools.a

Baidu web disk share address: Baidu web disk, extract code: kwas # Configure the OpenGL environment

  • Go to Xcode -> macOS -> Cocoa App

  • Adding a dependency library

  • Unzip the downloaded data

  • Introduce the prepared data into the project

  • Add the include file location
"$(SRCROOT)/OpenGLTest/include"
Copy the code

  • CPP create a file of c++ type, call it main, and uncheck create headers at the same time
  • Delete appdelegate. h, Appdelegate. m, and main.m. H and viewController.m will need to be removed from the previous version because the author’s Xcode version is 11.5

  • Copy the following code into the main.cpp file
#include "GLTools.h" #include "GLShaderManager.h" #ifdef __APPLE__ #include <glut/glut.h> #else #define FREEGLUT_STATIC #include <GL/glut.h> #endif GLBatch triangleBatch; GLShaderManager shaderManager; Void ChangeSize(int w,int h) {glViewport(0,0, w, h); } void SetupRC() {// Set the figure color glClearColor(0.0f,0.0f,1.0f,1.0f); / / initialize shader manager shaderManager. InitializeStockShaders (); // Set the triangle, where the array vVert contains x,y, cartesian pairs of all 3 vertices. F GLfloat vVerts [] = {0.5, 0.0 f to 0.0 f to 0.5 f to 0.0 f to 0.0 f to 0.0 f to 0.5 f to 0.0 f,}; // Triangles.Begin(GL_TRIANGLES,3); triangleBatch.CopyVertexData3f(vVerts); triangleBatch.End(); } // Start rendering void RenderScene(void) {// Clear a specific buffer or set of buffers glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT); GLfloat vRed[] = {1.0f,0.0f,0.0f,1.0f}; // Pass to the storage shader, GLT_SHADER_IDENTITY shader, This shader is only using the specified color on the screen in a cartesian coordinates by default the first rendering geometry shaderManager. UseStockShader (GLT_SHADER_IDENTITY vRed); // Submit the shader triangleBatch.draw (); GlutSwapBuffers (); } int main(int argc,char* argv[]) {// Set the current working directory, for MAC OS X gltSetWorkingDirectory(argv[0]); // Initialize GLUT Library glutInit(&argc, argv); /* Initialize the double buffering window, The symbols GLUT_DOUBLE, GLUT_RGBA, GLUT_DEPTH and GLUT_STENCIL respectively refer to the double buffer window, RGBA color mode, depth test and template buffer */ glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL); GlutInitWindowSize (800,600); glutCreateWindow("Triangle"); 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(); glutMainLoop(); return 0; }Copy the code
  • There may be some problems running here, do the following, and it will run successfully

  • Duang! The triangle is drawn.