Skip to content

Cannot re-assign attribute data with the same numpy array #321

@mfrigerio17

Description

@mfrigerio17

Hi, the following trivial example fails:

import numpy as np

import glumpy
from glumpy import app, gloo, gl

vertex = """
attribute vec2 a_position;
void main() {
    gl_Position = vec4(a_position, 0.0, 1.0);
}
"""

fragment = """
uniform vec4 color;
void main() {
    gl_FragColor = color;
}
"""
# 10 random points, on the [-1,1] interval on the X axis
signal = np.c_[np.linspace(-1,1,10), np.random.uniform(-0.5,0.5,10)].astype(np.float32)

program = gloo.Program(vertex, fragment)
program['a_position'] = signal      # this works . . .
program['color'] = (1.0, 1.0, 0.16, 1.0)
window = app.Window()

@window.event
def on_draw(dt):
    window.clear((0.2,0.2,0.2,1))
    program.draw(gl.GL_LINE_STRIP)

@window.timer(0.1)
def on_timer(self):
    program['a_position'] = signal     # . . . but this does not

if __name__ == '__main__':
    app.run()

Error:

[i] Using PYGLFW (GL 2.1)
[i] Running at 60 frames/second
Traceback (most recent call last):
  File "/home/marco-irs/misc/opengl/glumpybug.py", line 37, in <module>
    app.run()
  File "/home/marco-irs/tmp/pyvenvs/osc/lib/python3.10/site-packages/glumpy/app/__init__.py", line 362, in run
    run(duration, framecount)
  File "/home/marco-irs/tmp/pyvenvs/osc/lib/python3.10/site-packages/glumpy/app/__init__.py", line 341, in run
    dt = clock.tick()
  File "/home/marco-irs/tmp/pyvenvs/osc/lib/python3.10/site-packages/glumpy/app/clock.py", line 368, in tick
    self.call_scheduled_functions(delta_t)
  File "/home/marco-irs/tmp/pyvenvs/osc/lib/python3.10/site-packages/glumpy/app/clock.py", line 307, in call_scheduled_functions
    item.func(ts - item.last_ts, *item.args, **item.kwargs)
  File "/home/marco-irs/misc/opengl/glumpybug.py", line 34, in on_timer
    program['a_position'] = signal  # this does not
  File "/home/marco-irs/tmp/pyvenvs/osc/lib/python3.10/site-packages/glumpy/gloo/program.py", line 355, in __setitem__
    self._attributes[name].set_data(data)
  File "/home/marco-irs/tmp/pyvenvs/osc/lib/python3.10/site-packages/glumpy/gloo/variable.py", line 366, in set_data
    self._data[...] = data
  File "/home/marco-irs/tmp/pyvenvs/osc/lib/python3.10/site-packages/glumpy/gloo/gpudata.py", line 136, in __setitem__
    np.ndarray.__setitem__(self, key, value)
ValueError: could not broadcast input array from shape (10,2) into shape (10,)

Issue #319 seems to be exactly the same, but I am not sure about the explanation there...
It looks like Glumpy (opengl??) reshapes the numpy array, so one cannot reassign the attribute data with the same array used in the first assignment (obviously in a real case one would change some coordinates, before the re-assignment).
Is this expected?
Commenting out the line that causes the error gives a working example, meaning the first assignment to a_position works as expected.

I have "equivalent" code for Vispy and it works fine.

Thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions