我试图在Mac OS上使用带有SDL 2.0.3和C++的"现代"OpenGL(3+),如果我直接调用OpenGL函数,它可以工作,但是当我尝试将它包装在类中时它不会工作.更具体地说,我想将Vertex数组对象存储为类成员并glGenVertexArrays
在其上调用类方法,但这会导致段错误.
直接调用的最小工作示例(没有任何用处但不会崩溃):
#include#include #include int main() { SDL_Init(SDL_INIT_VIDEO); SDL_Window *sdlWindow = SDL_CreateWindow( "OpenGL Works", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI ); SDL_GLContext sdlGLContext = SDL_GL_CreateContext(sdlWindow); unsigned int vertexArrayObject; // This works glGenVertexArrays(1, &vertexArrayObject); // Wait some seconds before closing the window SDL_Delay(3000); SDL_GL_DeleteContext(sdlGLContext); SDL_DestroyWindow(sdlWindow); return EXIT_SUCCESS; }
不使用简单类(分段错误):
#include#include #include #include class Graphics { public: void createVAO(); private: unsigned int vertexArrayObject; }; void Graphics::createVAO() { glGenVertexArrays(1, &this->vertexArrayObject); } int main() { SDL_Init(SDL_INIT_VIDEO); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_Window *sdlWindow = SDL_CreateWindow( "OpenGL Shit", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_ALLOW_HIGHDPI ); SDL_GLContext sdlGLContext = SDL_GL_CreateContext(sdlWindow); std::unique_ptr g; g->createVAO(); // Wait some seconds before closing the window SDL_Delay(3000); SDL_GL_DeleteContext(sdlGLContext); SDL_DestroyWindow(sdlWindow); return EXIT_SUCCESS; }
同样的事情发生了glGenBuffers
.我想知道它是否只是由于我缺乏c ++技能或者是一个与OpenGL和/或SDL相关的更严重问题而导致的"简单"C++问题.我在Stack Overflow上发现了很多关于类似症状的问题,但是大多数问题是由glewExperimental = GL_TRUE;
GLUT或GLEW 缺少的选项造成的,我根本没有使用或者我缺少OpenGL上下文,因为我能够在屏幕:
glGenVertexArrays(1,&vao)的分段错误;
关于glGenVertexArrays调用的程序崩溃
在OpenGL中实现VBO,窗口保持黑色
关于glGenVertexArrays的OpenGL Segfaults
我希望有人能解释为什么这会崩溃,因为我不能:(
我正在使用Mac OS 10.9.3和编译器Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
,这是编译器调用:c++ -o OpenGLTest main.cpp -I /usr/local/include/SDL2 -lSDL2 -framework OpenGL
std::unique_ptr<Graphics> g; g->createVAO();
你在哪里居住g
的东西?
现在看起来你正在取消引用一个NULL指针.
试试这个:
std::unique_ptr<Graphics> g( new Graphics );