现代 OpenGL 上下文故障

Modern OpenGL context failure

本文关键字:故障 上下文 OpenGL 现代      更新时间:2023-10-16

好的,我设法在我的属性结构中创建了一个带有 3.2 版wglcreatecontextattribARB的 OpenGL 上下文(所以我初始化了一个 3.2 opengl 上下文)。

它可以工作,但奇怪的是,当我使用 glBindBuffer e,g 时。我仍然收到未引用的链接器错误,不应该有更新的上下文阻止这种情况吗?

我在Windows BTW上,Linux不必处理较旧和较新的上下文(它直接支持其版本的核心)。代码:

PIXELFORMATDESCRIPTOR pfd;
    HGLRC tmpRC;
    int iFormat;
    if (!(hDC = GetDC(hWnd)))
    {
        CMsgBox("Unable to create a device context. Program will now close.", "Error");
        return false;
    }
    ZeroMemory(&pfd, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = attribs->colorbits;
    pfd.cDepthBits = attribs->depthbits;
    pfd.iLayerType = PFD_MAIN_PLANE;
    if (!(iFormat = ChoosePixelFormat(hDC, &pfd)))
    {
        CMsgBox("Unable to find a suitable pixel format. Program will now close.", "Error");
        return false;
    }
    if (!SetPixelFormat(hDC, iFormat, &pfd))
    {
        CMsgBox("Unable to initialize the pixel formats. Program will now close.", "Error");
        return false;
    }
    if (!(tmpRC=wglCreateContext(hDC)))
    {
        CMsgBox("Unable to create a rendering context. Program will now close.", "Error");
        return false;
    }
    if (!wglMakeCurrent(hDC, tmpRC))
    {
        CMsgBox("Unable to activate the rendering context. Program will now close.", "Error");
        return false;
    }
    strncpy(vers, (char*)glGetString(GL_VERSION), 3);
    vers[3] = '';
    if (sscanf(vers, "%i.%i", &glv, &glsubv) != 2)
    {
        CMsgBox("Unable to retrieve the OpenGL version. Program will now close.", "Error");
        return false;
    }
    hRC = NULL;
    if (glv > 2) // Have OpenGL 3.+ support
    {
        if ((wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB")))
        {
            int attribs[] = {WGL_CONTEXT_MAJOR_VERSION_ARB, glv, WGL_CONTEXT_MINOR_VERSION_ARB, glsubv,WGL_CONTEXT_FLAGS_ARB, 0,0};
            hRC = wglCreateContextAttribsARB(hDC, 0, attribs);
            wglMakeCurrent(NULL, NULL);
            wglDeleteContext(tmpRC);
            if (!wglMakeCurrent(hDC, hRC))
            {
                CMsgBox("Unable to activate the rendering context. Program will now close.", "Error");
                return false;
            }
            moderncontext = true;
        }
    }
    if (hRC == NULL)
    {
        hRC = tmpRC;
        moderncontext = false;
    }

你仍然需要

  1. 使用专有名称和函数签名声明函数指针。
  2. 使用wglGetProcAddress获取这些指针的正确内存位置
  3. #define 实际的 OpenGL API 名称到相应的函数指针。

没错,OpenGL API函数实际上是函数指针。

如果您没有时间和耐心来执行此操作,那么建议使用 OpenGL 加载程序库,如 GL3W 或 GLEW。这也将为您省去先创建虚拟上下文,然后再创建"真实"上下文的负担。

另请参阅 OpenGL wiki 页面,了解如何加载函数指针。