Mac builds OpenGL window environment (glew glfw)

install glew glfw

brew install glew
brew install glfw

The directory after brew installation is under /usr/local/Cellar, and the path will be used later.

Create a project with Clion

CMakeList.txt is configured as follows:

cmake_minimum_required(VERSION 3.6)
project(CGL3DDemo)

set(GLM_H /usr/local/Cellar/glm/0.9.9.8/include)
set(GLEW_H /usr/local/Cellar/glew/2.2.0_1/include)
set(GLFW_H /usr/local/Cellar/glfw/3.3.4/include)
include_directories(${
    
    GLM_H} ${
    
    GLEW_H} ${
    
    GLFW_H})

set(GLEW_LINK /usr/local/Cellar/glew/2.2.0_1/lib/libGLEW.2.2.dylib)
set(GLFW_LINK /usr/local/Cellar/glfw/3.3.4/lib/libglfw.3.dylib)
link_libraries(${
    
    OPENGL} ${
    
    GLEW_LINK} ${
    
    GLFW_LINK})

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES main.cpp)
add_executable(CGL3DDemo ${
    
    SOURCE_FILES})

target_link_libraries(CGL3DDemo "-framework OpenGL")
target_link_libraries(CGL3DDemo "-framework GLUT")

test

show an empty window

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>

int main()
{
    
    
    std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE); // 在 Mac 上要加这一句配置,否则不显示
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "CGL3DDemo", nullptr, nullptr);
    if (window == nullptr) {
    
    
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    if (glewInit() != GLEW_OK) {
    
    
        std::cout << "Failed to initialize GLEW" << std::endl;
        return -1;
    }

    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    glViewport(0, 0, width, height);

    while (!glfwWindowShouldClose(window))
    {
    
    
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;

}

Displayed as follows:
Please add a picture description

Guess you like

Origin blog.csdn.net/u011520181/article/details/120732360