我试图运行XCode中使用GLFW(3.0.2版本)和GLEW(1.10.0版本)一个简单的OpenGL程序(4.6.3版本)。整个代码如下所示。GLEW在OS X 10.8.4崩溃XCode中
#include <GLFW/glfw3.h>
#include <OpenGL/OpenGL.h>
#include <iostream>
using namespace std;
void RenderScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
void InitGL()
{
glClearColor(1, 0, 0, 1);
}
void ErrorFunc(int code, const char *msg)
{
cerr << "Error " << code << ": " << msg << endl;
}
int main(void)
{
GLFWwindow* window;
/* Report errors */
glfwSetErrorCallback(ErrorFunc);
/* Initialize the library */
if (!glfwInit())
return -1;
/* Window hints */
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);
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Initialize OpenGL */
InitGL();
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
RenderScene();
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
return 0;
}
其中大部分来自GLFW的文档;只有渲染函数和GLEW初始化是我的。我添加了OpenGL,Cocoa和IOKit的框架,并链接到libGLEW.a和libglfw3.a。程序编译成功,但在尝试执行GLEW应该处理的函数时似乎崩溃。在这里,程序在glClearBufferfv
上崩溃。如果我评论一下,我会看到一个黑色背景的窗口。我的猜测是GLEW秘密无法工作,因为它报告没有错误,但似乎没有完成它的工作。
确切的错误信息的XCode抛出我是error: address doesn't contain a section that points to a section in a object file
与EXC_BAD_ACCESS
一个错误代码。如果我将glClearBufferfv
替换为glClearColor
,程序不会崩溃,但实际上它应该是红色时仍具有黑色背景。查询时,OpenGL返回版本字符串2.1 NVIDIA-8.12.47 310.40.00.05f01
,这解释了为什么对新函数的调用不起作用,但不应该GLEW设置正确的OpenGL上下文?此外,GLFW的文档说自从GLFW 2.7.2以来,他们一直在创建OpenGL 3+上下文。我真的不知道该怎么做。
没有GLEW时,Xcode抱怨'glClearBufferfv'没有定义。如果我使用'glClearColor'代替它运行,但总是给我黑色,无论我指定什么颜色。 –
用'glfwWindowHint'强制设置所需的OpenGL版本似乎也不起作用。它崩溃的任何版本2.1以上... –
GLEW不会创建一个OpenGL上下文;它依靠应用程序创建一个应用程序,然后才能获取其所有功能。就你而言,我认为GLFW将创建OpenGL上下文。 – radical7