Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library;
21

    
22
import android.opengl.GLES20;
23

    
24
import org.distorted.library.message.EffectMessage;
25
import org.distorted.library.type.Data1D;
26
import org.distorted.library.type.Data3D;
27
import org.distorted.library.type.Data4D;
28
import org.distorted.library.type.Dynamic1D;
29
import org.distorted.library.type.Dynamic3D;
30
import org.distorted.library.type.Dynamic4D;
31
import org.distorted.library.type.Static1D;
32
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
class EffectQueueFragment extends EffectQueue
38
  {
39
  private static final int NUM_UNIFORMS = 8;
40
  private static final int INDEX = EffectTypes.FRAGMENT.ordinal();
41
  private float[] mBuf;
42
  private static int mNumEffectsH;
43
  private static int mTypeH;
44
  private static int mUniformsH;
45
  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
   
48
  public EffectQueueFragment(DistortedObject obj)
49
    { 
50
    super(obj,NUM_UNIFORMS,INDEX);
51
   
52
    if( mMax[INDEX]>0 )
53
      {
54
      mBuf= new float[4*mMax[INDEX]];
55
      }
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  static void getUniforms(int mProgramH)
61
    {
62
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "fNumEffects");
63
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "fType");
64
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "fUniforms");
65
    }
66

    
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
  
69
  synchronized void compute(long currTime) 
70
    { 
71
    if( currTime==mTime ) return;
72
    if( mTime==0 ) mTime = currTime;
73
    long step = (currTime-mTime);
74
   
75
    for(int i=0; i<mNumEffects; i++)
76
      {
77
      if( mInter[0][i]!=null && mInter[0][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
                                          (mID[i]<<EffectTypes.LENGTH)+EffectTypes.FRAGMENT.type,
83
                                          mName[i],
84
                                          mBitmapID,
85
                                          null);
86
      
87
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
88
          {
89
          remove(i);
90
          i--;
91
          continue;
92
          }
93
        else mInter[0][i] = null;
94
        }
95

    
96
      if( mInter[1][i]!=null ) mInter[1][i].interpolateMain(     mBuf,            4*i  , mCurrentDuration[i]);
97

    
98
      if( mInter[2][i]!=null ) mInter[2][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i]);
99

    
100
      mCurrentDuration[i] += step;
101
      }
102
   
103
    mTime = currTime;  
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  protected void moveEffect(int index)
109
    {
110
    mBuf[4*index  ] = mBuf[4*index+4];
111
    mBuf[4*index+1] = mBuf[4*index+5];
112
    mBuf[4*index+2] = mBuf[4*index+6];
113
    mBuf[4*index+3] = mBuf[4*index+7];
114
              
115
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
116
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
117
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];  
118
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];  
119
    }
120
  
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
  
123
  synchronized void send() 
124
    {
125
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
126
      
127
    if( mNumEffects>0 )
128
      {     
129
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName,0);
130
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
131
      }  
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  synchronized void sendZero() 
137
    {
138
    GLES20.glUniform1i( mNumEffectsH, 0);
139
    }
140
    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142
// Do various post-processing on already computed effects.
143
// 1) move all Points and scale all Region radii by a ModelView matrix
144
// 2) in case of macroblock, pre-compute some values so that we don't have to do it in the fragment shader.
145
  
146
  void postprocess(float[] MVmatrix)
147
    {
148
    if( mNumEffects>0 )
149
      {
150
      float tx,ty;   
151
      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
152
      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.
153
   
154
      for(int i=0; i<mNumEffects; i++)
155
        {   
156
        tx = mBuf[4*i  ]-mObjHalfX; // we have to invert y and move everything by (half of bmp width, half of bmp height)
157
        ty =-mBuf[4*i+1]+mObjHalfY; //
158

    
159
        mUniforms[NUM_UNIFORMS*i+4] = 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.
160
        mUniforms[NUM_UNIFORMS*i+5] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //
161
        mUniforms[NUM_UNIFORMS*i+6] = w*mBuf[4*i+2];                                  // in fragment shader rx and ry radii are the last two values of the second vec4
162
        mUniforms[NUM_UNIFORMS*i+7] = h*mBuf[4*i+3];                                  //
163

    
164
        if( mName[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
165
          {
166
          mUniforms[NUM_UNIFORMS*i+1] = 2.0f*mObjHalfX/mUniforms[NUM_UNIFORMS*i];
167
          mUniforms[NUM_UNIFORMS*i+2] = 2.0f*mObjHalfY/mUniforms[NUM_UNIFORMS*i];
168
          }
169
        }
170
      }
171
    }
172
  
173
///////////////////////////////////////////////////////////////////////////////////////////////////
174
// macroblock, alpha, brightness, contrast, saturation
175

    
176
  synchronized long add(EffectNames eln, Data1D data)
177
    {
178
    if( mMax[INDEX]>mNumEffects )
179
      {
180
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
181

    
182
      if( data instanceof Dynamic1D)
183
        mInter[0][mNumEffects] = (Dynamic1D)data;
184
      else if( data instanceof Static1D )
185
        {
186
        mInter[0][mNumEffects] = null;
187
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
188
        }
189
      else return -1;
190

    
191
      mInter[1][mNumEffects] = null;
192
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
193
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
194

    
195
      mInter[2][mNumEffects] = null;
196

    
197
      return addBase(eln); 
198
      }
199
      
200
    return -1;
201
    }
202
  
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
// macroblock, alpha, brightness, contrast, saturation
205

    
206
  synchronized long add(EffectNames eln, Data1D data, Data4D region)
207
    {
208
    if( mMax[INDEX]>mNumEffects )
209
      {
210
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
211

    
212
      if( data instanceof Dynamic1D)
213
        mInter[0][mNumEffects] = (Dynamic1D)data;
214
      else if( data instanceof Static1D )
215
        {
216
        mInter[0][mNumEffects] = null;
217
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
218
        }
219
      else return -1;
220

    
221
      if( region instanceof Dynamic4D)
222
        mInter[1][mNumEffects] = (Dynamic4D)region;
223
      else if( region instanceof Static4D )
224
        {
225
        mInter[1][mNumEffects]  = null;
226
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
227
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
228
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
229
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
230
        }
231
      else return -1;
232

    
233
      mInter[2][mNumEffects] = null;
234

    
235
      return addBase(eln);
236
      }
237
      
238
    return -1;
239
    }
240
  
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242
// chroma
243

    
244
  synchronized long add(EffectNames eln, Data1D level, Data3D color, Data4D region)
245
    {
246
    if( mMax[INDEX]>mNumEffects )
247
      {
248
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
249

    
250
      if( level instanceof Dynamic1D)
251
        mInter[0][mNumEffects] = (Dynamic1D)level;
252
      else if( level instanceof Static1D )
253
        {
254
        mInter[0][mNumEffects] = null;
255
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
256
        }
257
      else return -1;
258

    
259
      if( color instanceof Dynamic3D)
260
        {
261
        mInter[2][mNumEffects] = (Dynamic3D)color;
262
        }
263
      else if( color instanceof Static3D )
264
        {
265
        mInter[2][mNumEffects] = null;
266
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)color).getX();
267
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)color).getY();
268
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static3D)color).getZ();
269
        }
270
      else return -1;
271

    
272
      if( region instanceof Dynamic4D)
273
        mInter[1][mNumEffects] = (Dynamic4D)region;
274
      else if( region instanceof Static4D )
275
        {
276
        mInter[1][mNumEffects]  = null;
277
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
278
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
279
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
280
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
281
        }
282
      else return -1;
283

    
284
      return addBase(eln); 
285
      }
286
      
287
    return -1;
288
    }
289
  
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
// chroma
292

    
293
  synchronized long add(EffectNames eln, Data1D level, Data3D color)
294
    {
295
    if( mMax[INDEX]>mNumEffects )
296
      {
297
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
298

    
299
      if( level instanceof Dynamic1D)
300
        mInter[0][mNumEffects] = (Dynamic1D)level;
301
      else if( level instanceof Static1D )
302
        {
303
        mInter[0][mNumEffects] = null;
304
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
305
        }
306
      else return -1;
307

    
308
      if( color instanceof Dynamic3D)
309
        {
310
        mInter[2][mNumEffects] = (Dynamic3D)color;
311
        }
312
      else if( color instanceof Static3D )
313
        {
314
        mInter[2][mNumEffects] = null;
315
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)color).getX();
316
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)color).getY();
317
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static3D)color).getZ();
318
        }
319
      else return -1;
320

    
321
      mInter[1][mNumEffects]  = null;          //
322
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;  // i.e. null region
323
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;  //
324

    
325
      return addBase(eln);
326
      }
327
       
328
    return -1;
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
// end of FragmentEffect   
333
  }
(14-14/17)