Getting transparency to work in glumpy with pygame

194 views Asked by At

I also have been having difficulties getting glumpy to work well with transparent colormaps, and transparency in general.

Below is a simple script that should draw two transparent glumpy Images, one on top of the other. The second simply overwrites the first, so you can't see the one behind the other.

I have experimented with different opengl enable/disable commands, but with no joy! Any help or suggestions would be much appreciated.

from pylab import *
import pygame
from pygame.locals import *

import glumpy
import OpenGL.GL as gl
import OpenGL.GLU as glu

# Set the width and height of the screen [width,height]
size=[256,256]
screen=pygame.display.set_mode(size,OPENGL|DOUBLEBUF)

## I think something here might get this to work, but I've
## experimented turining these off and on. Any suggestions?
gl.glDisable(gl.GL_DEPTH_TEST)
gl.glDisable(gl.GL_LIGHTING)
gl.glEnable(gl.GL_BLEND)
gl.glBlendFunc (gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

a = np.random.rand(256,256).astype('f')
a[0:128,:] = 1.0
b = np.random.rand(256,256).astype('f')
b[:,0:128] = 1.0

# cmaps
aCM = glumpy.colormap.Colormap("Rd", (0., (1.,1.,0.,0.0)), (1., (1.0,0.0,0.0,1.0)))
bCM = glumpy.colormap.Colormap("Bl", (0., (0.,1.0,1.,0.0)), (1., (0.0,0.0,1.0,1.0)))

# glumpy images
ai = glumpy.image.Image(a,colormap=aCM)
bi = glumpy.image.Image(b,colormap=bCM)

print('press q or ESC to quit')

while True :
    gl.glPushMatrix()
    gl.glLoadIdentity()
    gl.glViewport(0, 0, size[0],size[1])

    # draw a diagonal line underneath everything
    gl.glColor4d(1,0,0,1)
    gl.glBegin(gl.GL_LINES)
    gl.glVertex3d(0,0,-0.1)
    gl.glVertex3d(1,1.0,-0.1)
    gl.glEnd()

    # draw the glumpy images, with ai beneath bi
    ai.draw(x=-1,y=-1,z=0,width=2.0,height=2.0)
    bi.draw(x=-.9,y=-.9,z=0.1,width=1.8,height=2.0)

    gl.glPopMatrix()

    pygame.display.flip()
    gl.glClearColor(0.1, 0.1, 0.1, 0.1);
    gl.glClear(gl.GL_COLOR_BUFFER_BIT);

    for event in pygame.event.get(): # User did something
        if event.type == pygame.KEYDOWN:
            if event.key in [pygame.K_q, pygame.K_ESCAPE] :
                pygame.quit()
                sys.exit()
1

There are 1 answers

0
weemattisnot On

I believe that the problem above was caused by a bug in glumpy. I have outlined a very simple patch that appears to rectify the problem here: https://groups.google.com/forum/#!topic/glumpy-users/UUi4yzl0ktU