Add brightness and contrast controls, thanks @evanw

This commit is contained in:
Tom Payne
2012-07-22 02:24:17 +02:00
parent 883089c3d1
commit ec2ab8265c
4 changed files with 88 additions and 0 deletions

View File

@@ -56,6 +56,22 @@ ol.webgl.LayerRenderer.prototype.getMap = function() {
ol.webgl.LayerRenderer.prototype.getMatrix = goog.abstractMethod;
/**
* @inheritDoc
*/
ol.webgl.LayerRenderer.prototype.handleLayerBrightnessChange = function() {
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/
ol.webgl.LayerRenderer.prototype.handleLayerContrastChange = function() {
this.dispatchChangeEvent();
};
/**
* @inheritDoc
*/

View File

@@ -34,6 +34,7 @@ ol.DEBUG_WEBGL = false;
/**
* @constructor
* @extends {ol.webgl.shader.Fragment}
* @see https://github.com/evanw/glfx.js/blob/master/src/filters/adjust/brightnesscontrast.js
* @see https://github.com/evanw/glfx.js/blob/master/src/filters/adjust/huesaturation.js
*/
ol.webgl.map.shader.Fragment = function() {
@@ -41,6 +42,8 @@ ol.webgl.map.shader.Fragment = function() {
'precision mediump float;',
'',
'uniform mat4 uMatrix;',
'uniform float uBrightness;',
'uniform float uContrast;',
'uniform float uHue;',
'uniform float uOpacity;',
'uniform float uSaturation;',
@@ -71,6 +74,13 @@ ol.webgl.map.shader.Fragment = function() {
' color.rgb += (average - color.rgb) * (-uSaturation);',
' }',
'',
' color.rgb += uBrightness;',
' if (uContrast > 0.0) {',
' color.rgb = (color.rgb - 0.5) / (1.0 - uContrast) + 0.5;',
' } else {',
' color.rgb = (color.rgb - 0.5) * (1.0 + uContrast) + 0.5;',
' }',
'',
' color.a = color.a * uOpacity;',
'',
' gl_FragColor = color;',
@@ -156,6 +166,8 @@ ol.webgl.Map = function(target, opt_values) {
* @private
* @type {{aPosition: number,
* aTexCoord: number,
* uBrightness: WebGLUniformLocation,
* uContrast: WebGLUniformLocation,
* uHue: WebGLUniformLocation,
* uMatrix: WebGLUniformLocation,
* uOpacity: WebGLUniformLocation,
@@ -484,6 +496,8 @@ ol.webgl.Map.prototype.renderInternal = function() {
this.locations_ = {
aPosition: gl.getAttribLocation(program, 'aPosition'),
aTexCoord: gl.getAttribLocation(program, 'aTexCoord'),
uBrightness: gl.getUniformLocation(program, 'uBrightness'),
uContrast: gl.getUniformLocation(program, 'uContrast'),
uHue: gl.getUniformLocation(program, 'uHue'),
uMatrix: gl.getUniformLocation(program, 'uMatrix'),
uOpacity: gl.getUniformLocation(program, 'uOpacity'),
@@ -517,6 +531,8 @@ ol.webgl.Map.prototype.renderInternal = function() {
this.forEachVisibleLayer(function(layer, layerRenderer) {
gl.uniformMatrix4fv(
this.locations_.uMatrix, false, layerRenderer.getMatrix());
gl.uniform1f(this.locations_.uBrightness, layer.getBrightness());
gl.uniform1f(this.locations_.uContrast, layer.getContrast());
gl.uniform1f(this.locations_.uHue, layer.getHue());
gl.uniform1f(this.locations_.uOpacity, layer.getOpacity());
gl.uniform1f(this.locations_.uSaturation, layer.getSaturation());