[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[pygame] Re: [PyOpenGL-Users] Pygame surfaces as PyOpenGL textures?



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
class Exhaust:
    def __init__(self, num_points=100):
        self.num_points = num_points
        self.points = [(0, [], [], [0, 0, 0, 0])]*self.num_points
        self.index = 0

        # 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)

    def addPoint(self, ufo):
        ran = random.random

        # reverse direction to thrust
        f = -3 - (ran()/3 + 0.3)
        yaw = ufo.r_yaw + (ran() - 0.5)/2
        pitch = ufo.r_pitch + (ran() - 0.5)/2
        dx = f * math.sin(pitch) * math.sin(yaw) + ufo.dx
        dy = f * math.cos(pitch) + ufo.dy
        dz = f * math.sin(pitch) * math.cos(yaw) + ufo.dz

        # point is position, velocity, colour
        self.points[self.index] = (.05, [ufo.x, ufo.y, ufo.z],
            [dx, dy, dz], [1, .5, .8, 1])
        self.index = (self.index + 1)%self.num_points

    def animate(self, world):
        for fade, position, velocity, colour in self.points:
            if colour[3] <= 0: continue
            position[0] = position[0] + velocity[0]
            velocity[1] = velocity[1] #- world.gravity 
            position[1] = position[1] + velocity[1]
            position[2] = position[2] + velocity[2]
            colour[3] = colour[3] - fade

    def draw(self, world):
        # relative to the UFO
        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)