OpenGL调用损坏的内存

时间:2012-08-09 00:07:02

标签: winapi opengl-3

当我调用glGetIntergerv或任何其他opengl函数,并在gdb中逐步执行它时,gdb会跳过几行并继续单步执行代码。

下面是加载opengl和windows的代码。它是唯一一个在glGetIntergerv之前运行的代码,第一个是opengl调用。

HWND window;
HDC dev_context;

HGLRC rend_context;
//Creating the Window
    char const *name = "Opengl Test";
    HINSTANCE inst = (HINSTANCE)GetModuleHandle(0);
    WNDCLASS windowClass;
    DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
    windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    windowClass.lpfnWndProc = (WNDPROC) WndProcedure;
    windowClass.cbClsExtra = 0;
    windowClass.cbWndExtra = 0;
    windowClass.hInstance = inst;
    windowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);
    windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    windowClass.hbrBackground = NULL;
    windowClass.lpszMenuName = NULL;
    windowClass.lpszClassName = name;
    RegisterClass(&windowClass);

    window = CreateWindowEx(dwExStyle, name, name, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, 0, 0, NULL, NULL, inst, NULL);

//Context
    dev_context = GetDC( window );
    std::cout << dev_context << std::endl;
    //Get pixel format
    PIXELFORMATDESCRIPTOR pfd;
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
    pfd.nSize  = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion   = 1;
    pfd.dwFlags    = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 32;
    pfd.cDepthBits = 32;
    pfd.iLayerType = PFD_MAIN_PLANE;

    int nPixelFormat = ChoosePixelFormat(dev_context, &pfd);
    SetPixelFormat( dev_context, nPixelFormat, &pfd );

    HGLRC temp_rend_context = wglCreateContext( dev_context );
    wglMakeCurrent( dev_context, temp_rend_context );

    HGLRC (WINAPI *wglCreateContextAttribsARB) (HDC hDC, HGLRC hShareContext, const int *attribList) = (HGLRC (WINAPI *) (HDC hDC, HGLRC hShareContext, const int *attribList)) gl3wGetProcAddress("wglCreateContextAttribsARB");

    const int attribs[] = { WGL_CONTEXT_MAJOR_VERSION_ARB, 3,  WGL_CONTEXT_MINOR_VERSION_ARB, 0, WGL_CONTEXT_FLAGS_ARB, /*WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB*/0, 0};

    rend_context = wglCreateContextAttribsARB(dev_context, 0, attribs);
    wglMakeCurrent(0,0);
    wglDeleteContext(temp_rend_context);
    wglMakeCurrent(dev_context, rend_context);

    gl3wInit();

    int glVersion[2] = {-1, -1};
    glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]); //First gl call
    glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]);

以下是我的WndProcedure功能:

static LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam){
    switch(Msg){
        case WM_DESTROY:
            PostQuitMessage(WM_QUIT);
            return 0;
        default:
            return DefWindowProc(hWnd, Msg, wParam, lParam);
    }
}

我正在使用gl3w库来加载opengl函数。

2 个答案:

答案 0 :(得分:1)

听起来你在调用约定或参数列表时都会出现不匹配,或两者都不匹配,这会破坏堆栈,足以搞砸回叫地址。

仔细检查您正在编译的opengl .h文件是否与您正在调用的opengl .dll的版本匹配。仔细检查是否为.h文件定义并启用了Windows所需的任何条件定义。在Win API调用中调用约定的规范是STDCALL。如果你在.h文件中看不到gl函数的调用约定,那就要多了。

我模糊地回忆一下,STDCALL和cdecl调用约定以相同的顺序(从右到左)将参数压入堆栈,但是在调用之后谁负责调整堆栈指针有所不同。我相信STDCALL期望被调用者弹出堆栈,而使用cdecl,调用者在调用返回后恢复堆栈指针。

这意味着如果调用者正在进行cdecl调用但被调用者实际上是STDCALL,那么参数将使其进入调用就好了,但是返回时所有地狱都会破坏。根据不匹配的运行方式,堆栈指针根本不会被调整,或者它将被过度调整(调整两次)。

答案 1 :(得分:0)

这是我使用的代码创建GL上下文并使用GL3功能。 现在我知道这是C#,但你得到了图片。没有理由创建两个GL上下文来使用OpenGL3 ...除非我完全不知道你的意思。

void Init(IntPtr handle, bool fullscreen, bool vSync)
{
                this.handle = handle;
                #if WINDOWS
                //Get DC
                dc = WGL.GetDC(handle);
                WGL.SwapBuffers(dc);

                //Set BackBuffer format
                WGL.PIXELFORMATDESCRIPTOR pfd = new WGL.PIXELFORMATDESCRIPTOR();
                WGL.ZeroPixelDescriptor(ref pfd);
                pfd.nVersion        = 1;
                pfd.dwFlags         = WGL.PFD_DRAW_TO_WINDOW | WGL.PFD_SUPPORT_OPENGL | WGL.PFD_DOUBLEBUFFER;
                pfd.iPixelType      = (byte)WGL.PFD_TYPE_RGBA;
                pfd.cColorBits      = 24;
                pfd.cAlphaBits      = 8;
                pfd.cDepthBits      = 16;
                pfd.iLayerType      = (byte)WGL.PFD_MAIN_PLANE;
                unsafe{pfd.nSize = (ushort)sizeof(WGL.PIXELFORMATDESCRIPTOR);}

                int pixelFormatIndex = WGL.ChoosePixelFormat(dc, ref pfd);
                if (pixelFormatIndex == 0) Debug.ThrowError("Video", "ChoosePixelFormat failed");
                if (WGL.SetPixelFormat(dc, pixelFormatIndex, ref pfd) == 0) Debug.ThrowError("Video", "Failed to set PixelFormat");

                ctx = WGL.CreateContext(dc);
                if (ctx == IntPtr.Zero) Debug.ThrowError("Video", "Failed to create GL context");
                if (WGL.MakeCurrent(dc, ctx) == 0) Debug.ThrowError("Video", "Failed to make GL context current");

                WGL.Init();//<< load 'wglSwapIntervalEXT'
                WGL.SwapInterval(vSync ? 1 : 0);
}

加载GL扩展程序:

public const string DLL = "opengl32";
[DllImport(DLL, EntryPoint = "wglGetProcAddress", ExactSpelling = true)]
private static extern IntPtr getProcAddress(string procedureName);