From 0d2724dec4284d8e75182d549c68f6a8cdd483df Mon Sep 17 00:00:00 2001 From: Lauren Budorick Date: Tue, 12 Aug 2014 18:27:35 -0700 Subject: Raster styling all functional except brightness -- need to rework parsing --- src/renderer/painter_prerender.cpp | 4 ++++ src/renderer/painter_raster.cpp | 20 ++++++++++++++++ src/shader/raster.fragment.glsl | 30 +++++++++++++++++++++++- src/shader/raster_shader.cpp | 47 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 100 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/renderer/painter_prerender.cpp b/src/renderer/painter_prerender.cpp index f17014b622..1a54336923 100644 --- a/src/renderer/painter_prerender.cpp +++ b/src/renderer/painter_prerender.cpp @@ -34,6 +34,10 @@ void Painter::renderPrerenderedTexture(RasterBucket &bucket, const mat4 &matrix, rasterShader->setImage(0); rasterShader->setBuffer(buffer); rasterShader->setOpacity(properties.opacity); + rasterShader->setBrightness(properties.brightness[0], properties.brightness[1]); + rasterShader->setSaturation(properties.saturation); + rasterShader->setContrast(properties.contrast); + rasterShader->setSpin(spinWeights(properties.hue_rotate)); bucket.texture.bindTexture(); coveringRasterArray.bind(*rasterShader, tileStencilBuffer, BUFFER_OFFSET(0)); glDrawArrays(GL_TRIANGLES, 0, (GLsizei)tileStencilBuffer.index()); diff --git a/src/renderer/painter_raster.cpp b/src/renderer/painter_raster.cpp index 7dcc9b24d6..82e604e283 100644 --- a/src/renderer/painter_raster.cpp +++ b/src/renderer/painter_raster.cpp @@ -7,6 +7,8 @@ #include #include +#include + using namespace mbgl; void Painter::renderRaster(RasterBucket& bucket, std::shared_ptr layer_desc, const Tile::ID& id, const mat4 &matrix) { @@ -63,10 +65,16 @@ void Painter::renderRaster(RasterBucket& bucket, std::shared_ptr lay if (bucket.hasData()) { depthMask(false); +// const Tile::ID parent_id = id.parent(id.z); + useProgram(rasterShader->program); rasterShader->setMatrix(matrix); rasterShader->setBuffer(0); rasterShader->setOpacity(properties.opacity); + rasterShader->setBrightness(properties.brightness[0], properties.brightness[1]); + rasterShader->setSaturation(properties.saturation); + rasterShader->setContrast(properties.contrast); + rasterShader->setSpin(spinWeights(properties.hue_rotate)); glDepthRange(strata + strata_epsilon, 1.0f); @@ -76,3 +84,15 @@ void Painter::renderRaster(RasterBucket& bucket, std::shared_ptr lay } } + +std::array Painter::spinWeights(float spin) { + spin *= M_PI / 180; + float s = sin(spin); + float c = cos(spin); + std::array spin_weights = {{ + (2 * c + 1) / 3, + (-sqrtf(3.0) * s - c + 1) / 3, + (sqrtf(3.0) * s - c + 1) / 3 + }}; + return spin_weights; +} \ No newline at end of file diff --git a/src/shader/raster.fragment.glsl b/src/shader/raster.fragment.glsl index feb0a58f07..d56a5b5901 100644 --- a/src/shader/raster.fragment.glsl +++ b/src/shader/raster.fragment.glsl @@ -3,6 +3,34 @@ uniform float u_opacity; varying vec2 v_pos; +uniform float u_brightness_low; +uniform float u_brightness_high; + +uniform float u_saturation_factor; +uniform float u_contrast_factor; +uniform vec3 u_spin_weights; + void main() { - gl_FragColor = texture2D(u_image, v_pos) * u_opacity; + + vec4 color = texture2D(u_image, v_pos) * u_opacity; + vec3 rgb = color.rgb; + + // spin + rgb = vec3( + dot(rgb, u_spin_weights.xyz), + dot(rgb, u_spin_weights.zxy), + dot(rgb, u_spin_weights.yzx)); + + // saturation + float average = (color.r + color.g + color.b) / 3.0; + rgb += (average - rgb) * u_saturation_factor; + + // contrast + rgb = (rgb - 0.5) * u_contrast_factor + 0.5; + + // brightness + vec3 u_high_vec = vec3(u_brightness_low, u_brightness_low, u_brightness_low); + vec3 u_low_vec = vec3(u_brightness_high, u_brightness_high, u_brightness_high); + + gl_FragColor = vec4(mix(u_high_vec, u_low_vec, rgb), color.a); } diff --git a/src/shader/raster_shader.cpp b/src/shader/raster_shader.cpp index 1a21a2edc6..6531bad1a2 100644 --- a/src/shader/raster_shader.cpp +++ b/src/shader/raster_shader.cpp @@ -24,6 +24,11 @@ RasterShader::RasterShader() u_image = glGetUniformLocation(program, "u_image"); u_opacity = glGetUniformLocation(program, "u_opacity"); u_buffer = glGetUniformLocation(program, "u_buffer"); + u_brightness_low = glGetUniformLocation(program, "u_brightness_low"); + u_brightness_high = glGetUniformLocation(program, "u_brightness_high"); + u_saturation_factor = glGetUniformLocation(program, "u_saturation_factor"); + u_contrast_factor = glGetUniformLocation(program, "u_contrast_factor"); + u_spin_weights = glGetUniformLocation(program, "u_spin_weights"); // fprintf(stderr, "RasterShader:\n"); // fprintf(stderr, " - u_matrix: %d\n", u_matrix); @@ -57,3 +62,45 @@ void RasterShader::setBuffer(float new_buffer) { buffer = new_buffer; } } + +void RasterShader::setBrightness(float new_brightness_low, float new_brightness_high) { + if (brightness_low != new_brightness_low) { + glUniform1f(u_brightness_low, new_brightness_low); + brightness_low = new_brightness_low; + } + if (brightness_high!= new_brightness_high) { + glUniform1f(u_brightness_high, new_brightness_high); + brightness_high = new_brightness_high; + } +} + +void RasterShader::setSaturation(float new_saturation_factor) { + if (saturation_factor != new_saturation_factor) { + if (new_saturation_factor > 0) { + new_saturation_factor = 1 - 1 / (1.001 - new_saturation_factor); + } else { + new_saturation_factor = -new_saturation_factor; + } + glUniform1f(u_saturation_factor, new_saturation_factor); + saturation_factor = new_saturation_factor; + } +} + +void RasterShader::setContrast(float new_contrast_factor) { + if (contrast_factor != new_contrast_factor) { + if (new_contrast_factor > 0) { + new_contrast_factor = 1 / (1 - new_contrast_factor); + } else { + new_contrast_factor = 1 + new_contrast_factor; + } + glUniform1f(u_contrast_factor, new_contrast_factor); + contrast_factor = new_contrast_factor; + } +} + +void RasterShader::setSpin(std::array new_spin_weights) { + if (spin_weights != new_spin_weights) { + glUniform3fv(u_spin_weights, 1, new_spin_weights.data()); + spin_weights = new_spin_weights; + } +} \ No newline at end of file -- cgit v1.2.1