What’s the most efficient way of drawing a huge list of vertical lines in JUCE?
In my app I need to draw many colored waveforms - currently each waveform is a list of vertical lines, each line with possibly a different color. The waveform is likely to be redrawn numerous times in parallel lanes so it would be ideal to cache the lines and just re-draw the waveform at a different screen position.
What is the most efficient way of achieving this? Should I use openGL?
If they are waveforms for existing audio files, you could render them to a juce::Image file ahead of time, possibly in another thread, and store them somewhere to be reused, and have them live beyond the scope of the window. Then you’d just be painting the relevant section of the image.
If it’s a on the fly generated waveform that scrolls, like in many plugins these days, OpenGL is currently the ticket.
@Fandusss thank you for the reply! Yes the waveform will be generated on the fly. Do you know any resources/examples showing how to best do it in OpenGL?
I’m not aware of any open source code, I’m sorry! But I’ve implemented something myself and the general idea is to use a openGLRenderer and write shaders. Unfortunately you’ll need to learn like low level OpenGL. There’s no good way to use juce graphics and have the animation be smooth, you have to push geometry to the gpu and have a shader render that geometry.
When you say a ‘huge lists’ I presume you mean every horizonal pixel has a single vertical line, instead if every sample?
If it’s just horizontal pixel lines, then you’ll need to draw the maximum height line for each, so you’ll have to look at all the in between samples, otherwise it’ll change shape as you zoom, which will look bad. I apologise if you’re already doing this.
Yes the current most efficient way is to use OpenGL to render a line list with glDrawArrays(GL_LINES…
With an ‘OpenGLContext’ attached to the main window, it and every child will be drawn with OpenGL, I have found to be good for large Juce image drawing and lines, everything else is just meh because of the way Juce renders, I think. I would go the standard Juce way, and either use ‘g.drawLine’ or use a path and draw it to an offscreen image as @Fandusss suggested. OpenGL is on shaky ground for Macs, so using Juce lines may grant you some future proofing when things change.