OpenGL environment configuration under Mac CLion

1. Configure glew and glfw

Run the following two sentences under the terminal. After installation, you can find the corresponding directory under / usr / local / cell /.

brew install glew
brew install glfw3

The effect is as follows:

2. Configure glad

glad is designed to simplify development. It is not necessary. It is a function loader glad file generation website The configuration is as follows:

In the lower right corner, click generate to generate the appropriate glad compressed file. After decompression, put the header file in the directory / usr/local/include (glad and KHR folders), and copy the source file to the project directory, as shown in the following two figures:

3. Configure CMakeLists file

The following is the detailed configuration of CMakeLists:

cmake_minimum_required(VERSION 3.12)


set(GLEW_H /usr/local/Cellar/glew/2.1.0/include/GL)
set(GLFW_H /usr/local/Cellar/glfw/3.2.1/include/GLFW)
set(GLAD_H /usr/local/include/glad)
set(KH_H /usr/local/include/KHR)
include_directories(${GLEW_H} ${GLFW_H} ${GLAD_H} ${KH_H})

# Add target link
set(GLEW_LINK /usr/local/Cellar/glew/2.1.0/lib/libGLEW.2.1.dylib)
set(GLFW_LINK /usr/local/Cellar/glfw/3.2.1/lib/libglfw.3.dylib)
link_libraries(${OPENGL} ${GLEW_LINK} ${GLFW_LINK})

# Execute compile command
# This glad.c said in step 2 that it should be pulled into the project. Pay attention to the path
set(SOURCE_FILES "src/glad.c" "main.cpp")
add_executable(OpenGL ${SOURCE_FILES})

# mac, it's important to take this step
if (APPLE)
    target_link_libraries(OpenGL "-framework OpenGL")
    target_link_libraries(OpenGL "-framework GLUT")

Note that after this configuration, you need to change the "include < KHR / khrplatform. H > in the" glad.h "folder under / usr/local/include/glad to" include < khrplatform. H >

  1. Open the glad.c file in CLion, and click command + include < glad.h > to skip to glad.h
  2. Then in glad.h, command+f finds "include < KHR / khrplatform. H >" and changes it to "include < khrplatform. H >

As follows:

4. Run test code

Copy the following code in main.cpp. If you can see the result shown in the figure, it will be successful.

#include <glew.h>
#include <glfw3.h>
#include <iostream>
using namespace std;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
    //If you press ESC and set windowShouldClose to True, the external loop will close the application
    if(key==GLFW_KEY_ESCAPE && action == GLFW_PRESS)
        glfwSetWindowShouldClose(window, GL_TRUE);

int main(void)
    //Initialize GLFW Library
        return -1;
    //Create window and context
    GLFWwindow* window = glfwCreateWindow(640, 480, "hello world", NULL, NULL);
        //NULL will be returned if creation fails
    //Establish the context of the current window

    glfwSetKeyCallback(window, key_callback); //Registering Callbacks 
    //Loop until the user closes the window
        /*******Polling events*******/

        //Select empty color RGBA
        glClearColor(0.2, 0.3, 0.3, 1);

        //Start drawing a triangle
        glColor3f(1, 0, 0); //Red
        glVertex3f(0, 1, 1);

        glColor3f(0, 1, 0); //Green
        glVertex3f(-1, -1, 0);

        glColor3f(0, 0, 1); //Blue
        glVertex3f(1, -1, 0);
        //End a drawing step

        //Draw another trapezoid, and pay attention to the stroke order
        glColor3f(0.5, 0.5, 0.5); //Grey
        glVertex2d(0.5, 0.5);
        glVertex2d(1, 1);
        glVertex2d(1, 0);
        glVertex2d(0.5, 0);

        /******Exchange buffer, update content on window******/
    return 0;

reference material

Tags: brew Mac

Posted on Wed, 22 Apr 2020 10:49:44 -0400 by lhaynes