Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueFragment.java @ 42e08626

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

    
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
      }
164
    }
165
  
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
// alpha, brightness, contrast, saturation
168

    
169
  synchronized long add(EffectNames eln, Data1D data)
170
    {
171
    if( mMax[INDEX]>mNumEffects )
172
      {
173
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
174

    
175
      if( data instanceof Dynamic1D)
176
        mInter[0][mNumEffects] = (Dynamic1D)data;
177
      else if( data instanceof Static1D )
178
        {
179
        mInter[0][mNumEffects] = null;
180
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
181
        }
182
      else return -1;
183

    
184
      mInter[1][mNumEffects] = null;
185
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
186
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
187

    
188
      mInter[2][mNumEffects] = null;
189

    
190
      return addBase(eln); 
191
      }
192
      
193
    return -1;
194
    }
195
  
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
// alpha, brightness, contrast, saturation
198

    
199
  synchronized long add(EffectNames eln, Data1D data, Data4D region)
200
    {
201
    if( mMax[INDEX]>mNumEffects )
202
      {
203
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
204

    
205
      if( data instanceof Dynamic1D)
206
        mInter[0][mNumEffects] = (Dynamic1D)data;
207
      else if( data instanceof Static1D )
208
        {
209
        mInter[0][mNumEffects] = null;
210
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
211
        }
212
      else return -1;
213

    
214
      if( region instanceof Dynamic4D)
215
        mInter[1][mNumEffects] = (Dynamic4D)region;
216
      else if( region instanceof Static4D )
217
        {
218
        mInter[1][mNumEffects]  = null;
219
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
220
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
221
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
222
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
223
        }
224
      else return -1;
225

    
226
      mInter[2][mNumEffects] = null;
227

    
228
      return addBase(eln);
229
      }
230
      
231
    return -1;
232
    }
233
  
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// chroma
236

    
237
  synchronized long add(EffectNames eln, Data1D level, Data3D color, Data4D region)
238
    {
239
    if( mMax[INDEX]>mNumEffects )
240
      {
241
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
242

    
243
      if( level instanceof Dynamic1D)
244
        mInter[0][mNumEffects] = (Dynamic1D)level;
245
      else if( level instanceof Static1D )
246
        {
247
        mInter[0][mNumEffects] = null;
248
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
249
        }
250
      else return -1;
251

    
252
      if( color instanceof Dynamic3D)
253
        {
254
        mInter[2][mNumEffects] = (Dynamic3D)color;
255
        }
256
      else if( color instanceof Static3D )
257
        {
258
        mInter[2][mNumEffects] = null;
259
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)color).getX();
260
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)color).getY();
261
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static3D)color).getZ();
262
        }
263
      else return -1;
264

    
265
      if( region instanceof Dynamic4D)
266
        mInter[1][mNumEffects] = (Dynamic4D)region;
267
      else if( region instanceof Static4D )
268
        {
269
        mInter[1][mNumEffects]  = null;
270
        mBuf[4*mNumEffects  ] = ((Static4D)region).getX();
271
        mBuf[4*mNumEffects+1] = ((Static4D)region).getY();
272
        mBuf[4*mNumEffects+2] = ((Static4D)region).getZ();
273
        mBuf[4*mNumEffects+3] = ((Static4D)region).getW();
274
        }
275
      else return -1;
276

    
277
      return addBase(eln); 
278
      }
279
      
280
    return -1;
281
    }
282
  
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284
// chroma
285

    
286
  synchronized long add(EffectNames eln, Data1D level, Data3D color)
287
    {
288
    if( mMax[INDEX]>mNumEffects )
289
      {
290
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
291

    
292
      if( level instanceof Dynamic1D)
293
        mInter[0][mNumEffects] = (Dynamic1D)level;
294
      else if( level instanceof Static1D )
295
        {
296
        mInter[0][mNumEffects] = null;
297
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
298
        }
299
      else return -1;
300

    
301
      if( color instanceof Dynamic3D)
302
        {
303
        mInter[2][mNumEffects] = (Dynamic3D)color;
304
        }
305
      else if( color instanceof Static3D )
306
        {
307
        mInter[2][mNumEffects] = null;
308
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)color).getX();
309
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)color).getY();
310
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static3D)color).getZ();
311
        }
312
      else return -1;
313

    
314
      mInter[1][mNumEffects]  = null;          //
315
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;  // i.e. null region
316
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;  //
317

    
318
      return addBase(eln);
319
      }
320
       
321
    return -1;
322
    }
323

    
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
// end of FragmentEffect   
326
  }
(15-15/18)