[英]Text Clipping with Qt OpenGL
我目前正在使用Qt5.1并尝试在QGLWidget中绘制一些OpenGL内容:
void Widget::paintGL() {
startClipping(10, height()-110,100,100);
qglColor(Qt::red);
glBegin(GL_QUADS);
glVertex2d(0,0);
glVertex2d(500,0);
glVertex2d(500,500);
glVertex2d(0,500);
glEnd();
qglColor(Qt::green);
this->renderText(50, 50, "SCISSOR TEST STRING");
endClipping();
}
四边形被正确剪裁但文本没有。 我尝试了三种实现startClipping方法的方法:剪刀测试,将视口设置为剪切区域和模板缓冲区。 它们都没有工作,整个字符串被绘制而不是在剪切区域的边缘切断。
现在我的问题是:这种行为是Qt的错误还是有什么东西,我错过了或者我可以尝试的另一种可能性?
经过一周的尝试,我突然找到了一种非常简单的方法来实现,我正在寻找什么。 使用QPainter和它的方法代替QGLWidget的renderText()
简单地使文本剪辑工作:
QPainter *painter = new QPainter();
painter->begin();
painter->setClipping(true);
painter->setClipPath(...); // or
painter->setClipRect(...); // or
painter->setClipRegion(...);
painter->drawText(...);
painter->end();
据我了解,这是设计的。 根据文档( https://qt-project.org/doc/qt-4.8/qglwidget.html#renderText ):
Note: This function clears the stencil buffer.
Note: This function temporarily disables depth-testing when the text is drawn.
但是对于'xyz版'(重载函数)
Note: If depth testing is enabled before this function is called, then the drawn text will be depth-tested against the models that have already been drawn in the scene. Use glDisable(GL_DEPTH_TEST) before calling this function to annotate the models without depth-testing the text.
因此,如果您在原始代码中使用第二个版本(通过包含z值,例如0),我认为您得到了您想要的。 如果你做一个“真正的”3D场景(例如3D图上的轴标签),我想你会想要这样做。
该文档还提到使用drawText。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.