Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueFragment.java @ b1e91f2c

1
package org.distorted.library;
2

    
3
import android.opengl.GLES20;
4

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

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

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

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

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

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

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

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

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