wglMakeCurrent 失败,返回 1282

wglMakeCurrent fails with 1282

我正在使用 OpenGL 创建 visual studio Win32 应用程序。但是,当我调用 glewInit 然后调用 GetLastError() 时,出现错误 127 ("The specified procedure could not be found.") 我静态链接到 opengl 如下:

#define GLEW_STATIC
#include <glew.h>
#include <wglew.h>
#include <gl/gl.h>
#include <gl/glu.h>

// link with libraries
#pragma comment(lib, "openGL32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "glew32s.lib")

我的猜测是,出于某种原因,在 运行 时,我的应用程序找不到该函数,但我不确定为什么,这让我抓狂。我试过将库直接放在可执行目录中,但没有成功。这是有问题的代码:

      // create a device context
  m_deviceContext = GetDC(m_windowHandle);

  // choose a pixel format
  PIXELFORMATDESCRIPTOR pfd;
  memset(&pfd, 0, sizeof(pfd));

  pfd.nSize = sizeof(pfd);
  pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; // double-buffering, opengl, and render to window
  pfd.iPixelType = PFD_TYPE_RGBA;
  pfd.cColorBits = 32; // 32 bits of color information (the higher, the more colors)
  pfd.cDepthBits = 32; // 32 bits of color information (the higher, the more depth levels)
  pfd.iLayerType = PFD_MAIN_PLANE;

  int pixelFormat = ChoosePixelFormat(m_deviceContext, &pfd);
  assert(pixelFormat);

  // set the pixel format
  BOOL res = SetPixelFormat(m_deviceContext, pixelFormat, &pfd);
  assert(res);

  // this creates an openGL 2.1 context for the device context
  HGLRC tempContext = wglCreateContext(m_deviceContext);
  wglMakeCurrent(m_deviceContext, tempContext);

  // enable GLEW so we can try to create a 3.2 context instead
  DWORD error = GetLastError();
  GLenum err = glewInit();
  error = GetLastError();
  assert(err == GLEW_OK);

关于我的设置的一些细节。我将工作目录更改为生成可执行文件的位置 (($ProjectDir)\Binaries\Win32)。 在此之前一切正常。我尝试添加额外的依赖项并更改额外的库目录,但没有成功。

我的程序找不到 glewInit 过程缺少什么?感谢任何帮助或建议。

编辑:调用 wglMakeCurrent() 后 glGetError() returns 1282 (INVALID_OPERATION):

  // attempt to create the opengl 3.2 context
  int attr[] = {
    WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
    WGL_CONTEXT_MINOR_VERSION_ARB, 2,
    WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB,
    0
  };

  if(wglewIsSupported("WGL_ARB_create_context") == 1)
  {
    CheckOpenGLError();
    m_renderContext = wglCreateContextAttribsARB(m_deviceContext, NULL, attr);
    CheckOpenGLError();
    wglMakeCurrent(NULL, NULL);
    CheckOpenGLError();   // <-- This guy calls glGetError() and returns 1282
    wglDeleteContext(tempContext);
    CheckOpenGLError();
    wglMakeCurrent(m_deviceContext, m_renderContext);
  }

我认为的问题不是。看起来我的问题是对 wglMakeCurrent 的调用失败了,我无法渲染到我的 window。在做了一些研究之后,文档 here 表明使用 NULL 作为 HDC 调用是完全有效的,因为它被忽略了,渲染上下文应该不是当前的。

新问题:wglMakeCurrent(NULL, NULL) 失败的原因有哪些?我也试过 wglMakeCurrent(m_deviceContext, NULL) 以防万一,但它也失败了。

I am creating a visual studio Win32 application using OpenGL. However when I call glewInit and then GetLastError(), I get error 127 ("The specified procedure could not be found.")

GLEW 可能会在初始化后使 OpenGL 处于错误状态,这一直是一个众所周知的问题。忽略它。初始化 GLEW 的惯用方式是

if( GLEW_OK != glewInit() ) {
    handle_glew_init_error_condition();
}
else {
    /* flush the OpenGL error state, ignoring all errors */
    while( GL_NO_ERROR != glGetError() );
}

请注意,必须在循环中调用 glGetError,因为它允许设置多个错误条件,并且 glGetError 只会一个接一个地报告和清除它们。

I am statically linking to opengl as follows:

当然不是。 .lib 文件也可以用于动态链接;在这种情况下,它们包含 DLL 的名称和入口点的 table。


有关 wglMakeCurrent(NULL, NULL) 的更新

一旦您取消绑定 OpenGL 上下文,glGetError 应该报告什么错误状态?没有它可以使用的 OpenGL 上下文,因此它向您报告了这一点。 glGetError 本身正在执行无效操作,因为您在没有激活 OpenGL 上下文的情况下调用它。