2013-07-18 113 views
3

我目前正在Qt5.1工作,并试图将QGLWidget来绘图中得出一些OpenGL的东西文本剪辑:使用Qt的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或有东西,我错过了或另一种可能性,我可以尝试?

回答

0

经过一个星期的尝试,我突然发现一个非常简单的方法来实现,我在找什么。 使用QPainter和它的方法,而不是QGLWidget来绘图的renderText()只是使文本剪辑工作:

QPainter *painter = new QPainter(); 
painter->begin(); 

painter->setClipping(true); 

painter->setClipPath(...); // or 
painter->setClipRect(...); // or 
painter->setClipRegion(...); 

painter->drawText(...); 

painter->end(); 
0

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