Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
194 views
in Technique[技术] by (71.8m points)

c++ - initializing glad in openGL throws runtime error

I am trying to use openGL and have just setup glad, however when I call gladLoadGLLoader it give mes the error "Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention. ". I have never seen this error before, the exact line of code is the following

    glGetString = (PFNGLGETSTRINGPROC)load("glGetString");

my code is just in a window wrapper class

Window::Window(int _width, int _height, int x, int y, const char* title)
    {
    
        width = _width;
        height = _height;
        windowX = x;
        windowY = y;
        windowTitle = title;


        RECT wr;
        wr.left = 100;
        wr.right = width + wr.left;
        wr.top = 100;
        wr.bottom = height + wr.top;
        if (AdjustWindowRect(&wr, WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU, FALSE) == 0)
        {
            Exception::WindowException e("AdjustWindowRect failed", __FILE__, __LINE__, -1);
            throw e;
        }


        /// 
        /// temporary window
        /// 
    

        HWND temporaryHandle = CreateWindowExA(0l, WindowClass::getInstance().getClassName(), "temporary Window", WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
            0, 0, 1, 1, NULL, NULL, WindowClass::getInstance().getHInstance(), NULL);

        HDC temporaryDeviceContext = GetDC(temporaryHandle);

        PIXELFORMATDESCRIPTOR temporaryPFD = { 0 };
        temporaryPFD.nSize = sizeof(PIXELFORMATDESCRIPTOR);
        temporaryPFD.nVersion = 1;
        temporaryPFD.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        temporaryPFD.iPixelType = PFD_TYPE_RGBA;
        temporaryPFD.cColorBits = 32;
        temporaryPFD.cAlphaBits = 8;
        temporaryPFD.cDepthBits = 24;


        int temporaryPFDSuccess = ChoosePixelFormat(temporaryDeviceContext, &temporaryPFD);

        if (temporaryPFDSuccess == 0)
        {
            Exception::WindowException e("ChoosePixelFormat failed", __FILE__, __LINE__, -1);
            throw e;
        }


        if (SetPixelFormat(temporaryDeviceContext, temporaryPFDSuccess, &temporaryPFD) == false)
        {
            Exception::WindowException e("SetPixelFormat failed", __FILE__, __LINE__, -1);
            throw e;
        }


        HGLRC temporaryContext = wglCreateContext(temporaryDeviceContext);
        if (temporaryContext == 0)
        {
            Exception::WindowException e("wglCreateContext failed", __FILE__, __LINE__, -1);
            throw e;
        }

        if (wglMakeCurrent(temporaryDeviceContext, temporaryContext) == 0)
        {
            Exception::WindowException e("wglMakeCurrent failed", __FILE__, __LINE__, -1);
            throw e;

        }
    

        PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB = nullptr;
        wglChoosePixelFormatARB = reinterpret_cast<PFNWGLCHOOSEPIXELFORMATARBPROC>(wglGetProcAddress("wglChoosePixelFormatARB"));
        if (wglChoosePixelFormatARB == nullptr)
        {
            Exception::WindowException e("wglChoosePixelFormatARB did not load properly", __FILE__, __LINE__, -1);
            throw e;
        }

        PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = nullptr;
        wglCreateContextAttribsARB = reinterpret_cast<PFNWGLCREATECONTEXTATTRIBSARBPROC>(wglGetProcAddress("wglCreateContextAttribsARB"));
        if (wglCreateContextAttribsARB == nullptr)
        {
            Exception::WindowException e("wglCreateContetAttribsARB did not load properly", __FILE__, __LINE__, -1);
            throw e;
        }



        ///
        /// real window
        /// 


        windowHandle = CreateWindowExA(0l, WindowClass::getInstance().getClassName(),
            windowTitle, WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
            windowX, windowY, wr.right - wr.left, wr.bottom - wr.top, nullptr, nullptr, WindowClass::getInstance().getHInstance(), this);

        HDC realDeviceContext = GetDC(windowHandle);

        const int pixelAttribs[] = {
        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_ACCELERATION_ARB, WGL_FULL_ACCELERATION_ARB,
        WGL_COLOR_BITS_ARB, 32,
        WGL_ALPHA_BITS_ARB, 8,
        WGL_DEPTH_BITS_ARB, 24,
        WGL_STENCIL_BITS_ARB, 8,
        WGL_SAMPLE_BUFFERS_ARB, GL_TRUE,
        WGL_SAMPLES_ARB, 4,
        0
        };


        int pixelFormatID;
        UINT numFormats;
        bool status = wglChoosePixelFormatARB(realDeviceContext, pixelAttribs, NULL, 1, &pixelFormatID, &numFormats);
    
        if (status == false || numFormats == 0)
        {
            Exception::WindowException e("wglChoosePixelFormatARB", __FILE__, __LINE__, -1);
            throw e;
        }


        PIXELFORMATDESCRIPTOR realPFD;
        DescribePixelFormat(realDeviceContext, pixelFormatID, sizeof(realPFD), &realPFD);
        SetPixelFormat(realDeviceContext, pixelFormatID, &realPFD);

        const int major_min = 4, minor_min = 5;
        int  contextAttribs[] = {
            WGL_CONTEXT_MAJOR_VERSION_ARB, major_min,
            WGL_CONTEXT_MINOR_VERSION_ARB, minor_min,
            WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_CORE_PROFILE_BIT_ARB,
            0
        };
    
        HGLRC realContext = wglCreateContextAttribsARB(realDeviceContext, 0, contextAttribs);
        if (realContext == NULL)
        {
            Exception::WindowException e("wglCreateContextAttribsARB failed", __FILE__, __LINE__, -1);
            throw e;
        }
    

        wglMakeCurrent(NULL, NULL);
        wglDeleteContext(temporaryContext);
        ReleaseDC(temporaryHandle, temporaryDeviceContext);
        DestroyWindow(temporaryHandle);
        int madeCurrent = wglMakeCurrent(realDeviceContext, realContext);
        if (madeCurrent == 0)
        {
            Exception::WindowException e("wglMakeCurrent failed", __FILE__, __LINE__, -1);
            throw e;
        }
    
    
        if (!gladLoadGLLoader((GLADloadproc)wglGetProcAddress))
        {
            PostQuitMessage(0);
            return;
        }


        glClear(GL_COLOR_BUFFER_BIT);
        SwapBuffers(realDeviceContext);


        ShowWindow(windowHandle, SW_SHOW);


    }
question from:https://stackoverflow.com/questions/66057453/initializing-glad-in-opengl-throws-runtime-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...