Alex
Alex

Reputation: 3

Can't make OpenGL, glew, and SDL2 work together

// 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;
}

My stencil buffer wasn't working so I came to this in my investigation. All SDL_GL_SetAttribute functions return 0. The same code (excluding glew) was tested on a laptop with Ubuntu and returns 24/8 for depth/stencil. What am I doing wrong?

Upvotes: 0

Views: 802

Answers (2)

derhass
derhass

Reputation: 45362

You cannot change the attributes of an existing context. SDL_GL_SetAttribute will set the attributes that SDL will use the next time a GL context is created.

Now you actually tried to create the context later:

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

The most likely explanation is that the

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

actually does work, and your code is just not comatible with a core profile.

Upvotes: 3

BDL
BDL

Reputation: 22174

The documentation of SDL_GL_SetAttribute states

Use this function to set an OpenGL window attribute before window creation.

This functions do not have any effect if called after the window has been created.

Upvotes: 4

Related Questions