【问题标题】:Text Clipping with Qt OpenGL使用 Qt OpenGL 进行文本剪辑
【发布时间】:2013-07-17 07:50:54
【问题描述】:

我目前正在使用 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 的错误还是我错过了什么或者我可以尝试的其他可能性??

【问题讨论】:

    标签: c++ qt opengl clipping


    【解决方案1】:

    经过一周的尝试,我突然找到了一种非常简单的方法来实现我正在寻找的东西。 使用 QPainter 和它的方法而不是 QGLWidget 的 renderText() 只会使文本剪辑工作:

    QPainter *painter = new QPainter();
    painter->begin();
    
    painter->setClipping(true);
    
    painter->setClipPath(...);   // or
    painter->setClipRect(...);   // or
    painter->setClipRegion(...);
    
    painter->drawText(...);
    
    painter->end();
    

    【讨论】:

      【解决方案2】:

      据我了解,这是设计使然。根据文档(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。

      【讨论】:

        猜你喜欢
        • 2012-03-11
        • 1970-01-01
        • 2014-12-19
        • 1970-01-01
        • 2012-10-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-23
        相关资源
        最近更新 更多