Qt& OpenGL - QGLWidget & QPainter:深度缓冲
我将 QGLWidget 子类化,并将绘画代码放在 PaintEvent 中而不是 PaintGL 中,因为我想使用 QPainter 在使用 OpenGL 完成的 3D 内容上绘制 2D 叠加层。
当我没有覆盖时,我的深度缓冲工作正常。如果覆盖层被绘制,我的深度缓冲区就会擅离职守:我可以看到应该被前面的对象隐藏的东西。
初始化GL看起来像这样:
qglClearColor(Qt::black);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
我的paintEvent代码的结构如下:
makeCurrent();
...openGLStuff...
if (I need my overlay)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
QPainter painter(this);
... do QPainter stuff ...
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
}
swapBuffers();
根据if,相同的场景看起来不错(覆盖关闭)或错误(覆盖打开)。除了奇怪的深度缓冲区问题之外,它工作得很好。
我(疯狂)的猜测是 QPainter 的构造禁用了深度缓冲。任何提示将不胜感激。我想一个后备解决方案是将我的覆盖层渲染成纹理并让 OpenGL 将其混合进去。
I subclass QGLWidget and have my painting code in paintEvent instead of paintGL as I want to paint a 2D overlay using QPainter over my 3D stuff done with OpenGL.
My depth buffering works fine when I don't have an overlay. If the overlay is painted, my depth buffer goes AWOL: I can see stuff that should be hidden by objects in front.
initializeGL looks like this:
qglClearColor(Qt::black);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
The structure of my paintEvent code is as follows:
makeCurrent();
...openGLStuff...
if (I need my overlay)
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
QPainter painter(this);
... do QPainter stuff ...
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
}
swapBuffers();
Depending on the if, the same scene looks alright (overlay off) or wrong (overlay on). Apart from the weird depth buffer problem, it works perfectly well.
My (wild) guess is that construction of the QPainter disables depth buffering. Any hint would be greatly appreciated. I suppose a fallback solution would be to render my overlay into a texture and have OpenGL blend it in.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不根据需要启用和禁用深度测试呢?你不需要“初始化”OpenGL,它是一个状态机。这些“初始化”语句属于您的绘图代码,属于需要它们的上下文。
Why don't you just enable and disable depth testing as needed? You don't "initialize" OpenGL, it's a state machine. Those "initializing" statements belong in your drawing code, into the context they where they are needed.