use GLShader for LineTexLayer
This commit is contained in:
parent
0fb0aa59c2
commit
c107e197f5
53
vtm/resources/assets/shaders/linetex_layer.glsl
Normal file
53
vtm/resources/assets/shaders/linetex_layer.glsl
Normal file
@ -0,0 +1,53 @@
|
||||
#ifdef GLES
|
||||
precision mediump float;
|
||||
#endif
|
||||
uniform mat4 u_mvp;
|
||||
uniform vec4 u_color;
|
||||
uniform float u_pscale;
|
||||
uniform float u_width;
|
||||
attribute vec4 a_pos0;
|
||||
attribute vec4 a_pos1;
|
||||
attribute vec2 a_len0;
|
||||
attribute vec2 a_len1;
|
||||
attribute float a_flip;
|
||||
varying vec2 v_st;
|
||||
void
|
||||
main(){
|
||||
vec4 pos;
|
||||
if (a_flip == 0.0) {
|
||||
// vec2 dir = u_width * a_pos0.zw;
|
||||
pos = vec4(a_pos0.xy + (u_width * a_pos0.zw), 0.0, 1.0);
|
||||
v_st = vec2(a_len0.x / u_pscale, 1.0);
|
||||
}
|
||||
else {
|
||||
// vec2 dir = u_width * a_pos1.zw;
|
||||
pos = vec4(a_pos1.xy - (u_width * a_pos1.zw), 0.0, 1.0);
|
||||
v_st = vec2(a_len1.x / u_pscale, -1.0);
|
||||
}
|
||||
gl_Position = u_mvp * pos;
|
||||
}
|
||||
|
||||
$$
|
||||
|
||||
#extension GL_OES_standard_derivatives : enable
|
||||
#ifdef GLES
|
||||
precision mediump float;
|
||||
#endif
|
||||
uniform vec4 u_color;
|
||||
uniform vec4 u_bgcolor;
|
||||
uniform float u_pwidth;
|
||||
varying vec2 v_st;
|
||||
void
|
||||
main(){
|
||||
/* distance on perpendicular to the line */
|
||||
float dist = abs(v_st.t);
|
||||
float fuzz = fwidth(v_st.t);
|
||||
float fuzz_p = fwidth(v_st.s);
|
||||
float line_w = smoothstep(0.0, fuzz, 1.0 - dist);
|
||||
float stipple_w = smoothstep(0.0, fuzz, u_pwidth - dist);
|
||||
/* triangle waveform in the range 0..1 for regular pattern */
|
||||
float phase = abs(mod(v_st.s, 2.0) - 1.0);
|
||||
/* interpolate between on/off phase, 0.5 = equal phase length */
|
||||
float stipple_p = smoothstep(0.5 - fuzz_p, 0.5 + fuzz_p, phase);
|
||||
gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));
|
||||
}
|
@ -22,6 +22,7 @@ import java.nio.ShortBuffer;
|
||||
|
||||
import org.oscim.backend.GL20;
|
||||
import org.oscim.core.GeometryBuffer;
|
||||
import org.oscim.renderer.GLShader;
|
||||
import org.oscim.renderer.GLState;
|
||||
import org.oscim.renderer.GLUtils;
|
||||
import org.oscim.renderer.GLViewport;
|
||||
@ -250,49 +251,45 @@ public final class LineTexLayer extends RenderElement {
|
||||
sbuf.position(sbuf.position() + 6);
|
||||
}
|
||||
|
||||
static class Shader extends GLShader {
|
||||
int uMVP, uColor, uWidth, uBgColor, uScale;
|
||||
int uPatternWidth, uPatternScale;
|
||||
int aPos0, aPos1, aLen0, aLen1, aFlip;
|
||||
|
||||
Shader(String shaderFile) {
|
||||
if (!create(shaderFile))
|
||||
return;
|
||||
|
||||
uMVP = getUniform("u_mvp");
|
||||
|
||||
uScale = getUniform("u_scale");
|
||||
uColor = getUniform("u_color");
|
||||
uWidth = getUniform("u_width");
|
||||
uBgColor = getUniform("u_bgcolor");
|
||||
|
||||
uPatternWidth = getUniform("u_pwidth");
|
||||
uPatternScale = getUniform("u_pscale");
|
||||
|
||||
aPos0 = getAttrib("a_pos0");
|
||||
aPos1 = getAttrib("a_pos1");
|
||||
aLen0 = getAttrib("a_len0");
|
||||
aLen1 = getAttrib("a_len1");
|
||||
aFlip = getAttrib("a_flip");
|
||||
}
|
||||
}
|
||||
|
||||
public final static class Renderer {
|
||||
private static Shader shader;
|
||||
|
||||
/* factor to normalize extrusion vector and scale to coord scale */
|
||||
private final static float COORD_SCALE_BY_DIR_SCALE =
|
||||
MapRenderer.COORD_SCALE / LineLayer.DIR_SCALE;
|
||||
|
||||
private static int shader;
|
||||
private static int hVertexPosition0;
|
||||
private static int hVertexPosition1;
|
||||
private static int hVertexLength0;
|
||||
private static int hVertexLength1;
|
||||
private static int hVertexFlip;
|
||||
private static int hMatrix;
|
||||
private static int hTexColor;
|
||||
private static int hBgColor;
|
||||
//private static int hScale;
|
||||
private static int hWidth;
|
||||
private static int hPatternScale;
|
||||
private static int hPatternWidth;
|
||||
|
||||
private static int mVertexFlipID;
|
||||
|
||||
public static void init() {
|
||||
|
||||
shader = GLUtils.createProgram(vertexShader, fragmentShader);
|
||||
if (shader == 0) {
|
||||
log.error("Could not create program.");
|
||||
return;
|
||||
}
|
||||
|
||||
hMatrix = GL.glGetUniformLocation(shader, "u_mvp");
|
||||
hTexColor = GL.glGetUniformLocation(shader, "u_color");
|
||||
hBgColor = GL.glGetUniformLocation(shader, "u_bgcolor");
|
||||
//hScale = GL.glGetUniformLocation(shader, "u_scale");
|
||||
hWidth = GL.glGetUniformLocation(shader, "u_width");
|
||||
hPatternScale = GL.glGetUniformLocation(shader, "u_pscale");
|
||||
hPatternWidth = GL.glGetUniformLocation(shader, "u_pwidth");
|
||||
|
||||
hVertexPosition0 = GL.glGetAttribLocation(shader, "a_pos0");
|
||||
hVertexPosition1 = GL.glGetAttribLocation(shader, "a_pos1");
|
||||
hVertexLength0 = GL.glGetAttribLocation(shader, "a_len0");
|
||||
hVertexLength1 = GL.glGetAttribLocation(shader, "a_len1");
|
||||
hVertexFlip = GL.glGetAttribLocation(shader, "a_flip");
|
||||
shader = new Shader("linetex_layer");
|
||||
|
||||
int[] vboIds = GLUtils.glGenBuffers(1);
|
||||
mVertexFlipID = vboIds[0];
|
||||
@ -327,28 +324,35 @@ public final class LineTexLayer extends RenderElement {
|
||||
public static RenderElement draw(RenderElement curLayer, GLViewport v,
|
||||
float div, ElementLayers layers) {
|
||||
|
||||
if (shader == 0)
|
||||
return curLayer.next;
|
||||
//if (shader == 0)
|
||||
// return curLayer.next;
|
||||
|
||||
GLState.blend(true);
|
||||
GLState.useProgram(shader);
|
||||
//GLState.useProgram(shader);
|
||||
shader.useProgram();
|
||||
|
||||
GLState.enableVertexArrays(-1, -1);
|
||||
|
||||
GL.glEnableVertexAttribArray(hVertexPosition0);
|
||||
GL.glEnableVertexAttribArray(hVertexPosition1);
|
||||
GL.glEnableVertexAttribArray(hVertexLength0);
|
||||
GL.glEnableVertexAttribArray(hVertexLength1);
|
||||
GL.glEnableVertexAttribArray(hVertexFlip);
|
||||
int aLen0 = shader.aLen0;
|
||||
int aLen1 = shader.aLen1;
|
||||
int aPos0 = shader.aPos0;
|
||||
int aPos1 = shader.aPos1;
|
||||
int aFlip = shader.aFlip;
|
||||
|
||||
v.mvp.setAsUniform(hMatrix);
|
||||
GL.glEnableVertexAttribArray(aPos0);
|
||||
GL.glEnableVertexAttribArray(aPos1);
|
||||
GL.glEnableVertexAttribArray(aLen0);
|
||||
GL.glEnableVertexAttribArray(aLen1);
|
||||
GL.glEnableVertexAttribArray(aFlip);
|
||||
|
||||
v.mvp.setAsUniform(shader.uMVP);
|
||||
|
||||
int maxIndices = MapRenderer.maxQuads * 6;
|
||||
|
||||
MapRenderer.bindQuadIndicesVBO(true);
|
||||
|
||||
GL.glBindBuffer(GL20.GL_ARRAY_BUFFER, mVertexFlipID);
|
||||
GL.glVertexAttribPointer(hVertexFlip, 1,
|
||||
GL.glVertexAttribPointer(shader.aFlip, 1,
|
||||
GL20.GL_BYTE, false, 0, 0);
|
||||
|
||||
layers.vbo.bind();
|
||||
@ -364,19 +368,21 @@ public final class LineTexLayer extends RenderElement {
|
||||
LineTexLayer ll = (LineTexLayer) l;
|
||||
LineStyle line = ll.line.current();
|
||||
|
||||
GLUtils.setColor(hTexColor, line.stippleColor, 1);
|
||||
GLUtils.setColor(hBgColor, line.color, 1);
|
||||
GLUtils.setColor(shader.uColor, line.stippleColor, 1);
|
||||
GLUtils.setColor(shader.uBgColor, line.color, 1);
|
||||
|
||||
float pScale = (int) (s + 0.5f);
|
||||
if (pScale < 1)
|
||||
pScale = 1;
|
||||
|
||||
GL.glUniform1f(hPatternScale, (MapRenderer.COORD_SCALE * line.stipple) / pScale);
|
||||
GL.glUniform1f(hPatternWidth, line.stippleWidth);
|
||||
GL.glUniform1f(shader.uPatternScale,
|
||||
(MapRenderer.COORD_SCALE * line.stipple) / pScale);
|
||||
|
||||
GL.glUniform1f(shader.uPatternWidth, line.stippleWidth);
|
||||
//GL.glUniform1f(hScale, scale);
|
||||
|
||||
/* keep line width fixed */
|
||||
GL.glUniform1f(hWidth, ll.width / s * COORD_SCALE_BY_DIR_SCALE);
|
||||
GL.glUniform1f(shader.uWidth, ll.width / s * COORD_SCALE_BY_DIR_SCALE);
|
||||
|
||||
/* add offset vertex */
|
||||
int vOffset = -STRIDE;
|
||||
@ -392,20 +398,16 @@ public final class LineTexLayer extends RenderElement {
|
||||
/* i / 6 * (24 shorts per block * 2 short bytes) */
|
||||
int add = (l.offset + i * 8) + vOffset;
|
||||
|
||||
GL.glVertexAttribPointer(hVertexPosition0,
|
||||
4, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aPos0, 4, GL20.GL_SHORT, false, STRIDE,
|
||||
add + STRIDE);
|
||||
|
||||
GL.glVertexAttribPointer(hVertexLength0,
|
||||
2, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aLen0, 2, GL20.GL_SHORT, false, STRIDE,
|
||||
add + STRIDE + LEN_OFFSET);
|
||||
|
||||
GL.glVertexAttribPointer(hVertexPosition1,
|
||||
4, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aPos1, 4, GL20.GL_SHORT, false, STRIDE,
|
||||
add);
|
||||
|
||||
GL.glVertexAttribPointer(hVertexLength1,
|
||||
2, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aLen1, 2, GL20.GL_SHORT, false, STRIDE,
|
||||
add + LEN_OFFSET);
|
||||
|
||||
GL.glDrawElements(GL20.GL_TRIANGLES, numIndices,
|
||||
@ -421,20 +423,16 @@ public final class LineTexLayer extends RenderElement {
|
||||
/* i / 6 * (24 shorts per block * 2 short bytes) */
|
||||
int add = (l.offset + i * 8) + vOffset;
|
||||
|
||||
GL.glVertexAttribPointer(hVertexPosition0,
|
||||
4, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aPos0, 4, GL20.GL_SHORT, false, STRIDE,
|
||||
add + 2 * STRIDE);
|
||||
|
||||
GL.glVertexAttribPointer(hVertexLength0,
|
||||
2, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aLen0, 2, GL20.GL_SHORT, false, STRIDE,
|
||||
add + 2 * STRIDE + LEN_OFFSET);
|
||||
|
||||
GL.glVertexAttribPointer(hVertexPosition1,
|
||||
4, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aPos1, 4, GL20.GL_SHORT, false, STRIDE,
|
||||
add + STRIDE);
|
||||
|
||||
GL.glVertexAttribPointer(hVertexLength1,
|
||||
2, GL20.GL_SHORT, false, STRIDE,
|
||||
GL.glVertexAttribPointer(aLen1, 2, GL20.GL_SHORT, false, STRIDE,
|
||||
add + STRIDE + LEN_OFFSET);
|
||||
|
||||
GL.glDrawElements(GL20.GL_TRIANGLES, numIndices,
|
||||
@ -445,107 +443,15 @@ public final class LineTexLayer extends RenderElement {
|
||||
|
||||
MapRenderer.bindQuadIndicesVBO(false);
|
||||
|
||||
GL.glDisableVertexAttribArray(hVertexPosition0);
|
||||
GL.glDisableVertexAttribArray(hVertexPosition1);
|
||||
GL.glDisableVertexAttribArray(hVertexLength0);
|
||||
GL.glDisableVertexAttribArray(hVertexLength1);
|
||||
GL.glDisableVertexAttribArray(hVertexFlip);
|
||||
GL.glDisableVertexAttribArray(aPos0);
|
||||
GL.glDisableVertexAttribArray(aPos1);
|
||||
GL.glDisableVertexAttribArray(aLen0);
|
||||
GL.glDisableVertexAttribArray(aLen1);
|
||||
GL.glDisableVertexAttribArray(aFlip);
|
||||
|
||||
//GL.glBindTexture(GL20.GL_TEXTURE_2D, 0);
|
||||
|
||||
return l;
|
||||
}
|
||||
|
||||
final static String vertexShader = ""
|
||||
+ "precision mediump float;"
|
||||
+ "uniform mat4 u_mvp;"
|
||||
+ "uniform vec4 u_color;"
|
||||
+ "uniform float u_pscale;"
|
||||
+ "uniform float u_width;"
|
||||
+ "attribute vec4 a_pos0;"
|
||||
+ "attribute vec4 a_pos1;"
|
||||
+ "attribute vec2 a_len0;"
|
||||
+ "attribute vec2 a_len1;"
|
||||
+ "attribute float a_flip;"
|
||||
+ "varying vec2 v_st;"
|
||||
+ "void main() {"
|
||||
+ " vec4 pos;"
|
||||
+ " if (a_flip == 0.0){"
|
||||
//+ " vec2 dir = u_width * a_pos0.zw;"
|
||||
+ " pos = vec4(a_pos0.xy + (u_width * a_pos0.zw), 0.0, 1.0);"
|
||||
+ " v_st = vec2(a_len0.x / u_pscale, 1.0);"
|
||||
+ " } else {"
|
||||
//+ " vec2 dir = u_width * a_pos1.zw;"
|
||||
+ " pos = vec4(a_pos1.xy - (u_width * a_pos1.zw), 0.0, 1.0);"
|
||||
+ " v_st = vec2(a_len1.x / u_pscale, -1.0);"
|
||||
+ " }"
|
||||
+ " gl_Position = u_mvp * pos;"
|
||||
+ "}";
|
||||
|
||||
//*
|
||||
final static String fragmentShader = ""
|
||||
+ "#extension GL_OES_standard_derivatives : enable\n"
|
||||
+ " precision mediump float;"
|
||||
+ " uniform vec4 u_color;"
|
||||
+ " uniform vec4 u_bgcolor;"
|
||||
+ " uniform float u_pwidth;"
|
||||
+ " varying vec2 v_st;"
|
||||
+ " void main() {"
|
||||
/* distance on perpendicular to the line */
|
||||
+ " float dist = abs(v_st.t);"
|
||||
+ " float fuzz = fwidth(v_st.t);"
|
||||
+ " float fuzz_p = fwidth(v_st.s);"
|
||||
+ " float line_w = smoothstep(0.0, fuzz, 1.0 - dist);"
|
||||
+ " float stipple_w = smoothstep(0.0, fuzz, u_pwidth - dist);"
|
||||
/* triangle waveform in the range 0..1 for regular pattern */
|
||||
+ " float phase = abs(mod(v_st.s, 2.0) - 1.0);"
|
||||
/* interpolate between on/off phase, 0.5 = equal phase length */
|
||||
+ " float stipple_p = smoothstep(0.5 - fuzz_p, 0.5 + fuzz_p, phase);"
|
||||
+ " gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));"
|
||||
+ " } "; //*/
|
||||
|
||||
/* final static String fragmentShader = ""
|
||||
* + "#extension GL_OES_standard_derivatives : enable\n"
|
||||
* + " precision mediump float;"
|
||||
* + " uniform sampler2D tex;"
|
||||
* + " uniform float u_scale;"
|
||||
* + " uniform vec4 u_color;"
|
||||
* + " uniform vec4 u_bgcolor;"
|
||||
* + " varying vec2 v_st;"
|
||||
* + " void main() {"
|
||||
* + " float len = texture2D(tex, v_st).a;"
|
||||
* + " float tex_w = abs(v_st.t);"
|
||||
* + " vec2 st_width = fwidth(v_st);"
|
||||
* + " float fuzz = max(st_width.s, st_width.t);"
|
||||
* //+ " float fuzz = fwidth(v_st.t);"
|
||||
* //+ " float line_w = 1.0 - smoothstep(1.0 - fuzz, 1.0, tex_w);"
|
||||
* //+ " float stipple_w = 1.0 - smoothstep(0.7 - fuzz, 0.7, tex_w);"
|
||||
* +
|
||||
* " float stipple_p = 1.0 - smoothstep(1.0 - fuzz, 1.0, length(vec2(len*u_scale, v_st.t)));"
|
||||
* + " gl_FragColor = u_bgcolor * stipple_p;"
|
||||
* // +
|
||||
* " gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));"
|
||||
* + "}"; // */
|
||||
/* final static String fragmentShader = ""
|
||||
* + "#extension GL_OES_standard_derivatives : enable\n"
|
||||
* + " precision mediump float;"
|
||||
* + " uniform sampler2D tex;"
|
||||
* + " uniform vec4 u_color;"
|
||||
* + " uniform vec4 u_bgcolor;"
|
||||
* + " uniform float u_pwidth;"
|
||||
* + " varying vec2 v_st;"
|
||||
* + " void main() {"
|
||||
* + " float dist = texture2D(tex, v_st).a;"
|
||||
* + " float tex_w = abs(v_st.t);"
|
||||
* + " vec2 st_width = fwidth(v_st);"
|
||||
* + " float fuzz = max(st_width.s, st_width.t);"
|
||||
* + " float line_w = (1.0 - smoothstep(1.0 - fuzz, 1.0, tex_w));"
|
||||
* +
|
||||
* " float stipple_w = (1.0 - smoothstep(u_pwidth - fuzz, u_pwidth, tex_w));"
|
||||
* + " float stipple_p = smoothstep(0.495, 0.505, dist);"
|
||||
* +
|
||||
* " gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));"
|
||||
* + " } "; // */
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user