OpenGL, Shaders, Line plot



If i were to use the starter code in the OpenGL example provided with JUCE for creating a plugin that plots an FFT, do I even need to write a custom shader as is in the provided code? Wouldn’t a simple opengl line draw function from x1,y1 to x2,y2 for the generated FFT data array be enough? I also do not know anything about writing custom shaders (I only know what a shader is :P). I basically need someone to either tell me if my assumption that shaders aren’t really necessary for such a trivial operation such as a line draw is correct or not. And if not why! Thanks.


Hello !

Well, I suggest that you try to do the same thing without OpenGL first, using the Graphics class drawing functions or the Path class. Then, you might want to add the OpenGL context to enable some acceleration, and finally you can try to do something using OpenGL API and shaders.

By the way, a shader can be either a fragment or a vertex shader (for the most known and useful in JUCE) which isn’t the same thing at all. A possible scenario for a custom code displaying a FFT plot using OpenGL + shaders would be :

  • A render function which is supposed to draw N triangles and provides in a buffer their coordinates depending on the last FFT result
  • A basic vertex shader which uses the content of the buffer to set the position of the triangles
  • A basic fragment shader which is just here to set the colour inside each triangle

To do so, very basic vertex and fragment shaders are needed such as (very simple example) :

// ==================================================================
// Vertex shader
in vec2 position;

void main(void)
    gl_Position = vec4(position.x, position.y, 0.5, 1.0);

// ==================================================================
// Fragment shader
out vec4 color;

void main(void)
   color = vec4(0.0, 0.8, 1.0, 1.0);

As you can see, the hard part isn’t in the shaders themselves :wink:




So just so I’m clear there is no NEED to write a shader for a simple buffer-by-buffer FFT line plot. If the inbuilt JUCE drawLine() function is slow, a basic openGL line drawing function should be sufficient? Using a shader, as per the JUCEOpenGLAppExample would be overkill. Yes?


@thearpeggiator > in OpenGL 3+ API, there is nothing like an OpenGL basic line. Everything is made from combinations of triangles, and their position + colour is set by vertex + fragment shaders such as the ones in my example. And again, before following this path, I suggest you try to do the same thing with JUCE standard graphics functions :wink:


I took the effort to go through an OpenGL tutorial (ver > 3.3) and everything you said made perfect sense after that! (specifically about the vertex and fragment shaders) :slight_smile: Thanks for pointing me in the right direction. I’ll update how my project turns out soon.