Project

General

Profile

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

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

1 d333eb6b 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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22
import android.opengl.GLES20;
23
24 a4835695 Leszek Koltunski
import org.distorted.library.type.Float2D;
25
import org.distorted.library.type.Float3D;
26
import org.distorted.library.type.Float4D;
27
import org.distorted.library.type.Interpolator;
28
import org.distorted.library.type.Interpolator1D;
29
import org.distorted.library.type.Interpolator2D;
30
31 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
32
33 d07f2950 Leszek Koltunski
class EffectQueueFragment extends EffectQueue
34 6a06a912 Leszek Koltunski
  {
35 1e438fc7 Leszek Koltunski
  private static final int NUM_UNIFORMS = 9;
36
  private static final int INDEX = EffectTypes.FRAGMENT.ordinal();
37 6a06a912 Leszek Koltunski
  private float[] mBuf;
38
  private static int mNumEffectsH;
39
  private static int mTypeH;
40
  private static int mUniformsH;
41
  
42
///////////////////////////////////////////////////////////////////////////////////////////////////
43
   
44 d07f2950 Leszek Koltunski
  public EffectQueueFragment(DistortedObject obj)
45 6a06a912 Leszek Koltunski
    { 
46 1e438fc7 Leszek Koltunski
    super(obj,NUM_UNIFORMS,INDEX);
47 6a06a912 Leszek Koltunski
   
48 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>0 )
49 6a06a912 Leszek Koltunski
      {
50 1e438fc7 Leszek Koltunski
      mBuf= new float[4*mMax[INDEX]];
51 6a06a912 Leszek Koltunski
      }
52
    }
53
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
  static void getUniforms(int mProgramH)
57
    {
58
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "fNumEffects");
59
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "fType");
60
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "fUniforms");
61
    }
62
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64
  
65
  synchronized void compute(long currTime) 
66
    { 
67
    if( currTime==mTime ) return;
68
    if( mTime==0 ) mTime = currTime;
69
    long step = (currTime-mTime);
70
   
71
    for(int i=0; i<mNumEffects; i++)
72
      {
73
      if( mInterI[i]==null ) continue;    
74
      
75
      if( mInterP[i]!=null ) mInterP[i].interpolateMain(mBuf, 4*i, mCurrentDuration[i]);
76
        
77
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )      
78
        {
79
        for(int j=0; j<mNumListeners; j++)   
80
          EffectMessageSender.newMessage( mListeners.elementAt(j),
81
                                          EffectMessage.EFFECT_FINISHED, 
82 1e438fc7 Leszek Koltunski
                                          (mID[i]<<EffectTypes.LENGTH)+EffectTypes.FRAGMENT.type,
83 6a06a912 Leszek Koltunski
                                          mType[i], 
84 c6e1c219 Leszek Koltunski
                                          mBitmapID,
85
                                          null);
86 6a06a912 Leszek Koltunski
      
87
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
88
          {
89
          remove(i);
90
          i--;
91
          continue;
92
          }
93
        }
94
           
95
      mCurrentDuration[i] += step;
96
      }
97
   
98
    mTime = currTime;  
99
    }
100
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
103
  protected void moveEffect(int index)
104
    {
105
    mBuf[4*index  ] = mBuf[4*index+4];
106
    mBuf[4*index+1] = mBuf[4*index+5];
107
    mBuf[4*index+2] = mBuf[4*index+6];
108
    mBuf[4*index+3] = mBuf[4*index+7];
109
              
110
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
111
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
112
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];  
113
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];  
114
    }
115
  
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
  
118
  synchronized void send() 
119
    {
120
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
121
      
122
    if( mNumEffects>0 )
123
      {     
124
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
125
      GLES20.glUniform3fv( mUniformsH,3*mNumEffects, mUniforms,0);
126
      }  
127
    }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131
  synchronized void sendZero() 
132
    {
133
    GLES20.glUniform1i( mNumEffectsH, 0);
134
    }
135
    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137
// Do various post-processing on already computed effects.
138
// 1) move all Points and scale all Region radii by a ModelView matrix
139
// 2) in case of macroblock, pre-compute some values so that we don't have to do it in the fragment shader.
140
  
141
  void postprocess(float[] MVmatrix)
142
    {
143
    if( mNumEffects>0 )
144
      {
145
      float tx,ty;   
146
      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
147
      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.
148
   
149
      for(int i=0; i<mNumEffects; i++)
150
        {   
151
        tx = mBuf[4*i  ]-mObjHalfX; // we have to invert y and move everything by (half of bmp width, half of bmp height)
152
        ty =-mBuf[4*i+1]+mObjHalfY; //
153
      
154
        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
155
        mUniforms[NUM_UNIFORMS*i+5] = h*mBuf[4*i+3];                                  // 
156
     // mUniforms[NUM_UNIFORMS*i+6] =                                                 // this value is not used in Fragment Shader   
157
        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.
158
        mUniforms[NUM_UNIFORMS*i+8] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //  
159
        
160
        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
161
          {
162
          mUniforms[NUM_UNIFORMS*i+1] = 2.0f*mObjHalfX/mUniforms[NUM_UNIFORMS*i];
163
          mUniforms[NUM_UNIFORMS*i+2] = 2.0f*mObjHalfY/mUniforms[NUM_UNIFORMS*i];
164
          }
165
        }
166
      }
167
    }
168
  
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
       
171
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Interpolator2D point)
172
    {
173 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
174 6a06a912 Leszek Koltunski
      {
175
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
176
      mInterI[mNumEffects] = inter;
177
      mInterP[mNumEffects] = point;
178 a4835695 Leszek Koltunski
179
      if( region==null )
180
        {
181
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
182
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
183
        }
184
      else
185
        {
186
        float z = region.getZ();
187
        float w = region.getW();
188
189
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
190
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
191
        }
192
193 6a06a912 Leszek Koltunski
      return addBase(eln); 
194
      }
195
      
196
    return -1;
197
    }
198
  
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200
201 e0a16874 Leszek Koltunski
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Float2D point)
202 6a06a912 Leszek Koltunski
    {
203 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
204 6a06a912 Leszek Koltunski
      {
205
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
206
      mInterI[mNumEffects] = inter;
207
      mInterP[mNumEffects] = null;
208 a4835695 Leszek Koltunski
      mBuf[4*mNumEffects  ] = point.getX();
209
      mBuf[4*mNumEffects+1] = point.getY();
210
211
      if( region==null )
212
        {
213
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
214
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
215
        }
216
      else
217
        {
218
        float z = region.getZ();
219
        float w = region.getW();
220
221
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
222
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
223
        }
224
225 6a06a912 Leszek Koltunski
      return addBase(eln);
226
      }
227
      
228
    return -1;
229
    }
230
  
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
       
233
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, Interpolator2D point)
234
    {
235 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
236 6a06a912 Leszek Koltunski
      {
237
      mInterI[mNumEffects] = inter;
238
      mInterP[mNumEffects] = point;
239 a4835695 Leszek Koltunski
240
      if( region==null )
241
        {
242
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
243
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
244
        }
245
      else
246
        {
247
        float z = region.getZ();
248
        float w = region.getW();
249
250
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
251
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
252
        }
253
254
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
255
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
256
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
257 6a06a912 Leszek Koltunski
     
258
      return addBase(eln); 
259
      }
260
      
261
    return -1;
262
    }
263
  
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
266 e0a16874 Leszek Koltunski
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, Float2D point)
267 6a06a912 Leszek Koltunski
    {
268 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
269 6a06a912 Leszek Koltunski
      {
270
      mInterI[mNumEffects] = inter;
271
      mInterP[mNumEffects] = null;
272 a4835695 Leszek Koltunski
      mBuf[4*mNumEffects  ] = point.getX();
273
      mBuf[4*mNumEffects+1] = point.getY();
274
275
      if( region==null )
276
        {
277
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
278
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
279
        }
280
      else
281
        {
282
        float z = region.getZ();
283
        float w = region.getW();
284
285
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
286
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
287
        }
288
289
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
290
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
291
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
292 6a06a912 Leszek Koltunski
   
293
      return addBase(eln);
294
      }
295
       
296
    return -1;
297
    }
298
  
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300
       
301
  synchronized long add(EffectNames eln, float t, Float3D c, Float4D region, Interpolator2D point)
302
    {
303 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
304 6a06a912 Leszek Koltunski
      {
305
      mInterI[mNumEffects] = null;
306
      mInterP[mNumEffects] = point;
307 a4835695 Leszek Koltunski
308
      if( region==null )
309
        {
310
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
311
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
312
        }
313
      else
314
        {
315
        float z = region.getZ();
316
        float w = region.getW();
317
318
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
319
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
320
        }
321
322 6a06a912 Leszek Koltunski
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
323 a4835695 Leszek Koltunski
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
324
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
325
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
326 6a06a912 Leszek Koltunski
     
327
      return addBase(eln); 
328
      }
329
      
330
    return -1;
331
    }
332
  
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
335 e0a16874 Leszek Koltunski
  synchronized long add(EffectNames eln, float t, Float3D c, Float4D region, Float2D point)
336 6a06a912 Leszek Koltunski
    {
337 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
338 6a06a912 Leszek Koltunski
      {
339
      mInterI[mNumEffects] = null;
340
      mInterP[mNumEffects] = null;
341 a4835695 Leszek Koltunski
      mBuf[4*mNumEffects  ] = point.getX();
342
      mBuf[4*mNumEffects+1] = point.getY();
343
344
      if( region==null )
345
        {
346
        mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
347
        mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
348
        }
349
      else
350
        {
351
        float z = region.getZ();
352
        float w = region.getW();
353
354
        mBuf[4*mNumEffects+2] = z<=0.0f ? 1000*mObjHalfX : z;
355
        mBuf[4*mNumEffects+3] = w<=0.0f ? 1000*mObjHalfY : w;
356
        }
357
358 6a06a912 Leszek Koltunski
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
359 a4835695 Leszek Koltunski
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.getX();
360
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.getY();
361
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.getZ();
362 6a06a912 Leszek Koltunski
   
363
      return addBase(eln);
364
      }
365
      
366
    return -1;
367
    }
368
    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
// end of FragmentEffect   
371
  }