Sign in to follow this  
soconne

OpenGL losing HDC, rendering stops, need help

Recommended Posts

I have an app that renders to a Panel control. So upon startup I initialize openGL to use the HDC of the Panel and create a HGLRC associated with that HDC. Well I'm also using a suite of docking controls, http://www.devexpress.com, called ExpressBars. All I'm using it for is to dock the different panels in the app. Anyway, when I undock and redock a panel, the original Panel that I set OpenGL to render to, its HDC value changes. I havn't been able to figure out why the heck its Handle changes, but it does. So how do I reinitialize OpenGL to use the new HDC? I've tried simply re-initializing it, but NOT re-creating the HGLRC because I would lose all of my textures, VBO's, ect... But when I reset OpenGL to use the new HDC, meaning I call SetPixelFormat again on the HDC, when I try and render a previously initialized VBO my app crashes with an error from the Nvidia driver. Anyway, here's my code for initializing the context.
bool RenderSystemOpenGL::start(HDC hdc, const int width, const int height, const int bpp, const int depthBpp)
{
        hDC = hdc;

        PIXELFORMATDESCRIPTOR pfd = {
    	        sizeof(PIXELFORMATDESCRIPTOR),
                1,
                PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER,
                PFD_TYPE_RGBA,
                depthBpp,
                0,0,0,0,0,0,
                0,0,
                0,0,0,0,0,
                bpp,
                0,
                0,
                PFD_MAIN_PLANE,
                0,
                0,0,
        };
        int PixelFormat = ChoosePixelFormat(hDC, &pfd);
        SetPixelFormat(hDC, PixelFormat, &pfd);

        static bool flag = false;
        if(!flag)
        {
                wglDeleteContext(hRC);
                hRC = wglCreateContext(hDC);

                if(hRC == NULL)
    	                return false;

                if(wglMakeCurrent(hDC, hRC) == false)
    	                return false;

                initExtensions(hDC);
                ilInit();

                resize(width, height);

                glEnable(GL_DEPTH_TEST);
                glDepthFunc(GL_LEQUAL);

                glEnable(GL_NORMALIZE);
        }

        return true;
}

The purpose of the static flag variable is because I set the pixel format for 3 different Panels so I can switch between then during rendering. This already works just fine. But how do I reset OpenGL to use the new HDC value without it crashing when I tried to access the old VBO or texture ?

Share this post


Link to post
Share on other sites
Firstly, the fact that the HDC value changes should not matter UNLESS the device context that it represents has changed. This means you can use GetDC(...) and ReleaseDC(...) to get a handle to the context when you need one.

Since you say that rendering stops after the HDC value "changes", I can only assume that for some bizzare reason, the device context is being destroyed and recreated, in which case, you'll HAVE to recreate the rendering context. But fear not..

Before you destroy the old rendering context, you can create a new rendering context, then use wglShareLists(...) to give the new context access to all your loaded textures etc. You can then proceed to destroy the old context.

Share this post


Link to post
Share on other sites
I fixed it. I'm using the same code as above, I just forgot to set flag = true inside the if statement. So it was crashing because in the middle of rendering, it created a new HGLRC, which deleted all the old data. Thanks for your help though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this