Last week I completed 3 post-processing shaders. I completed a shader for controlling intensity profiles, a shader for adjusting the brightness and contrast of an image, and a shader for curves adjustment. This blog will focus on the brightness and contrast shader as well as the curves adjustment shader.
When changing the brightness of an image, a constant is added or subtracted from the luminance of every pixel in the scene.
Changing the contrast of an image changes the range of luminance values present. It essentialy expands or compresses the color of a pixel around a constant.
Below is the shader code for brightness and contrast control:
uniform float brightness;
uniform float contrast;
uniform sampler2D scene;
void main()
{
//sample scene color
vec3 color = texture2D(scene, gl_TexCoord[0].st).rgb;
//contrast color
vec3 colorContrasted = (color - 0.5) * contrast + 0.5;
//and brightness constant
vec3 bright = colorContrasted + vec3(brightness,brightness,brightness);
gl_FragColor.rgb = bright;
}
Below are screenshots of the shader:
The curves adjustment shader uses a color map/ramp to remap the colors to a new set of colors based on the map. Below is the shader code:
uniform sampler2D scene;
uniform sampler2D ramp;
void main()
{
vec3 color = texture2D(scene, gl_TexCoord[0].st).rgb;
vec3 outColor;
outColor.r = texture2D(ramp, vec2(color.x , 0.0)).r;
outColor.g = texture2D(ramp, vec2(color.y , 0.0)).g;
outColor.b = texture2D(ramp, vec2(color.z , 0.0)).b;
gl_FragColor.rgb = outColor;
}
To remap the color we use the RGB values of the color to sample for the color ramp. This is done here:
outColor.r = texture2D(ramp, vec2(color.x , 0.0)).r;
outColor.g = texture2D(ramp, vec2(color.y , 0.0)).g;
outColor.b = texture2D(ramp, vec2(color.z , 0.0)).b;
uniform float brightness;
uniform float contrast;
uniform sampler2D scene;
void main()
{
//sample scene color
vec3 color = texture2D(scene, gl_TexCoord[0].st).rgb;
//contrast color
vec3 colorContrasted = (color - 0.5) * contrast + 0.5;
//and brightness constant
vec3 bright = colorContrasted + vec3(brightness,brightness,brightness);
gl_FragColor.rgb = bright;
}
Below are screenshots of the shader:
Brightness = 0, Contrast = 1 |
Contrast Below 1 |
Contrast Above 1 |
Brightness Increased ( > 0) |
Brightness Decreased ( < 0) |
uniform sampler2D scene;
uniform sampler2D ramp;
void main()
{
vec3 color = texture2D(scene, gl_TexCoord[0].st).rgb;
vec3 outColor;
outColor.r = texture2D(ramp, vec2(color.x , 0.0)).r;
outColor.g = texture2D(ramp, vec2(color.y , 0.0)).g;
outColor.b = texture2D(ramp, vec2(color.z , 0.0)).b;
gl_FragColor.rgb = outColor;
}
To remap the color we use the RGB values of the color to sample for the color ramp. This is done here:
outColor.r = texture2D(ramp, vec2(color.x , 0.0)).r;
outColor.g = texture2D(ramp, vec2(color.y , 0.0)).g;
outColor.b = texture2D(ramp, vec2(color.z , 0.0)).b;
No comments:
Post a Comment