无法使用通过 brew 安装 GLFW 的 OpenGL 3.3 上下文 macOS 创建 window

unable to create window with OpenGL 3.3 context macOS with GLFW installed through brew

在使用命令 brew install glfw.

通过 brew 安装 glfw 后,我无法使用 2.1 以上的版本创建 GLFW windows

基本上我的问题是这段代码有效:

import qualified Graphics.UI.GLFW as GLFW

configAndCreateWindow :: IO (Maybe GLFW.Window)
configAndCreateWindow = do              
    GLFW.windowHint (GLFW.WindowHint'ContextVersionMajor 2)
    GLFW.windowHint (GLFW.WindowHint'ContextVersionMinor 1)
    GLFW.createWindow 100 100 "test" Nothing Nothing

main :: IO ()
main = do
    GLFW.init
    maybeWindow <- configAndCreateWindow
    case maybeWindow of
        Nothing -> putStrLn "Failure :("
        Just _ -> putStrLn "Success!"

但如果我改变

GLFW.windowHint (GLFW.WindowHint'ContextVersionMajor 2)
GLFW.windowHint (GLFW.WindowHint'ContextVersionMinor 1)

GLFW.windowHint (GLFW.WindowHint'ContextVersionMajor 3)
GLFW.windowHint (GLFW.WindowHint'ContextVersionMinor 3)

它坏了。

为了确保它与 GLFW-b 无关,我还编写了一个 c 程序:

#define GLFW_INCLUDE_GL_3
#include <GLFW/glfw3.h>
#include <stdio.h>

int main ()
{
    glfwInit();

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    GLFWwindow* window = glfwCreateWindow(800, 600, "GLFW test", NULL, NULL);
    if (window == NULL) {
        printf("Success!");
    } else {
        printf("Failure :(");
    }
}

如果我更改上下文版本号,它就像 Haskell 示例一样工作。

在 MacOS 上请求 OpenGL 上下文时,必须设置两个附加标志:

glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);