簡體   English   中英

使用Qt OpenGL進行文本剪輯

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM