Project

General

Profile

« Previous | Next » 

Revision a1b36fc8

Added by Leszek Koltunski over 7 years ago

Simplify BLUR shader.

View differences:

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

  
22 22
import android.content.res.Resources;
23 23
import android.opengl.GLES30;
24
import android.opengl.Matrix;
25 24

  
26 25
import org.distorted.library.message.EffectMessage;
27 26
import org.distorted.library.program.DistortedProgram;
......
72 71
  private static DistortedFramebuffer mPostBuffer = new DistortedFramebuffer(true,DistortedSurface.TYPE_SYST,1,1);
73 72
                 DistortedFramebuffer mMainBuffer = new DistortedFramebuffer(true,DistortedSurface.TYPE_SYST,1,1);
74 73

  
75
  private static float[] mMVPMatrix = new float[16];
76
  private static float[] mTmpMatrix = new float[16];
77

  
78 74
  // BLUR effect
79 75
  private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
80 76
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
......
103 99
  private static float[] offsetsCache = new float[MAX_BLUR + MAX_BLUR*MAX_BLUR/4];
104 100

  
105 101
  private static DistortedProgram mBlurProgram;
106
  private static int mRadiusH,mOffsetsH,mWeightsH,mObjDH,mMVPMatrixH;
102
  private static int mRadiusH,mOffsetsH,mWeightsH,mDepthH;
107 103
  private static float[] mWeights = new float[MAX_BLUR];
108 104
  private static float[] mOffsets = new float[MAX_BLUR];
109 105
  // another effect ....
......
131 127
    mRadiusH    = GLES30.glGetUniformLocation( blurProgramH, "u_Radius");
132 128
    mOffsetsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Offsets");
133 129
    mWeightsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Weights");
134
    mObjDH      = GLES30.glGetUniformLocation( blurProgramH, "u_objD");
135
    mMVPMatrixH = GLES30.glGetUniformLocation( blurProgramH, "u_MVPMatrix");
130
    mDepthH     = GLES30.glGetUniformLocation( blurProgramH, "u_Depth");
136 131
    }
137 132

  
138 133
///////////////////////////////////////////////////////////////////////////////////////////////////
......
235 230
    if( mNumEffects>0 && compute(time) )
236 231
      {
237 232
      mMainBuffer.setAsInput();
238

  
239
      Matrix.setIdentityM(mTmpMatrix, 0);
240
      Matrix.translateM(mTmpMatrix, 0, 0, 0, -surface.mDistance);
241
      Matrix.multiplyMM(mMVPMatrix, 0, surface.mProjectionMatrix, 0, mTmpMatrix, 0);
242

  
243 233
      float w = surface.mWidth;
244 234
      float h = surface.mHeight;
245 235

  
246
      mBlurProgram.useProgram();
247

  
248 236
      int radius = (int)mUniforms[0];
249 237
      if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
250 238
      computeGaussianKernel(radius);
......
253 241
      radius = (radius+1)/2;
254 242
      for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h;
255 243

  
256
      GLES30.glUniform1fv( mWeightsH, radius+1, weightsCache,offset);
257
      GLES30.glUniform1i( mRadiusH, radius);
258
      GLES30.glUniform2f( mObjDH , w, h );
259

  
260 244
      mPostBuffer.resizeFast( (int)w, (int)h);
261 245
      mPostBuffer.setAsOutput(time);
262 246
      GLES30.glViewport(0, 0, (int)w, (int)h);
263 247

  
264
      Matrix.setIdentityM(mTmpMatrix, 0);
265
      Matrix.translateM(mTmpMatrix, 0, 0, 0, -mPostBuffer.mDistance);
266
      Matrix.multiplyMM(mMVPMatrix, 0, mPostBuffer.mProjectionMatrix, 0, mTmpMatrix, 0);
248
      mBlurProgram.useProgram();
249
      GLES30.glUniform1fv( mWeightsH, radius+1, weightsCache,offset);
250
      GLES30.glUniform1i( mRadiusH, radius);
251
      GLES30.glUniform1f( mDepthH , 1.0f-surface.mNear);
267 252

  
268 253
      // horizontal blur
269 254
      GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
270
      GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
271 255
      GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
272 256
      GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
273 257
      GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
src/main/res/raw/blit_fragment_shader.glsl
18 18
//////////////////////////////////////////////////////////////////////////////////////////////
19 19

  
20 20
precision lowp float;
21
  
22
uniform sampler2D u_Texture;  // The input texture.
21

  
23 22
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment.
23
uniform sampler2D u_Texture;  // The input texture.
24 24

  
25 25
//////////////////////////////////////////////////////////////////////////////////////////////
26 26

  
src/main/res/raw/blur_vertex_shader.glsl
19 19

  
20 20
precision lowp float;
21 21

  
22
uniform vec2 u_objD;             // object width X object height.
23
uniform mat4 u_MVPMatrix;        // the combined model/view/projection matrix.
24

  
25
attribute vec2 a_Position;       // Per-vertex position information we will pass in.
22
uniform float u_Depth;           // distance from the near plane to render plane, in clip coords
23
attribute vec2 a_Position;       // Per-vertex position.
26 24
attribute vec2 a_TexCoordinate;  // Per-vertex texture coordinate information we will pass in.
27
varying   vec2 v_TexCoordinate;  //
25
varying vec2 v_TexCoordinate;    //
28 26

  
29 27
//////////////////////////////////////////////////////////////////////////////////////////////
30
// empty 2D vertex shader for postprocessing
31 28

  
32 29
void main()
33 30
  {
34 31
  v_TexCoordinate = a_TexCoordinate;
35
  gl_Position     = u_MVPMatrix*vec4( u_objD*a_Position, 0.0, 1.0);
32
  gl_Position     = vec4(2.0*a_Position,u_Depth,1.0);
36 33
  }

Also available in: Unified diff