-1

Using pyGame and pyOpenGL, I have created a 2D surface, I used this 815x815 PNG created in Photoshop as a texture: Image I'm using as a texture

Here is how I'm loading the image, using pygame, and converting it into a texture.

pygame.init()
display = (800,600)
pygame.display.set_mode(display, DOUBLEBUF|OPENGL)
myTex = pygame.image.load('0.png')
myTex = myTex.convert_alpha()
myTexData = pygame.image.tostring(myTex, 'RGBA', 1)
myTexID = 0
glGenTextures(1, myTexID)
glBindTexture(GL_TEXTURE_2D, myTexID)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myTex.get_width(), myTex.get_height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, myTexData)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)

Here is the code I'm using to draw the square and the texture.

glBlendFunc(GL_SRC_ALPHA, GL_ONE)
glBegin(GL_QUADS)
glVertex2fv((-1,-1))
glTexCoord2f(0,1)
glVertex2fv((-1,1))
glTexCoord2f(1,1)
glVertex2fv((1,1))
glTexCoord2f(1,0)
glVertex2fv((1,-1))
glTexCoord2f(0,0)
glEnd()

Here is what happens when I run the code. OpenGL screenshot How do I get the texture to display properly, and why does this happen?

EDIT

It is some kind of transparency error, as changing the background of the image from transparent to white fixes the problem, however, convert_alpha() did not solve the problem. Also, I don't know why this is being downvoted, it is a legitimate question.

Bretsky
  • 423
  • 8
  • 23

1 Answers1

1

I suspect the culprit is transparency. Depending on how you created the png, the circle is going to have at least some pixels that are transparent to a certain degree. If pygame doesn't know those transparency values, it will render it as a the base color, regardless of whether the pixel should be visible or not. When you read the documentation, there is this blurb about pygame.image.load:

For alpha transparency, like in .png images use the convert_alpha() method after loading so that the image has per pixel transparency.

In the comments, there is a user created function (by anonymous, so... try at your own risk) that uses this method:

def load_image(file, colorkey=False):
    file = os.path.join('data', file)
    try:
        image = pygame.image.load(file)
        colorkey = image.get_at((0, 0))
        if colorkey is True:
            image.set_colorkey(colorkey, pygame.RLEACCEL)
    except:
        print 'Unable to load: ' + file
    return image.convert_alpha() #Convert any transparency in the image

You could try using that, or I have a feeling you can simply:

myTex = pygame.image.load('0.png')
myTex = myTex.convert_alpha()