pythonopenglpyopengltexturing

Artefacts in texturing in PyOpenGL


I have used the Python Imaging Library to load a .ttf font. Here is the code:

self.rect = Image.new("RGBA", (600,100), (255,255,255))
self.draw = ImageDraw.Draw(self.rect)
self.font = ImageFont.truetype("font.ttf", 96)
self.draw.text((5,0), "activatedgeek", (0,0,0), font=self.font)
self.texture = self.loadFont(self.rect)

Here is the loadFont() function of the respective class:

def loadFont(self, im):
        try:
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBA", 0, -1)
        except SystemError:
            ix, iy, image = im.size[0], im.size[1], im.tostring("raw", "RGBX", 0, -1)

        retid = gl.glGenTextures(1)
        gl.glPixelStorei(gl.GL_UNPACK_ALIGNMENT,1)
        gl.glBindTexture(gl.GL_TEXTURE_2D,retid)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_LINEAR_MIPMAP_LINEAR)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_S, gl.GL_CLAMP)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_WRAP_T, gl.GL_CLAMP)
        gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_GENERATE_MIPMAP, gl.GL_TRUE)
        gl.glTexImage2D(gl.GL_TEXTURE_2D,0,3,ix,iy,0,gl.GL_RGBA,gl.GL_UNSIGNED_BYTE,image)
        return retid

Here is a snapshot I have taken using glReadPixels() unfortunately same as one rendered on the window created using PyQt.

Snapshot OpenGL context

It shows an unwanted border, some artefact. Please help me rectify this.


Solution

  • Have you considered using a more reasonable wrap state, such as GL_CLAMP_TO_EDGE? I have a strong feeling that this is related to border color beyond the edges of your texture image.

    There are a number of approaches you could take to solve an issue like this, ranging from pre-multiplied alpha to an extra texel border around the entire image, but the simplest thing to try would be GL_CLAMP_TO_EDGE.

    GL_CLAMP is something of a joke as far as wrap modes go, it does not clamp the range of texture coordinates to texel centers and calamity ensues when the nearest texel becomes the border color. Needless to say, this behavior is usually undesirable.