26#if defined(USE_ANDROID_GLES2) || defined(ocpnUSE_GLSL)
28#ifdef USE_ANDROID_GLES2
33#ifdef USE_ANDROID_GLES2
34const GLchar* preamble =
37const GLchar* preamble =
48static const GLchar* color_tri_vertex_shader_source =
49 "attribute vec2 position;\n"
50 "uniform mat4 MVMatrix;\n"
51 "uniform mat4 TransformMatrix;\n"
52 "uniform vec4 color;\n"
53 "varying vec4 fragColor;\n"
55 " fragColor = color;\n"
56 " gl_Position = MVMatrix * TransformMatrix * vec4(position, 0.0, 1.0);\n"
59static const GLchar* color_tri_fragment_shader_source =
60 "precision lowp float;\n"
61 "varying vec4 fragColor;\n"
63 " gl_FragColor = fragColor;\n"
67static const GLchar* texture_2D_vertex_shader_source =
68 "attribute vec2 aPos;\n"
69 "attribute vec2 aUV;\n"
70 "uniform mat4 MVMatrix;\n"
71 "uniform mat4 TransformMatrix;\n"
72 "varying vec2 varCoord;\n"
74 " gl_Position = MVMatrix * TransformMatrix * vec4(aPos, 0.0, 1.0);\n"
78static const GLchar* texture_2D_fragment_shader_source =
79 "precision lowp float;\n"
80 "uniform sampler2D uTex;\n"
81 "varying vec2 varCoord;\n"
83 " gl_FragColor = texture2D(uTex, varCoord);\n"
88static const GLchar* circle_filled_vertex_shader_source =
89 "precision highp float;\n"
90 "attribute vec2 aPos;\n"
91 "uniform mat4 MVMatrix;\n"
92 "uniform mat4 TransformMatrix;\n"
94 " gl_Position = MVMatrix * TransformMatrix * vec4(aPos, 0.0, 1.0);\n"
97static const GLchar* circle_filled_fragment_shader_source =
98 "precision highp float;\n"
99 "uniform float border_width;\n"
100 "uniform float circle_radius;\n"
101 "uniform vec4 circle_color;\n"
102 "uniform vec4 border_color;\n"
103 "uniform vec2 circle_center;\n"
105 "float d = distance(gl_FragCoord.xy, circle_center);\n"
106 "if (d < (circle_radius - border_width)) { gl_FragColor = circle_color; }\n"
107 "else if (d < circle_radius) { gl_FragColor = border_color; }\n"
108 "else { gl_FragColor = vec4(0.0, 0.0, 0.0, 0.0); }\n"
112static const GLchar* texture_2DA_vertex_shader_source =
113 "attribute vec2 aPos;\n"
114 "attribute vec2 aUV;\n"
115 "uniform mat4 MVMatrix;\n"
116 "uniform mat4 TransformMatrix;\n"
117 "varying vec2 varCoord;\n"
119 " gl_Position = MVMatrix * TransformMatrix * vec4(aPos, 0.0, 1.0);\n"
123static const GLchar* texture_2DA_fragment_shader_source =
124 "precision lowp float;\n"
125 "uniform sampler2D uTex;\n"
126 "varying vec2 varCoord;\n"
127 "uniform vec4 color;\n"
129 " gl_FragColor = texture2D(uTex, varCoord) + color;\n"
134static const GLchar* AALine_vertex_shader_source =
135 "uniform vec2 uViewPort; //Width and Height of the viewport\n"
136 "varying vec2 vLineCenter;\n"
137 "attribute vec2 position;\n"
138 "uniform mat4 MVMatrix;\n"
139 "uniform mat4 TransformMatrix;\n"
142 " vec4 pp = MVMatrix * vec4(position, 0.0, 1.0);\n"
143 " gl_Position = pp;\n"
144 " vec2 vp = uViewPort;\n"
145 " vLineCenter = 0.5*(pp.xy + vec2(1, 1))*vp;\n"
149static const GLchar* AALine_fragment_shader_source =
150 "precision mediump float;\n"
151 "uniform float uLineWidth;\n"
152 "uniform vec4 color;\n"
153 "uniform float uBlendFactor; //1.5..2.5\n"
154 "varying vec2 vLineCenter;\n"
157 " vec4 col = color;\n"
158 " float d = length(vLineCenter-gl_FragCoord.xy);\n"
159 " float w = uLineWidth;\n"
163 " if(float((w/2-d)/(w/2)) < .5){\n"
164 " //col.w *= pow(float((w-d)/w), uBlendFactor);\n"
165 " col.w *= pow(float((w/2-d)/(w/2)), uBlendFactor);\n"
168 " gl_FragColor = col;\n"
173static const GLchar *ring_vertex_shader_source =
174 "precision highp float;\n"
175 "attribute vec2 aPos;\n"
176 "uniform mat4 MVMatrix;\n"
177 "uniform mat4 TransformMatrix;\n"
179 " gl_Position = MVMatrix * TransformMatrix * vec4(aPos, 0.0, 1.0);\n"
182static const GLchar *ring_fragment_shader_source =
183 "precision highp float;\n"
184 "uniform float border_width;\n"
185 "uniform float circle_radius;\n"
186 "uniform float ring_width;\n"
187 "uniform vec4 circle_color;\n"
188 "uniform vec4 border_color;\n"
189 "uniform vec2 circle_center;\n"
190 "uniform float sector_1;\n"
191 "uniform float sector_2;\n"
194 "const float PI = 3.14159265358979323846264;\n"
195 "bool bdraw = false;\n"
197 "float angle = atan(gl_FragCoord.y-circle_center.y, "
198 "gl_FragCoord.x-circle_center.x);\n"
199 "angle = PI/2.0 - angle;\n"
200 "if(angle < 0.0) angle += PI * 2.0;\n"
202 "if(sector_2 > PI * 2.0){\n"
203 " if((angle > sector_1) && (angle < (PI * 2.0) )){\n"
206 " if(angle < sector_2 - (PI * 2.0)){\n"
210 " if((angle > sector_1) && (angle < sector_2)){\n"
216 " float d = distance(gl_FragCoord.xy, circle_center);\n"
217 " if (d > circle_radius) {\n"
219 " } else if( d > (circle_radius - border_width)) {\n"
220 " gl_FragColor = border_color;\n"
221 " } else if( d > (circle_radius - border_width - ring_width)) {\n"
222 " gl_FragColor = circle_color;\n"
223 " } else if( d > (circle_radius - border_width - ring_width - "
225 " gl_FragColor = border_color;\n"
242bool bShadersLoaded[2];
244bool loadShaders(
int index) {
246 if (bShadersLoaded[index]) {
247 reConfigureShaders(index);
256 if (!pcolor_tri_shader_program[index]) {
258 shaderProgram->addShaderFromSource(color_tri_vertex_shader_source, GL_VERTEX_SHADER);
259 shaderProgram->addShaderFromSource(color_tri_fragment_shader_source, GL_FRAGMENT_SHADER);
260 shaderProgram->linkProgram();
262 if (shaderProgram->isOK())
263 pcolor_tri_shader_program[index] = shaderProgram;
266 if (!ptexture_2D_shader_program[index]) {
268 shaderProgram->addShaderFromSource(texture_2D_vertex_shader_source, GL_VERTEX_SHADER);
269 shaderProgram->addShaderFromSource(texture_2D_fragment_shader_source, GL_FRAGMENT_SHADER);
270 shaderProgram->linkProgram();
272 if (shaderProgram->isOK())
273 ptexture_2D_shader_program[index] = shaderProgram;
276 if (!pcircle_filled_shader_program[index]) {
278 shaderProgram->addShaderFromSource(circle_filled_vertex_shader_source, GL_VERTEX_SHADER);
279 shaderProgram->addShaderFromSource(circle_filled_fragment_shader_source, GL_FRAGMENT_SHADER);
280 shaderProgram->linkProgram();
282 if (shaderProgram->isOK())
283 pcircle_filled_shader_program[index] = shaderProgram;
286 if (!ptexture_2DA_shader_program[index]) {
288 shaderProgram->addShaderFromSource(texture_2DA_vertex_shader_source, GL_VERTEX_SHADER);
289 shaderProgram->addShaderFromSource(texture_2DA_fragment_shader_source, GL_FRAGMENT_SHADER);
290 shaderProgram->linkProgram();
292 if (shaderProgram->isOK())
293 ptexture_2DA_shader_program[index] = shaderProgram;
296 if (!pAALine_shader_program[index]) {
298 shaderProgram->addShaderFromSource(AALine_fragment_shader_source, GL_FRAGMENT_SHADER);
299 shaderProgram->addShaderFromSource(AALine_vertex_shader_source, GL_VERTEX_SHADER);
300 shaderProgram->linkProgram();
302 if (shaderProgram->isOK())
303 pAALine_shader_program[index] = shaderProgram;
307 if (!pring_shader_program[index]) {
309 shaderProgram->addShaderFromSource(ring_fragment_shader_source, GL_FRAGMENT_SHADER);
310 shaderProgram->addShaderFromSource(ring_vertex_shader_source, GL_VERTEX_SHADER);
311 shaderProgram->linkProgram();
313 if (shaderProgram->isOK())
314 pring_shader_program[index] = shaderProgram;
317 bShadersLoaded[index] =
true;
318 reConfigureShaders(index);
323void reConfigureShaders(
int index) {
326void unloadShaders() {
327 bShadersLoaded[0] = bShadersLoaded[1] =
false;
332 shaderProgram->addShaderFromSource(color_tri_vertex_shader_source, GL_VERTEX_SHADER);
333 shaderProgram->addShaderFromSource(color_tri_fragment_shader_source, GL_FRAGMENT_SHADER);
334 shaderProgram->linkProgram();
336 if (shaderProgram->isOK())
337 return shaderProgram;