【问题标题】:wglCreateContextAttribsARB() gives me 4.4 when I ask for 2.0当我要求 2.0 时,wglCreateContextAttribsARB() 给了我 4.4
【发布时间】:2014-10-04 18:52:16
【问题描述】:

在我的机器上,当我调用 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;
}

【问题讨论】:

    标签: c windows winapi opengl wgl


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-26
      • 1970-01-01
      相关资源
      最近更新 更多