使用OpenGL 3.3运行mac os x c ++程序

时间:2017-10-03 19:24:45

标签: c++ macos opengl glfw glew

我正在运行Mac OS X Sierra 10.12.6(16G29)。我正在开发一个macbook pro。

我已经安装了brew和以下软件包:

brew install glfw3
brew install glew
brew install glm

这是我的c ++程序:

#include <iostream>
#include <GLFW/glfw3.h>
GLFWwindow* window;
#include <GL/gl.h>

int main(int argc, const char * argv[])
{
    if (!glfwInit())
    {
        return -1;
    }

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

    // Should be true for macOS, according to GLFW docs, to get core profile.
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    // According to Apple docs, non-core profiles are limited to version 2.1.
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    window = glfwCreateWindow(640, 480, "Test 1", NULL, NULL);
    if( window == NULL )
    {
        return -1;
    }

    glfwMakeContextCurrent(window);

    // glGetString(GL_VERSION) is NULL at this point

    return 0;
}

这是我正在运行的命令行来编译我的程序:

g++ program.cpp -I/opt/X11/include -L/opt/X11/lib -lglfw -lGL -lGLEW

存在运行时问题,因为我得到一个NULL窗口。 我在Linux虚拟机上编译了相同的代码,它运行得很好......

1 个答案:

答案 0 :(得分:0)

GLFW文档中回答了这个问题:http://www.glfw.org/docs/latest/window_guide.html

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// Should be true for macOS, according to GLFW docs, to get core profile.
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
// According to Apple docs, non-core profiles are limited to version 2.1.
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

另见:https://developer.apple.com/opengl/OpenGL-Capabilities-Tables.pdf