OpenGL alpha channel not working

Max lu_gio at hotmail.com
Sun Sep 22 19:55:44 EDT 2002


"""
Hi!
I wrote this little test... it's working without errors, only strange
behavor.
I have 2 texures RGBA, one loaded and blitted into another and one generated
with pygame.font.
Can someone tell me why the font over the picture is working and the picture
over the font is not??

If you want to test it, please sobstitute the GIFNAME and COLORKEY.
Thanks
"""
#-----------------------------------------------BEGIN
#-----------------------------------------------BEGIN
#-----------------------------------------------BEGIN

GIFNAME='fish1ne.gif'  #------------- PUT your image here!!! mine was 60x42
gif with colorkey (255,0,255)
COLORKEY=(255,0,255)

from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

import os.path
import sys
import pygame

def TestI():
    pygame.init()
    pygame.display.set_mode((640,480), OPENGL|DOUBLEBUF|HWSURFACE,32 )

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    glOrtho(0,640,480,0,0,-1)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()

    glEnable(GL_BLEND)
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
    glEnable(GL_TEXTURE_2D)
    glEnable(GL_DEPTH_TEST)

    psurf=pygame.surface.Surface((16*8,16*8),SRCALPHA,32)
    psurf.set_colorkey((0,0,0,0))

    immge = pygame.image.load(GIFNAME)
    immge.set_colorkey(COLORKEY)

    psurf.blit(immge,immge.get_rect())
    spsurf = pygame.image.tostring ( psurf, "RGBA" )

    tx1 = AllocateTexture(psurf)

    mfont=pygame.font.Font(None,64)
    im = mfont.render('A',0,(255,255,255))
    im.set_colorkey((0,0,0,0))

    tx2 = AllocateTexture(im)

    while 1:
        event = pygame.event.poll()
        if event.type == QUIT or (event.type == KEYDOWN and event.key ==
K_ESCAPE):
            sys.exit()

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)

        glPushMatrix()
        glTranslate(100,100,0.5)
        mdraw(psurf, tx1)

        glPushMatrix()
        glTranslate(100,0,0)
        glScalef(5,5,0)
        mdraw(im, tx2)

        glPushMatrix()
        glTranslate(300,100,0)
        mdraw(psurf, tx1)

        glPushMatrix()
        glTranslate(300,0,0.5)
        glScalef(5,5,0)
        mdraw(im, tx2)

        pygame.display.flip()
        pygame.time.wait(10)

def mdraw(sf, txx):
    glBindTexture(GL_TEXTURE_2D, txx)
    w, h=sf.get_size()

    glBegin(GL_QUADS)

    glTexCoord2f(0, 0)
    glVertex2i(0, 0)

    glTexCoord2f(1, 0)
    glVertex2i( w, 0)

    glTexCoord2f(1,1)
    glVertex2i( w, h)

    glTexCoord2f(0,1)
    glVertex2i(0, h)

    glEnd()
    glPopMatrix()

def AllocateTexture(surface):
    image = pygame.image.tostring ( surface, "RGBA" )
    ix, iy = surface.get_size ()

    idImg = glGenTextures(1)
    glBindTexture(GL_TEXTURE_2D, idImg )
    glPixelStorei(GL_UNPACK_ALIGNMENT,1)

    gluBuild2DMipmaps(GL_TEXTURE_2D, 4, ix, iy, GL_RGBA, GL_UNSIGNED_BYTE,
image)
    return idImg

TestI()

#-----------------------------------------------END
#-----------------------------------------------END
#-----------------------------------------------END






More information about the Python-list mailing list