2013-10-19 54 views
0

好吧,我設法創建了一個OpenGL上下文與wglcreatecontextattribARB版本3.2在我的attrib結構(所以我已經初始化3.2 opengl上下文)。現代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; 
    } 
+0

我想這個問題屬於stackoverflow。 – Shekhar

回答

4

您仍需要與apropriate名和函數簽名

  1. 聲明函數指針。
  2. 爲這些指針獲取正確的內存位置wglGetProcAddress
  3. #將實際的OpenGL API名稱定義爲相應的函數指針。

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

如果您沒有時間和耐心來做到這一點,那麼建議使用OpenGL加載器庫,如GL3W或GLEW。這也將爲您節省首先創建您的虛擬背景,然後創建「真實」背景的負擔。

另請參閱OpenGL wiki page on loading function pointers

+0

那我能問一下,創建一個更新的上下文的全部用處是什麼?如果需要和舊的上下文一樣(使用函數指針和擴展聲明來訪問新的API),是不是創建一個完全沒用的新上下文? – user209347

+0

@ user209347簡短回答:不,它不是無用的。閱讀[wgl_create_context](https://www.opengl.org/registry/specs/ARB/wgl_create_context.txt)中的「概述」部分,瞭解其背後的動機。 – user2802841

+0

另請參見wglGetProcAddress – luiscubal

相關問題