-1

我有一个 Windows 应用程序,它以一种相当简单的方式使用 OpenGL 将纹理图像 (QUADS) 渲染到其客户区域中的多个窗口中。

该应用程序在 Windows 7 上运行良好,但在 Windows 8 上它有时会闪烁并显示背景以响应用户操作,例如鼠标单击窗口。确切地说:有时当鼠标单击某个窗口时(不是每次鼠标单击时),所有窗口都会短暂闪烁。

我正在研究这种行为的可能原因。我尝试捕捉 WM_ERASEBKGND 消息,但这似乎不是原因。我尝试使用不同的 GPU 卡(和相应的驱动程序),它发生在所有(Nvidia Quadro 和 ATI Radeon HD)上。此外,我让一些图像窗口使用 GDI 进行绘图,并且确实 - 当其他窗口闪烁时,这些图像不会(以及菜单、按钮区域和状态栏)。

更新:由于窗口是 C# 用户控件,我将 AllPaintingInWmPaint 和 UserPaint 样式设置为“true”。DoubleBuffer 为“假”,因为将其设置为“真”会导致闪烁更加频繁。

底线:使用 OpenGL 绘制的所有窗口偶尔会闪烁一次,并且只有它们,并且仅在 Win8 上(不在 Win7 上)。

我非常感谢有关此原因的提示。我的代码如下。我像这样初始化上下文:

void InitWindow(HWND *hWnd, HDC *hDC, HGLRC *hRC)
{
    *hDC = GetDC(*hWnd);

    PIXELFORMATDESCRIPTOR pfd;
    ZeroMemory(&pfd, sizeof(pfd));
    pfd.nSize = sizeof(pfd);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | 
            PFD_DOUBLEBUFFER;

    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 24;
    // pfd.cRedBits = 8;    
    // pfd.cGreenBits = 8;
    // pfd.cBlueBits = 8;
    pfd.cAlphaBits = 0;
    pfd.cDepthBits = 0;
    pfd.iLayerType = PFD_MAIN_PLANE;

    int format = ChoosePixelFormat(*hDC, &pfd);
    SetPixelFormat(*hDC, format, &pfd);

    *hRC = wglCreateContext(*hDC);

    wglMakeCurrent(*hDC, *hRC);
}

并像这样绘制到屏幕上:

int Render(HWND hWnd, Raster* raster)
{
    Size RasterSize = raster->GetSize();

    HDC hDC = NULL;
    HGLRC hRC = NULL;

    InitWindow(&hWnd, &hDC, &hRC);

    GLenum raster_pixel_format = GL_BGRA_EXT;
    GLint internal_format = GL_RGBA;

    GLuint texture;
    glGenTextures(1, &texture);
    glBindTexture (GL_TEXTURE_2D, texture);

    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvf (GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

    glTexImage2D (GL_TEXTURE_2D, 0, internal_format, 
            RasterSize.width, RasterSize.height,
            0, raster_pixel_format, GL_UNSIGNED_BYTE,
            raster->GetData());

    glBindTexture(GL_TEXTURE_2D, texture);

    RECT wndRect;
    ::GetClientRect(hWnd, &wndRect);
    GLsizei wndWidth = wndRect.right;
    GLsizei wndHeight = wndRect.bottom;

    glEnable(GL_TEXTURE_2D);

    // this is usually stated in window coordinates,
    // but since we know the raster gets its size from 
    // the window - we can use raster coordinates
    glViewport(0, 0, RasterSize.width, RasterSize.height);

    glBegin( GL_QUADS );    

    glTexCoord2d(0.0,0.0); glVertex2d(-1.0,+1.0);
    glTexCoord2d(1.0,0.0); glVertex2d(+1.0,+1.0);
    glTexCoord2d(1.0,1.0); glVertex2d(+1.0,-1.0);
    glTexCoord2d(0.0,1.0); glVertex2d(-1.0,-1.0);

    glEnd();

    glDisable(GL_TEXTURE_2D);

    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(1, &texture);

    SwapBuffers(hDC);
    wglMakeCurrent(NULL, NULL);

    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);

    return SUCCESS;
}
4

1 回答 1

0

问题是由于使用具有多个视频卡的机器。禁用任何一张卡解决了它。

于 2015-10-05T12:51:09.560 回答