Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectListFragment.java @ b3618cb5

1
package org.distorted.library;
2

    
3
import android.opengl.GLES20;
4

    
5
///////////////////////////////////////////////////////////////////////////////////////////////////
6

    
7
class EffectListFragment extends EffectList
8
  {
9
  private static final int NUM_UNIFORMS = 9; 
10
  private float[] mBuf;
11
  private static int mNumEffectsH;
12
  private static int mTypeH;
13
  private static int mUniformsH;
14
  
15
///////////////////////////////////////////////////////////////////////////////////////////////////
16
   
17
  public EffectListFragment(DistortedObject obj)
18
    { 
19
    super(obj,NUM_UNIFORMS,FRAGMENT);
20
   
21
    if( mMax[FRAGMENT]>0 )
22
      {
23
      mBuf= new float[4*mMax[FRAGMENT]];
24
      }
25
    }
26
  
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
// Only max Byte.MAX_VALUE concurrent effects per bitmap.
29
// If you want more, change type of the mNumEffects, mIDIndex and mFreeIndexes variables to shorts.
30
  
31
  static boolean setMax(int m)
32
    {
33
    if( (mCreated==false && !Distorted.isInitialized()) || m<=mMax[FRAGMENT] ) 
34
      {
35
           if( m<0              ) m = 0;
36
      else if( m>Byte.MAX_VALUE ) m = Byte.MAX_VALUE;
37
      
38
      mMax[FRAGMENT] = m;
39
      return true;
40
      }
41
   
42
    return false;
43
    }
44
 
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

    
47
  static int getMax()
48
    {
49
    return mMax[FRAGMENT];
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  static void getUniforms(int mProgramH)
55
    {
56
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "fNumEffects");
57
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "fType");
58
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "fUniforms");
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
  
63
  synchronized void compute(long currTime) 
64
    { 
65
    if( currTime==mTime ) return;
66
    if( mTime==0 ) mTime = currTime;
67
    long step = (currTime-mTime);
68
   
69
    for(int i=0; i<mNumEffects; i++)
70
      {
71
      if( mInterI[i]==null ) continue;    
72
      
73
      if( mInterP[i]!=null ) mInterP[i].interpolateMain(mBuf, 4*i, mCurrentDuration[i]);
74
        
75
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )      
76
        {
77
        for(int j=0; j<mNumListeners; j++)   
78
          EffectMessageSender.newMessage( mListeners.elementAt(j),
79
                                          EffectMessage.EFFECT_FINISHED, 
80
                                          (mID[i]<<DistortedObject.TYPE_NUM)+Distorted.TYPE_FRAG, 
81
                                          mType[i], 
82
                                          mBitmapID); 
83
      
84
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
85
          {
86
          remove(i);
87
          i--;
88
          continue;
89
          }
90
        }
91
           
92
      mCurrentDuration[i] += step;
93
      }
94
   
95
    mTime = currTime;  
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  protected void moveEffect(int index)
101
    {
102
    mBuf[4*index  ] = mBuf[4*index+4];
103
    mBuf[4*index+1] = mBuf[4*index+5];
104
    mBuf[4*index+2] = mBuf[4*index+6];
105
    mBuf[4*index+3] = mBuf[4*index+7];
106
              
107
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
108
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
109
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];  
110
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];  
111
    }
112
  
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
  
115
  synchronized void send() 
116
    {
117
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
118
      
119
    if( mNumEffects>0 )
120
      {     
121
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
122
      GLES20.glUniform3fv( mUniformsH,3*mNumEffects, mUniforms,0);
123
      }  
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  synchronized void sendZero() 
129
    {
130
    GLES20.glUniform1i( mNumEffectsH, 0);
131
    }
132
    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
// Do various post-processing on already computed effects.
135
// 1) move all Points and scale all Region radii by a ModelView matrix
136
// 2) in case of macroblock, pre-compute some values so that we don't have to do it in the fragment shader.
137
  
138
  void postprocess(float[] MVmatrix)
139
    {
140
    if( mNumEffects>0 )
141
      {
142
      float tx,ty;   
143
      float w = (float)Math.sqrt(MVmatrix[0]*MVmatrix[0] + MVmatrix[4]*MVmatrix[4]);  // The scale factors are the lengths of the first 3 vectors of the upper-left 3x3 submatrix; here
144
      float h = (float)Math.sqrt(MVmatrix[1]*MVmatrix[1] + MVmatrix[5]*MVmatrix[5]);  // m[2]=m[6]=m[8]=m[9]=0 so it is really only the upper-left 2x2 matrix.
145
   
146
      for(int i=0; i<mNumEffects; i++)
147
        {   
148
        tx = mBuf[4*i  ]-mObjHalfX; // we have to invert y and move everything by (half of bmp width, half of bmp height)
149
        ty =-mBuf[4*i+1]+mObjHalfY; //
150
      
151
        mUniforms[NUM_UNIFORMS*i+4] = w*mBuf[4*i+2];                                  // in fragment shader rx and ry radii are the second and third values of the Region thus 9*i+4 and 9*i+5
152
        mUniforms[NUM_UNIFORMS*i+5] = h*mBuf[4*i+3];                                  // 
153
     // mUniforms[NUM_UNIFORMS*i+6] =                                                 // this value is not used in Fragment Shader   
154
        mUniforms[NUM_UNIFORMS*i+7] = MVmatrix[0]*tx + MVmatrix[4]*ty + MVmatrix[12]; // multiply the ModelView matrix times the (x,y,0,1) point, i.e. the (x,y) point on the surface of the bitmap.
155
        mUniforms[NUM_UNIFORMS*i+8] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //  
156
        
157
        if( mType[i]==EffectNames.MACROBLOCK.ordinal() ) // fill up the .y and .z components of the Interpolated values already to avoid having to compute this in the fragment shader
158
          {
159
          mUniforms[NUM_UNIFORMS*i+1] = 2.0f*mObjHalfX/mUniforms[NUM_UNIFORMS*i];
160
          mUniforms[NUM_UNIFORMS*i+2] = 2.0f*mObjHalfY/mUniforms[NUM_UNIFORMS*i];
161
          }
162
        }
163
      }
164
    }
165
  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
       
168
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Interpolator2D point)
169
    {
170
    if( mMax[FRAGMENT]>mNumEffects )
171
      {
172
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
173
      mInterI[mNumEffects] = inter;
174
      mInterP[mNumEffects] = point;
175
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
176
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
177
   
178
      return addBase(eln); 
179
      }
180
      
181
    return -1;
182
    }
183
  
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185

    
186
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, float x, float y)
187
    {
188
    if( mMax[FRAGMENT]>mNumEffects )
189
      {
190
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
191
      mInterI[mNumEffects] = inter;
192
      mInterP[mNumEffects] = null;
193
      mBuf[4*mNumEffects  ] = x;
194
      mBuf[4*mNumEffects+1] = y;
195
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
196
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
197
   
198
      return addBase(eln);
199
      }
200
      
201
    return -1;
202
    }
203
  
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
       
206
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, Interpolator2D point)
207
    {
208
    if( mMax[FRAGMENT]>mNumEffects )
209
      {
210
      mInterI[mNumEffects] = inter;
211
      mInterP[mNumEffects] = point;
212
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
213
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
214
   
215
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
216
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
217
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
218
     
219
      return addBase(eln); 
220
      }
221
      
222
    return -1;
223
    }
224
  
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, float x, float y)
228
    {
229
    if( mMax[FRAGMENT]>mNumEffects )
230
      {
231
      mInterI[mNumEffects] = inter;
232
      mInterP[mNumEffects] = null;
233
      mBuf[4*mNumEffects  ] = x;
234
      mBuf[4*mNumEffects+1] = y;
235
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
236
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
237
      
238
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
239
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
240
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
241
   
242
      return addBase(eln);
243
      }
244
       
245
    return -1;
246
    }
247
  
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
       
250
  synchronized long add(EffectNames eln, float t, Float3D c, Float4D region, Interpolator2D point)
251
    {
252
    if( mMax[FRAGMENT]>mNumEffects )
253
      {
254
      mInterI[mNumEffects] = null;
255
      mInterP[mNumEffects] = point;
256
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
257
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
258
   
259
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
260
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
261
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
262
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
263
     
264
      return addBase(eln); 
265
      }
266
      
267
    return -1;
268
    }
269
  
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271

    
272
  synchronized long add(EffectNames eln, float t, Float3D c, Float4D region, float x, float y)
273
    {
274
    if( mMax[FRAGMENT]>mNumEffects )
275
      {
276
      mInterI[mNumEffects] = null;
277
      mInterP[mNumEffects] = null;
278
      mBuf[4*mNumEffects  ] = x;
279
      mBuf[4*mNumEffects+1] = y;
280
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
281
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
282
      
283
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
284
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
285
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
286
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
287
   
288
      return addBase(eln);
289
      }
290
      
291
    return -1;
292
    }
293
    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
// end of FragmentEffect   
296
  }
(9-9/29)