Project

General

Profile

« Previous | Next » 

Revision 1c67457f

Added by Leszek Koltunski almost 8 years ago

Separable Box blur fully works now!

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;
24 25

  
25 26
import org.distorted.library.message.EffectMessage;
26 27
import org.distorted.library.program.DistortedProgram;
......
51 52
  private static final int NUM_CACHE    = 0;
52 53
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
53 54

  
54
  private static final FloatBuffer mQuadPositions, mQuadTexture1, mQuadTexture2;
55
  private static final FloatBuffer mQuadPositions, mQuadTextureNor, mQuadTextureInv;
55 56

  
56 57
  static
57 58
    {
58 59
    int dataLength      = 4;
59 60
    int bytes_per_float = 4;
60 61

  
61
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
62
    float[] textureData1= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
63
    float[] textureData2= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
62
    float[] position  = { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
63
    float[] textureNor= {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
64
    float[] textureInv= {  0.0f,  0.0f,   1.0f, 0.0f,  0.0f, 1.0f,  1.0f, 1.0f };
64 65

  
65 66
    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
66
    mQuadPositions.put(positionData).position(0);
67
    mQuadTexture1  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadTexture1.put(textureData1).position(0);
69
    mQuadTexture2  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
70
    mQuadTexture2.put(textureData2).position(0);
67
    mQuadPositions.put(position).position(0);
68
    mQuadTextureNor  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
    mQuadTextureNor.put(textureNor).position(0);
70
    mQuadTextureInv  = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
71
    mQuadTextureInv.put(textureInv).position(0);
71 72
    }
72 73

  
73 74
  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(1,1);
75
  private static float[] mMVPMatrix = new float[16];
76
  private static float[] mTmpMatrix = new float[16];
74 77

  
75 78
  // BLUR effect
76 79
  private static DistortedProgram mBlurProgram;
......
151 154
    }
152 155

  
153 156
///////////////////////////////////////////////////////////////////////////////////////////////////
154
// w,h - width and height of hte input texture. MVP - Model-View-Projection matrix to apply to the
157
// w,h - width and height of the input texture. MVP - Model-View-Projection matrix to apply to the
155 158
// texture; df - output FBO.
156 159

  
157 160
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
......
169 172
    GLES30.glUniform1fv( mWeightsH, radius+1, mWeights,0);
170 173
    GLES30.glUniform1i( mRadiusH, radius);
171 174
    GLES30.glUniform2f( mObjDH , w, h );
172
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
173 175

  
174
    mBufferFBO.resizeFast(df.mWidth, df.mHeight);
176
    mBufferFBO.resizeFast( (int)w, (int)h);
175 177
    mBufferFBO.setAsOutput();
178
    GLES30.glViewport(0, 0, (int)w, (int)h);
179

  
180
    Matrix.setIdentityM(mTmpMatrix, 0);
181
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -mBufferFBO.mDistance);
182
    Matrix.multiplyMM(mMVPMatrix, 0, mBufferFBO.mProjectionMatrix, 0, mTmpMatrix, 0);
176 183

  
177 184
    // horizontal blur
178 185
    GLES30.glUniform1f( mStepH , 1/h );
186
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
179 187
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
180
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTexture2);
188
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
181 189
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
182 190

  
183 191
    mBufferFBO.setAsInput();
184 192
    df.setAsOutput();
193
    GLES30.glViewport(0, 0, df.mWidth, df.mHeight);
185 194

  
186 195
    // vertical blur
187 196
    GLES30.glUniform1f( mStepH , 1/w );
197
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
188 198
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
189
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTexture2);
199
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
190 200
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
191 201
    }
192 202

  

Also available in: Unified diff