wglShareLists失败,错误6:ERROR_INVALID_HANDLE句柄无效(wglSh

2019-09-29 10:07发布

我试着两个类之间共享一个HPBUFFERARB:TGLForm和TGLForm2。 (我试过FBO,但有一个老的Borland Builder中6版采用FBO我不能管理)

我的目标是两个openGL的窗口来显示同样的缓冲。

因此,我宣布第一种形式这个对象之外:

struct GLRenderToTexture
{
struct
{
    HDC          hdc;
    HGLRC        hGlRc;
    HPBUFFERARB  hBuffer;
    PFNWGLGETEXTENSIONSSTRINGARBPROC wglGetExtensionsStringARB;
    PFNWGLCHOOSEPIXELFORMATARBPROC wglChoosePixelFormatARB;
    PFNWGLCREATEPBUFFERARBPROC wglCreatePbufferARB;
    PFNWGLGETPBUFFERDCARBPROC wglGetPbufferDCARB;
    PFNWGLQUERYPBUFFERARBPROC wglQueryPbufferARB;
    PFNWGLDESTROYPBUFFERARBPROC wglDestroyPbufferARB;
    PFNWGLRELEASEPBUFFERDCARBPROC wglReleasePbufferDCARB;
    PFNWGLBINDTEXIMAGEARBPROC wglBindTexImageARB;
    PFNWGLRELEASETEXIMAGEARBPROC wglReleaseTexImageARB;
} wgl;
unsigned int  texture;  // the texture we're going to render to
};

GLRenderToTexture  RTT;

我intialize它以具有相同的像素格式为第一GLForm:

void __fastcall TGLForm::FormCreate(TObject *Sender)
{
    ghDC = GetDC(Handle);
    if (!bSetupPixelFormat(ghDC)) Close();
    ghRC = wglCreateContext(ghDC);
wglMakeCurrent(ghDC, ghRC);
    InitializeGL();

    int     pixelFormats;
    int     intAttrs[32] ={WGL_RED_BITS_ARB,8,WGL_GREEN_BITS_ARB,8,WGL_BLUE_BITS_ARB,8,WGL_ALPHA_BITS_ARB,8,WGL_DRAW_TO_PBUFFER_ARB, GL_TRUE,WGL_BIND_TO_TEXTURE_RGBA_ARB, GL_TRUE,WGL_SUPPORT_OPENGL_ARB,GL_TRUE,WGL_ACCELERATION_ARB,WGL_FULL_ACCELERATION_ARB,WGL_DOUBLE_BUFFER_ARB,GL_FALSE,0}; // 0 terminate the list
    unsigned int numFormats = 0;
    // get an acceptable pixel format to create the PBuffer with
    if (RTT.wgl.wglChoosePixelFormatARB(ghDC, intAttrs, NULL, 1, &pixelFormats, &numFormats)==FALSE)
        AnsiString error = AnsiString().sprintf("wglChoosePixelFormatARB returned %i", GetLastError()); // GetLastError will tell us why it failed

    //Set some p-buffer attributes so that we can use this p-buffer as a 2d texture target
    const int attributes[]= {WGL_TEXTURE_FORMAT_ARB,  WGL_TEXTURE_RGBA_ARB, // p-buffer will have RBA texture format
                    WGL_TEXTURE_TARGET_ARB, WGL_TEXTURE_2D_ARB, 0}; // Of texture target will be GL_TEXTURE_2D
    // the size of the PBuffer must be the same size as the texture
    RTT.wgl.hBuffer= RTT.wgl.wglCreatePbufferARB(ghDC, pixelFormats, ClientWidth, ClientHeight, attributes);
    RTT.wgl.hdc= RTT.wgl.wglGetPbufferDCARB(RTT.wgl.hBuffer);
    RTT.wgl.hGlRc= wglCreateContext(RTT.wgl.hdc);

wglMakeCurrent(NULL,NULL);
}

这是我第一次drawScene函数:在“PaintGL()”绘图这种形式的完美得出:

void TGLForm::DrawSceneForm1()
{
wglMakeCurrent(ghDC, ghRC);
            ClientWidth = 1920;
            ClientHeight = 1080;

    // create a texture to use as the backbuffer
    glGenTextures(1, &RTT.texture);
    glBindTexture(GL_TEXTURE_2D, RTT.texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    // make sure this is the same color format as the screen
    glTexImage2D(GL_TEXTURE_2D, 0, 4,  ClientWidth, ClientHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);


    // switch to the texture context
wglMakeCurrent(RTT.wgl.hdc, RTT.wgl.hGlRc);
    glEnable(GL_TEXTURE_2D);              // Enable Texture Mapping
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);

    glClear(GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClearColor(0,0,0,1);
    glClear(GL_COLOR_BUFFER_BIT);

    glDisable(GL_TEXTURE_2D);


    // switch back to the screen context
wglMakeCurrent(ghDC, ghRC);
    wglShareLists(ghRC, RTT.wgl.hGlRc);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);

    glClear(GL_DEPTH_BUFFER_BIT);
    glViewport(0, 0, ClientWidth, ClientHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


wglMakeCurrent(RTT.wgl.hdc, RTT.wgl.hGlRc);
    glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, RTT.texture);
        PaintGL();
    glDisable(GL_TEXTURE_2D);


wglMakeCurrent(ghDC, ghRC);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, RTT.texture);
    RTT.wgl.wglBindTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);

    glBegin(GL_QUADS);
        glColor4ub(255,255,255,255);
        glTexCoord2f (0.0, 0.0); glVertex2f (-1.0, -1.0);
        glTexCoord2f (1.0, 0.0); glVertex2f (1.0, -1.0);
        glTexCoord2f (1.0, 1.0); glVertex2f (1.0, 1.0);
        glTexCoord2f (0.0, 1.0); glVertex2f (-1.0, 1.0);
    glEnd();

    RTT.wgl.wglReleaseTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);
    glDisable(GL_TEXTURE_2D);

glFlush();
SwapBuffers(ghDC);

wglMakeCurrent(NULL,NULL);
}

这里是我的第二GLForm的drawScene函数:问题是,我只看到了彩色四但这QUAD没有纹理,或纹理是空的:

void TGLForm2::DrawSceneForm2()
{
wglMakeCurrent(ghDC2, ghRC2);
    ClientWidth = 1920;
    ClientHeight = 1080;

    wglShareLists(RTT.wgl.hGlRc, ghRC2);
    if (wglShareLists(RTT.wgl.hGlRc,ghRC2) == FALSE)
        SCmsgError(AnsiString().sprintf("wglShareLists returned %i", GetLastError()));

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE); //ARC

    glClear(GL_DEPTH_BUFFER_BIT);
    glViewport(0, 0, ClientWidth, ClientHeight);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glEnable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, RTT.texture);
    RTT.wgl.wglBindTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);

    glBegin(GL_QUADS);
        glColor4ub(200,200,200,200);
        glTexCoord2f (0.0, 0.0); glVertex2f (-1.0, -1.0);
        glTexCoord2f (1.0, 0.0); glVertex2f (1.0, -1.0);
        glTexCoord2f (1.0, 1.0); glVertex2f (1.0, 1.0);
        glTexCoord2f (0.0, 1.0); glVertex2f (-1.0, 1.0);
    glEnd();

    RTT.wgl.wglReleaseTexImageARB(RTT.wgl.hBuffer, WGL_FRONT_LEFT_ARB);
    glDisable(GL_TEXTURE_2D);

    glFlush();
    SwapBuffers(ghDC);
}

=>我如何可以检查此纹理为空或不?

其导出为位图和检查呢?

=>在DrawSceneForm2的wglShareLists返回与GetLastError函数的错误:

错误6:ERROR_INVALID_HANDLE句柄无效。

=>是否有人看到这个wglShareList或在我的代码是错误的?

Answer 1:

当调用wglShareLists ,上下文必须不是最新的。 你做任何事情之前Preferrably分享。 共享上下文将共享创建之后就好了什么。 最好的办法是创建一个需要在启动时共享的所有上下文。 如果你使用WGL_ARB_create_context,那么你甚至可以做到这一点原子内设立的呼叫。

如果你因为某些原因(虽然,为什么呢?)然后wglMakeCurrent(0,0); 第一(你做相反的事情在你的代码,你做的背景下目前只是分享之前)。



Answer 2:

我有一个类似的问题,其中:

wglShareLists返回0

GetLastError()返回3221684311(0xc0070057)

事实证明,你不能做太多与hglrc2(传入wglShareLists第二个参数)调用wglShareLists之前。 在我的情况下,我创建,并且glUseProgram着色器,然后试图得到上面所示的错误wglShareLists。 移动第二RC的wglShareLists后立即wglCreateContext(HDC)的工作。 我能跨过2个上下文共享纹理。



文章来源: wglShareLists fails with error 6 : ERROR_INVALID_HANDLE The handle is invalid