I`m working on ripple effect shader, want to simulate rain drop.
I merged together livewallpaper and RadiusBlur samples and trying to implement my own shader.
If I understand correctly, RadiusBlur shader sample works as following:
1. One sprite is created (512x512)
2. Our screen is bigger/smaller than display, so texture coordinates should be mapped to actual screen coordinates:
1) we use vertex shader for mapping
2) basically vertex function should looks something like this -
Using cpp Syntax Highlighting
- gl_Position = projection_matrix * modelview_matrix * vec4(vertex, 1.0);
Parsed in 0.004 seconds, using GeSHi 1.0.8.4
3) RadiusBlur sample implements following vertex shader:
Using cpp Syntax Highlighting
- public static final String VERTEXSHADER =
- "uniform mat4 " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + ";\n" +
- "attribute vec4 " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n" +
- "attribute vec2 " + ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES + ";\n" +
- "varying vec2 " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ";\n" +
- "void main() {\n" +
- " " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + " = " + ShaderProgramConstants.ATTRIBUTE_TEXTURECOORDINATES + ";\n" +
- " gl_Position = " + ShaderProgramConstants.UNIFORM_MODELVIEWPROJECTIONMATRIX + " * " + ShaderProgramConstants.ATTRIBUTE_POSITION + ";\n" +
- "}";
Parsed in 0.007 seconds, using GeSHi 1.0.8.4
gl_Position is a coordinates of vertex and calculated like projection*modelView * coordinates of texture vertex(one of boarders?).
4)My pixel shader code is based on this guide and listed below:
Using cpp Syntax Highlighting
- "precision mediump float;\n" +
- "uniform sampler2D " + ShaderProgramConstants.UNIFORM_TEXTURE_0 + ";\n" +
- "varying mediump vec2 " + ShaderProgramConstants.VARYING_TEXTURECOORDINATES + ";\n" +
- "uniform vec2 " + RadialBlurShaderProgram.UNIFORM_RESOLUTION + ";\n" +
- "uniform vec2 " + RadialBlurShaderProgram.UNIFORM_TIME + ";\n" +
- "void main() {\n" +
- /* The actual (unburred) sample. */
- /*ripple effect shader*/
- " vec2 cPos = -1.0 + 2.0 * " + RadialBlurShaderProgram.VARYING_TEXTURECOORDINATES + " / " + RadialBlurShaderProgram.UNIFORM_RESOLUTION + ".xy;\n" +
- " float cLength = length(cPos);\n" +
- " vec2 uv = gl_FragCoord.xy/" + RadialBlurShaderProgram.UNIFORM_RESOLUTION + ".xy" + "+(cPos/cLength)*cos(cLength*12.0-" + RadialBlurShaderProgram.UNIFORM_TIME + "*4.0)*0.03;\n" +
- " vec3 col = texture2D(" + ShaderProgramConstants.UNIFORM_TEXTURE_0 + ",uv).xyz;\n" +
- " gl_FragColor = vec4(col,1.0); \n" + // color
- "}";
Parsed in 0.008 seconds, using GeSHi 1.0.8.4
UNIFORM_TIME is changing with every frame and causes ripple effect fall off.
Main issue - following fragment shader does not do anything, it does not affect texture. If I change vec2 cPos = -1.0 + 2.0 to some other values - it adds some distortion to texture, but it does not change texture on frame update as it should. I do believe I messed up something with coordinates (texture coordinates/frament/scene), but can`t figure what. I implemented this framgent shader using clear Android OpenGL API and it works (used another shader sample), but can`t manage to do the same with AndEngine. If someone encountered something like this before - I would appreciate any help.
Thanks in advance.



