当我要求2.0时,wglCreateContextAttribsARB()给了我4.4

时间:2014-08-11 20:27:15

标签: c windows winapi opengl wgl

在我的机器上,当我呼叫wglCreateContextAttribsARB()要求2.0上下文时,我回到了2.1.2。

这似乎足以纠正,所以它并不担心我。然而,当在朋友的机器上调用时,相同的精确代码给出了4.4上下文。这是否有意义,或者我应该在某处寻找错误?代码如下。 GKGLLoaderGKGLContext模块是我自己编写的模块,您可能只能通过查看其用法来弄清楚它们是如何工作的,因此我不会发布他们的文章源文件(除非有人认为它可能是相关的)。

#include <GraphicsKit/GKGLLoader.h>
#include <GraphicsKit/GKGLContext.h>

static
LRESULT CALLBACK DemoWindowProc(
    HWND win, UINT msg, WPARAM wparam, LPARAM lparam)
{
    if( msg == WM_CLOSE )
    {
        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(win, msg, wparam, lparam);
}

ATOM
RegisterDemoWindowClass(void)
{
    WNDCLASSEX cls;

    cls.cbSize = sizeof(cls);
    cls.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    cls.lpfnWndProc = DemoWindowProc;
    cls.cbClsExtra = 0;
    cls.cbWndExtra = 0;
    cls.hInstance = GetCurrentProcess();
    cls.hIcon = NULL;
    cls.hCursor = LoadCursor(NULL, IDC_ARROW);
    cls.hbrBackground = GetStockObject(BLACK_BRUSH);
    cls.lpszMenuName = NULL;
    cls.lpszClassName = L"GraphicsDemo";
    cls.hIconSm = NULL;

    return RegisterClassEx(&cls);
}

HWND
CreateDemoWindow(void)
{
    HWND win;

    win = CreateWindowEx(
        WS_EX_APPWINDOW | WS_EX_OVERLAPPEDWINDOW,
        L"GraphicsDemo",
        L"GraphicsDemo",
        WS_TILEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT,
        500, 500,
        NULL, NULL, GetCurrentProcess(), NULL
    );

    return win;
}

BOOL
ChooseDemoPixelFormat(HWND win, int* formatID)
{
    unsigned numFormats;

    int attribList[] =
    {
        WGL_DRAW_TO_WINDOW_ARB, TRUE,
        WGL_SUPPORT_OPENGL_ARB, TRUE,
        WGL_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
        WGL_DOUBLE_BUFFER_ARB, TRUE,
        WGL_SWAP_METHOD_ARB, WGL_SWAP_EXCHANGE_ARB,
        WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
        WGL_COLOR_BITS_ARB, 24,
        WGL_RED_BITS_ARB, 8,
        WGL_GREEN_BITS_ARB, 8,
        WGL_BLUE_BITS_ARB, 8,
        WGL_ALPHA_BITS_ARB, 8,
        0, 0
    };

    if(wglChoosePixelFormatARB(
        GetDC(win), attribList, NULL, 1, formatID, &numFormats))
    {
        if( numFormats == 0 )
        {
            _cprintf(
                "wglChoosePixelFormatARB() returned no "
                "formats [GetLastError(): (0x%X)]\n",
                (unsigned)GetLastError());

            SetLastError(ERROR_NOT_SUPPORTED);

            return FALSE;
        }

        return TRUE;
    }

    return FALSE;
}

HGLRC
CreateDemoContext(HWND win)
{
    int attribList[] = {
        WGL_CONTEXT_MAJOR_VERSION_ARB, 2,
        WGL_CONTEXT_MINOR_VERSION_ARB, 0,
        0
    };

    return wglCreateContextAttribsARB(GetDC(win), NULL, attribList);
}

int
APIENTRY
WinMain(
    HINSTANCE instance,
    HINSTANCE prevInstance,
    LPSTR cmdline,
    int showCmd)
{
    GKGLContext* dummyCtx;
    GKGLLoader* loader;

    MSG msg;

    GKGLContext* ctx;
    HWND win;
    HGLRC glrc;

    PIXELFORMATDESCRIPTOR pfd;
    int formatID;

    AllocConsole();

    if( ! RegisterDemoWindowClass() )
    {
        MessageBox(
            NULL, L"RegisterDemoWindowClass() failed", L"ERROR", MB_OK);
        return 0;
    }

    win = CreateDemoWindow();
    if( win == NULL )
    {
        MessageBox(
            NULL, L"CreateDemoWindow() failed", L"ERROR", MB_OK);
        return 0;
    }

    dummyCtx = GKGLContextNewDummy();
    if( dummyCtx == NULL )
    {
        MessageBox(NULL, L"GKGLDummyWindow() failed", L"ERROR", MB_OK);
        return 0;
    }

    loader = GKGLLoaderNew();
    GKGLSetContext(dummyCtx);
    #define GKGLProc(major, minor, name, ret, params, paramNames) dummyCtx->name = (void*)GKGLLoaderProcAddress(loader, # name, major, minor);
    #define GKGLVoidProc(major, minor, name, ret, params, paramNames) dummyCtx->name = (void*)GKGLLoaderProcAddress(loader, # name, major, minor);
    #include <GraphicsKit/GKGLProcs.h>
    #include <GraphicsKit/GKWGLProcs.h>
    #undef GKGLProc
    #undef GKGLVoidProc

    if( ! ChooseDemoPixelFormat(win, &formatID) )
    {
        MessageBox(
            NULL, L"ChooseDemoPixelFormat() failed", L"ERROR", MB_OK);
        return 0;
    }

    if( ! SetPixelFormat(GetDC(win), formatID, &pfd) )
    {
        MessageBox(
            NULL, L"SetPixelFormat() failed", L"ERROR", MB_OK);
        return 0;
    }

    glrc = CreateDemoContext(win);
    if( glrc == NULL )
    {
        MessageBox(
            NULL, L"CreateDemoContext() failed", L"ERROR", MB_OK);
        return 0;
    }

    GKGLSetContext(NULL);
    wglDeleteContext(dummyCtx->glrc);
    DestroyWindow(dummyCtx->win);

    ctx = GKGLContextNew(win, glrc);
    GKGLSetContext(ctx);
    #define GKGLProc(major, minor, name, ret, params, paramNames) ctx->name = (void*)GKGLLoaderProcAddress(loader, # name, major, minor);
    #define GKGLVoidProc(major, minor, name, ret, params, paramNames) ctx->name = (void*)GKGLLoaderProcAddress(loader, # name, major, minor);
    #include <GraphicsKit/GKGLProcs.h>
    #include <GraphicsKit/GKWGLProcs.h>
    #undef GKGLProc
    #undef GKGLVoidProc

    _cprintf("OpenGL Vendor:    %s\n", glGetString(GL_VENDOR));
    _cprintf("OpenGL Renderer:  %s\n", glGetString(GL_RENDERER));
    _cprintf("OpenGL Version:   %s\n", glGetString(GL_VERSION));
    _cprintf("OpenGL Shading Language: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));

    ShowWindow(win, showCmd);

    while( TRUE )
    {
        if( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
        {
            if( msg.message == WM_QUIT )
                break;

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        glClearColor(0, 1, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        SwapBuffers(GetDC(win));
    }

    return msg.wParam;
}

1 个答案:

答案 0 :(得分:4)

只要该4.4上下文是兼容性配置文件,它就完全符合规范。来自WGL_ARB_create_context extension spec

  

如果请求的版本小于或等于3.0,则为上下文       return可以实现以下任何版本:

     
      
  • 任何不低于要求且不超过3.0的版本。
  •   
  • 版本3.1,如果还实现了GL_ARB_compatibility扩展名。
  •   
  • 版本3.2或更高版本的兼容性配置文件。
  •