Project

General

Profile

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

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

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
      
60
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
61
          {
62
          remove(i);
63
          i--;
64
          continue;
65
          }
66
        }
67
           
68
      mCurrentDuration[i] += step;
69
      }
70
   
71
    mTime = currTime;  
72
    }
73

    
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

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

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

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

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