From 6b380914249d32b190a47ec3b7e539c36547608d Mon Sep 17 00:00:00 2001 From: codeanticode Date: Sun, 10 Feb 2013 08:59:04 -0500 Subject: [PATCH] Updated shader examples on Android --- .../Topics/Shaders/BlurFilter/data/blur.glsl | 48 ++++++++++--------- .../Topics/Shaders/EdgeDetect/data/edges.glsl | 44 +++++++++-------- .../Topics/Shaders/EdgeFilter/data/edges.glsl | 44 +++++++++-------- .../Topics/Shaders/LowLevelGL/LowLevelGL.pde | 6 +-- .../Topics/Shaders/LowLevelGL/data/vert.glsl | 13 ++--- .../Shaders/ToonShading/data/ToonVert.glsl | 14 +++--- .../Demos/Graphics/LowLevelGL/LowLevelGL.pde | 6 +-- .../Demos/Graphics/LowLevelGL/data/vert.glsl | 13 ++--- 8 files changed, 99 insertions(+), 89 deletions(-) diff --git a/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl b/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl index 2d55794e5..2aa9d6079 100644 --- a/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl +++ b/android/examples/Topics/Shaders/BlurFilter/data/blur.glsl @@ -3,38 +3,40 @@ precision mediump float; precision mediump int; #endif -uniform sampler2D textureSampler; -uniform vec2 texcoordOffset; +#define PROCESSING_TEXTURE_SHADER + +uniform sampler2D texture; +uniform vec2 texOffset; varying vec4 vertColor; -varying vec4 vertTexcoord; +varying vec4 vertTexCoord; void main(void) { // 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); + vec2 tc0 = vertTexCoord.st + vec2(-texOffset.s, -texOffset.t); + vec2 tc1 = vertTexCoord.st + vec2( 0.0, -texOffset.t); + vec2 tc2 = vertTexCoord.st + vec2(+texOffset.s, -texOffset.t); + vec2 tc3 = vertTexCoord.st + vec2(-texOffset.s, 0.0); + vec2 tc4 = vertTexCoord.st + vec2( 0.0, 0.0); + vec2 tc5 = vertTexCoord.st + vec2(+texOffset.s, 0.0); + vec2 tc6 = vertTexCoord.st + vec2(-texOffset.s, +texOffset.t); + vec2 tc7 = vertTexCoord.st + vec2( 0.0, +texOffset.t); + vec2 tc8 = vertTexCoord.st + vec2(+texOffset.s, +texOffset.t); + + vec4 col0 = texture2D(texture, tc0); + vec4 col1 = texture2D(texture, tc1); + vec4 col2 = texture2D(texture, tc2); + vec4 col3 = texture2D(texture, tc3); + vec4 col4 = texture2D(texture, tc4); + vec4 col5 = texture2D(texture, tc5); + vec4 col6 = texture2D(texture, tc6); + vec4 col7 = texture2D(texture, tc7); + vec4 col8 = texture2D(texture, 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; + 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 933cf91d9..62109e3e3 100644 --- a/android/examples/Topics/Shaders/EdgeDetect/data/edges.glsl +++ b/android/examples/Topics/Shaders/EdgeDetect/data/edges.glsl @@ -3,35 +3,37 @@ precision mediump float; precision mediump int; #endif -uniform sampler2D textureSampler; -uniform vec2 texcoordOffset; +#define PROCESSING_TEXTURE_SHADER + +uniform sampler2D texture; +uniform vec2 texOffset; varying vec4 vertColor; -varying vec4 vertTexcoord; +varying vec4 vertTexCoord; void main(void) { // 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); + vec2 tc0 = vertTexCoord.st + vec2(-texOffset.s, -texOffset.t); + vec2 tc1 = vertTexCoord.st + vec2( 0.0, -texOffset.t); + vec2 tc2 = vertTexCoord.st + vec2(+texOffset.s, -texOffset.t); + vec2 tc3 = vertTexCoord.st + vec2(-texOffset.s, 0.0); + vec2 tc4 = vertTexCoord.st + vec2( 0.0, 0.0); + vec2 tc5 = vertTexCoord.st + vec2(+texOffset.s, 0.0); + vec2 tc6 = vertTexCoord.st + vec2(-texOffset.s, +texOffset.t); + vec2 tc7 = vertTexCoord.st + vec2( 0.0, +texOffset.t); + vec2 tc8 = vertTexCoord.st + vec2(+texOffset.s, +texOffset.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 col0 = texture2D(texture, tc0); + vec4 col1 = texture2D(texture, tc1); + vec4 col2 = texture2D(texture, tc2); + vec4 col3 = texture2D(texture, tc3); + vec4 col4 = texture2D(texture, tc4); + vec4 col5 = texture2D(texture, tc5); + vec4 col6 = texture2D(texture, tc6); + vec4 col7 = texture2D(texture, tc7); + vec4 col8 = texture2D(texture, tc8); 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 933cf91d9..62109e3e3 100644 --- a/android/examples/Topics/Shaders/EdgeFilter/data/edges.glsl +++ b/android/examples/Topics/Shaders/EdgeFilter/data/edges.glsl @@ -3,35 +3,37 @@ precision mediump float; precision mediump int; #endif -uniform sampler2D textureSampler; -uniform vec2 texcoordOffset; +#define PROCESSING_TEXTURE_SHADER + +uniform sampler2D texture; +uniform vec2 texOffset; varying vec4 vertColor; -varying vec4 vertTexcoord; +varying vec4 vertTexCoord; void main(void) { // 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); + vec2 tc0 = vertTexCoord.st + vec2(-texOffset.s, -texOffset.t); + vec2 tc1 = vertTexCoord.st + vec2( 0.0, -texOffset.t); + vec2 tc2 = vertTexCoord.st + vec2(+texOffset.s, -texOffset.t); + vec2 tc3 = vertTexCoord.st + vec2(-texOffset.s, 0.0); + vec2 tc4 = vertTexCoord.st + vec2( 0.0, 0.0); + vec2 tc5 = vertTexCoord.st + vec2(+texOffset.s, 0.0); + vec2 tc6 = vertTexCoord.st + vec2(-texOffset.s, +texOffset.t); + vec2 tc7 = vertTexCoord.st + vec2( 0.0, +texOffset.t); + vec2 tc8 = vertTexCoord.st + vec2(+texOffset.s, +texOffset.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 col0 = texture2D(texture, tc0); + vec4 col1 = texture2D(texture, tc1); + vec4 col2 = texture2D(texture, tc2); + vec4 col3 = texture2D(texture, tc3); + vec4 col4 = texture2D(texture, tc4); + vec4 col5 = texture2D(texture, tc5); + vec4 col6 = texture2D(texture, tc6); + vec4 col7 = texture2D(texture, tc7); + vec4 col8 = texture2D(texture, tc8); 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/LowLevelGL/LowLevelGL.pde b/android/examples/Topics/Shaders/LowLevelGL/LowLevelGL.pde index 7731fe161..5d10bbc13 100644 --- a/android/examples/Topics/Shaders/LowLevelGL/LowLevelGL.pde +++ b/android/examples/Topics/Shaders/LowLevelGL/LowLevelGL.pde @@ -40,8 +40,8 @@ void draw() { pgl = beginPGL(); flatShader.bind(); - vertLoc = pgl.getAttribLocation(flatShader.glProgram, "inVertex"); - colorLoc = pgl.getAttribLocation(flatShader.glProgram, "inColor"); + vertLoc = pgl.getAttribLocation(flatShader.glProgram, "vertex"); + colorLoc = pgl.getAttribLocation(flatShader.glProgram, "color"); pgl.enableVertexAttribArray(vertLoc); pgl.enableVertexAttribArray(colorLoc); @@ -101,4 +101,4 @@ void updateGeometry() { FloatBuffer allocateDirectFloatBuffer(int n) { return ByteBuffer.allocateDirect(n * Float.SIZE/8).order(ByteOrder.nativeOrder()).asFloatBuffer(); -} +} diff --git a/android/examples/Topics/Shaders/LowLevelGL/data/vert.glsl b/android/examples/Topics/Shaders/LowLevelGL/data/vert.glsl index 95c266a76..1a0177008 100644 --- a/android/examples/Topics/Shaders/LowLevelGL/data/vert.glsl +++ b/android/examples/Topics/Shaders/LowLevelGL/data/vert.glsl @@ -18,15 +18,16 @@ Boston, MA 02111-1307 USA */ -uniform mat4 projmodelviewMatrix; +#define PROCESSING_COLOR_SHADER -attribute vec4 inVertex; -attribute vec4 inColor; +uniform mat4 transform; + +attribute vec4 vertex; +attribute vec4 color; varying vec4 vertColor; void main() { - gl_Position = projmodelviewMatrix * inVertex; - - vertColor = inColor; + gl_Position = transform * vertex; + vertColor = color; } \ No newline at end of file diff --git a/android/examples/Topics/Shaders/ToonShading/data/ToonVert.glsl b/android/examples/Topics/Shaders/ToonShading/data/ToonVert.glsl index f314485aa..01b77b2c9 100644 --- a/android/examples/Topics/Shaders/ToonShading/data/ToonVert.glsl +++ b/android/examples/Topics/Shaders/ToonShading/data/ToonVert.glsl @@ -2,25 +2,27 @@ // tutorial from lighthouse 3D: // http://www.lighthouse3d.com/tutorials/glsl-tutorial/toon-shader-version-ii/ -uniform mat4 modelviewMatrix; -uniform mat4 projmodelviewMatrix; +#define PROCESSING_LIGHT_SHADER + +uniform mat4 modelview; +uniform mat4 transform; uniform mat3 normalMatrix; uniform vec3 lightNormal[8]; -attribute vec4 inVertex; -attribute vec3 inNormal; +attribute vec4 vertex; +attribute vec3 normal; varying vec3 vertNormal; varying vec3 vertLightDir; void main() { // Vertex in clip coordinates - gl_Position = projmodelviewMatrix * inVertex; + gl_Position = transform * vertex; // Normal vector in eye coordinates is passed // to the fragment shader - vertNormal = normalize(normalMatrix * inNormal); + vertNormal = normalize(normalMatrix * normal); // Assuming that there is only one directional light. // Its normal vector is passed to the fragment shader diff --git a/java/examples/Demos/Graphics/LowLevelGL/LowLevelGL.pde b/java/examples/Demos/Graphics/LowLevelGL/LowLevelGL.pde index 2bd1a42e7..d9db7537a 100644 --- a/java/examples/Demos/Graphics/LowLevelGL/LowLevelGL.pde +++ b/java/examples/Demos/Graphics/LowLevelGL/LowLevelGL.pde @@ -38,9 +38,9 @@ void draw() { pgl = beginPGL(); flatShader.bind(); - - vertLoc = pgl.getAttribLocation(flatShader.glProgram, "inVertex"); - colorLoc = pgl.getAttribLocation(flatShader.glProgram, "inColor"); + + vertLoc = pgl.getAttribLocation(flatShader.glProgram, "vertex"); + colorLoc = pgl.getAttribLocation(flatShader.glProgram, "color"); pgl.enableVertexAttribArray(vertLoc); pgl.enableVertexAttribArray(colorLoc); diff --git a/java/examples/Demos/Graphics/LowLevelGL/data/vert.glsl b/java/examples/Demos/Graphics/LowLevelGL/data/vert.glsl index 95c266a76..1a0177008 100644 --- a/java/examples/Demos/Graphics/LowLevelGL/data/vert.glsl +++ b/java/examples/Demos/Graphics/LowLevelGL/data/vert.glsl @@ -18,15 +18,16 @@ Boston, MA 02111-1307 USA */ -uniform mat4 projmodelviewMatrix; +#define PROCESSING_COLOR_SHADER -attribute vec4 inVertex; -attribute vec4 inColor; +uniform mat4 transform; + +attribute vec4 vertex; +attribute vec4 color; varying vec4 vertColor; void main() { - gl_Position = projmodelviewMatrix * inVertex; - - vertColor = inColor; + gl_Position = transform * vertex; + vertColor = color; } \ No newline at end of file