summaryrefslogtreecommitdiff
path: root/src/mbgl/renderer/painter_raster.cpp
blob: 98299ed342c7c235ec6cee6f8861fde615dfdca3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <mbgl/renderer/painter.hpp>
#include <mbgl/renderer/paint_parameters.hpp>
#include <mbgl/renderer/render_tile.hpp>
#include <mbgl/gl/gl.hpp>
#include <mbgl/renderer/raster_bucket.hpp>
#include <mbgl/style/layers/raster_layer.hpp>
#include <mbgl/style/layers/raster_layer_impl.hpp>
#include <mbgl/shader/shaders.hpp>

namespace mbgl {

using namespace style;

static float saturationFactor(float saturation) {
    if (saturation > 0) {
        return 1 - 1 / (1.001 - saturation);
    } else {
        return -saturation;
    }
}

static float contrastFactor(float contrast) {
    if (contrast > 0) {
        return 1 / (1 - contrast);
    } else {
        return 1 + contrast;
    }
}

static std::array<float, 3> spinWeights(float spin) {
    spin *= util::DEG2RAD;
    float s = std::sin(spin);
    float c = std::cos(spin);
    std::array<float, 3> spin_weights = {{
        (2 * c + 1) / 3,
        (-std::sqrt(3.0f) * s - c + 1) / 3,
        (std::sqrt(3.0f) * s - c + 1) / 3
    }};
    return spin_weights;
}

void Painter::renderRaster(PaintParameters& parameters,
                           RasterBucket& bucket,
                           const RasterLayer& layer,
                           const RenderTile& tile) {
    if (pass != RenderPass::Translucent) return;

    const RasterPaintProperties& properties = layer.impl->paint;

    if (bucket.hasData()) {
        auto& rasterShader = parameters.shaders.raster;
        auto& rasterVAO = parameters.shaders.coveringRasterArray;

        context.program = rasterShader.getID();
        rasterShader.u_matrix = tile.matrix;
        rasterShader.u_buffer_scale = 1.0f;
        rasterShader.u_opacity0 = properties.rasterOpacity;
        rasterShader.u_opacity1 = 0;

        rasterShader.u_brightness_low = properties.rasterBrightnessMin;
        rasterShader.u_brightness_high = properties.rasterBrightnessMax;
        rasterShader.u_saturation_factor = saturationFactor(properties.rasterSaturation);
        rasterShader.u_contrast_factor = contrastFactor(properties.rasterContrast);
        rasterShader.u_spin_weights = spinWeights(properties.rasterHueRotate);

        context.stencilTest = false;

        rasterShader.u_image0 = 0; // GL_TEXTURE0
        rasterShader.u_image1 = 1; // GL_TEXTURE1
        rasterShader.u_tl_parent = {{ 0.0f, 0.0f }};
        rasterShader.u_scale_parent = 1.0f;

        context.depthFunc = gl::DepthTestFunction::LessEqual;
        context.depthTest = true;
        context.depthMask = false;
        setDepthSublayer(0);

        bucket.drawRaster(rasterShader, rasterVertexBuffer, rasterVAO, context);
    }
}

} // namespace mbgl