在使用GLEW、GLFW和g++编译时,如何在OS X 10.10下启用OpenGL 4.1

3

我有一台2014年的RMBP,升级到最新版本的OS X应该保证我兼容OpenGL 4.1。我已经组建了一个最小工作示例:

#include <iostream>

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

using namespace std;

int main(){
    int windowWidth = 800;
    int windowHeight = 800;
    string windowTitle = "title";

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);

    if( !glfwInit() )
    {
        cerr << "Failed to initialize GLFW.\n";
        return 1;
    } else {
        clog << "Initialized GLFW." << endl;
    }

    GLFWwindow* pWindow = glfwCreateWindow(windowWidth, windowHeight, windowTitle.c_str(), 0, NULL);

    glfwSetWindowPos(pWindow, 700, 200);
    glfwMakeContextCurrent(pWindow);
    if( pWindow == NULL )
    {
        cerr << "Failed to open GLFW window.\n";
        glfwTerminate();
        return 1;
    }

    std::cout << "GL Version: " << glGetString(GL_VERSION) << "\n";
    std::cout << "GLSL Version: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << "\n";
    std::cout << "Vendor: " << glGetString(GL_VENDOR) << std::endl;
    std::cout << "Renderer: " << glGetString(GL_RENDERER) << std::endl;

    while (!glfwWindowShouldClose(pWindow))
    {
        glfwPollEvents();
    }
    glfwDestroyWindow(pWindow);
    return 0;
}

这将打开一个窗口,它可以正常工作,但输出如下: GL 版本:2.1 INTEL-10.0.86 GLSL 版本:1.20 供应商:Intel Inc. 渲染器:Intel Iris OpenGL 引擎

这是错误的!我应该得到4.1兼容性。我下载了https://github.com/tomdalling/opengl-series上的示例,在xCode下,它确实升级到了4.1,所以我知道我的电脑是有能力的。

这实际上是为了一个更大的项目;我尝试将我的项目导入xcode中,并与Tom Dalling的项目合并,但它就是不起作用,我总是得到GL 2.1。

我正在使用以上代码进行编译。

g++ main.cpp -o GLTEST -lglfw -framework Cocoa -framework OpenGL -framework IOKit -framework CoreVideo \

也许我错过了某个选项。非常感谢您的帮助!


就我个人而言,在OS X上,glew只会带来问题。我把它删除后一切都好了。然后,我不得不导入<OpenGL/OpenGL.h><OpenGL/gl3.h>,这样才能正常工作。 - Bjorn
1个回答

3
阅读关于窗口创建提示的内容网站glfw,您将发现:

这些提示每次使用glfwInit初始化库时都设置为默认值

Tom Dalling的(工作)代码在提示版本之前调用glfwInit,而你的代码是在提示之后调用glfwInit。所以我怀疑这可能与此有关。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接