Color Curve in a 2D GLSL Filter

This topic is 979 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

# version 120

uniform sampler2D bgl_RenderedTexture;

float interp(float xa[6], float ya[6], float x){
float xi = 0.0;
int i = 0;
for(int ii=0; ii<6; ii ++){
float xix = xa[ii];
if (xix >= x){
xi = xix;
i = ii;
break;
}
}
float x_min = xa[i-1];
float y_min = ya[i-1];
float y_max = ya[i];
float factor = (x - x_min) / (xi - x_min);
return y_min + (y_max - y_min) * factor;
}

float x_array[6] = float[6](0.0, 0.35, 0.46, 0.53, 0.7, 1.0);
float y_array[6] = float[6](0.0, 0.41, 0.52, 0.52, 0.64, 1.0);

void main(void)
{
vec4 color = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
float xc = interp(x_array, y_array, color.x);
float yc = interp(x_array, y_array, color.y);
float zc = interp(x_array, y_array, color.z);
color = vec4(xc, yc, zc, 1.0);

gl_FragColor = color.rgba;
}


Share on other sites

BUMP, seems that my own text got gone.

So my question is:

how to make this be smooth rather than linear?

Share on other sites

Could try replacing factor with:

float factor = smoothstep(x_min, xi, x);

Share on other sites

How does it work?

Share on other sites

# version 120

uniform sampler2D bgl_RenderedTexture;

float interp(float xa[4], float ya[4], float x){
float xi = 0.0;
int i = 0;
for(int ii=0; ii<6; ii ++){
float xix = xa[ii];
if (xix >= x){
xi = xix;
i = ii;
break;
}
}
float x_min = xa[i-1];
float y_min = ya[i-1];
float y_max = ya[i];
float factor = smoothstep(x_min, xi, x);
return y_min + (y_max - y_min) * factor;
}

float x_array[4] = float[4](0.0, 0.49, 0.53, 1.0);
float y_array[4] = float[4](0.0, 0.4, 0.6, 1.0);

void main(void)
{
vec4 color = texture2D(bgl_RenderedTexture, gl_TexCoord[0].st);
float xc = interp(x_array, y_array, color.x);
float yc = interp(x_array, y_array, color.y);
float zc = interp(x_array, y_array, color.z);
color = vec4(xc, yc, zc, 1.0);

gl_FragColor = color.rgba;
}


Share on other sites

BUMP, why's nobody telling anything? :(

Share on other sites

How does it work?

You did not say what you mean with "it". If you mean "How does the smoothstep function work?", the answer can be found on https://en.wikipedia.org/wiki/Smoothstep and https://www.opengl.org/sdk/docs/man/html/smoothstep.xhtml

Or you could create the color gradient with a painting program and load it as a texture. That would allow for more complex gradients with less math operations.

Share on other sites

I'm not experienced in 2D filters. I don't understand much.

However, it seems that the problem with smoothstep is that it doesn't work in 1.20 version. Must try changing to later one.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 11
• 13
• 9
• 9
• Forum Statistics

• Total Topics
634080
• Total Posts
3015381
×