2

I'm drawing triangles/polygons in QPainter using antialiasing, which come from the rendering of 3D models. If two polygons are drawn immediately next to each other there is a gap between them.

Here is some code (in PyQt) to demonstrate the issue:

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_()

Screenshot from code

How can I prevent this problem? As suggest below by vahancho, I could add borders to each polygon, or I could grow by half a pixel in each direction. Unfortunately this makes the joins visible if the fill/pen is partially transparent.

xioxox
  • 2,526
  • 1
  • 22
  • 22
  • 1
    What if you try to remove this line: `painter.setPen(QPen(Qt.NoPen))`? – vahancho Nov 03 '14 at 15:29
  • That does fix it in this case. I could add 1 pixel borders to each polygon with the same colour as the fill. Unfortunately it doesn't work if I want transparent triangles. – xioxox Nov 03 '14 at 15:36

1 Answers1

0

Try to do a Gaussian blur of 1 pixel radius while finalizing would avoid the problem. If you were writing a game, you should turn off the anti-aliasing during the rendering, place the step to the end. Which means you should do something like SSAA in the end, once and for all, but not anti-aliasing in each drawing call.

lymastee
  • 21
  • 3