From fff68b7390da3a1b70da6f972a4668bc5281d2f6 Mon Sep 17 00:00:00 2001 From: codeanticode Date: Wed, 12 Dec 2012 20:30:53 +0000 Subject: [PATCH] updated shader examples for android --- .../Topics/Shaders/BlurFilter/data/blur.glsl | 58 ++++++++----------- .../Topics/Shaders/EdgeDetect/data/edges.glsl | 58 ++++++++----------- .../Topics/Shaders/EdgeFilter/data/edges.glsl | 58 ++++++++----------- .../Topics/Shaders/SepBlur/SepBlur.pde | 47 --------------- .../Topics/Shaders/SepBlur/data/blur.glsl | 58 ------------------- 5 files changed, 73 insertions(+), 206 deletions(-) delete mode 100644 android/examples/Topics/Shaders/SepBlur/SepBlur.pde delete mode 100644 android/examples/Topics/Shaders/SepBlur/data/blur.glsl diff --git a/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl b/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl index b11a04d64..2d55794e5 100644 --- a/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl +++ b/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl @@ -9,40 +9,32 @@ uniform vec2 texcoordOffset; varying vec4 vertColor; varying vec4 vertTexcoord; -#define KERNEL_SIZE 9 - -// Gaussian kernel -// 1 2 1 -// 2 4 2 -// 1 2 1 -float kernel[KERNEL_SIZE]; - -vec2 offset[KERNEL_SIZE]; - void main(void) { - int i = 0; - vec4 sum = vec4(0.0); - - offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t); - offset[1] = vec2(0.0, -texcoordOffset.t); - offset[2] = vec2(texcoordOffset.s, -texcoordOffset.t); - - offset[3] = vec2(-texcoordOffset.s, 0.0); - offset[4] = vec2(0.0, 0.0); - offset[5] = vec2(texcoordOffset.s, 0.0); - - offset[6] = vec2(-texcoordOffset.s, texcoordOffset.t); - offset[7] = vec2(0.0, texcoordOffset.t); - offset[8] = vec2(texcoordOffset.s, texcoordOffset.t); - - kernel[0] = 1.0/16.0; kernel[1] = 2.0/16.0; kernel[2] = 1.0/16.0; - kernel[3] = 2.0/16.0; kernel[4] = 4.0/16.0; kernel[5] = 2.0/16.0; - kernel[6] = 1.0/16.0; kernel[7] = 2.0/16.0; kernel[8] = 1.0/16.0; - - for(i = 0; i < KERNEL_SIZE; i++) { - vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]); - sum += tmp * kernel[i]; - } + // Grouping texcoord variables in order to make it work in the GMA 950. See post #13 + // in this thread: + // http://www.idevgames.com/forums/thread-3467.html + vec2 tc0 = vertTexcoord.st + vec2(-texcoordOffset.s, -texcoordOffset.t); + vec2 tc1 = vertTexcoord.st + vec2( 0.0, -texcoordOffset.t); + vec2 tc2 = vertTexcoord.st + vec2(+texcoordOffset.s, -texcoordOffset.t); + vec2 tc3 = vertTexcoord.st + vec2(-texcoordOffset.s, 0.0); + vec2 tc4 = vertTexcoord.st + vec2( 0.0, 0.0); + vec2 tc5 = vertTexcoord.st + vec2(+texcoordOffset.s, 0.0); + vec2 tc6 = vertTexcoord.st + vec2(-texcoordOffset.s, +texcoordOffset.t); + vec2 tc7 = vertTexcoord.st + vec2( 0.0, +texcoordOffset.t); + vec2 tc8 = vertTexcoord.st + vec2(+texcoordOffset.s, +texcoordOffset.t); + + vec4 col0 = texture2D(textureSampler, tc0); + vec4 col1 = texture2D(textureSampler, tc1); + vec4 col2 = texture2D(textureSampler, tc2); + vec4 col3 = texture2D(textureSampler, tc3); + vec4 col4 = texture2D(textureSampler, tc4); + vec4 col5 = texture2D(textureSampler, tc5); + vec4 col6 = texture2D(textureSampler, tc6); + vec4 col7 = texture2D(textureSampler, tc7); + vec4 col8 = texture2D(textureSampler, tc8); + vec4 sum = (1.0 * col0 + 2.0 * col1 + 1.0 * col2 + + 2.0 * col3 + 4.0 * col4 + 2.0 * col4 + + 1.0 * col5 + 2.0 * col6 + 1.0 * col7) / 16.0; gl_FragColor = vec4(sum.rgb, 1.0) * vertColor; } diff --git a/android/examples/Topics/Shaders/EdgeDetect/data/edges.glsl b/android/examples/Topics/Shaders/EdgeDetect/data/edges.glsl index b032743d0..933cf91d9 100644 --- a/android/examples/Topics/Shaders/EdgeDetect/data/edges.glsl +++ b/android/examples/Topics/Shaders/EdgeDetect/data/edges.glsl @@ -9,40 +9,30 @@ uniform vec2 texcoordOffset; varying vec4 vertColor; varying vec4 vertTexcoord; -#define KERNEL_SIZE 9 - -// Edge detection kernel -// -1 -1 -1 -// -1 +8 -1 -// -1 -1 -1 -float kernel[KERNEL_SIZE]; - -vec2 offset[KERNEL_SIZE]; - void main(void) { - int i = 0; - vec4 sum = vec4(0.0); + // Grouping texcoord variables in order to make it work in the GMA 950. See post #13 + // in this thread: + // http://www.idevgames.com/forums/thread-3467.html + vec2 tc0 = vertTexcoord.st + vec2(-texcoordOffset.s, -texcoordOffset.t); + vec2 tc1 = vertTexcoord.st + vec2( 0.0, -texcoordOffset.t); + vec2 tc2 = vertTexcoord.st + vec2(+texcoordOffset.s, -texcoordOffset.t); + vec2 tc3 = vertTexcoord.st + vec2(-texcoordOffset.s, 0.0); + vec2 tc4 = vertTexcoord.st + vec2( 0.0, 0.0); + vec2 tc5 = vertTexcoord.st + vec2(+texcoordOffset.s, 0.0); + vec2 tc6 = vertTexcoord.st + vec2(-texcoordOffset.s, +texcoordOffset.t); + vec2 tc7 = vertTexcoord.st + vec2( 0.0, +texcoordOffset.t); + vec2 tc8 = vertTexcoord.st + vec2(+texcoordOffset.s, +texcoordOffset.t); + + vec4 col0 = texture2D(textureSampler, tc0); + vec4 col1 = texture2D(textureSampler, tc1); + vec4 col2 = texture2D(textureSampler, tc2); + vec4 col3 = texture2D(textureSampler, tc3); + vec4 col4 = texture2D(textureSampler, tc4); + vec4 col5 = texture2D(textureSampler, tc5); + vec4 col6 = texture2D(textureSampler, tc6); + vec4 col7 = texture2D(textureSampler, tc7); + vec4 col8 = texture2D(textureSampler, tc8); - offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t); - offset[1] = vec2(0.0, -texcoordOffset.t); - offset[2] = vec2(texcoordOffset.s, -texcoordOffset.t); - - offset[3] = vec2(-texcoordOffset.s, 0.0); - offset[4] = vec2(0.0, 0.0); - offset[5] = vec2(texcoordOffset.s, 0.0); - - offset[6] = vec2(-texcoordOffset.s, texcoordOffset.t); - offset[7] = vec2(0.0, texcoordOffset.t); - offset[8] = vec2(texcoordOffset.s, texcoordOffset.t); - - kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0; - kernel[3] = -1.0; kernel[4] = 8.0; kernel[5] = -1.0; - kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0; - - for(i = 0; i < KERNEL_SIZE; i++) { - vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]); - sum += tmp * kernel[i]; - } - - gl_FragColor = vec4(sum.rgb, 1.0) * vertColor; + vec4 sum = 8.0 * col4 - (col0 + col1 + col2 + col3 + col5 + col6 + col7 + col8); + gl_FragColor = vec4(sum.rgb, 1.0) * vertColor; } diff --git a/android/examples/Topics/Shaders/EdgeFilter/data/edges.glsl b/android/examples/Topics/Shaders/EdgeFilter/data/edges.glsl index b032743d0..933cf91d9 100644 --- a/android/examples/Topics/Shaders/EdgeFilter/data/edges.glsl +++ b/android/examples/Topics/Shaders/EdgeFilter/data/edges.glsl @@ -9,40 +9,30 @@ uniform vec2 texcoordOffset; varying vec4 vertColor; varying vec4 vertTexcoord; -#define KERNEL_SIZE 9 - -// Edge detection kernel -// -1 -1 -1 -// -1 +8 -1 -// -1 -1 -1 -float kernel[KERNEL_SIZE]; - -vec2 offset[KERNEL_SIZE]; - void main(void) { - int i = 0; - vec4 sum = vec4(0.0); + // Grouping texcoord variables in order to make it work in the GMA 950. See post #13 + // in this thread: + // http://www.idevgames.com/forums/thread-3467.html + vec2 tc0 = vertTexcoord.st + vec2(-texcoordOffset.s, -texcoordOffset.t); + vec2 tc1 = vertTexcoord.st + vec2( 0.0, -texcoordOffset.t); + vec2 tc2 = vertTexcoord.st + vec2(+texcoordOffset.s, -texcoordOffset.t); + vec2 tc3 = vertTexcoord.st + vec2(-texcoordOffset.s, 0.0); + vec2 tc4 = vertTexcoord.st + vec2( 0.0, 0.0); + vec2 tc5 = vertTexcoord.st + vec2(+texcoordOffset.s, 0.0); + vec2 tc6 = vertTexcoord.st + vec2(-texcoordOffset.s, +texcoordOffset.t); + vec2 tc7 = vertTexcoord.st + vec2( 0.0, +texcoordOffset.t); + vec2 tc8 = vertTexcoord.st + vec2(+texcoordOffset.s, +texcoordOffset.t); + + vec4 col0 = texture2D(textureSampler, tc0); + vec4 col1 = texture2D(textureSampler, tc1); + vec4 col2 = texture2D(textureSampler, tc2); + vec4 col3 = texture2D(textureSampler, tc3); + vec4 col4 = texture2D(textureSampler, tc4); + vec4 col5 = texture2D(textureSampler, tc5); + vec4 col6 = texture2D(textureSampler, tc6); + vec4 col7 = texture2D(textureSampler, tc7); + vec4 col8 = texture2D(textureSampler, tc8); - offset[0] = vec2(-texcoordOffset.s, -texcoordOffset.t); - offset[1] = vec2(0.0, -texcoordOffset.t); - offset[2] = vec2(texcoordOffset.s, -texcoordOffset.t); - - offset[3] = vec2(-texcoordOffset.s, 0.0); - offset[4] = vec2(0.0, 0.0); - offset[5] = vec2(texcoordOffset.s, 0.0); - - offset[6] = vec2(-texcoordOffset.s, texcoordOffset.t); - offset[7] = vec2(0.0, texcoordOffset.t); - offset[8] = vec2(texcoordOffset.s, texcoordOffset.t); - - kernel[0] = -1.0; kernel[1] = -1.0; kernel[2] = -1.0; - kernel[3] = -1.0; kernel[4] = 8.0; kernel[5] = -1.0; - kernel[6] = -1.0; kernel[7] = -1.0; kernel[8] = -1.0; - - for(i = 0; i < KERNEL_SIZE; i++) { - vec4 tmp = texture2D(textureSampler, vertTexcoord.st + offset[i]); - sum += tmp * kernel[i]; - } - - gl_FragColor = vec4(sum.rgb, 1.0) * vertColor; + vec4 sum = 8.0 * col4 - (col0 + col1 + col2 + col3 + col5 + col6 + col7 + col8); + gl_FragColor = vec4(sum.rgb, 1.0) * vertColor; } diff --git a/android/examples/Topics/Shaders/SepBlur/SepBlur.pde b/android/examples/Topics/Shaders/SepBlur/SepBlur.pde deleted file mode 100644 index c08bc40ee..000000000 --- a/android/examples/Topics/Shaders/SepBlur/SepBlur.pde +++ /dev/null @@ -1,47 +0,0 @@ -// Separable-blur shader (works by applying two successive passes -// in each direction of the image) - -PShader blur; -PGraphics src; -PGraphics pass1, pass2; - -void setup() { - size(200, 200, P2D); - orientation(LANDSCAPE); - - blur = loadShader("blur.glsl"); - blur.set("sigma", 5.0f); - - src = createGraphics(width, height, P2D); - - pass1 = createGraphics(width, height, P2D); - pass1.noSmooth(); - - pass2 = createGraphics(width, height, P2D); - pass2.noSmooth(); -} - -void draw() { - src.beginDraw(); - src.background(0); - src.fill(255); - src.ellipse(width/2, height/2, 100, 100); - src.endDraw(); - - // Applying the blur shader along the vertical direction - blur.set("horizontalPass", 0); - pass1.beginDraw(); - pass1.shader(blur); - pass1.image(src, 0, 0); - pass1.endDraw(); - - // Applying the blur shader along the horizontal direction - blur.set("horizontalPass", 1); - pass2.beginDraw(); - pass2.shader(blur); - pass2.image(pass1, 0, 0); - pass2.endDraw(); - - image(pass2, 0, 0); -} - diff --git a/android/examples/Topics/Shaders/SepBlur/data/blur.glsl b/android/examples/Topics/Shaders/SepBlur/data/blur.glsl deleted file mode 100644 index c47044e7a..000000000 --- a/android/examples/Topics/Shaders/SepBlur/data/blur.glsl +++ /dev/null @@ -1,58 +0,0 @@ -// Adapted from: -// http://callumhay.blogspot.com/2010/09/gaussian-blur-shader-glsl.html - -#ifdef GL_ES -precision mediump float; -precision mediump int; -#endif - -uniform sampler2D textureSampler; - -// The inverse of the texture dimensions along X and Y -uniform vec2 texcoordOffset; - -varying vec4 vertColor; -varying vec4 vertTexcoord; - -uniform int horizontalPass; // 0 or 1 to indicate vertical or horizontal pass -uniform float sigma; // The sigma value for the gaussian function: higher value means more blur - // A good value for 9x9 is around 3 to 5 - // A good value for 7x7 is around 2.5 to 4 - // A good value for 5x5 is around 2 to 3.5 - // ... play around with this based on what you need :) - -const float pi = 3.14159265; - -// Blur size divided by two (it is hard-coded because GLSL ES doesn't support -// variable loop length). -const float iter = 4.5; - -void main() { - vec2 blurMultiplyVec = 0 < horizontalPass ? vec2(1.0, 0.0) : vec2(0.0, 1.0); - - // Incremental Gaussian Coefficent Calculation (See GPU Gems 3 pp. 877 - 889) - vec3 incrementalGaussian; - incrementalGaussian.x = 1.0 / (sqrt(2.0 * pi) * sigma); - incrementalGaussian.y = exp(-0.5 / (sigma * sigma)); - incrementalGaussian.z = incrementalGaussian.y * incrementalGaussian.y; - - vec4 avgValue = vec4(0.0, 0.0, 0.0, 0.0); - float coefficientSum = 0.0; - - // Take the central sample first... - avgValue += texture2D(textureSampler, vertTexcoord.st) * incrementalGaussian.x; - coefficientSum += incrementalGaussian.x; - incrementalGaussian.xy *= incrementalGaussian.yz; - - // Go through the remaining 8 vertical samples (4 on each side of the center) - for (float i = 1.0; i <= iter; i++) { - avgValue += texture2D(textureSampler, vertTexcoord.st - i * texcoordOffset * - blurMultiplyVec) * incrementalGaussian.x; - avgValue += texture2D(textureSampler, vertTexcoord.st + i * texcoordOffset * - blurMultiplyVec) * incrementalGaussian.x; - coefficientSum += 2.0 * incrementalGaussian.x; - incrementalGaussian.xy *= incrementalGaussian.yz; - } - - gl_FragColor = avgValue / coefficientSum; -} \ No newline at end of file