关于C#:wglMakeCurrent失败,出现1282

wglMakeCurrent fails with 1282

我正在使用OpenGL创建Visual Studio Win32应用程序。但是,当我依次调用glewInit和GetLastError()时,出现错误127("找不到指定的过程。")我静态链接到opengl,如下所示:

1
2
3
4
5
6
7
8
9
10
#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")

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
      // 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()返回1282(INVALID_OPERATION):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
  // 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的调用失败,并且我无法渲染到我的窗口。经过研究后,此处的文档建议使用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的惯用方式是

1
2
3
4
5
6
7
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的名称和入口点表。

关于wglMakeCurrent(NULL,NULL)的更新

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