1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
import enum
import math
import importlib
from OpenGL import GL
from OpenGL.GL import shaders
import numpy as np
from ...Qt import QtGui, QT_LIB
from ..GLGraphicsItem import GLGraphicsItem
if QT_LIB in ["PyQt5", "PySide2"]:
QtOpenGL = QtGui
else:
QtOpenGL = importlib.import_module(f"{QT_LIB}.QtOpenGL")
__all__ = ['GLScatterPlotItem']
class DirtyFlag(enum.Flag):
POSITION = enum.auto()
COLOR = enum.auto()
SIZE = enum.auto()
class GLScatterPlotItem(GLGraphicsItem):
"""Draws points at a list of 3D positions."""
_shaderProgram = None
def __init__(self, parentItem=None, **kwds):
super().__init__()
glopts = kwds.pop('glOptions', 'additive')
self.setGLOptions(glopts)
self.pos = None
self.size = 10
self.color = [1.0,1.0,1.0,0.5]
self.pxMode = True
self.m_vbo_position = QtOpenGL.QOpenGLBuffer(QtOpenGL.QOpenGLBuffer.Type.VertexBuffer)
self.m_vbo_color = QtOpenGL.QOpenGLBuffer(QtOpenGL.QOpenGLBuffer.Type.VertexBuffer)
self.m_vbo_size = QtOpenGL.QOpenGLBuffer(QtOpenGL.QOpenGLBuffer.Type.VertexBuffer)
self.dirty_bits = DirtyFlag(0)
self.setParentItem(parentItem)
self.setData(**kwds)
def setData(self, **kwds):
"""
Update the data displayed by this item. All arguments are optional;
for example it is allowed to update spot positions while leaving
colors unchanged, etc.
==================== ==================================================
**Arguments:**
pos (N,3) array of floats specifying point locations.
color (N,4) array of floats (0.0-1.0) specifying
spot colors OR a tuple of floats specifying
a single color for all spots.
size (N,) array of floats specifying spot sizes or
a single value to apply to all spots.
pxMode If True, spot sizes are expressed in pixels.
Otherwise, they are expressed in item coordinates.
==================== ==================================================
"""
args = ['pos', 'color', 'size', 'pxMode']
for k in kwds.keys():
if k not in args:
raise Exception('Invalid keyword argument: %s (allowed arguments are %s)' % (k, str(args)))
if 'pos' in kwds:
pos = kwds.pop('pos')
self.pos = np.ascontiguousarray(pos, dtype=np.float32)
self.dirty_bits |= DirtyFlag.POSITION
if 'color' in kwds:
color = kwds.pop('color')
if isinstance(color, np.ndarray):
color = np.ascontiguousarray(color, dtype=np.float32)
self.dirty_bits |= DirtyFlag.COLOR
if isinstance(color, QtGui.QColor):
color = color.getRgbF()
self.color = color
if 'size' in kwds:
size = kwds.pop('size')
if isinstance(size, np.ndarray):
size = np.ascontiguousarray(size, dtype=np.float32)
self.dirty_bits |= DirtyFlag.SIZE
self.size = size
self.pxMode = kwds.get('pxMode', self.pxMode)
self.update()
def upload_vbo(self, vbo, arr):
if arr is None:
vbo.destroy()
return
if not vbo.isCreated():
vbo.create()
vbo.bind()
if vbo.size() != arr.nbytes:
vbo.allocate(arr, arr.nbytes)
else:
vbo.write(0, arr, arr.nbytes)
vbo.release()
@staticmethod
def getShaderProgram():
klass = GLScatterPlotItem
if klass._shaderProgram is not None:
return klass._shaderProgram
ctx = QtGui.QOpenGLContext.currentContext()
fmt = ctx.format()
if ctx.isOpenGLES():
if fmt.version() >= (3, 0):
glsl_version = "#version 300 es\n"
sources = SHADER_CORE
else:
glsl_version = "#version 100\n"
sources = SHADER_LEGACY
else:
if fmt.version() >= (3, 1):
glsl_version = "#version 140\n"
sources = SHADER_CORE
else:
glsl_version = "#version 120\n"
sources = SHADER_LEGACY
compiled = [shaders.compileShader([glsl_version, v], k) for k, v in sources.items()]
program = shaders.compileProgram(*compiled)
# bind generic vertex attrib 0 to "a_position" so that
# vertex attrib 0 definitely gets enabled later.
GL.glBindAttribLocation(program, 0, "a_position")
GL.glBindAttribLocation(program, 1, "a_color")
GL.glBindAttribLocation(program, 2, "a_size")
GL.glLinkProgram(program)
klass._shaderProgram = program
return program
def paint(self):
if self.pos is None:
return
self.setupGLState()
mat_mvp = self.mvpMatrix()
mat_mvp = np.array(mat_mvp.data(), dtype=np.float32)
mat_modelview = self.modelViewMatrix()
mat_modelview = np.array(mat_modelview.data(), dtype=np.float32)
view = self.view()
if self.pxMode:
scale = 0
else:
scale = 2.0 * math.tan(math.radians(0.5 * view.opts["fov"])) / view.width()
context = QtGui.QOpenGLContext.currentContext()
if DirtyFlag.POSITION in self.dirty_bits:
self.upload_vbo(self.m_vbo_position, self.pos)
if DirtyFlag.COLOR in self.dirty_bits:
self.upload_vbo(self.m_vbo_color, self.color)
if DirtyFlag.SIZE in self.dirty_bits:
self.upload_vbo(self.m_vbo_size, self.size)
self.dirty_bits = DirtyFlag(0)
if not context.isOpenGLES():
if _is_compatibility_profile(context):
GL.glEnable(GL.GL_POINT_SPRITE)
GL.glEnable(GL.GL_PROGRAM_POINT_SIZE)
program = self.getShaderProgram()
enabled_locs = []
loc = 0
self.m_vbo_position.bind()
GL.glVertexAttribPointer(loc, 3, GL.GL_FLOAT, False, 0, None)
self.m_vbo_position.release()
enabled_locs.append(loc)
loc = 1
if isinstance(self.color, np.ndarray):
self.m_vbo_color.bind()
GL.glVertexAttribPointer(loc, 4, GL.GL_FLOAT, False, 0, None)
self.m_vbo_color.release()
enabled_locs.append(loc)
else:
GL.glVertexAttrib4f(loc, *self.color)
loc = 2
if isinstance(self.size, np.ndarray):
self.m_vbo_size.bind()
GL.glVertexAttribPointer(loc, 1, GL.GL_FLOAT, False, 0, None)
self.m_vbo_size.release()
enabled_locs.append(loc)
else:
GL.glVertexAttrib1f(loc, self.size)
for loc in enabled_locs:
GL.glEnableVertexAttribArray(loc)
with program:
loc = GL.glGetUniformLocation(program, "u_mvp")
GL.glUniformMatrix4fv(loc, 1, False, mat_mvp)
loc = GL.glGetUniformLocation(program, "u_modelview")
GL.glUniformMatrix4fv(loc, 1, False, mat_modelview)
loc = GL.glGetUniformLocation(program, "u_scale")
GL.glUniform1f(loc, scale)
GL.glDrawArrays(GL.GL_POINTS, 0, len(self.pos))
for loc in enabled_locs:
GL.glDisableVertexAttribArray(loc)
def _is_compatibility_profile(context):
# https://stackoverflow.com/questions/73745603/detect-the-opengl-context-profile-before-version-3-2
sformat = context.format()
profile = sformat.profile()
# >= 3.2 has {Compatibility,Core}Profile
# <= 3.1 is NoProfile
if profile == sformat.OpenGLContextProfile.CompatibilityProfile:
compat = True
elif profile == sformat.OpenGLContextProfile.CoreProfile:
compat = False
else:
compat = False
version = sformat.version()
if version <= (2, 1):
compat = True
elif version == (3, 0):
if sformat.testOption(sformat.FormatOption.DeprecatedFunctions):
compat = True
elif version == (3, 1):
if context.hasExtension(b'GL_ARB_compatibility'):
compat = True
return compat
## See:
##
## http://stackoverflow.com/questions/9609423/applying-part-of-a-texture-sprite-sheet-texture-map-to-a-point-sprite-in-ios
## http://stackoverflow.com/questions/3497068/textured-points-in-opengl-es-2-0
##
##
SHADER_LEGACY = {
GL.GL_VERTEX_SHADER : """
uniform float u_scale;
uniform mat4 u_modelview;
uniform mat4 u_mvp;
attribute vec4 a_position;
attribute vec4 a_color;
attribute float a_size;
varying vec4 v_color;
void main() {
gl_Position = u_mvp * a_position;
v_color = a_color;
gl_PointSize = a_size;
if (u_scale != 0.0) {
// pxMode=False
// the modelview matrix transforms the vertex to
// camera space, where the camera is at (0, 0, 0).
vec4 cpos = u_modelview * a_position;
float dist = length(cpos.xyz);
// equations:
// xDist = dist * 2.0 * tan(0.5 * fov)
// pxSize = xDist / view_width
// let:
// u_scale = 2.0 * tan(0.5 * fov) / view_width
// then:
// pxSize = dist * u_scale
float pxSize = dist * u_scale;
gl_PointSize /= pxSize;
}
}
""",
GL.GL_FRAGMENT_SHADER : """
#ifdef GL_ES
precision mediump float;
#endif
varying vec4 v_color;
void main()
{
vec2 xy = (gl_PointCoord - 0.5) * 2.0;
if (dot(xy, xy) <= 1.0) gl_FragColor = v_color;
else discard;
}
"""
}
SHADER_CORE = {
GL.GL_VERTEX_SHADER : """
uniform float u_scale;
uniform mat4 u_modelview;
uniform mat4 u_mvp;
in vec4 a_position;
in vec4 a_color;
in float a_size;
out vec4 v_color;
void main() {
gl_Position = u_mvp * a_position;
v_color = a_color;
gl_PointSize = a_size;
if (u_scale != 0.0) {
// pxMode=False
// the modelview matrix transforms the vertex to
// camera space, where the camera is at (0, 0, 0).
vec4 cpos = u_modelview * a_position;
float dist = length(cpos.xyz);
// equations:
// xDist = dist * 2.0 * tan(0.5 * fov)
// pxSize = xDist / view_width
// let:
// u_scale = 2.0 * tan(0.5 * fov) / view_width
// then:
// pxSize = dist * u_scale
float pxSize = dist * u_scale;
gl_PointSize /= pxSize;
}
}
""",
GL.GL_FRAGMENT_SHADER : """
#ifdef GL_ES
precision mediump float;
#endif
in vec4 v_color;
out vec4 fragColor;
void main()
{
vec2 xy = (gl_PointCoord - 0.5) * 2.0;
if (dot(xy, xy) <= 1.0) fragColor = v_color;
else discard;
}
"""
}
|