lines drawn on top of images have weird colors

35 views
Skip to first unread message

Timothy Lillicrap

unread,
Mar 5, 2012, 2:51:54 PM3/5/12
to glumpy-users
Hi There,

I'm drawing some simple opengl lines ontop of my glumpy images. When
I do so, the colors of the lines get messed up (i.e. they look very
different from when drawn without the glumpy image underneath.
Presumably this is because of some kind of blending going on, but I
have tried everything I can think of with regards to blending (i.e.
turning blending off, adjusting the components taken from source and
destination) and cannot get the lines to show up in the simple colors
I intend.

Any idea why this would happen even if I turn off blending to draw my
lines? Any ideas how I might fix this issue?

Thanks for any help,

Tim.

Nicolas Rougier

unread,
Mar 6, 2012, 3:27:51 AM3/6/12
to glumpy-users

Could you post a minimal example ?

Nicolas


On Mar 5, 8:51 pm, Timothy Lillicrap <timothy.lillic...@gmail.com>
wrote:

Timothy Lillicrap

unread,
Mar 6, 2012, 5:11:00 AM3/6/12
to glumpy-users
Here's a pared back example. I've put a bunch of comments on the line
which causes trouble. Essentially, when I don't draw the glumpy image
the gl lines look fine. As soon as I draw it, anywhere it seems, the
gl lines have "wrong" colors.


import numpy, glumpy
import Image
import pdb
import numpy as np
import OpenGL
import OpenGL.GL as gl
import OpenGL.GLU as glu
import OpenGL.GLUT as glut

refimg = np.random.uniform(0,1, (512,512,4)).astype(np.float32)
refimg[:,:,3] = np.ones_like(refimg[:,:,3])

img = np.ones((100,100,4)).astype(np.float32)
img *= (0,0,1.,.5)


fig = glumpy.figure( (512,512) )
grefimg = glumpy.Image(refimg)
image = glumpy.Image(img)
image.x = 0.
image.y = 0.
grefimg.x = 0.
grefimg.y = 0.

@fig.event
def on_init():
print 'Inititalization'

@fig.event
def on_draw():
print 'Drawing requested'
fig.clear()
gl.glEnable( gl.GL_BLEND )
gl.glBlendFunc( gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA )

grefimg.update()

### THIS IS THE LINE THAT CAUSES TROUBLE.
### IF IT IS COMMENTED OUT THE LINES DRAWN WITH THE OPEN GL CALLS
LOOK PROPERLY RED
### IF IT IS UNCOMMENTED, THE GL LINES LOOK BLOOD RED.
grefimg.draw(-200.,-200., 0., width=fig.width,height=fig.height)

# draw some simple lines
gl.glDisable(gl.GL_BLEND)
radius = 10.
gl.glPushMatrix()
gl.glLoadIdentity()
gl.glTranslatef(50,50,1.)
gl.glLineWidth(10.0)
gl.glDisable(gl.GL_BLEND)
gl.glBegin(gl.GL_LINE_LOOP)
gl.glColor4f(1.0,0.0,0.0,1.0)
for angle in np.arange(0.,2*np.pi,(2*np.pi)/100.):
gl.glVertex3f(np.sin(angle) * radius, np.cos(angle) *
radius, 0.)
gl.glEnd()
gl.glPopMatrix()

# draw some simple lines
radius = 10.
gl.glPushMatrix()
gl.glLoadIdentity()
gl.glTranslatef(400,400,1.)
gl.glLineWidth(10.0)
gl.glDisable(gl.GL_BLEND)
gl.glBegin(gl.GL_LINE_LOOP)
gl.glColor4f(1.0,0.0,0.0,1.0)
for angle in np.arange(0.,2*np.pi,(2*np.pi)/100.):
gl.glVertex3f(np.sin(angle) * radius, np.cos(angle) *
radius, 0.)
gl.glEnd()
gl.glPopMatrix()


@fig.event
def on_mouse_motion(x, y, dx, dy):
print 'Mouse motion (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' % (x,y,dx,dy)

@fig.event
def on_mouse_drag(x, y, dx, dy, button):
print 'Mouse drag (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f, button=%d)' %
(x,y,dx,dy,button)
image.x = image.x + dx
image.y = image.y + dy
fig.redraw()

fig.show()

Timothy Lillicrap

unread,
Mar 6, 2012, 6:34:56 AM3/6/12
to glumpy-users
Here's a better pared back example that makes the issue more clear I
think. Again, the drawing of the glumpy image causes the issue: see
my comment in the code.

#####################################################################

import numpy, glumpy
import Image
import pdb
import numpy as np
import OpenGL
import OpenGL.GL as gl
import OpenGL.GLU as glu
import OpenGL.GLUT as glut

refimg = np.random.uniform(0.1,0.5, (512,512,4)).astype(np.float32)
radius = 10.
gl.glPushMatrix()
gl.glLoadIdentity()
gl.glTranslatef(400,400,1.)
gl.glLineWidth(10.0)
# gl.glBlendFunc(gl.GL_ONE,gl.GL_ZERO)
# gl.glBlendFuncSeparate(gl.GL_ONE, gl.GL_ZERO, gl.GL_ONE,
gl.GL_ZERO);
# gl.glDisable(gl.GL_BLEND)
gl.glBegin(gl.GL_LINE_LOOP)
gl.glColor4f(1.0,0.0,0.0,1.0)
for angle in np.arange(0.,2*np.pi,(2*np.pi)/100.):
gl.glVertex3f(np.sin(angle) * radius, np.cos(angle) *
radius, 0.)
gl.glEnd()
gl.glPopMatrix()

@fig.event
def on_mouse_motion(x, y, dx, dy):
print 'Mouse motion (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f)' % (x,y,dx,dy)

@fig.event
def on_mouse_drag(x, y, dx, dy, button):
print 'Mouse drag (x=%.1f, y=%.1f, dx=%.1f, dy=%.1f, button=%d)' %
(x,y,dx,dy,button)
image.x = image.x + dx
image.y = image.y + dy
fig.redraw()

fig.show()

#####################################################################


On Mar 6, 8:27 am, Nicolas Rougier <nicolas.roug...@gmail.com> wrote:

Nicolas Rougier

unread,
Mar 6, 2012, 7:36:02 AM3/6/12
to glumpy-users

Ok, you have to disable texture before drawing your lines:

gl.glDisable(gl.GL_TEXTURE_2D)


Nicolas



On Mar 6, 12:34 pm, Timothy Lillicrap <timothy.lillic...@gmail.com>

Timothy Lillicrap

unread,
Mar 6, 2012, 8:03:37 AM3/6/12
to glumpy-users
Thanks. I should have found this one :(... Think I tried enabling/
disabling just about every other setting.
Reply all
Reply to author
Forward
0 new messages