我正在尝试使用GLFW在Mac上获得高于版本2的OpenGL上下文。我的配置是Mavericks(10.9.1)+ XCode,我有一个Nvidia Geforce 650M GPU,可能支持OpenGL 4.1 Full Profile。我使用以下代码:
static void test_error_cb (int error, const char *description)
{
fprintf(stderr, "%d: %s\n", error, description);
}
int main(void) { GLFWwindow *窗口;
glfwSetErrorCallback(test_error_cb);
// Initialise GLFW
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
exit(EXIT_FAILURE);
}
//Request Specific Version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// Open OpenGL window
window = glfwCreateWindow(500, 500, "Split view demo", NULL, NULL);
if (!window)
{
fprintf(stderr, "Failed to open GLFW window\n");
glfwTerminate();
exit(EXIT_FAILURE);
}
// Set callback functions
glfwSetFramebufferSizeCallback(window, framebufferSizeFun);
glfwSetWindowRefreshCallback(window, windowRefreshFun);
glfwSetCursorPosCallback(window, cursorPosFun);
glfwSetMouseButtonCallback(window, mouseButtonFun);
glfwSetKeyCallback(window, key_callback);
// Enable vsync
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glfwGetFramebufferSize(window, &width, &height);
framebufferSizeFun(window, width, height);
//Check Version
int major, minor, rev;
major = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
minor = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
rev = glfwGetWindowAttrib(window, GLFW_CONTEXT_REVISION);
printf("OpenGL version recieved: %d.%d.%d\n", major, minor, rev);
printf("Supported OpenGL is %s\n", (const char*)glGetString(GL_VERSION));
printf("Supported GLSL is %s\n", (const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));
// Main loop
for (;;)
{
// Only redraw if we need to
if (do_redraw)
{
// Draw all views
drawAllViews();
// Swap buffers
glfwSwapBuffers(window);
do_redraw = 0;
}
// Wait for new events
glfwWaitEvents();
// Check if the window should be closed
if (glfwWindowShouldClose(window))
break;
}
// Close OpenGL window and terminate GLFW
glfwTerminate();
exit(EXIT_SUCCESS);
}
目前glfwCreateWindow函数失败。没有任何提示(即没有glfwWindowHint调用)我只能在1.20处使用带有glsl版本的OpenGL 2.1。指教。
答案 0 :(得分:1)
在OSX上访问大于2.1的GL版本需要Core上下文。
取消注释GLFW_OPENGL_PROFILE
提示。
答案 1 :(得分:0)
尝试在#include <GLFW/glfw3.h>
之前添加此内容并取消注释您的个人资料提示。
#define GLFW_INCLUDE_GLCOREARB
答案 2 :(得分:0)
不确定为什么要禁用核心配置文件提示。我没有答案,但您可能通过错误回调获取更多信息,例如,
extern "C"
{
static void test_error_cb (int error, const char *description)
{
fprintf(stderr, "%d: %s\n", error, description);
}
}
...
{
glfwSetErrorCallback(test_error_cb);
if (glfwInit() != GL_TRUE)
{
....
这是一个可以在glfwInit
之前使用的功能。