无法使OpenGL,Glew和SDL2协同工作

时间:2017-11-08 20:15:44

标签: windows opengl sdl sdl-2 glew

// SDL 2.0.6, glew 2.1.0

SDL_Init(SDL_INIT_EVENTS | SDL_INIT_VIDEO);

SDL_Window *w = SDL_CreateWindow("Open GL", 0, 0, 1000, 1000, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

SDL_GLContext ctx = SDL_GL_CreateContext(w);

// values returned by SDL_GL_GetAttribute are commented
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); // doesn't help
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_CONTEXT_MAJOR_VERSION, 4); // 4
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_CONTEXT_MINOR_VERSION, 5); // 5, these two accept even 10.10 without error actually, I also tried not calling them and had 2.1 in return
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_DOUBLEBUFFER, 1); // 1
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_RED_SIZE, 8); // 8
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_GREEN_SIZE, 8); // 8
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_BLUE_SIZE, 8); // 8
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_DEPTH_SIZE, 24); // 16
SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_STENCIL_SIZE, 8); // 0

//SDL_GLContext ctx = SDL_GL_CreateContext(w); // nothing gets rendered if this is called here instead

//SDL_GL_MakeCurrent(w, ctx); // doesn't help

if (ctx == 0){ // never fails
    cout << "context creation error" << endl;
}

glewExperimental = true;
GLenum e = GLEW_OK;
e = glewInit();
if (e != GLEW_OK){ // never fails
    cout << "glew error" << endl;
}

我的模板缓冲区没有工作,所以我在调查中找到了这个。所有SDL_GL_SetAttribute函数返回0.相同的代码(不包括glew)在具有Ubuntu的笔记本电脑上进行了测试,并返回24/8深度/模板。我做错了什么?

2 个答案:

答案 0 :(得分:4)

documentation of SDL_GL_SetAttribute

  

使用此功能在创建窗口之前设置OpenGL窗口属性

如果在创建窗口后调用此函数,则此函数无效。

答案 1 :(得分:3)

您无法更改现有上下文的属性。 SDL_GL_SetAttribute将设置SDL将在下次创建GL上下文时使用的属性。

现在你真的试图在以后创建上下文:

//SDL_GLContext ctx = SDL_GL_CreateContext(w); // nothing gets rendered if this is called here instead

最可能的解释是

SDL_GL_SetAttribute(SDL_GLattr::SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); // doesn't help

实际上确实有效,而且您的代码与核心配置文件无法匹配。