Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I can't find any information on glCreateContextAttribsARB returning errors if a version is unsuported by the driver. So how do i check if it is? I don't want the program to hard crash because glCreateContextAttribsARB failed and i had no chance to check why it did.

Here's my code, i try to keep it minimalistic, the OpenGLVersion uses the glGetIntegerv to get the major and minor didgits and creates a number out of it. It checks if the version is 'least 3.3, though i've no idea if this actually does anything unless i set up glCreateContextAttribsARB with the wrong numbers.

So when does OpenGL "get a version", so that the glGetIntegerv returns a valid information? Is it something built in or does it return something valid only after i set up the context? How do i check if the OpenGL version i want to use is supported?

void CreateOpenGLContext(HWND hWnd){

    //Set the pixel format

    PIXELFORMATDESCRIPTOR pfd =
    {
        sizeof(PIXELFORMATDESCRIPTOR),
        1,
        PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
        PFD_TYPE_RGBA,
        32,
        0, 0, 0, 0, 0, 0,
        0,
        0,
        0,
        0, 0, 0, 0,
        24,
        8,
        0,
        PFD_MAIN_PLANE,
        0,
        0, 0, 0
    };

    hDC = GetDC( hWnd );
    GLuint PixelFormat = ChoosePixelFormat( hDC, &pfd );
    SetPixelFormat( hDC, PixelFormat, &pfd );

    //Create an OpenGL context to get access to the WGL extensions

    hRC = wglCreateContext( hDC );
    wglMakeCurrent( hDC, hRC );

    //Load functions

    if(glload::LoadFunctions() == glload::LS_LOAD_FAILED){
        MessageBox( NULL, TEXT("Failed to load OpenGL extensions."), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION );
        PostQuitMessage(0);
    }

    //Load WGL extensions

    if(glload::LoadWinFunctions(hDC) == glload::LS_LOAD_FAILED){
        MessageBox( NULL, TEXT("Failed to load WGL extensions."), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION );
        PostQuitMessage(0);
    }

    //Use WGL extensions to create an OpenGL 3.3 context

    const int contextAttributes[] =
    {
        WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
        WGL_CONTEXT_MINOR_VERSION_ARB, 3,
        WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
        0
    };

    HGLRC hRC3 = wglCreateContextAttribsARB( hDC, hRC, contextAttributes);

    wglMakeCurrent( hDC, hRC3 );
    wglDeleteContext( hRC );
    hRC = hRC3;

    //Check if the system supports OpenGL version 3.3

    if(OpenGLVersion() < 33){
        MessageBox( NULL, TEXT("Your system doesn't support OpenGL version 3.3"), TEXT("Error"), MB_OK | MB_ICONEXCLAMATION );
        PostQuitMessage(0);
    }
}
share|improve this question
    
You should use GLEE –  bobobobo May 2 '12 at 17:22
    
how does that help me, i know how to load extensions –  dreta May 2 '12 at 17:28
    
I could be wrong, but you specify the version you'd like (3.3), create the context, and afterwards (if successful) int glVersion[2] = {0, 0}; glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]); glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]); will tell you what you actually got (i.e. what is supported) –  melak47 May 2 '12 at 17:30
    
yea i'm doing that, i just don't know if this is going to yield the minimum version supported, f.e. 2.1 if the version i specify in the wgl function isn't availible, i want to know when the number is set and if wglCreateContextARB is going to crash if the version isn't supported or something and what kind of version do i get when i first set up the context without the wgl extensions that are required for opengl 3.x and up –  dreta May 2 '12 at 17:48
    
@bobobobo: GLEE is dead. Hasn't been updated in years. –  Nicol Bolas May 2 '12 at 17:56
add comment

1 Answer

up vote 3 down vote accepted

I'd like to know if the way i'm checking if the OpenGL version of choice (3.3) is availible.

Allow me to state this more clearly:

The only way to know if your OpenGL implementation supports a particular version is to attempt to create a context with that version and see if you get a valid context as a result. wglCreateContextAttribsARB will either return the version you requested (or a backwards-compatible higher version), or it will fail. And failure is the only way to know what is and is not supported.


I can't find any information on glCreateContextAttribsARB returning errors if a version is unsuported by the driver.

Ahem:

On failure wglCreateContextAttribsARB returns NULL. Extended error information can be obtained with GetLastError. Conditions that cause failure include:

...

If the pixel format associated with does not support OpenGL contexts providing the requested API major and minor version, forward-compatible flag, and/or debug context flag, then ERROR_INVALID_PIXEL_FORMAT is generated.

So just check to see if you got an actual context. If you didn't, then it failed.

But really, there's not much point to having a check/fail/retry cycle. You wrote your code expecting some minimum version of OpenGL. That version is what you should ask for. If you happen to get a version higher than this version, great. But no matter what, you will get a version of OpenGL that is backwards compatible with what you asked for.

share|improve this answer
    
Yea, it returns NULL, but that's related to the function attributes and it doesn't say in the errors if the OpenGL version is supported or not, only says if they're defined so you won't try 7.52 or something. I know that i can expect a version, i am doing it, i want to use OpenGL 3.3 and if it isn't supported i exit. It's just that i don't know if wglCreateContext will set the version as the maximum supported one so i can check it or will it just return null. I'd like to know if the way i'm checking if the OpenGL version of choice (3.3) is availible. –  dreta May 2 '12 at 18:15
    
since you're reading it probably, i'll say additionaly "Querying the GL_VERSION string with glGetString (or the GL_MAJOR_VERSION and GL_MINOR_VERSION values with glGetIntegerv, in a 3.0 or later context) will return the actual version supported by a context." doesn't help me either, because the only exeptions mentioned are that if you specify openGL 3.2 or higher you get 'least that, so i still don't know if i'll get f.e. 2.1 as a resault of version check if that's the latest version supported by the driver, i need a clarification on this specifically –  dreta May 2 '12 at 18:17
    
@dreta: See my edit. –  Nicol Bolas May 2 '12 at 18:24
    
Thank you very much. –  dreta May 2 '12 at 18:40
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.