- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I have some relatively simple GLSL shaders used for CAE visualization. They basically interpolate colors in HSV space (from blue->red) based on a single value varying from 0-1. Works great on every nVidia and ATI card. Fails on all Intel HD including latest drivers as the color gets clamped to blue no matter what the varying value.
uniform float t;
uniform float scale;
uniform float maxP;
uniform float minP;
uniform vec3 min_hsl_color;
uniform vec3 max_hsl_color;
attribute float color_flag;
attribute float P;
varying vec4 color;
vec3 RGBToHSL(vec3 color)
{
vec3 hsl; // init to 0 to avoid warnings ? (and reverse if + remove first part)
float fmin = min(min(color.r, color.g), color.b); //Min. value of RGB
float fmax = max(max(color.r, color.g), color.b); //Max. value of RGB
float delta = fmax - fmin; //Delta RGB value
hsl.z = (fmax + fmin) / 2.0; // Luminance
if (delta == 0.0) //This is a gray, no chroma...
{
hsl.x = 0.0; // Hue
hsl.y = 0.0; // Saturation
}
else //Chromatic data...
{
if (hsl.z < 0.5)
hsl.y = delta / (fmax + fmin); // Saturation
else
hsl.y = delta / (2.0 - fmax - fmin); // Saturation
float deltaR = (((fmax - color.r) / 6.0) + (delta / 2.0)) / delta;
float deltaG = (((fmax - color.g) / 6.0) + (delta / 2.0)) / delta;
float deltaB = (((fmax - color.b) / 6.0) + (delta / 2.0)) / delta;
if (color.r == fmax )
hsl.x = deltaB - deltaG; // Hue
else if (color.g == fmax)
hsl.x = (1.0 / 3.0) + deltaR - deltaB; // Hue
else if (color.b == fmax)
hsl.x = (2.0 / 3.0) + deltaG - deltaR; // Hue
if (hsl.x < 0.0)
hsl.x += 1.0; // Hue
else if (hsl.x > 1.0)
hsl.x -= 1.0; // Hue
}
return hsl;
}
float HueToRGB(float f1, float f2, float hue)
{
if (hue < 0.0)
hue += 1.0;
else if (hue > 1.0)
hue -= 1.0;
float res;
if ((6.0 * hue) < 1.0)
res = f1 + (f2 - f1) * 6.0 * hue;
else if ((2.0 * hue) < 1.0)
res = f2;
else if ((3.0 * hue) < 2.0)
res = f1 + (f2 - f1) * ((2.0 / 3.0) - hue) * 6.0;
else
res = f1;
return res;
}
vec3 HSLToRGB(vec3 hsl)
{
vec3 rgb;
if (hsl.y == 0.0)
rgb = vec3(hsl.z); // Luminance
else
{
float f2;
if (hsl.z < 0.5)
f2 = hsl.z * (1.0 + hsl.y);
else
f2 = (hsl.z + hsl.y) - (hsl.y * hsl.z);
float f1 = 2.0 * hsl.z - f2;
rgb.r = HueToRGB(f1, f2, hsl.x + (1.0/3.0));
rgb.g = HueToRGB(f1, f2, hsl.x);
rgb.b= HueToRGB(f1, f2, hsl.x - (1.0/3.0));
}
return rgb;
}
vec3 interphsl(float frac,vec3 LoColor, vec3 HiColor)
{
if ( frac > 1.0 )
frac = 1.0;
if ( frac < 0.0 )
frac = 0.0;
// Apply fractional distance in HSV color space to find color
// Note that dS, dL, and dH may all be either + or -
//
float dS = HiColor.y - LoColor.y;
float dL = HiColor.z - LoColor.z;
float s = LoColor.y + ( dS * frac );
float l = LoColor.z + ( dL * frac );
float dir = 1.0;
float dH = HiColor.x - LoColor.x;
if ( abs( dH ) < 0.5 )
{
if ( dH > 0.0 )
dH = 1.0 - dH;
else
dH = abs( dH ) - 1.0;
dir = -1.0;
}
float h = LoColor.x + ( dir * dH * frac );
// Limit h to range of 0-360
//
if ( h < 0.0 )
h += 1.0;
else
{
if ( h > 1.0 )
h -= 1.0;
}
// Now make a color to return
//
vec3 result= vec3( h, s, l );
return result;
}
void main(void)
{
gl_Position = gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
gl_ClipVertex = gl_ModelViewMatrix * gl_Vertex;
if(color_flag>0.0){
P=abs(P);
float theMax=max(maxP,abs(minP));
float len=(P)/(theMax);
vec3 interp_hsl_color = interphsl(len*abs(t),min_hsl_color,max_hsl_color);
vec3 rgbcol=HSLToRGB(interp_hsl_color);
color=vec4(rgbcol,1.0);
}else{
color=gl_Color;
}
}
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I may have found the problem
The line
P=abs(P) is illegal as P is an attribute.
Accepted, sometimes by the nVidia compiler.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I may have found the problem
The line
P=abs(P) is illegal as P is an attribute.
Accepted, sometimes by the nVidia compiler.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page