Modern OpenGL context failure

OK, I managed to create an OpenGL context with wglcreatecontextattribARB with version 3.2 in my attrib struct (So I have initialized a 3.2 opengl context).

It works, but the strange thing is, when I use glBindBuffer e,g. I still get unreferenced linker error, shouldn't a newer context prevent this?

I'm on windows BTW, Linux doesn't have to deal with older and newer contexts (it directly supports the core of its version). The code:

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;
    }

You will still need to

  • Declare function pointers with the apropriate names and function signatures.
  • Fetch the correct memory locations for those pointers with wglGetProcAddress
  • #define the actual OpenGL API names to the corresponding function pointers.
  • That's right, the OpenGL API functions are actually function pointers.

    If you don't have the time and patience to do this, then it is advised to use an OpenGL loader library, like GL3W or GLEW. This'll also save you of the burden of first creating your dummy context and then the "real" context.

    Also see the OpenGL wiki page on loading function pointers.

    链接地址: http://www.djcxy.com/p/38456.html

    上一篇: OpenGL无法与VBO绘制三角形

    下一篇: 现代OpenGL上下文失败