Issues painting from my own OpenGL Widget (not QGL) on windows
-
Hey everyone,
I'm implementing my own GL widget derived from QWidget ( not from QGLWidget ), but I'm having an annoying glitch somewhere. Right now I'm only developing for vs2008, using Qt-4.7.3.
When my application start running, I don't get any output from GL, only the default "gray" background. When I resize the window, sometimes my output will show up, sometimes it won't. And occasionally my output will show up for part of the window from the top left to a position that was the bottom right some time in the past.
Yellow = My gl output (with glClearColor/glClear)
!http://i.imgur.com/vbCc6.png()!
Did anyone have a similar situation or know what's going on? I've tried following QGLWidget line by line, but I don't see the difference.
Smallest repro case:
@class MyGLWidget: public QWidget
{public:
MyGLWidget(QWidget* parent) :
QWidget(parent)
{setAttribute(Qt::WA_PaintOnScreen);
setAttribute(Qt::WA_NoSystemBackground);
}QPaintEngine* paintEngine() const
{
return 0;
}void initializeGL()
{
HWND window = winId();
BOGART_ASSERT(window);PIXELFORMATDESCRIPTOR format = {
sizeof (PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
24,
0,0,0,0,0,0, //RGB bits/shift, not used
0,
0,
0,
0,0,0,0, //accum RGBA bits, not used
32,
0,//stencil
0,
PFD_MAIN_PLANE,
0,0,0,0//not used
};HDC dc = GetDC(window);
int pixelFormatIndex = ChoosePixelFormat(dc,&format);
BOGART_ASSERT(pixelFormatIndex)
BOOL success = SetPixelFormat(dc,pixelFormatIndex,&format);
BOGART_ASSERT(success != FALSE);HGLRC renderContext = wglCreateContext(dc);
BOGART_ASSERT(renderContext);wglMakeCurrent(dc,renderContext);
ReleaseDC(window,dc);
}bool event(QEvent* event)
{
switch (event->type())
{
case QEvent::ParentChange:
BOGART_FAIL(); //not an issue yet
break;
case QEvent::Paint:
{
glClearColor(1.0f,1.0f,0.0f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);HDC dc = GetDC(winId()); BOOL success = SwapBuffers(dc); BOGART_ASSERT(success != FALSE); ReleaseDC(winId(),dc);
}
return true;
case QEvent::Resize:
QResizeEvent* re = (QResizeEvent*)event;
glViewport(0,0,re->size().width(),re->size().height());
break;
}return QWidget::event(event);
}
};INT WINAPI WinMain(HINSTANCE instance, HINSTANCE previousInstance, LPSTR commandLine, INT commandAmount)
{
int argc = 0;
QApplication app(argc,0);QMainWindow* mainWindow = new QMainWindow();
mainWindow->showNormal();MyGLWidget* gl = new MyGLWidget(mainWindow);
mainWindow->setCentralWidget(gl);gl->initializeGL();
app.exec();
}@