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

时间:2019-09-03 22:40:58

标签: c haskell opengl glfw

在使用命令brew install glfw通过brew安装glfw之后,我无法创建2.1版以上的GLFW窗口。

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

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示例相同。

1 个答案:

答案 0 :(得分:2)

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

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