• Create Account

### #ActualTsus

Posted 05 June 2012 - 02:44 AM

Hi!

If you want a more or less correct rainbow, you could do the following:
(Though it’s probably a little overkill. )

In the vertex shader, add to the y-coordinate an effect parameter “time” that you increment each frame and pass the sum on to the pixel shader. The “time” parameter lets your rainbow move. By using frac() (gives the fractional part) you can turn the value into something periodic in [0..1]. Perhaps scale the value before using frac, to adjust “how many” rainbows you see, i.e. how often it repeats.

Now, the overkill part:
If you assume the scalar value being passed in from the vertex shader to be a wave length, you can use the CIE response curves to retrieve the activation of the RGB receptors. This way you get a true rainbow. Modelling the CIE curves with Gaussian mixture models gives eventually:

// CIE response curves using Gaussians fitting
const vec4 CIEX0 = vec4(0.26714125, 0.173056848724526, -0.0517890668554628, 0.369341509681465);
const vec4 CIEX1 = vec4(0.0, 0.510852785928701, 0.636521548441552, -0.324530476950362);
const vec4 CIEX2 = vec4(1.0622, 0.547302197035226, 0.0899535691555178, 1.10399973088081);

const vec4 CIEY0 = vec4(0.2671425, 0.86798560108836, 0.150307921271593, -0.354744089805774);
const vec4 CIEY1 = vec4(0, 0.10539332389757, 0.168752691961971, -0.289650515359526);
const vec4 CIEY2 = vec4(1.0002, 0.445956775505726, 0.0920541376951253, 0.814888040084084);

const vec4 CIEZ0 = vec4(0.26714375, 0.174251742295476, -0.0569218355789753, 1.72408897831517);
const vec4 CIEZ1 = vec4(0.0, 0.0542544622978704, 0.0457454482464726, -0.442679263574661);
const vec4 CIEZ2 = vec4(1.7826, 0.711309229610584, 0.285040831286585, -0.407629686738774);

float Gaussian(const float x0, const float s, const float w, const float x)
{
return w * exp( -(x - x0) * (x - x0) / (2.0 * s * s + 1.0e-20) );
}

float GaussianMixture(const float lambda, const vec4 Data0, const vec4 Data1, const vec4 Data2)
{
float t = (lambda - 0.380) / (0.780 - 0.380);
float g0 = Gaussian(Data0.y, Data0.z, Data0.w, t);
float g1 = Gaussian(Data1.y, Data1.z, Data1.w, t);
float g2 = Gaussian(Data2.y, Data2.z, Data2.w, t);

return min(max(g0 + g1 + g2 + Data0.x, Data1.x), Data2.x);
}

vec3 Spectrum2RGB(const float lambda)
{
float x = GaussianMixture(lambda, CIEX0, CIEX1, CIEX2);
float y = GaussianMixture(lambda, CIEY0, CIEY1, CIEY2);
float z = GaussianMixture(lambda, CIEZ0, CIEZ1, CIEZ2);

// E to D65
// 0.26713798 is for mapping spectrum 1.0 into rgb (1.0, 1.0, 1.0)
x = x * 0.9504700 / 0.26713798;
y = y * 1.0000000 / 0.26713798;
z = z * 1.0888300 / 0.26713798;

// sRGB (D65)
vec3 rgb;
rgb.r = (x * ( 3.2404542) + y * (-1.5371385) + z * (-0.4985314));
rgb.g = (x * (-0.9692660) + y * ( 1.8760108) + z * ( 0.0415560));
rgb.b = (x * ( 0.0556434) + y * (-0.2040259) + z * ( 1.0572252));

return rgb;
}

Spectrum2RGB converts from wave length to color. (The GLSL code is from Toshiya Hachisuka’s GPU SPPM implementation: “Parallel Progressive Photon Mapping on GPUs”.)

You could as well just use the wavelength variable and apply some sin/cos stuff and use different phase shifts for the RGB components. This is probably more what was intended by your teachers. ;)
Something like:
color.r = sin( input.posY_plus_time );
color.g = sin( input.posY_plus_time + 2/3*Pi);
color.b = sin( input.posY_plus_time + 4/3*Pi);


Have fun with that.
Best regards!

### #1Tsus

Posted 04 June 2012 - 05:26 PM

Hi!

If you want a more or less correct rainbow, you could do the following:
(Though it’s probably a little overkill. )

In the vertex shader, add to the y-coordinate an effect parameter “time” that you increment each frame and pass the sum on to the pixel shader. The “time” parameter lets your rainbow move. By using frac() (gives the fractional part) you can turn the value into something periodic in [0..1]. Perhaps scale the value before using frac, to adjust “how many” rainbows you see, i.e. how often it repeats.

Now, the overkill part:
If you assume the scalar value being passed in from the vertex shader to be a wave length, you can use the CIE response curves to retrieve the activation of the RGB receptors. This way you get a true rainbow. Modelling the CIE curves with Gaussian mixture models gives eventually:

// CIE response curves using Gaussians fitting
const vec4 CIEX0 = vec4(0.26714125, 0.173056848724526, -0.0517890668554628, 0.369341509681465);
const vec4 CIEX1 = vec4(0.0, 0.510852785928701, 0.636521548441552, -0.324530476950362);
const vec4 CIEX2 = vec4(1.0622, 0.547302197035226, 0.0899535691555178, 1.10399973088081);

const vec4 CIEY0 = vec4(0.2671425, 0.86798560108836, 0.150307921271593, -0.354744089805774);
const vec4 CIEY1 = vec4(0, 0.10539332389757, 0.168752691961971, -0.289650515359526);
const vec4 CIEY2 = vec4(1.0002, 0.445956775505726, 0.0920541376951253, 0.814888040084084);

const vec4 CIEZ0 = vec4(0.26714375, 0.174251742295476, -0.0569218355789753, 1.72408897831517);
const vec4 CIEZ1 = vec4(0.0, 0.0542544622978704, 0.0457454482464726, -0.442679263574661);
const vec4 CIEZ2 = vec4(1.7826, 0.711309229610584, 0.285040831286585, -0.407629686738774);

float Gaussian(const float x0, const float s, const float w, const float x)
{
return w * exp( -(x - x0) * (x - x0) / (2.0 * s * s + 1.0e-20) );
}

float GaussianMixture(const float lambda, const vec4 Data0, const vec4 Data1, const vec4 Data2)
{
float t = (lambda - 0.380) / (0.780 - 0.380);
float g0 = Gaussian(Data0.y, Data0.z, Data0.w, t);
float g1 = Gaussian(Data1.y, Data1.z, Data1.w, t);
float g2 = Gaussian(Data2.y, Data2.z, Data2.w, t);

return min(max(g0 + g1 + g2 + Data0.x, Data1.x), Data2.x);
}

vec3 Spectrum2RGB(const float lambda)
{
float x = GaussianMixture(lambda, CIEX0, CIEX1, CIEX2);
float y = GaussianMixture(lambda, CIEY0, CIEY1, CIEY2);
float z = GaussianMixture(lambda, CIEZ0, CIEZ1, CIEZ2);

// E to D65
// 0.26713798 is for mapping spectrum 1.0 into rgb (1.0, 1.0, 1.0)
x = x * 0.9504700 / 0.26713798;
y = y * 1.0000000 / 0.26713798;
z = z * 1.0888300 / 0.26713798;

// sRGB (D65)
vec3 rgb;
rgb.r = (x * ( 3.2404542) + y * (-1.5371385) + z * (-0.4985314));
rgb.g = (x * (-0.9692660) + y * ( 1.8760108) + z * ( 0.0415560));
rgb.b = (x * ( 0.0556434) + y * (-0.2040259) + z * ( 1.0572252));

return rgb;
}

Spectrum2RGB converts from wave length to color. (The GLSL code is from Toshiya Hachisuka’s GPU SPPM implementation: “Parallel Progressive Photon Mapping on GPUs”.)

You could as well just use the wavelength variable and apply some sin/cos stuff and use different phase shifts for the RGB components. This is probably more what was intended by your teachers. ;)

Have fun with that.
Best regards!

PARTNERS