wglMakeCurrent以1282失败

时间:2015-02-22 20:59:17

标签: c++ opengl

我正在使用OpenGL创建一个visual studio Win32应用程序。但是,当我调用glewInit然后调用GetLastError()时,我得到错误127(“无法找到指定的过程。”)我静态链接到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程序,我错过了什么?我感谢任何帮助或建议。

编辑:glGetError()在调用wglMakeCurrent()后返回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的调用失败了,我无法渲染到我的窗口。在做了一些研究之后,文档here表明用NULL作为HDC调用是完全有效的,因为它被忽略了,渲染上下文应该不是最新的。

新问题:wglMakeCurrent(NULL,NULL)失败的原因是什么?我已经尝试了wglMakeCurrent(m_deviceContext,NULL)以防万一,但它也失败了。

1 个答案:

答案 0 :(得分:2)

  

我正在使用OpenGL创建一个visual studio Win32应用程序。但是,当我调用glewInit然后调用GetLastError()时,我收到错误127(“无法找到指定的过程。”)

一个众所周知的问题是,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仅报告并逐个清除它们。

  

我静态链接到opengl如下:

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


关于wglMakeCurrent(NULL,NULL)的更新

取消绑定OpenGL上下文后,glGetError应报告的错误状态是什么?它没有可以使用的OpenGL上下文,所以它会报告你。 glGetError本身正在执行无效操作,因为您在没有激活OpenGL上下文的情况下调用它。

相关问题