Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueFragment.java @ 8c893ffc

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
                                          mType[i], 
84
                                          mBitmapID,
85
                                          null);
86
      
87
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
88
          {
89
          remove(i);
90
          i--;
91
          continue;
92
          }
93
        }
94

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

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

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

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

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

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

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

    
158
        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.
159
        mUniforms[NUM_UNIFORMS*i+5] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //
160
        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
161
        mUniforms[NUM_UNIFORMS*i+7] = h*mBuf[4*i+3];                                  //
162

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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