How do I draw a rainbow in Freeglut?

前端 未结 2 1266
死守一世寂寞
死守一世寂寞 2020-12-20 18:11

I\'m trying to draw a rainbow-coloured plot legend in openGL. Here is what I\'ve got so far:

glBegin(GL_QUADS);
for (int i = 0; i != legendElements; ++i)
{
          


        
2条回答
  •  时光说笑
    2020-12-20 19:13

    I generate spectral colors like this:

    void spectral_color(double &r,double &g,double &b,double l) // RGB <- lambda l = < 380,780 > [nm]
        {
             if (l<380.0) r=     0.00;
        else if (l<400.0) r=0.05-0.05*sin(M_PI*(l-366.0)/ 33.0);
        else if (l<435.0) r=     0.31*sin(M_PI*(l-395.0)/ 81.0);
        else if (l<460.0) r=     0.31*sin(M_PI*(l-412.0)/ 48.0);
        else if (l<540.0) r=     0.00;
        else if (l<590.0) r=     0.99*sin(M_PI*(l-540.0)/104.0);
        else if (l<670.0) r=     1.00*sin(M_PI*(l-507.0)/182.0);
        else if (l<730.0) r=0.32-0.32*sin(M_PI*(l-670.0)/128.0);
        else              r=     0.00;
             if (l<454.0) g=     0.00;
        else if (l<617.0) g=     0.78*sin(M_PI*(l-454.0)/163.0);
        else              g=     0.00;
             if (l<380.0) b=     0.00;
        else if (l<400.0) b=0.14-0.14*sin(M_PI*(l-364.0)/ 35.0);
        else if (l<445.0) b=     0.96*sin(M_PI*(l-395.0)/104.0);
        else if (l<510.0) b=     0.96*sin(M_PI*(l-377.0)/133.0);
        else              b=     0.00;
        }
    
    • l is input wavelength [nm] < 380,780 >
    • r,g,b is output RGB color < 0,1 >

    This is simple rough sin wave approximation of real spectral color data. You can also create table from this and interpolate it or use texture ... output colors are:

    spectral_colors

    there are also different approaches like:

    1. linear color - composite gradients

      • like this: http://www.physics.sfasu.edu/astro/color/spectra.html
      • but the output is not good enough for me
    2. human eye X,Y,Z sensitivity curves integration

      you have to have really precise X,Y,Z curves, even slight deviation causes 'unrealistic' colors like in this example

      human eye XYZ sensitivity

    To make it better you have to normalize colors and add exponential sensitivity corrections. Also these curves are changing with every generation and are different in different regions of world. So unless you are doing some special medical/physics softs it is not a good idea to go this way.

    spectral colors comparison

    | <- 380nm ----------------------------------------------------------------- 780nm -> |

    [edit1] here is mine new physically more accurate conversion

    I strongly recommend to use this approach instead (it is more accurate and better in any way)

提交回复
热议问题