Skip to content

avoid PyOpenGL automatic array conversion#2131

Merged
j9ac9k merged 9 commits intopyqtgraph:masterfrom
pijyoi:error_on_copy
Dec 9, 2021
Merged

avoid PyOpenGL automatic array conversion#2131
j9ac9k merged 9 commits intopyqtgraph:masterfrom
pijyoi:error_on_copy

Conversation

@pijyoi
Copy link
Copy Markdown
Contributor

@pijyoi pijyoi commented Dec 4, 2021

Following #2111, this PR tries to eliminate PyOpenGL automatic array conversions in the OpenGL items by making the conversions explicitly. The places where conversion was taking place was discovered by setting OpenGL.ERROR_ON_COPY = True in the examples and running them. With this PR, all included OpenGL examples are able to run without error with the above attribute set.

GLImageItem and GLVolumeItem deserve a mention. Pyqtgraph uses Fortran-major axes and thus transposes the texture data before uploading them. GLImageItem was fixed to avoid the transpose by changing the texture coordinate code. The texture coordinate code of GLVolumeItem was too complicated to fix, thus a memory copy is forced.

The script below was used to verify that GLImageItem draws the same before and after this PR.

import pyqtgraph as pg
import pyqtgraph.opengl as gl
import numpy as np
import scipy.misc

pg.mkQApp()

glv = gl.GLViewWidget()
glv.setCameraParams(elevation=90, azimuth=0)
# X points down, Y points right
glv.show()

image = scipy.misc.face()
tex, _ = pg.makeRGBA(image)
glii = gl.GLImageItem(tex)
s = 1/128
glii.scale(s, s, 1)
w, h = tex.shape[:2]    # GLImageItem uses transposed coords?
w *= s
h *= s
glii.translate(-w/2, -h/2, 0)
glv.addItem(glii)

spots = np.array([[-w/2, -h/2, 0, 1, 0, 0, 0.5],
                  [-w/2,  h/2, 0, 0, 1, 0, 0.5],
                  [ w/2,  h/2, 0, 0, 0, 1, 0.5],
                  [ w/2, -h/2, 0, 0, 1, 1, 0.5]])
glspi = gl.GLScatterPlotItem(pos=spots[:,:3], size=50, color=spots[:,3:])
glv.addItem(glspi)

pg.exec()

This PR may fix #825.

this is probably also fixing a bug:
the code was using np.empty() but was not filling up all the elements.
transpose texture coords instead of image data

this doesn't alter the displayed image's orientation.
@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented Dec 8, 2021

Given this comment #825 (comment) think we should take the draft label off this PR and merge?

@pijyoi pijyoi marked this pull request as ready for review December 8, 2021 22:58
@j9ac9k j9ac9k merged commit cf64431 into pyqtgraph:master Dec 9, 2021
@pijyoi pijyoi deleted the error_on_copy branch December 10, 2021 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Memory leak when calling gl.GLMeshItem.setMeshData()

2 participants