簡體   English   中英

現代OpenGL上下文失敗

[英]Modern OpenGL context failure

好的,我設法在我的attrib結構中使用wglcreatecontextattribARB和3.2版創建了一個OpenGL上下文(因此,我已經初始化了一個3.2 opengl上下文)。

它可以工作,但是奇怪的是,當我使用glBindBuffer e.g時。 我仍然會收到未引用的鏈接器錯誤,是否應使用較新的上下文來防止此錯誤?

我在Windows順便說一句,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] = '\0';
    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. #將實際的OpenGL API名稱定義為相應的函數指針。

沒錯,OpenGL API函數實際上是函數指針。

如果您沒有時間和耐心來執行此操作,則建議使用GL3W或GLEW之類的OpenGL加載程序庫。 這還將減輕您首先創建虛擬上下文然后創建“真實”上下文的負擔。

另請參見有關加載函數指針OpenGL Wiki頁面

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM