Re: [PyOpenGL-Users] Pygame surfaces as PyOpenGL textures?
Brought to you by:
mcfletch
From: Richard J. <ric...@op...> - 2001-11-10 22:18:48
|
On Sat, 10 Nov 2001 22:59, Jan Ekholm wrote: > Has anybody done this before? I looked at NeHe:s tutorials, where the > above code partially comes from, but he uses "pil" for loading the images, > and I found no way to set transparency using that. Pil didn't seem to like > any kinds of images that has an alpha channel in the image, which was the > reason for trying to it using pygame. PIL honours the alpha channel in images just fine. Here's the relevant code from one of my object's __init__ method (complete class is attached): # texture self.texture = Image.open('point_64.png') ix, iy = self.texture.size image = self.texture.tostring("raw", "RGBA", 0, -1) self.pointtex = glGenTextures(1) glBindTexture(GL_TEXTURE_2D, self.pointtex) glPixelStorei(GL_UNPACK_ALIGNMENT,1) glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ix, iy, 0, GL_RGBA, GL_UNSIGNED_BYTE, image) and then in the draw() method: def draw(self, world): glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR) glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST) glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE) glBlendFunc(GL_SRC_ALPHA, GL_ONE); glEnable(GL_BLEND) glDisable(GL_DEPTH_TEST) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, self.pointtex) glDisable(GL_LIGHTING) glColor(1, 1, 1) glBegin(GL_QUADS) for life, position, velocity, colour in self.points: if colour[3] <= 0: continue glColor4f(*colour) x, y, z = position glTexCoord2f(0, 0) glVertex3f(x, y, z) glTexCoord2f(1, 0) glVertex3f(x+4, y, z) glTexCoord2f(1, 1) glVertex3f(x+4, y+4, z) glTexCoord2f(0, 1) glVertex3f(x, y+4, z) glEnd() glEnable(GL_LIGHTING) glEnable(GL_DEPTH_TEST) glDisable(GL_BLEND) glDisable(GL_TEXTURE_2D) (sorry about the complete lack of commenting :) Also, the code attached isn't very optimised. I'm still learning OpenGL, no time to make it fast or pretty :) Richard |