How does one create animated rgb rainbow effect on objects(lines,quads,triangles)?

Well i got stuck for quite a, while trying to answer myself this question i tried everything from, do to for loops to change colors gradiently from red to red in rgb spectrum but my project would stop working or will not give any results at all.
In reality i want to make object in my case line to change colors in the whole rgb spectrum gradiently like this http://csharphelper.com/blog/2017/08...inbow-colors-c
Could anybody help me with this problem ?

If link doesn"t work then.

If anybody`s wondering im using C++

Last edited by StabberKnight906; 02-12-2018 at 08:57 AM.
Reason: Additional information and linking

This is a perfect job for texture mapping. Upload your color gradient to a 1D texture. And then in the shader, take some value (with a min/max associated with those values), map it from (min..max) to (0..1), and then do a filtered texture lookup into your 1D texture. If you've enabled LINEAR filtering, the texture sampling will automatically interpolate the correct color from your gradient texture map even if its texture coordinate lies between 2 texel centers in the texture map.

One other quick note: If the number of texels in your texture map is large, you may not care about this next point -- just use your 0..1 texture coordinate (which runs from the left edge of the left-most texel in the texture to the right edge of the right-most texel in the texture), and that'll be close enough for a decent result. However...

When you're using a texture as a linear lookup function like this, it's actually more correct to have your texture coordinates run between the center of the left texel to the center of the right texel. The reason is because that in OpenGL, the "data" associated with each texel (a color, in your case) is conceptually located at the centers of the texels (cell-centered data). That way, you get smooth interpolation between your min and max colors over the entire 0..1 range.

To get this modified texture coordinate from your 0..1 input value is easy. See this for details. But basically, take your 0..1 value (we'll call this tc), and run it through this expression:

Code glsl:

tc *(M-1)/float(N)+0.5/N

where M = number of "used" texels in your texture, and N = total number of texels in your texture. If you're using the whole width of the texture for data, then M == N. All this expression does is "squish" your 0..1 value into one that runs between the centers of the edge texels instead of the edges of the edge texels.

Now you might be wondering if you don't do this, what happens when you sample your texture within a 1/2 texel of the edge of a texture (or more correctly, the edge of a texture MIP level). That's where the wrap mode you've set on the texture axis comes in. If you use GL_CLAMP_TO_EDGE, then you'll get the same color value all the way out from the edge texel's center out to the edge of the texture.