Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 2 of 2

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

  1. #1
    Newbie Newbie
    Join Date
    Feb 2018
    Posts
    1

    Question 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. Click image for larger version. 

Name:	howto_animate_rainbow_colors.jpg 
Views:	10 
Size:	7.7 KB 
ID:	2668

    If anybody`s wondering im using C++
    Last edited by StabberKnight906; 02-12-2018 at 08:57 AM. Reason: Additional information and linking

  2. #2
    Senior Member OpenGL Guru Dark Photon's Avatar
    Join Date
    Oct 2004
    Location
    Druidia
    Posts
    4,260
    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.
    Last edited by Dark Photon; 02-13-2018 at 07:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •