Project

General

Profile

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

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

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

    
30
  static void getUniforms(int mProgramH)
31
    {
32
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "fNumEffects");
33
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "fType");
34
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "fUniforms");
35
    }
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
  
39
  synchronized void compute(long currTime) 
40
    { 
41
    if( currTime==mTime ) return;
42
    if( mTime==0 ) mTime = currTime;
43
    long step = (currTime-mTime);
44
   
45
    for(int i=0; i<mNumEffects; i++)
46
      {
47
      if( mInterI[i]==null ) continue;    
48
      
49
      if( mInterP[i]!=null ) mInterP[i].interpolateMain(mBuf, 4*i, mCurrentDuration[i]);
50
        
51
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )      
52
        {
53
        for(int j=0; j<mNumListeners; j++)   
54
          EffectMessageSender.newMessage( mListeners.elementAt(j),
55
                                          EffectMessage.EFFECT_FINISHED, 
56
                                          (mID[i]<<EffectTypes.LENGTH)+EffectTypes.FRAGMENT.type,
57
                                          mType[i], 
58
                                          mBitmapID,
59
                                          null);
60
      
61
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
62
          {
63
          remove(i);
64
          i--;
65
          continue;
66
          }
67
        }
68
           
69
      mCurrentDuration[i] += step;
70
      }
71
   
72
    mTime = currTime;  
73
    }
74

    
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76

    
77
  protected void moveEffect(int index)
78
    {
79
    mBuf[4*index  ] = mBuf[4*index+4];
80
    mBuf[4*index+1] = mBuf[4*index+5];
81
    mBuf[4*index+2] = mBuf[4*index+6];
82
    mBuf[4*index+3] = mBuf[4*index+7];
83
              
84
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
85
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
86
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];  
87
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];  
88
    }
89
  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
  
92
  synchronized void send() 
93
    {
94
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
95
      
96
    if( mNumEffects>0 )
97
      {     
98
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
99
      GLES20.glUniform3fv( mUniformsH,3*mNumEffects, mUniforms,0);
100
      }  
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  synchronized void sendZero() 
106
    {
107
    GLES20.glUniform1i( mNumEffectsH, 0);
108
    }
109
    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
// Do various post-processing on already computed effects.
112
// 1) move all Points and scale all Region radii by a ModelView matrix
113
// 2) in case of macroblock, pre-compute some values so that we don't have to do it in the fragment shader.
114
  
115
  void postprocess(float[] MVmatrix)
116
    {
117
    if( mNumEffects>0 )
118
      {
119
      float tx,ty;   
120
      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
121
      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.
122
   
123
      for(int i=0; i<mNumEffects; i++)
124
        {   
125
        tx = mBuf[4*i  ]-mObjHalfX; // we have to invert y and move everything by (half of bmp width, half of bmp height)
126
        ty =-mBuf[4*i+1]+mObjHalfY; //
127
      
128
        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
129
        mUniforms[NUM_UNIFORMS*i+5] = h*mBuf[4*i+3];                                  // 
130
     // mUniforms[NUM_UNIFORMS*i+6] =                                                 // this value is not used in Fragment Shader   
131
        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.
132
        mUniforms[NUM_UNIFORMS*i+8] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //  
133
        
134
        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
135
          {
136
          mUniforms[NUM_UNIFORMS*i+1] = 2.0f*mObjHalfX/mUniforms[NUM_UNIFORMS*i];
137
          mUniforms[NUM_UNIFORMS*i+2] = 2.0f*mObjHalfY/mUniforms[NUM_UNIFORMS*i];
138
          }
139
        }
140
      }
141
    }
142
  
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144
       
145
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Interpolator2D point)
146
    {
147
    if( mMax[INDEX]>mNumEffects )
148
      {
149
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
150
      mInterI[mNumEffects] = inter;
151
      mInterP[mNumEffects] = point;
152
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
153
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
154
   
155
      return addBase(eln); 
156
      }
157
      
158
    return -1;
159
    }
160
  
161
///////////////////////////////////////////////////////////////////////////////////////////////////
162

    
163
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, float x, float y)
164
    {
165
    if( mMax[INDEX]>mNumEffects )
166
      {
167
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
168
      mInterI[mNumEffects] = inter;
169
      mInterP[mNumEffects] = null;
170
      mBuf[4*mNumEffects  ] = x;
171
      mBuf[4*mNumEffects+1] = y;
172
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
173
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
174
   
175
      return addBase(eln);
176
      }
177
      
178
    return -1;
179
    }
180
  
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
       
183
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, Interpolator2D point)
184
    {
185
    if( mMax[INDEX]>mNumEffects )
186
      {
187
      mInterI[mNumEffects] = inter;
188
      mInterP[mNumEffects] = point;
189
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
190
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
191
   
192
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
193
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
194
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
195
     
196
      return addBase(eln); 
197
      }
198
      
199
    return -1;
200
    }
201
  
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, float x, float y)
205
    {
206
    if( mMax[INDEX]>mNumEffects )
207
      {
208
      mInterI[mNumEffects] = inter;
209
      mInterP[mNumEffects] = null;
210
      mBuf[4*mNumEffects  ] = x;
211
      mBuf[4*mNumEffects+1] = y;
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, float t, Float3D c, Float4D region, Interpolator2D point)
228
    {
229
    if( mMax[INDEX]>mNumEffects )
230
      {
231
      mInterI[mNumEffects] = null;
232
      mInterP[mNumEffects] = point;
233
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
234
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
235
   
236
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
237
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
238
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
239
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
240
     
241
      return addBase(eln); 
242
      }
243
      
244
    return -1;
245
    }
246
  
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  synchronized long add(EffectNames eln, float t, Float3D c, Float4D region, float x, float y)
250
    {
251
    if( mMax[INDEX]>mNumEffects )
252
      {
253
      mInterI[mNumEffects] = null;
254
      mInterP[mNumEffects] = null;
255
      mBuf[4*mNumEffects  ] = x;
256
      mBuf[4*mNumEffects+1] = y;
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
// end of FragmentEffect   
273
  }
(13-13/30)