move shaders to repective renderer classes

This commit is contained in:
Hannes Janetzek 2013-01-11 23:44:30 +01:00
parent 007ab2e3bf
commit c462793b99
4 changed files with 142 additions and 142 deletions

View File

@ -56,15 +56,15 @@ public final class LineRenderer {
private static int[] hLineMode = new int[2]; private static int[] hLineMode = new int[2];
static boolean init() { static boolean init() {
lineProgram[0] = GlUtils.createProgram(Shaders.lineVertexShader, lineProgram[0] = GlUtils.createProgram(lineVertexShader,
Shaders.lineFragmentShader); lineFragmentShader);
if (lineProgram[0] == 0) { if (lineProgram[0] == 0) {
Log.e(TAG, "Could not create line program."); Log.e(TAG, "Could not create line program.");
return false; return false;
} }
lineProgram[1] = GlUtils.createProgram(Shaders.lineVertexShader, lineProgram[1] = GlUtils.createProgram(lineVertexShader,
Shaders.lineSimpleFragmentShader); lineSimpleFragmentShader);
if (lineProgram[1] == 0) { if (lineProgram[1] == 0) {
Log.e(TAG, "Could not create simple line program."); Log.e(TAG, "Could not create simple line program.");
return false; return false;
@ -212,4 +212,65 @@ public final class LineRenderer {
return l; return l;
} }
private final static String lineVertexShader = ""
+ "precision mediump float;"
+ "uniform mat4 u_mvp;"
+ "uniform float u_width;"
+ "attribute vec2 a_position;"
+ "attribute vec2 a_st;"
+ "varying vec2 v_st;"
+ "const float dscale = 8.0/2048.0;"
+ "void main() {"
// scale extrusion to u_width pixel
// just ignore the two most insignificant bits of a_st :)
+ " vec2 dir = dscale * u_width * a_st;"
+ " gl_Position = u_mvp * vec4(a_position + dir, 0.0,1.0);"
// last two bits of a_st hold the texture coordinates
+ " v_st = u_width * (abs(mod(a_st,4.0)) - 1.0);"
// use bit operations when available (gles 1.3)
// + " v_st = u_width * vec2(ivec2(a_st) & 3 - 1);"
+ "}";
private final static String lineSimpleFragmentShader = ""
+ "precision mediump float;"
+ "uniform float u_wscale;"
+ "uniform float u_width;"
+ "uniform int u_mode;"
+ "uniform vec4 u_color;"
+ "varying vec2 v_st;"
+ "void main() {"
+ " float len;"
+ " if (u_mode == 0)"
+ " len = u_width - abs(v_st.s);"
+ " else "
+ " len = u_width - length(v_st);"
// fade to alpha. u_wscale is the width in pixel which should be
// faded, u_width - len the position of this fragment on the
// perpendicular to this line segment, only works with no
// perspective
+ " gl_FragColor = u_color * min(1.0, len / u_wscale);"
+ "}";
private final static String lineFragmentShader = ""
+ "#extension GL_OES_standard_derivatives : enable\n"
+ "precision mediump float;"
+ "uniform float u_wscale;"
+ "uniform float u_width;"
+ "uniform int u_mode;"
+ "uniform vec4 u_color;"
+ "varying vec2 v_st;"
+ "void main() {"
+ " float len;"
+ " float fuzz;"
+ " if (u_mode == 0){"
+ " len = u_width - abs(v_st.s);"
+ " fuzz = u_wscale + fwidth(v_st.s);"
+ " } else {"
+ " len = u_width - length(v_st);"
+ " vec2 st_width = fwidth(v_st);"
+ " fuzz = u_wscale + max(st_width.s, st_width.t);"
+ " }"
+ " gl_FragColor = u_color * min(1.0, len / fuzz);"
+ "}";
} }

View File

@ -69,10 +69,10 @@ public final class PolygonRenderer {
static boolean init() { static boolean init() {
// Set up the program for rendering polygons // Set up the program for rendering polygons
// polygonProgram = GlUtils.createProgram(Shaders.polygonVertexShaderZ, // polygonProgram = GlUtils.createProgram(polygonVertexShaderZ,
// Shaders.polygonFragmentShaderZ); // polygonFragmentShaderZ);
polygonProgram = GlUtils.createProgram(Shaders.polygonVertexShader, polygonProgram = GlUtils.createProgram(polygonVertexShader,
Shaders.polygonFragmentShader); polygonFragmentShader);
if (polygonProgram == 0) { if (polygonProgram == 0) {
// Log.e(TAG, "Could not create polygon program."); // Log.e(TAG, "Could not create polygon program.");
@ -337,4 +337,43 @@ public final class PolygonRenderer {
GlUtils.checkGlError("draw debug"); GlUtils.checkGlError("draw debug");
} }
private final static String polygonVertexShader = ""
+ "precision highp float;"
+ "uniform mat4 u_mvp;"
+ "attribute vec4 a_position;"
+ "void main() {"
+ " gl_Position = u_mvp * a_position;"
+ "}";
private final static String polygonFragmentShader = ""
+ "precision highp float;"
+ "uniform vec4 u_color;"
+ "void main() {"
+ " gl_FragColor = u_color;"
+ "}";
private final static String polygonVertexShaderZ = ""
+ "precision highp float;"
+ "uniform mat4 u_mvp;"
+ "attribute vec4 a_position;"
+ "varying float z;"
+ "void main() {"
+ " gl_Position = u_mvp * a_position;"
+ " z = gl_Position.z;"
+ "}";
private final static String polygonFragmentShaderZ = ""
+ "precision highp float;"
+ "uniform vec4 u_color;"
+ "varying float z;"
+ "void main() {"
+ "if (z < -1.0)"
+ " gl_FragColor = vec4(0.0, z + 2.0, 0.0, 1.0)*0.8;"
+ "else if (z < 0.0)"
+ " gl_FragColor = vec4(z * -1.0, 0.0, 0.0, 1.0)*0.8;"
+ "else if (z < 1.0)"
+ " gl_FragColor = vec4(0.0, 0.0, z, 1.0)*0.8;"
+ "else"
+ " gl_FragColor = vec4(0.0, z - 1.0, 0.0, 1.0)*0.8;"
+ "}";
} }

View File

@ -17,138 +17,6 @@ package org.oscim.renderer;
public final class Shaders { public final class Shaders {
final static String lineVertexShader = ""
+ "precision mediump float;"
+ "uniform mat4 u_mvp;"
+ "uniform float u_width;"
+ "attribute vec2 a_position;"
+ "attribute vec2 a_st;"
+ "varying vec2 v_st;"
+ "const float dscale = 8.0/2048.0;"
+ "void main() {"
// scale extrusion to u_width pixel
// just ignore the two most insignificant bits of a_st :)
+ " vec2 dir = dscale * u_width * a_st;"
+ " gl_Position = u_mvp * vec4(a_position + dir, 0.0,1.0);"
// last two bits of a_st hold the texture coordinates
+ " v_st = u_width * (abs(mod(a_st,4.0)) - 1.0);"
// use bit operations when available (gles 1.3)
// + " v_st = u_width * vec2(ivec2(a_st) & 3 - 1);"
+ "}";
final static String lineSimpleFragmentShader = ""
+ "precision mediump float;"
+ "uniform float u_wscale;"
+ "uniform float u_width;"
+ "uniform int u_mode;"
+ "uniform vec4 u_color;"
+ "varying vec2 v_st;"
+ "void main() {"
+ " float len;"
+ " if (u_mode == 0)"
+ " len = u_width - abs(v_st.s);"
+ " else "
+ " len = u_width - length(v_st);"
// fade to alpha. u_wscale is the width in pixel which should be
// faded, u_width - len the position of this fragment on the
// perpendicular to this line segment, only works with no
// perspective
+ " gl_FragColor = u_color * min(1.0, len / u_wscale);"
+ "}";
final static String lineFragmentShader = ""
+ "#extension GL_OES_standard_derivatives : enable\n"
+ "precision mediump float;"
+ "uniform float u_wscale;"
+ "uniform float u_width;"
+ "uniform int u_mode;"
+ "uniform vec4 u_color;"
+ "varying vec2 v_st;"
+ "void main() {"
+ " float len;"
+ " float fuzz;"
+ " if (u_mode == 0){"
+ " len = u_width - abs(v_st.s);"
+ " fuzz = u_wscale + fwidth(v_st.s);"
+ " } else {"
+ " len = u_width - length(v_st);"
+ " vec2 st_width = fwidth(v_st);"
+ " fuzz = u_wscale + max(st_width.s, st_width.t);"
+ " }"
+ " gl_FragColor = u_color * min(1.0, len / fuzz);"
+ "}";
final static String polygonVertexShader = ""
+ "precision highp float;"
+ "uniform mat4 u_mvp;"
+ "attribute vec4 a_position;"
+ "void main() {"
+ " gl_Position = u_mvp * a_position;"
+ "}";
final static String polygonFragmentShader = ""
+ "precision highp float;"
+ "uniform vec4 u_color;"
+ "void main() {"
+ " gl_FragColor = u_color;"
+ "}";
final static String polygonVertexShaderZ = ""
+ "precision highp float;"
+ "uniform mat4 u_mvp;"
+ "attribute vec4 a_position;"
+ "varying float z;"
+ "void main() {"
+ " gl_Position = u_mvp * a_position;"
+ " z = gl_Position.z;"
+ "}";
final static String polygonFragmentShaderZ = ""
+ "precision highp float;"
+ "uniform vec4 u_color;"
+ "varying float z;"
+ "void main() {"
+ "if (z < -1.0)"
+ " gl_FragColor = vec4(0.0, z + 2.0, 0.0, 1.0)*0.8;"
+ "else if (z < 0.0)"
+ " gl_FragColor = vec4(z * -1.0, 0.0, 0.0, 1.0)*0.8;"
+ "else if (z < 1.0)"
+ " gl_FragColor = vec4(0.0, 0.0, z, 1.0)*0.8;"
+ "else"
+ " gl_FragColor = vec4(0.0, z - 1.0, 0.0, 1.0)*0.8;"
+ "}";
final static String textVertexShader = ""
+ "precision highp float; "
+ "attribute vec4 vertex;"
+ "attribute vec2 tex_coord;"
+ "uniform mat4 u_mv;"
+ "uniform mat4 u_proj;"
+ "uniform float u_scale;"
+ "uniform float u_swidth;"
+ "varying vec2 tex_c;"
+ "const vec2 div = vec2(1.0/2048.0,1.0/2048.0);"
+ "const float coord_scale = 0.125;"
+ "void main() {"
+ " vec4 pos;"
+ " if (mod(vertex.x, 2.0) == 0.0){"
+ " pos = u_proj * (u_mv * vec4(vertex.xy + vertex.zw * u_scale, 0.02, 1.0));"
+ " } else {"
// // place as billboard
+ " vec4 dir = u_mv * vec4(vertex.xy, 0.0, 1.0);"
+ " pos = u_proj * (dir + vec4(vertex.zw * (coord_scale * u_swidth), 0.02, 0.0));"
+ " }"
+ " gl_Position = pos;"
+ " tex_c = tex_coord * div;"
+ "}";
final static String textFragmentShader = ""
+ "precision highp float;"
+ "uniform sampler2D tex;"
+ "varying vec2 tex_c;"
+ "void main() {"
+ " gl_FragColor = texture2D(tex, tex_c.xy);"
+ "}";
// final static String lineVertexZigZagShader = "" // final static String lineVertexZigZagShader = ""
// + "precision mediump float;" // + "precision mediump float;"
// + "uniform mat4 mvp;" // + "uniform mat4 mvp;"

View File

@ -44,8 +44,8 @@ public final class TextureRenderer {
private final static int MAX_ITEMS = 50; private final static int MAX_ITEMS = 50;
static void init() { static void init() {
mTextureProgram = GlUtils.createProgram(Shaders.textVertexShader, mTextureProgram = GlUtils.createProgram(textVertexShader,
Shaders.textFragmentShader); textFragmentShader);
hTextureMVMatrix = GLES20.glGetUniformLocation(mTextureProgram, "u_mv"); hTextureMVMatrix = GLES20.glGetUniformLocation(mTextureProgram, "u_mv");
hTextureProjMatrix = GLES20.glGetUniformLocation(mTextureProgram, "u_proj"); hTextureProjMatrix = GLES20.glGetUniformLocation(mTextureProgram, "u_proj");
@ -144,4 +144,36 @@ public final class TextureRenderer {
return layer.next; return layer.next;
} }
private final static String textVertexShader = ""
+ "precision highp float; "
+ "attribute vec4 vertex;"
+ "attribute vec2 tex_coord;"
+ "uniform mat4 u_mv;"
+ "uniform mat4 u_proj;"
+ "uniform float u_scale;"
+ "uniform float u_swidth;"
+ "varying vec2 tex_c;"
+ "const vec2 div = vec2(1.0/2048.0,1.0/2048.0);"
+ "const float coord_scale = 0.125;"
+ "void main() {"
+ " vec4 pos;"
+ " if (mod(vertex.x, 2.0) == 0.0){"
+ " pos = u_proj * (u_mv * vec4(vertex.xy + vertex.zw * u_scale, 0.02, 1.0));"
+ " } else {"
// // place as billboard
+ " vec4 dir = u_mv * vec4(vertex.xy, 0.0, 1.0);"
+ " pos = u_proj * (dir + vec4(vertex.zw * (coord_scale * u_swidth), 0.02, 0.0));"
+ " }"
+ " gl_Position = pos;"
+ " tex_c = tex_coord * div;"
+ "}";
private final static String textFragmentShader = ""
+ "precision highp float;"
+ "uniform sampler2D tex;"
+ "varying vec2 tex_c;"
+ "void main() {"
+ " gl_FragColor = texture2D(tex, tex_c.xy);"
+ "}";
} }