Variable transparency in colourmaps

65 views
Skip to first unread message

Andrew Sullivan

unread,
Dec 5, 2013, 2:28:21 PM12/5/13
to glumpy...@googlegroups.com
I can't figure out how to get transparency working with colormaps (if it is indeed possible), I assumed the 4th component is alpha but this doesn't work.
This is my code:

figure = glumpy.figure((m * scaleFactor, n * scaleFactor))
colourMapChemical = glumpy.colormap.Colormap("orange",
                                (0.00, (1.0, 0.6, 0.2)),
                                (1.00, (1.0, 1.0, 1.0)))
image = glumpy.image.Image(v, interpolation = 'nearest', colormap = colourMapChemical)
colourMapBoundaries = glumpy.colormap.Colormap("Boundries",
                                (0.00, (1.0, 0.1, 0.0, 1.0)),
                                (1.00, (1.0, 0.0, 1.0, 0.0)))
image2= glumpy.image.Image(areaInSimulation, interpolation = 'nearest', colormap = colourMapBoundaries)


@figure.event
def on_draw():
    figure.clear()
    
    OpenGL.GL.glEnable( OpenGL.GL.GL_BLEND ) 
    OpenGL.GL.glBlendFunc( OpenGL.GL.GL_SRC_ALPHA, OpenGL.GL.GL_ONE_MINUS_SRC_ALPHA )
    
    image2.draw( x=0, y=0, z=1, width=figure.width, height=figure.height )
    image.draw( x=0, y=0, z=0, width=figure.width, height=figure.height )
    
dt = 0.1
@figure.event
def on_idle(elapsed):
   
    image2.update()
    image.update()
    figure.redraw()

glumpy.show()

Nicolas Rougier

unread,
Dec 11, 2013, 9:30:43 AM12/11/13
to glumpy...@googlegroups.com

Could you post a complete example (with fake data) such I can test it ?

Nicolas

Matthew Egbert

unread,
Jun 1, 2014, 5:11:30 PM6/1/14
to glumpy...@googlegroups.com
I also have been having difficulties getting glumpy to work well with transparent colormaps, and transparency in general.  

Here 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 would be much appreciated. Any thoughts?

Thanks in advance!
Matthew

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

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

############################ END
glumpy_transparency_test.py

Matthew Egbert

unread,
Jun 2, 2014, 7:46:47 AM6/2/14
to glumpy...@googlegroups.com
Working with another example I found online, I managed to get transparency working, but similar to Andrew Sullivan's comment above, I could not get custom colormaps with an alpha channel working. As requested above (in response to Andrew's post), here is an example bit of code that works for transparency, but does not work for transparency within a colormap (to switch between the two cases, you have to comment out one of the lines that defines B_img).

From this development I have two questions.

1. Why does transparency appear to work for the openGL context in the code below, but not in my original SDL/pyGame openGL context in the posting I made above?
2. Why does transparency not appear to work for custom colormaps?

Thanks for any thoughts / suggestions anyone can provide!

Best,
Matthew

####################### START
import glumpy 
import numpy as np 
import OpenGL.GL as gl
import OpenGL.GLU as glu

A = np.random.uniform(0,1, (512,512,2)).astype(np.float32) 

waterCM = glumpy.colormap.Colormap("water", (0., (0.,1.,0.,0.0)), (1., (0.1,0.5,1.0,1.)))

B = np.ones((100,100)).astype(np.float32) 
B[0:20,50:] = 0.25
B[40:60,50:] = 0.5
B[80:,50:] = 0.75

fig = glumpy.figure( (512,512) ) 
A_img = glumpy.Image(A)
A_img.x,A_img.y = 0,0 

## comment out one of the following two lines
B_img = glumpy.Image(B,colormap=waterCM,vmin=0.0,vmax=1.0)   ## doesn't work
# B_img = glumpy.Image(B,vmin=0.0,vmax=1.0)   ## works

B_img.x,B_img.y = 0,0 

gl.glEnable( gl.GL_BLEND ) 
gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA ) 

@fig.event 
def on_draw(): 
    fig.clear() 

    A[...] = np.random.uniform(0,1, (512,512,2)) 
    A_img.update() 
    A_img.draw(x=A_img.x, y=A_img.y, z=0.0, 
               width=fig.width, height=fig.height) 

    B_img.draw(x=B_img.x-50, y=B_img.y-50, z=1.0, 
               width=100, height=100) 

@fig.event 
def on_mouse_motion(x, y, dx, dy): 
    B_img.x = x 
    B_img.y = y 
    fig.redraw() 

fig.show() 

######################## END

Matthew Egbert

unread,
Jun 2, 2014, 8:04:08 AM6/2/14
to glumpy...@googlegroups.com
I retract my first question.  The problem in both situations appears to be the use of custom colormaps with transparency.

Matthew

Matthew Egbert

unread,
Jun 2, 2014, 8:39:22 AM6/2/14
to glumpy...@googlegroups.com
I think I may have found a solution.

In /usr/local/lib/python2.7/dist-packages/glumpy/image/filter.py, around line 264, I changed one line, and transparency with custom colormaps now appears to work.

I think that before it was just reading the rgb values from the colormap, and now it also reads the alpha values.

        # Get colormap lut
        if self.colormap:
            #Z = self.colormap.LUT['rgb'][1:].flatten().view((np.float32,3)) ### this was replaced
            Z = self.colormap.LUT['rgba'][1:].flatten().view((np.float32,4)) ### with this
            self._color_lut = Texture(Z)


Maybe someone could commit this to the code repo? I tried, but I;ve never really collaborated on a google code project. I'm not sure if it is permissions, or what, but when I try to push my changes to the server, it says 

fatal: remote error: Repository not found

Cheers,
Matthew
Reply all
Reply to author
Forward
0 new messages