glewInit函数抛出错误代码0x0000007F(C ++)

时间:2015-06-18 04:54:26

标签: c++ opengl glew

首先,我创建了一个旧的OpenGL上下文来加载OpenGL扩展。之后,我尝试创建一个新的OpenGL上下文,但函数'wglCreateContextAttribsARB'抛出了错误代码0xC007000D。所以我锁定了错误的位置,我看到glewInit是抛出错误代码0x0000007F的函数,但是glewInit返回GLEW_OK,这意味着glewInit中有任何错误......

PIXELFORMATDESCRIPTOR *PixelFormat = new PIXELFORMATDESCRIPTOR[1];
    HWND *hFakeWnd = new HWND[1];
    int ChoosenPixelFormat = 0;
    UINT NumFormats = 0;

    *hFakeWnd = CreateWindowEx(0, cWindow.lpszClassName, L"Fake", 0, 0, 0, 1, 1, HWND_DESKTOP, NULL, GetModuleHandle(NULL), NULL);

    ZeroMemory(PixelFormat, sizeof(PIXELFORMATDESCRIPTOR));
    PixelFormat->nSize = sizeof(PIXELFORMATDESCRIPTOR);
    PixelFormat->nVersion = 1;
    PixelFormat->iPixelType = PFD_TYPE_RGBA;
    PixelFormat->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    PixelFormat->cColorBits = 32;
    PixelFormat->cDepthBits = 24;
    PixelFormat->cStencilBits = 8;
    PixelFormat->iLayerType = PFD_MAIN_PLANE;

    hDC = GetDC(*hFakeWnd);

    if(hDC == NULL) {
        PostErrorMessage(hWnd, "Error getting a device context from a window!");
        return false;
    }

    ChoosenPixelFormat = ChoosePixelFormat(hDC, PixelFormat);

    if(!ChoosenPixelFormat) {
        PostErrorMessage(hWnd, "Error choosing a valid pixel format (old)!");
        return false;
    }

    if(!SetPixelFormat(hDC, ChoosenPixelFormat, PixelFormat)) {
        PostErrorMessage(hWnd, "Error setting a pixel format (old)!");
        return false;
    }

    hRC = wglCreateContext(hDC);

    if(!hRC) {
        PostErrorMessage(hWnd, "Error creating an old OpenGL context!");
        return false;
    }

    wglMakeCurrent(hDC, hRC);

    //MessageBoxA(hWnd, (char *)glGetString(GL_VERSION), "Version", MB_OK | MB_ICONERROR);
    glewExperimental = true;

    if(glewInit() != GLEW_OK) {
        PostErrorMessage(hWnd, "Error linking OpenGL functions!");
        return false;
    }

    destroyOpenGLContext(*hFakeWnd, hDC, hRC);

    hDC = GetDC(hWnd);

    if(hDC == NULL) {
        PostErrorMessage(hWnd, "Error getting a device context from a window!");
        return false;
    }

    const int *AttribList = new int[] {
        WGL_DRAW_TO_WINDOW_ARB, GL_TRUE,
        WGL_SUPPORT_OPENGL_ARB, GL_TRUE,
        WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
        WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
        WGL_COLOR_BITS_ARB, 32,
        WGL_DEPTH_BITS_ARB, 24,
        WGL_STENCIL_BITS_ARB, 8,
        0
    };

    if(!wglChoosePixelFormatARB(hDC, AttribList, NULL, 1, &ChoosenPixelFormat, &NumFormats)) {
        PostErrorMessage(hWnd, "Error choosing a valid pixel format (new)!");
        return false;
    }

    const int *ContextAttribList = new int[] {
        WGL_CONTEXT_MAJOR_VERSION_ARB, 4, 
        WGL_CONTEXT_MINOR_VERSION_ARB, 5, 
        0
    };

    hRC = wglCreateContextAttribsARB(hDC, NULL, ContextAttribList);

    char error[9];
    int nError = GetLastError();
    _itoa_s(nError, error, (size_t)9, 16);
    PostErrorMessage(hWnd, error);

    if(!hRC) {
        PostErrorMessage(hWnd, "Error creating an OpenGL context (new)!");
        return false;
    }

    wglMakeCurrent(hDC, hRC);

    delete[] AttribList;
    delete[] ContextAttribList;
    delete[] PixelFormat;
    delete[] hFakeWnd;

    return true;

0 个答案:

没有答案