summaryrefslogtreecommitdiff
path: root/src/shader/icon_shader.cpp
blob: 6455d870a241cee47d77c095b62a71fc8c91cf00 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <mbgl/shader/icon_shader.hpp>
#include <mbgl/shader/shaders.hpp>
#include <mbgl/platform/gl.hpp>

#include <cstdio>

using namespace mbgl;

IconShader::IconShader()
    : Shader(
         shaders[ICON_SHADER].vertex,
         shaders[ICON_SHADER].fragment
         ) {
    if (!valid) {
        fprintf(stderr, "invalid icon shader\n");
        return;
    }

    a_pos = glGetAttribLocation(program, "a_pos");
    a_offset = glGetAttribLocation(program, "a_offset");
    a_tex = glGetAttribLocation(program, "a_tex");
    a_angle = glGetAttribLocation(program, "a_angle");
    a_minzoom = glGetAttribLocation(program, "a_minzoom");
    a_maxzoom = glGetAttribLocation(program, "a_maxzoom");
    a_rangeend = glGetAttribLocation(program, "a_rangeend");
    a_rangestart = glGetAttribLocation(program, "a_rangestart");
    a_labelminzoom = glGetAttribLocation(program, "a_labelminzoom");

    u_matrix = glGetUniformLocation(program, "u_matrix");
    u_exmatrix = glGetUniformLocation(program, "u_exmatrix");
    u_angle = glGetUniformLocation(program, "u_angle");
    u_zoom = glGetUniformLocation(program, "u_zoom");
    u_flip = glGetUniformLocation(program, "u_flip");
    u_fadedist = glGetUniformLocation(program, "u_fadedist");
    u_minfadezoom = glGetUniformLocation(program, "u_minfadezoom");
    u_maxfadezoom = glGetUniformLocation(program, "u_maxfadezoom");
    u_fadezoom = glGetUniformLocation(program, "u_fadezoom");
    u_opacity = glGetUniformLocation(program, "u_opacity");
    u_texsize = glGetUniformLocation(program, "u_texsize");

    // fprintf(stderr, "IconShader:\n");
    // fprintf(stderr, "    - u_matrix: %d\n", u_matrix);
    // fprintf(stderr, "    - u_exmatrix: %d\n", u_exmatrix);
    // fprintf(stderr, "    - u_angle: %d\n", u_angle);
    // fprintf(stderr, "    - u_zoom: %d\n", u_zoom);
    // fprintf(stderr, "    - u_flip: %d\n", u_flip);
    // fprintf(stderr, "    - u_fadedist: %d\n", u_fadedist);
    // fprintf(stderr, "    - u_minfadezoom: %d\n", u_minfadezoom);
    // fprintf(stderr, "    - u_maxfadezoom: %d\n", u_maxfadezoom);
    // fprintf(stderr, "    - u_fadezoom: %d\n", u_fadezoom);
    // fprintf(stderr, "    - u_opacity: %d\n", u_opacity);
}

void IconShader::bind(char *offset) {
    const int stride = 20;

    glEnableVertexAttribArray(a_pos);
    glVertexAttribPointer(a_pos, 2, GL_SHORT, false, stride, offset + 0);

    glEnableVertexAttribArray(a_offset);
    glVertexAttribPointer(a_offset, 2, GL_SHORT, false, stride, offset + 4);

    glEnableVertexAttribArray(a_labelminzoom);
    glVertexAttribPointer(a_labelminzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 8);

    glEnableVertexAttribArray(a_minzoom);
    glVertexAttribPointer(a_minzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 9);

    glEnableVertexAttribArray(a_maxzoom);
    glVertexAttribPointer(a_maxzoom, 1, GL_UNSIGNED_BYTE, false, stride, offset + 10);

    glEnableVertexAttribArray(a_angle);
    glVertexAttribPointer(a_angle, 1, GL_UNSIGNED_BYTE, false, stride, offset + 11);

    glEnableVertexAttribArray(a_rangeend);
    glVertexAttribPointer(a_rangeend, 1, GL_UNSIGNED_BYTE, false, stride, offset + 12);

    glEnableVertexAttribArray(a_rangestart);
    glVertexAttribPointer(a_rangestart, 1, GL_UNSIGNED_BYTE, false, stride, offset + 13);

    glEnableVertexAttribArray(a_tex);
    glVertexAttribPointer(a_tex, 2, GL_SHORT, false, stride, offset + 16);
}

void IconShader::setExtrudeMatrix(const std::array<float, 16>& new_exmatrix) {
    if (exmatrix != new_exmatrix) {
        glUniformMatrix4fv(u_exmatrix, 1, GL_FALSE, new_exmatrix.data());
        exmatrix = new_exmatrix;
    }
}

void IconShader::setAngle(float new_angle) {
    if (angle != new_angle) {
        glUniform1f(u_angle, new_angle);
        angle = new_angle;
    }
}

void IconShader::setZoom(float new_zoom) {
    if (zoom != new_zoom) {
        glUniform1f(u_zoom, new_zoom);
        zoom = new_zoom;
    }
}

void IconShader::setFlip(float new_flip) {
    if (flip != new_flip) {
        glUniform1f(u_flip, new_flip);
        flip = new_flip;
    }
}

void IconShader::setFadeDist(float new_fadedist) {
    if (fadedist != new_fadedist) {
        glUniform1f(u_fadedist, new_fadedist);
        fadedist = new_fadedist;
    }
}

void IconShader::setMinFadeZoom(float new_minfadezoom) {
    if (minfadezoom != new_minfadezoom) {
        glUniform1f(u_minfadezoom, new_minfadezoom);
        minfadezoom = new_minfadezoom;
    }
}

void IconShader::setMaxFadeZoom(float new_maxfadezoom) {
    if (maxfadezoom != new_maxfadezoom) {
        glUniform1f(u_maxfadezoom, new_maxfadezoom);
        maxfadezoom = new_maxfadezoom;
    }
}

void IconShader::setFadeZoom(float new_fadezoom) {
    if (fadezoom != new_fadezoom) {
        glUniform1f(u_fadezoom, new_fadezoom);
        fadezoom = new_fadezoom;
    }
}

void IconShader::setOpacity(float new_opacity) {
    if (opacity != new_opacity) {
        glUniform1f(u_opacity, new_opacity);
        opacity = new_opacity;
    }
}

void IconShader::setTextureSize(const std::array<float, 2> &new_texsize) {
    if (texsize != new_texsize) {
        glUniform2fv(u_texsize, 1, new_texsize.data());
        texsize = new_texsize;
    }
}