在我的机器上,当我打电话给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;
}

最佳答案

这完全在规范中,只要4.4上下文是一个兼容性概要文件。从WGL_ARB_create_context extension spec开始:
如果请求的版本小于或等于3.0,则上下文
returned可以实现以下任何版本:
任何不低于请求的版本且不高于3.0的版本。
版本3.1,如果还实现了gl_arb_兼容性扩展。
版本3.2或更高版本的兼容性配置文件。

09-10 04:06