mac搭建OpenGL环境

GLFW编译与安装

# 默认安装路径在/usr/local
cd glfw
make
make install

vscode配置库

  • c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": [
                "${workspaceFolder}/include",
                "/usr/local/include"
            ],
            "defines": [],
            "macFrameworkPath": [
                "/System/Library/Frameworks",
                "/Library/Frameworks"
            ],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
  • tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "build",
            "command": "/usr/bin/clang++",
            "args": [
                "-std=c++17",
                "-stdlib=libc++",
                "-fPIC",

                "-I${workspaceFolder}/include",
                "-I${workspaceFolder}/src",

                "-L${workspaceFolder}/lib",
                "-L/usr/local/lib",
                "-l",
                "glfw3",

                "-framework",
                "OpenGL",
                "-framework",
                "Cocoa",
                "-framework",
                "IOKit",
                "-framework",
                "CoreVideo",

                "-g",
                "${workspaceFolder}/src/*.cpp",
                
                "-o",
                "${workspaceFolder}/bin/engine"
            ],
            "options": {
                "cwd": "/usr/bin"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build"
        }
    ]
}
  • launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceRoot}/bin/engine",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}/bin",
            "environment": [],
            "externalConsole": false,
            "MIMode": "lldb",
            "preLaunchTask": "build"
        }
    ]
}

示例

# include "GLFW/glfw3.h"

int main(int argc, char* argv[]) {
    GLFWwindow* win;
    if (!glfwInit()) {
        return -1;
    }
    win = glfwCreateWindow(640, 480, "test", NULL, NULL);
    if (!win) {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(win);
    while (!glfwWindowShouldClose(win)) {
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
        glBegin(GL_TRIANGLES);
        {
            glColor3f(1.0, 0.0, 0.0);
            glVertex2f(0, 0.5);
            glColor3f(0.0, 1.0, 0.0);
            glVertex2f(-0.5, -0.5);
            glColor3f(0.0, 0.0, 1.0);
            glVertex2f(0.5, -0.5);
        }
        glEnd();
        glfwSwapBuffers(win);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}

你可能感兴趣的:(mac搭建OpenGL环境)