Martin_from_K
Martin_from_K

Reputation: 191

python openGL: How to change the color of a grid?

win I am just starting with some 3D graphics experiments using python and openGL but I am already struggling trying to change the color of a grid, because I want a black grid on a white background. I simply copied the pyqtgraph example GLSurfacePlot.py and changed the line

g = gl.GLGridItem ()

to any of these, but none changed the color:

g = gl.GLGridItem (size = QtGui.QVector3D(10,10,1), color = (0.0, 0.0, 0.0, 1.0) )
g = gl.GLGridItem (size = QtGui.QVector3D(10,10,1), color = (1, 1, 0, 1) )
g = gl.GLGridItem (size = QtGui.QVector3D(10,10,1), color = 'k')
g = gl.GLGridItem (size = QtGui.QVector3D(10,10,1), color = pg.glColor((0.0, 0.0, 0.0, 1.0)) )
color = QtGui.QColor("b")
g = gl.GLGridItem (size = QtGui.QVector3D(10,10,1), color = color)

What am I doing wrong? I have win 10, python 3.7, pygtgraph 0.10.0, PyopenGL 3.1.5, PyQt5 5.11.3

thanks for any help

Martin

Upvotes: 2

Views: 858

Answers (1)

Alex Sveshnikov
Alex Sveshnikov

Reputation: 4329

Unfortunately it's a bug in GLGridItem, it does not use color parameter in its constructor:

def __init__(self, size=None, color=None, antialias=True, glOptions='translucent'):
     GLGraphicsItem.__init__(self)
     self.setGLOptions(glOptions)
     self.antialias = antialias
     if size is None:
         size = QtGui.QVector3D(20,20,1)
     self.setSize(size=size)
     self.setSpacing(1, 1, 1)

and just directly sets the white color with 30% transparency when drawing it:

def paint(self):
    .......................
     glColor4f(1, 1, 1, .3)
    .......................

If you want to set the color of the grid you can either modify the standard module (use a color parameter in constructor) or copy the code from the module to your code and use it instead of the standard GLGridItem:

def __init__(self, size=None, color=None, antialias=True, glOptions='translucent'):
     GLGraphicsItem.__init__(self)
     self.setGLOptions(glOptions)
     self.color = color
     self.antialias = antialias
     if size is None:
         size = QtGui.QVector3D(20,20,1)
     self.setSize(size=size)
     self.setSpacing(1, 1, 1)

def paint(self):
    .......................
    if self.color is None:
       glColor4f(1, 1, 1, .3)
    else:
       glColor4f(*self.color)
    .......................

Upvotes: 1

Related Questions