Project

General

Profile

« Previous | Next » 

Revision 5ccafcca

Added by Leszek Koltunski almost 8 years ago

Separable Box blur fully works now!

View differences:

src/main/java/org/distorted/library/EffectQueuePostprocess.java
77 77

  
78 78
  // BLUR effect
79 79
  private static DistortedProgram mBlurProgram;
80
  private static int mRadiusH,mStepH,mWeightsH,mObjDH,mMVPMatrixH;
80
  private static int mRadiusH,mOffsetsH,mWeightsH,mObjDH,mMVPMatrixH;
81 81
  private float[] mWeights = new float[MAX_BLUR];
82

  
82
  private float[] mOffsets = new float[MAX_BLUR];
83 83
  // another effect ....
84 84

  
85 85
///////////////////////////////////////////////////////////////////////////////////////////////////
......
103 103

  
104 104
    int blurProgramH = mBlurProgram.getProgramHandle();
105 105
    mRadiusH    = GLES30.glGetUniformLocation( blurProgramH, "u_Radius");
106
    mStepH      = GLES30.glGetUniformLocation( blurProgramH, "u_Step");
106
    mOffsetsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Offsets");
107 107
    mWeightsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Weights");
108 108
    mObjDH      = GLES30.glGetUniformLocation( blurProgramH, "u_objD");
109 109
    mMVPMatrixH = GLES30.glGetUniformLocation( blurProgramH, "u_MVPMatrix");
......
167 167
    for(int i=0; i<=radius; i++)
168 168
      {
169 169
      mWeights[i] = 1.0f / (2.0f*radius+1.0f);
170
      mOffsets[i] = i/h;
170 171
      }
171 172

  
172 173
    GLES30.glUniform1fv( mWeightsH, radius+1, mWeights,0);
174
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
173 175
    GLES30.glUniform1i( mRadiusH, radius);
174 176
    GLES30.glUniform2f( mObjDH , w, h );
175 177

  
......
182 184
    Matrix.multiplyMM(mMVPMatrix, 0, mBufferFBO.mProjectionMatrix, 0, mTmpMatrix, 0);
183 185

  
184 186
    // horizontal blur
185
    GLES30.glUniform1f( mStepH , 1/h );
187

  
186 188
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
187 189
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
188 190
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
......
192 194
    df.setAsOutput();
193 195
    GLES30.glViewport(0, 0, df.mWidth, df.mHeight);
194 196

  
197
    float adjust = h/w;
198
    for(int i=0; i<=radius; i++) mOffsets[i] *= adjust;
199

  
195 200
    // vertical blur
196
    GLES30.glUniform1f( mStepH , 1/w );
201
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
197 202
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
198 203
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
199 204
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
src/main/res/raw/blur_fragment_shader.glsl
21 21

  
22 22
varying vec2 v_TexCoordinate;
23 23
uniform sampler2D u_Texture;
24
uniform float u_Step;
24
uniform float u_Offsets[MAX_BLUR];
25 25
uniform float u_Weights[MAX_BLUR];
26 26
uniform int u_Radius;
27 27

  
......
29 29

  
30 30
void main()
31 31
  {
32
  float lCoord = v_TexCoordinate.x;
33
  float rCoord = lCoord;
34
  float yCoord = v_TexCoordinate.y;
35

  
36
  vec4  pixel  = texture2D(u_Texture,vec2(lCoord,yCoord)) * u_Weights[0];
32
  vec4 pixel= texture2D(u_Texture,v_TexCoordinate) * u_Weights[0];
37 33

  
38 34
  for (int x = 1; x <= u_Radius; x+=1)
39 35
    {
40
    rCoord+= u_Step;
41
    lCoord-= u_Step;
42
    pixel += ( texture2D(u_Texture,vec2(rCoord,yCoord)) +
43
               texture2D(u_Texture,vec2(lCoord,yCoord)) ) * u_Weights[x];
36
    pixel += ( texture2D(u_Texture,vec2(v_TexCoordinate.x+u_Offsets[x],v_TexCoordinate.y)) +
37
               texture2D(u_Texture,vec2(v_TexCoordinate.x-u_Offsets[x],v_TexCoordinate.y)) ) * u_Weights[x];
44 38
    }
45 39

  
46 40
  gl_FragColor = pixel;

Also available in: Unified diff