【问题标题】:How to prevent visible gaps/joins between polygons in QPainter with antialiasing如何使用抗锯齿防止 QPainter 中多边形之间的可见间隙/连接
【发布时间】:2014-11-03 15:26:58
【问题描述】:

我正在使用抗锯齿在 QPainter 中绘制三角形/多边形,它来自 3D 模型的渲染。如果两个多边形紧挨着绘制,它们之间就会有间隙。

这里有一些代码(在 PyQt 中)来演示这个问题:

import sys
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Window(QLabel):

    def __init__(self):
        QLabel.__init__(self)

        pixmap = QPixmap(300,300)
        pixmap.fill()
        painter = QPainter(pixmap)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(QPen(Qt.NoPen))
        painter.setBrush(QBrush(QColor('black')))

        poly1 = QPolygonF()
        for x, y in ((0,0),(0,240.5),(210.3,0)):
            poly1 << QPointF(x,y)
        painter.drawPolygon(poly1)
        poly2 = QPolygonF()
        for x, y in ((210.3,0),(210.3,240),(0,240.5)):
            poly2 << QPointF(x,y)
        painter.drawPolygon(poly2)
        painter.end()

        self.setPixmap(pixmap)

app = QApplication(sys.argv)
win = Window()
win.show()
app.exec_()

如何防止这个问题?正如下面 vahancho 所建议的,我可以为每个多边形添加边框,或者我可以在每个方向上增长半个像素。不幸的是,如果填充/笔是部分透明的,这会使连接可见。

【问题讨论】:

  • 如果您尝试删除此行会怎样:painter.setPen(QPen(Qt.NoPen))?
  • 在这种情况下确实可以解决问题。我可以为每个多边形添加 1 个像素边框,颜色与填充颜色相同。不幸的是,如果我想要透明的三角形,它就行不通了。

标签: qt qt4 drawing qpainter


【解决方案1】:

尝试在最终确定时进行 1 像素半径的高斯模糊可以避免该问题。 如果你正在写游戏,你应该在渲染过程中关闭抗锯齿,将步骤放在最后。这意味着您应该一劳永逸地在最后执行类似 SSAA 的操作,但不要在每个绘图调用中进行抗锯齿处理。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 2021-11-29
    • 1970-01-01
    相关资源
    最近更新 更多