Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def init(cls):
"""OpenGL context initialization"""
gl.glHint(gl.GL_LINE_SMOOTH_HINT, gl.GL_NICEST)
def init(cls):
"""OpenGL context initialization"""
version = gl.glGetString(gl.GL_VERSION)
majorVersion = int(version[0])
assert majorVersion >= 2
gl.glEnable(gl.GL_VERTEX_PROGRAM_POINT_SIZE) # OpenGL 2
gl.glEnable(gl.GL_POINT_SPRITE) # OpenGL 2
if majorVersion >= 3: # OpenGL 3
gl.glEnable(gl.GL_PROGRAM_POINT_SIZE)
def prepare(self):
if self._cmap_texture is None:
# TODO share cmap texture accross Images
# put all cmaps in one texture
colormap = numpy.empty((16, 256, self.colormap.shape[1]),
dtype=self.colormap.dtype)
colormap[:] = self.colormap
format_ = gl.GL_RGBA if colormap.shape[-1] == 4 else gl.GL_RGB
self._cmap_texture = Texture(internalFormat=format_,
data=colormap,
format_=format_,
texUnit=self._CMAP_TEX_UNIT,
minFilter=gl.GL_NEAREST,
magFilter=gl.GL_NEAREST,
wrap=(gl.GL_CLAMP_TO_EDGE,
gl.GL_CLAMP_TO_EDGE))
if self._texture is None:
internalFormat = self._INTERNAL_FORMATS[self.data.dtype]
self._texture = Image(internalFormat,
self.data,
format_=gl.GL_RED,
texUnit=self._DATA_TEX_UNIT)
elif self._textureIsDirty:
self._textureIsDirty = True
self._texture.updateAll(format_=gl.GL_RED, data=self.data)
program = context.prog(*self._shaders)
program.use()
gl.glViewport(0, 0, width, height)
gl.glDisable(gl.GL_BLEND)
gl.glDisable(gl.GL_DEPTH_TEST)
gl.glDisable(gl.GL_SCISSOR_TEST)
# gl.glScissor(0, 0, width, height)
gl.glClearColor(0., 0., 0., 0.)
gl.glClear(gl.GL_COLOR_BUFFER_BIT)
gl.glUniform1i(program.uniforms['texture'], fbo.texture.texUnit)
gl.glEnableVertexAttribArray(program.attributes['position'])
gl.glVertexAttribPointer(program.attributes['position'],
4,
gl.GL_FLOAT,
gl.GL_FALSE,
0,
self._position)
fbo.texture.bind()
gl.glDrawArrays(gl.GL_TRIANGLE_STRIP, 0, len(self._position))
gl.glBindTexture(gl.GL_TEXTURE_2D, 0)
format_ = gl.GL_RGBA if colormap.shape[-1] == 4 else gl.GL_RGB
self._cmap_texture = Texture(internalFormat=format_,
data=colormap,
format_=format_,
texUnit=self._CMAP_TEX_UNIT,
minFilter=gl.GL_NEAREST,
magFilter=gl.GL_NEAREST,
wrap=(gl.GL_CLAMP_TO_EDGE,
gl.GL_CLAMP_TO_EDGE))
if self._texture is None:
internalFormat = self._INTERNAL_FORMATS[self.data.dtype]
self._texture = Image(internalFormat,
self.data,
format_=gl.GL_RED,
texUnit=self._DATA_TEX_UNIT)
elif self._textureIsDirty:
self._textureIsDirty = True
self._texture.updateAll(format_=gl.GL_RED, data=self.data)
def _renderOverlayGL(self):
"""Render overlay layer: overlay items and crosshair."""
plotWidth, plotHeight = self.getPlotBoundsInPixels()[2:]
# Scissor to plot area
gl.glScissor(self._plotFrame.margins.left,
self._plotFrame.margins.bottom,
plotWidth, plotHeight)
gl.glEnable(gl.GL_SCISSOR_TEST)
self._renderItems(overlay=True)
# Render crosshair cursor
if self._crosshairCursor is not None and self._mousePosInPixels is not None:
self._progBase.use()
gl.glUniform2i(self._progBase.uniforms['isLog'], False, False)
gl.glUniform1f(self._progBase.uniforms['tickLen'], 0.)
posAttrib = self._progBase.attributes['position']
matrixUnif = self._progBase.uniforms['matrix']
colorUnif = self._progBase.uniforms['color']
hatchStepUnif = self._progBase.uniforms['hatchStep']
gl.glViewport(0, 0, self._plotFrame.size[0], self._plotFrame.size[1])
gl.glUniformMatrix4fv(matrixUnif, 1, gl.GL_TRUE,
self._shaderValueDefinition()
vertexShader = self._shaders[0].substitute(
valueType=valueType)
fragmentShader = self._shaders[1].substitute(
sceneDecl=ctx.fragDecl,
scenePreCall=ctx.fragCallPre,
scenePostCall=ctx.fragCallPost,
valueType=valueType,
valueToColorDecl=valueToColorDecl,
valueToColorCall=valueToColorCall,
alphaSymbolDecl=self._MARKER_FUNCTIONS[self.marker])
program = ctx.glCtx.prog(vertexShader, fragmentShader,
attrib0=self.attrib0)
program.use()
gl.glEnable(gl.GL_VERTEX_PROGRAM_POINT_SIZE) # OpenGL 2
gl.glEnable(gl.GL_POINT_SPRITE) # OpenGL 2
# gl.glEnable(gl.GL_PROGRAM_POINT_SIZE)
program.setUniformMatrix('matrix', ctx.objectToNDC.matrix)
program.setUniformMatrix('transformMat',
ctx.objectToCamera.matrix,
safe=True)
ctx.setupProgram(program)
self._renderGL2PreDrawHook(ctx, program)
self._draw(program)
xPixel, yPixel = self._mousePosInPixels
xPixel, yPixel = xPixel + 0.5, yPixel + 0.5
vertices = numpy.array(((0., yPixel),
(self._plotFrame.size[0], yPixel),
(xPixel, 0.),
(xPixel, self._plotFrame.size[1])),
dtype=numpy.float32)
gl.glEnableVertexAttribArray(posAttrib)
gl.glVertexAttribPointer(posAttrib,
2,
gl.GL_FLOAT,
gl.GL_FALSE,
0, vertices)
gl.glLineWidth(lineWidth)
gl.glDrawArrays(gl.GL_LINES, 0, len(vertices))
gl.glDisable(gl.GL_SCISSOR_TEST)
imageDecl=self._shaderImageColorDecl()
)
program = ctx.glCtx.prog(self._shaders[0], fragment)
program.use()
ctx.viewport.light.setupProgram(ctx, program)
if not self.isBackfaceVisible:
gl.glCullFace(gl.GL_BACK)
gl.glEnable(gl.GL_CULL_FACE)
program.setUniformMatrix('matrix', ctx.objectToNDC.matrix)
program.setUniformMatrix('transformMat',
ctx.objectToCamera.matrix,
safe=True)
gl.glUniform1f(program.uniforms['alpha'], self._alpha)
shape = self._data.shape
gl.glUniform2f(program.uniforms['dataScale'], 1./shape[1], 1./shape[0])
gl.glUniform1i(program.uniforms['data'], self._texture.texUnit)
ctx.setupProgram(program)
self._texture.bind()
self._renderGL2PreDrawHook(ctx, program)
self._draw(program)
if not self.isBackfaceVisible:
gl.glDisable(gl.GL_CULL_FACE)
def prepareGL2(self, context):
if self._texture is None or self._update_texture:
if self._texture is not None:
self._texture.discard()
colormap = numpy.empty(
(16, self._colormap.shape[0], self._colormap.shape[1]),
dtype=self._colormap.dtype)
colormap[:] = self._colormap
format_ = gl.GL_RGBA if colormap.shape[-1] == 4 else gl.GL_RGB
self._texture = _glutils.Texture(
format_, colormap, format_,
texUnit=self._COLORMAP_TEXTURE_UNIT,
minFilter=gl.GL_NEAREST,
magFilter=gl.GL_NEAREST,
wrap=gl.GL_CLAMP_TO_EDGE)
self._update_texture = False