Project

General

Profile

Download (10.3 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / EffectQueuePostprocess.java @ 9d5bb851

1 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library;
21
22 c90b9e01 Leszek Koltunski
import android.content.res.Resources;
23 194ab46f Leszek Koltunski
import android.opengl.GLES30;
24 1c67457f Leszek Koltunski
import android.opengl.Matrix;
25 4c1dd6e9 Leszek Koltunski
26
import org.distorted.library.message.EffectMessage;
27 8fa96e69 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
28 c90b9e01 Leszek Koltunski
import org.distorted.library.program.FragmentCompilationException;
29
import org.distorted.library.program.FragmentUniformsException;
30
import org.distorted.library.program.LinkingException;
31
import org.distorted.library.program.VertexCompilationException;
32
import org.distorted.library.program.VertexUniformsException;
33 4c1dd6e9 Leszek Koltunski
import org.distorted.library.type.Data1D;
34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Static1D;
36
37 c90b9e01 Leszek Koltunski
import java.io.InputStream;
38 8fa96e69 Leszek Koltunski
import java.nio.ByteBuffer;
39
import java.nio.ByteOrder;
40
import java.nio.FloatBuffer;
41
42 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
43
44
class EffectQueuePostprocess extends EffectQueue
45
  {
46 0d81d0fb Leszek Koltunski
  private static final int MAX_BLUR = 50;
47
48 9d5bb851 Leszek Koltunski
  private static final int POS_DATA_SIZE= 2; // Post Program: size of the position data in elements
49
  private static final int TEX_DATA_SIZE= 2; // Post Program: size of the texture coordinate data in elements.
50 8fa96e69 Leszek Koltunski
51 667a1ad9 Leszek Koltunski
  private static final int NUM_UNIFORMS = 4;
52 4c1dd6e9 Leszek Koltunski
  private static final int NUM_CACHE    = 0;
53
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
54
55 1c67457f Leszek Koltunski
  private static final FloatBuffer mQuadPositions, mQuadTextureNor, mQuadTextureInv;
56 8fa96e69 Leszek Koltunski
57
  static
58
    {
59 667a1ad9 Leszek Koltunski
    int dataLength      = 4;
60
    int bytes_per_float = 4;
61 8fa96e69 Leszek Koltunski
62 1c67457f Leszek Koltunski
    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 };
65 8fa96e69 Leszek Koltunski
66 9d5bb851 Leszek Koltunski
    mQuadPositions = ByteBuffer.allocateDirect(POS_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
67 1c67457f Leszek Koltunski
    mQuadPositions.put(position).position(0);
68 9d5bb851 Leszek Koltunski
    mQuadTextureNor= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
69 1c67457f Leszek Koltunski
    mQuadTextureNor.put(textureNor).position(0);
70 9d5bb851 Leszek Koltunski
    mQuadTextureInv= ByteBuffer.allocateDirect(TEX_DATA_SIZE*dataLength*bytes_per_float).order(ByteOrder.nativeOrder()).asFloatBuffer();
71 1c67457f Leszek Koltunski
    mQuadTextureInv.put(textureInv).position(0);
72 8fa96e69 Leszek Koltunski
    }
73 4c1dd6e9 Leszek Koltunski
74 0d81d0fb Leszek Koltunski
  private static DistortedFramebuffer mBufferFBO = new DistortedFramebuffer(1,1);
75 1c67457f Leszek Koltunski
  private static float[] mMVPMatrix = new float[16];
76
  private static float[] mTmpMatrix = new float[16];
77 0d81d0fb Leszek Koltunski
78
  // BLUR effect
79
  private static DistortedProgram mBlurProgram;
80 5ccafcca Leszek Koltunski
  private static int mRadiusH,mOffsetsH,mWeightsH,mObjDH,mMVPMatrixH;
81 0d81d0fb Leszek Koltunski
  private float[] mWeights = new float[MAX_BLUR];
82 5ccafcca Leszek Koltunski
  private float[] mOffsets = new float[MAX_BLUR];
83 0d81d0fb Leszek Koltunski
  // another effect ....
84
85 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87
  EffectQueuePostprocess(long id)
88
    { 
89
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
90
    }
91
92 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  static void createProgram(Resources resources)
95
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
96
    {
97 f1d5ea12 Leszek Koltunski
    final InputStream postVertexStream   = resources.openRawResource(R.raw.blur_vertex_shader);
98
    final InputStream postFragmentStream = resources.openRawResource(R.raw.blur_fragment_shader);
99 c90b9e01 Leszek Koltunski
100 0d81d0fb Leszek Koltunski
    mBlurProgram = new DistortedProgram(postVertexStream,postFragmentStream,
101
                                        "#version 100\n",
102
                                        "#version 100\n#define MAX_BLUR "+MAX_BLUR);
103 c90b9e01 Leszek Koltunski
104 f1d5ea12 Leszek Koltunski
    int blurProgramH = mBlurProgram.getProgramHandle();
105
    mRadiusH    = GLES30.glGetUniformLocation( blurProgramH, "u_Radius");
106 5ccafcca Leszek Koltunski
    mOffsetsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Offsets");
107 0d81d0fb Leszek Koltunski
    mWeightsH   = GLES30.glGetUniformLocation( blurProgramH, "u_Weights");
108 f1d5ea12 Leszek Koltunski
    mObjDH      = GLES30.glGetUniformLocation( blurProgramH, "u_objD");
109
    mMVPMatrixH = GLES30.glGetUniformLocation( blurProgramH, "u_MVPMatrix");
110 4c1dd6e9 Leszek Koltunski
    }
111
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
  
114
  synchronized void compute(long currTime) 
115
    {
116
    if( currTime==mTime ) return;
117
    if( mTime==0 ) mTime = currTime;
118
    long step = (currTime-mTime);
119
   
120
    for(int i=0; i<mNumEffects; i++)
121
      {
122
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
123
        {
124
        for(int j=0; j<mNumListeners; j++)
125
          EffectMessageSender.newMessage( mListeners.elementAt(j),
126
                                          EffectMessage.EFFECT_FINISHED,
127
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
128
                                          mName[i],
129
                                          mObjectID);
130
131
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
132
          {
133
          remove(i);
134
          i--;
135
          continue;
136
          }
137
        else mInter[0][i] = null;
138
        }
139
140
      mCurrentDuration[i] += step;
141
      }
142
     
143
    mTime = currTime;  
144
    }  
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
  protected void moveEffect(int index)
149
    {
150
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
151
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
152
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
153 667a1ad9 Leszek Koltunski
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
154 4c1dd6e9 Leszek Koltunski
    }
155
156
///////////////////////////////////////////////////////////////////////////////////////////////////
157
158 9d5bb851 Leszek Koltunski
  private int computeKernel(int radius, float height)
159 4c1dd6e9 Leszek Koltunski
    {
160 0d81d0fb Leszek Koltunski
    if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
161
162
    for(int i=0; i<=radius; i++)
163
      {
164 9d5bb851 Leszek Koltunski
      // Box Blur size 'radius'
165 0d81d0fb Leszek Koltunski
      mWeights[i] = 1.0f / (2.0f*radius+1.0f);
166 9d5bb851 Leszek Koltunski
      mOffsets[i] = i*height;
167
168
      // Gaussian Blur size 'radius'
169
170
171 0d81d0fb Leszek Koltunski
      }
172
173 9d5bb851 Leszek Koltunski
    return radius;
174
    }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
// w,h - width and height of the input texture. MVP - Model-View-Projection matrix to apply to the
178
// texture; df - output FBO.
179
180
  synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
181
    {
182
    mBlurProgram.useProgram();
183
184
    int radius = computeKernel( (int)mUniforms[0], 1/h );
185
186 0d81d0fb Leszek Koltunski
    GLES30.glUniform1fv( mWeightsH, radius+1, mWeights,0);
187
    GLES30.glUniform1i( mRadiusH, radius);
188 194ab46f Leszek Koltunski
    GLES30.glUniform2f( mObjDH , w, h );
189 4c1dd6e9 Leszek Koltunski
190 1c67457f Leszek Koltunski
    mBufferFBO.resizeFast( (int)w, (int)h);
191 0d81d0fb Leszek Koltunski
    mBufferFBO.setAsOutput();
192 1c67457f Leszek Koltunski
    GLES30.glViewport(0, 0, (int)w, (int)h);
193
194
    Matrix.setIdentityM(mTmpMatrix, 0);
195
    Matrix.translateM(mTmpMatrix, 0, 0, 0, -mBufferFBO.mDistance);
196
    Matrix.multiplyMM(mMVPMatrix, 0, mBufferFBO.mProjectionMatrix, 0, mTmpMatrix, 0);
197 0d81d0fb Leszek Koltunski
198
    // horizontal blur
199 9d5bb851 Leszek Koltunski
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
200 1c67457f Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
201 9d5bb851 Leszek Koltunski
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
202
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
203 0d81d0fb Leszek Koltunski
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
204
205
    mBufferFBO.setAsInput();
206
    df.setAsOutput();
207 1c67457f Leszek Koltunski
    GLES30.glViewport(0, 0, df.mWidth, df.mHeight);
208 0d81d0fb Leszek Koltunski
209 5ccafcca Leszek Koltunski
    float adjust = h/w;
210
    for(int i=0; i<=radius; i++) mOffsets[i] *= adjust;
211
212 0d81d0fb Leszek Koltunski
    // vertical blur
213 5ccafcca Leszek Koltunski
    GLES30.glUniform1fv( mOffsetsH ,radius+1, mOffsets,0);
214 1c67457f Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
215 9d5bb851 Leszek Koltunski
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
216
    GLES30.glVertexAttribPointer(mBlurProgram.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTextureInv);
217 194ab46f Leszek Koltunski
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
218 d6e94c84 Leszek Koltunski
    }
219
220 4c1dd6e9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
221
// blur
222
223 85cbbc5e Leszek Koltunski
  synchronized long add(EffectNames eln, Data1D degree)
224 4c1dd6e9 Leszek Koltunski
    {
225
    if( mMax[INDEX]>mNumEffects )
226
      {
227
      if( degree instanceof Dynamic1D)
228
        {
229
        mInter[0][mNumEffects] = (Dynamic1D)degree;
230
        }
231
      else if( degree instanceof Static1D)
232
        {
233
        mInter[0][mNumEffects] = null;
234
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
235
        }
236
      else return -1;
237
238 85cbbc5e Leszek Koltunski
      mInter[1][mNumEffects] = null;
239
      mInter[2][mNumEffects] = null;
240 4c1dd6e9 Leszek Koltunski
241
      return addBase(eln);
242
      }
243
      
244
    return -1;
245
    }
246
  }