use procedural texture for regular stipple

This commit is contained in:
Hannes Janetzek 2013-02-24 10:20:20 +01:00
parent c8ab69ed2d
commit aaafe0cddc
3 changed files with 70 additions and 50 deletions

View File

@ -136,7 +136,7 @@ public class BaseMap {
int simpleShader = (pos.tilt < 1 ? 1 : 0); int simpleShader = (pos.tilt < 1 ? 1 : 0);
boolean clipped = false; boolean clipped = false;
boolean lineTexture = true; //boolean lineTexture = true;
for (Layer l = t.layers.baseLayers; l != null;) { for (Layer l = t.layers.baseLayers; l != null;) {
switch (l.type) { switch (l.type) {
@ -147,7 +147,7 @@ public class BaseMap {
case Layer.LINE: case Layer.LINE:
//if (!lineTexture) { //if (!lineTexture) {
LineRenderer.beginLines(); //LineRenderer.beginLines();
// lineTexture = true; // lineTexture = true;
//} //}
if (!clipped) { if (!clipped) {
@ -160,7 +160,7 @@ public class BaseMap {
break; break;
case Layer.TEXLINE: case Layer.TEXLINE:
LineRenderer.endLines(); //LineRenderer.endLines();
if (!clipped) { if (!clipped) {
// draw stencil buffer clip region // draw stencil buffer clip region
@ -168,7 +168,7 @@ public class BaseMap {
clipped = true; clipped = true;
} }
l = LineTexRenderer.draw(t.layers, l, pos, mvp, div); l = LineTexRenderer.draw(t.layers, l, pos, mvp, div);
lineTexture = false; //lineTexture = false;
break; break;
default: default:

View File

@ -46,14 +46,15 @@ public class LineTexRenderer {
private static int hTexColor; private static int hTexColor;
private static int hBgColor; private static int hBgColor;
private static int hScale; private static int hScale;
private static int hPatternScale;
private static int hWidth; private static int hWidth;
private static int hPatternScale;
private static int hPatternWidth;
private static int mIndicesBufferID; private static int mIndicesBufferID;
private static int mVertexFlipID; private static int mVertexFlipID;
// batch up up to 100 quads in one draw call // batch up up to 64 quads in one draw call
private static int maxQuads = 100; private static int maxQuads = 64;
private static int maxIndices = maxQuads * 6; private static int maxIndices = maxQuads * 6;
private static int[] mTexID; private static int[] mTexID;
@ -68,8 +69,9 @@ public class LineTexRenderer {
hTexColor = GLES20.glGetUniformLocation(shader, "u_color"); hTexColor = GLES20.glGetUniformLocation(shader, "u_color");
hBgColor = GLES20.glGetUniformLocation(shader, "u_bgcolor"); hBgColor = GLES20.glGetUniformLocation(shader, "u_bgcolor");
hScale = GLES20.glGetUniformLocation(shader, "u_scale"); hScale = GLES20.glGetUniformLocation(shader, "u_scale");
hPatternScale = GLES20.glGetUniformLocation(shader, "u_pscale");
hWidth = GLES20.glGetUniformLocation(shader, "u_width"); hWidth = GLES20.glGetUniformLocation(shader, "u_width");
hPatternScale = GLES20.glGetUniformLocation(shader, "u_pscale");
hPatternWidth = GLES20.glGetUniformLocation(shader, "u_pwidth");
hVertexPosition0 = GLES20.glGetAttribLocation(shader, "a_pos0"); hVertexPosition0 = GLES20.glGetAttribLocation(shader, "a_pos0");
hVertexPosition1 = GLES20.glGetAttribLocation(shader, "a_pos1"); hVertexPosition1 = GLES20.glGetAttribLocation(shader, "a_pos1");
@ -118,13 +120,11 @@ public class LineTexRenderer {
GLES20.GL_STATIC_DRAW); GLES20.GL_STATIC_DRAW);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0); GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
mTexID = new int[10]; // mTexID = new int[10];
// byte[] stipple = new byte[2];
byte[] stipple = new byte[2]; // stipple[0] = 32;
stipple[0] = 32; // stipple[1] = 32;
stipple[1] = 32; // mTexID[0] = GlUtils.loadStippleTexture(stipple);
mTexID[0] = GlUtils.loadStippleTexture(stipple);
} }
private final static int STRIDE = 12; private final static int STRIDE = 12;
@ -159,7 +159,7 @@ public class LineTexRenderer {
float s = pos.scale / div; float s = pos.scale / div;
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexID[0]); //GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTexID[0]);
Layer l = curLayer; Layer l = curLayer;
while (l != null && l.type == Layer.TEXLINE) { while (l != null && l.type == Layer.TEXLINE) {
@ -176,10 +176,12 @@ public class LineTexRenderer {
//GLES20.glUniform4f(hBgColor, 0x99 / 255f, 0x96 / 255f, 0x93 / 255f, 0.95f); //GLES20.glUniform4f(hBgColor, 0x99 / 255f, 0x96 / 255f, 0x93 / 255f, 0.95f);
// scale pattern to twice its size, then reset scale to 1. // scale pattern to twice its size, then reset scale to 1.
// (coord scale * pattern size) / scale // (coord scale * pattern size / tex size) / scale
GLES20.glUniform1f(hPatternScale, (8 * (64 / line.stipple)) / Math.max((int) s, 1)); //GLES20.glUniform1f(hPatternScale, (8 * line.stipple / 64) / Math.max((int) s, 1));
GLES20.glUniform1f(hPatternScale, (8 * line.stipple) / Math.max((int) s, 1));
GLES20.glUniform1f(hPatternWidth, line.stippleWidth);
//float f = Math.max((int)(s), 1); //float f = Math.max((int)(s), 1);
//Log.d(TAG, s + " : " + f);
//GLES20.glUniform1f(hPatternScale, (8 * line.stipple) / 1); //GLES20.glUniform1f(hPatternScale, (8 * line.stipple) / 1);
GLES20.glUniform1f(hScale, pos.scale); GLES20.glUniform1f(hScale, pos.scale);
@ -196,7 +198,7 @@ public class LineTexRenderer {
numIndices = maxIndices; numIndices = maxIndices;
// i / 6 * (24 shorts per block * 2 short bytes) // i / 6 * (24 shorts per block * 2 short bytes)
int add = (l.offset + i * 8) + vOffset;; int add = (l.offset + i * 8) + vOffset;
GLES20.glVertexAttribPointer(hVertexPosition0, GLES20.glVertexAttribPointer(hVertexPosition0,
4, GLES20.GL_SHORT, false, STRIDE, 4, GLES20.GL_SHORT, false, STRIDE,
@ -257,9 +259,9 @@ public class LineTexRenderer {
GLES20.glDisableVertexAttribArray(hVertexLength0); GLES20.glDisableVertexAttribArray(hVertexLength0);
GLES20.glDisableVertexAttribArray(hVertexLength1); GLES20.glDisableVertexAttribArray(hVertexLength1);
GLES20.glDisableVertexAttribArray(hVertexFlip); GLES20.glDisableVertexAttribArray(hVertexFlip);
GlUtils.checkGlError("..."); GlUtils.checkGlError(TAG);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0); //GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
return l; return l;
} }
@ -290,6 +292,26 @@ public class LineTexRenderer {
+ " gl_Position = u_mvp * pos;" + " 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() {"
// 1.0 minus distance on perpendicular to the line
+ " float dist = 1.0 - abs(v_st.t);"
+ " float fuzz = fwidth(v_st.t);"
+ " float fuzz_p = fwidth(v_st.s);"
+ " float line_w = smoothstep(0.0, fuzz, dist);"
+ " float stipple_w = smoothstep(0.0, u_pwidth + fuzz, dist);"
+ " float phase = abs(mod(v_st.s, 2.0) - 1.0);"
+ " 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 = "" final static String fragmentShader = ""
+ "#extension GL_OES_standard_derivatives : enable\n" + "#extension GL_OES_standard_derivatives : enable\n"
@ -311,22 +333,23 @@ public class LineTexRenderer {
+ " gl_FragColor = u_bgcolor * stipple_p;" + " gl_FragColor = u_bgcolor * stipple_p;"
// + " gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));" // + " gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));"
+ "}"; //*/ + "}"; //*/
//* /*
final static String fragmentShader = "" final static String fragmentShader = ""
+ "#extension GL_OES_standard_derivatives : enable\n" + "#extension GL_OES_standard_derivatives : enable\n"
+ " precision mediump float;" + " precision mediump float;"
+ " uniform sampler2D tex;" + " uniform sampler2D tex;"
+ " uniform vec4 u_color;" + " uniform vec4 u_color;"
+ " uniform vec4 u_bgcolor;" + " uniform vec4 u_bgcolor;"
+ " uniform float u_pwidth;"
+ " varying vec2 v_st;" + " varying vec2 v_st;"
+ " void main() {" + " void main() {"
+ " float len = texture2D(tex, v_st).a;" + " float dist = texture2D(tex, v_st).a;"
+ " float tex_w = abs(v_st.t);" + " float tex_w = abs(v_st.t);"
+ " vec2 st_width = fwidth(v_st);" + " vec2 st_width = fwidth(v_st);"
+ " float fuzz = max(st_width.s, st_width.t);" + " float fuzz = max(st_width.s, st_width.t);"
+ " float line_w = (1.0 - smoothstep(1.0 - fuzz, 1.0, tex_w));" + " 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_w = (1.0 - smoothstep(u_pwidth - fuzz, u_pwidth, tex_w));"
+ " float stipple_p = smoothstep(0.495, 0.505, len);" + " float stipple_p = smoothstep(0.495, 0.505, dist);"
+ " gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));" + " gl_FragColor = line_w * mix(u_bgcolor, u_color, min(stipple_w, stipple_p));"
+ " } "; //*/ + " } "; //*/

View File

@ -57,10 +57,10 @@
<!-- track|footway|path|cycleway --> <!-- track|footway|path|cycleway -->
<style-line name="footway" stroke="#aaffffff" width="1.8" cap="butt" fixed="true" <style-line name="footway" stroke="#aaffffff" width="1.8" cap="butt" fixed="true"
stipple="16" stipple-width="0.6" stipple-stroke="#d35c48"/> stipple="4" stipple-width="0.6" stipple-stroke="#d35c48"/>
<style-line name="highway:cycleway" from="footway" width="0.1" stipple-stroke="#4040ee"/> <style-line name="highway:cycleway" from="footway" width="0.1" stipple-stroke="#4040ee"/>
<style-line name="highway:track" from="footway" width="0.1" stipple="10" stipple-stroke="#c3bb88"/> <style-line name="highway:track" from="footway" width="0.1" stipple="4" stipple-stroke="#c3bb88"/>
<style-line name="highway:path" from="footway" width="0.1" stipple="10" stipple-stroke="#837b58"/> <style-line name="highway:path" from="footway" width="0.1" stipple="4" stipple-stroke="#837b58"/>
<!-- <style-line name="footway:z16" from="footway" width="-0.95" fixed="false" fade="-1"/>--> <!-- <style-line name="footway:z16" from="footway" width="-0.95" fixed="false" fade="-1"/>-->
<style-line name="footway:z17" stroke="#faf8f5" width="0.3"/> <style-line name="footway:z17" stroke="#faf8f5" width="0.3"/>
@ -68,7 +68,8 @@
<!-- de: ein Weg der für Reiter vorgeshen ist.. --> <!-- de: ein Weg der für Reiter vorgeshen ist.. -->
<style-line name="bridleway" stroke="#d3cb98" width="0.4" cap="butt"/> <style-line name="bridleway" stroke="#d3cb98" width="0.4" cap="butt"/>
<style-line name="steps" stroke="#f1f0f4" width="0.25" cap="butt" /> <style-line name="steps" stroke="#aaffffff" width="4.2" cap="butt" fixed="true"
stipple="2" stipple-width="1.0" stipple-stroke="#be6253"/>
<style-line name="water:outline" stroke="#a4bbcc" width="1.0" fixed="true" cap="butt" /> <style-line name="water:outline" stroke="#a4bbcc" width="1.0" fixed="true" cap="butt" />
<style-line name="water" stroke="#afcbf3" width="1.0" cap="butt" /> <style-line name="water" stroke="#afcbf3" width="1.0" cap="butt" />
@ -371,10 +372,10 @@
<rule e="way" k="tunnel" v="yes|true" zoom-min="11"> <rule e="way" k="tunnel" v="yes|true" zoom-min="11">
<!-- highway tunnels --> <!-- highway tunnels -->
<rule e="way" k="highway" v="*"> <rule e="way" k="highway" v="*">
<rule e="way" k="*" v="*" zoom-min="15"> <rule e="way" k="*" v="*" zoom-min="16">
<rule e="way" k="*" v="steps"> <rule e="way" k="*" v="steps">
<use-line name="steps"/> <use-line name="steps"/>
<use-outline name="0"/> <!-- <use-outline name="0"/>-->
</rule> </rule>
<!-- <rule e="way" k="*" v="track|footway|path|cycleway" zoom-min="16"> <!-- <rule e="way" k="*" v="track|footway|path|cycleway" zoom-min="16">
<use-line name="footway:z16"/> <use-line name="footway:z16"/>
@ -606,10 +607,10 @@
</rule> </rule>
<rule e="way" k="area" v="~|no|false"> <rule e="way" k="area" v="~|no|false">
<rule e="way" k="*" v="*" zoom-min="15"> <rule e="way" k="*" v="*" zoom-min="16">
<rule e="way" k="*" v="steps"> <rule e="way" k="*" v="steps">
<use-line name="steps"/> <use-line name="steps"/>
<use-outline name="2"/> <!-- <use-outline name="2"/>-->
</rule> </rule>
<!-- <rule e="way" k="*" v="track|footway|path|cycleway" zoom-min="16" zoom-max="16"> <!-- <rule e="way" k="*" v="track|footway|path|cycleway" zoom-min="16" zoom-max="16">
<use-line name="footway:z16"/> <use-line name="footway:z16"/>
@ -717,20 +718,16 @@
<style-outline name="bridge" stroke="#aa202020" width="0.08"/> <style-outline name="bridge" stroke="#aa202020" width="0.08"/>
<rule e="way" k="*" v="*" zoom-min="15"> <rule e="way" k="*" v="*" zoom-min="16">
<rule e="way" k="*" v="steps"> <rule e="way" k="*" v="steps">
<use-line name="steps"/> <use-line name="steps"/>
<use-outline name="bridge"/> <!-- <use-outline name="bridge"/>-->
</rule>
</rule> </rule>
<!-- <rule e="way" k="*" v="track|footway|path|cycleway" zoom-min="16" zoom-max="16">
<use-line name="footway:z16"/>
</rule>-->
<rule e="way" k="*" v="track|footway|path|cycleway" zoom-min="15"> <rule e="way" k="*" v="track|footway|path|cycleway" zoom-min="15">
<use-line name="footway:z17" cap="butt"/> <use-line name="footway:z17" cap="butt"/>
<use-outline name="bridge"/> <use-outline name="bridge"/>
</rule> </rule>
</rule>
<rule e="way" k="*" v="*" zoom-min="14"> <rule e="way" k="*" v="*" zoom-min="14">
<rule e="way" k="*" v="footway" > <rule e="way" k="*" v="footway" >
@ -920,7 +917,7 @@
<rule e="way" k="railway" v="rail|turntable" zoom-min="15"> <rule e="way" k="railway" v="rail|turntable" zoom-min="15">
<line stroke="#ddaa9988" width="2.0" cap="butt" fixed="true" fade="12" <line stroke="#ddaa9988" width="2.0" cap="butt" fixed="true" fade="12"
stipple="4" stipple-width="0.7" stipple-stroke="#ffffff"/> stipple="10" stipple-width="0.6" stipple-stroke="#ffffff"/>
</rule> </rule>
<!-- <rule e="way" k="railway" v="rail" zoom-max="14" zoom-min="13"> <!-- <rule e="way" k="railway" v="rail" zoom-max="14" zoom-min="13">