Project

General

Profile

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

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

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 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectMessage;
25 2fce34f4 Leszek Koltunski
import org.distorted.library.type.Data1D;
26 8c893ffc Leszek Koltunski
import org.distorted.library.type.Data3D;
27 2fce34f4 Leszek Koltunski
import org.distorted.library.type.Data4D;
28 568b29d8 Leszek Koltunski
import org.distorted.library.type.Dynamic1D;
29 8c893ffc Leszek Koltunski
import org.distorted.library.type.Dynamic3D;
30 2fce34f4 Leszek Koltunski
import org.distorted.library.type.Dynamic4D;
31
import org.distorted.library.type.Static1D;
32 568b29d8 Leszek Koltunski
import org.distorted.library.type.Static3D;
33
import org.distorted.library.type.Static4D;
34 a4835695 Leszek Koltunski
35 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37 d07f2950 Leszek Koltunski
class EffectQueueFragment extends EffectQueue
38 6a06a912 Leszek Koltunski
  {
39 2fce34f4 Leszek Koltunski
  private static final int NUM_UNIFORMS = 8;
40 1e438fc7 Leszek Koltunski
  private static final int INDEX = EffectTypes.FRAGMENT.ordinal();
41 6a06a912 Leszek Koltunski
  private float[] mBuf;
42
  private static int mNumEffectsH;
43
  private static int mTypeH;
44
  private static int mUniformsH;
45
  
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
   
48 d07f2950 Leszek Koltunski
  public EffectQueueFragment(DistortedObject obj)
49 6a06a912 Leszek Koltunski
    { 
50 1e438fc7 Leszek Koltunski
    super(obj,NUM_UNIFORMS,INDEX);
51 6a06a912 Leszek Koltunski
   
52 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>0 )
53 6a06a912 Leszek Koltunski
      {
54 1e438fc7 Leszek Koltunski
      mBuf= new float[4*mMax[INDEX]];
55 6a06a912 Leszek Koltunski
      }
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 a595ee16 Leszek Koltunski
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
78 6a06a912 Leszek Koltunski
        {
79
        for(int j=0; j<mNumListeners; j++)   
80
          EffectMessageSender.newMessage( mListeners.elementAt(j),
81 e458a4ba Leszek Koltunski
                                          EffectMessage.EFFECT_FINISHED,
82 1e438fc7 Leszek Koltunski
                                          (mID[i]<<EffectTypes.LENGTH)+EffectTypes.FRAGMENT.type,
83 e8c81a8e Leszek Koltunski
                                          mName[i],
84 c6e1c219 Leszek Koltunski
                                          mBitmapID,
85
                                          null);
86 6a06a912 Leszek Koltunski
      
87 e8c81a8e Leszek Koltunski
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
88 6a06a912 Leszek Koltunski
          {
89
          remove(i);
90
          i--;
91
          continue;
92
          }
93 e8c81a8e Leszek Koltunski
        else mInter[0][i] = null;
94 6a06a912 Leszek Koltunski
        }
95 a595ee16 Leszek Koltunski
96 8c893ffc Leszek Koltunski
      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 a595ee16 Leszek Koltunski
100 6a06a912 Leszek Koltunski
      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 e8c81a8e Leszek Koltunski
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName,0);
130 2fce34f4 Leszek Koltunski
      GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
131 6a06a912 Leszek Koltunski
      }  
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 2fce34f4 Leszek Koltunski
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 e8c81a8e Leszek Koltunski
        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 6a06a912 Leszek Koltunski
          {
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 2fce34f4 Leszek Koltunski
// macroblock, alpha, brightness, contrast, saturation
175
176
  synchronized long add(EffectNames eln, Data1D data)
177 6a06a912 Leszek Koltunski
    {
178 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
179 6a06a912 Leszek Koltunski
      {
180
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
181 a4835695 Leszek Koltunski
182 2fce34f4 Leszek Koltunski
      if( data instanceof Dynamic1D)
183 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = (Dynamic1D)data;
184 2fce34f4 Leszek Koltunski
      else if( data instanceof Static1D )
185 a4835695 Leszek Koltunski
        {
186 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = null;
187 2fce34f4 Leszek Koltunski
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
188 a4835695 Leszek Koltunski
        }
189 2fce34f4 Leszek Koltunski
      else return -1;
190 a4835695 Leszek Koltunski
191 d425545a Leszek Koltunski
      mInter[1][mNumEffects] = null;
192 2fce34f4 Leszek Koltunski
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;
193
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;
194 a4835695 Leszek Koltunski
195 8c893ffc Leszek Koltunski
      mInter[2][mNumEffects] = null;
196
197 6a06a912 Leszek Koltunski
      return addBase(eln); 
198
      }
199
      
200
    return -1;
201
    }
202
  
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204 2fce34f4 Leszek Koltunski
// macroblock, alpha, brightness, contrast, saturation
205 6a06a912 Leszek Koltunski
206 2fce34f4 Leszek Koltunski
  synchronized long add(EffectNames eln, Data1D data, Data4D region)
207 6a06a912 Leszek Koltunski
    {
208 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
209 6a06a912 Leszek Koltunski
      {
210 2fce34f4 Leszek Koltunski
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
211 a4835695 Leszek Koltunski
212 2fce34f4 Leszek Koltunski
      if( data instanceof Dynamic1D)
213 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = (Dynamic1D)data;
214 2fce34f4 Leszek Koltunski
      else if( data instanceof Static1D )
215 a4835695 Leszek Koltunski
        {
216 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = null;
217 2fce34f4 Leszek Koltunski
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
218 a4835695 Leszek Koltunski
        }
219 2fce34f4 Leszek Koltunski
      else return -1;
220 a4835695 Leszek Koltunski
221 2fce34f4 Leszek Koltunski
      if( region instanceof Dynamic4D)
222 d425545a Leszek Koltunski
        mInter[1][mNumEffects] = (Dynamic4D)region;
223 2fce34f4 Leszek Koltunski
      else if( region instanceof Static4D )
224
        {
225 d425545a Leszek Koltunski
        mInter[1][mNumEffects]  = null;
226 2fce34f4 Leszek Koltunski
        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 a4835695 Leszek Koltunski
        }
231 2fce34f4 Leszek Koltunski
      else return -1;
232 a4835695 Leszek Koltunski
233 8c893ffc Leszek Koltunski
      mInter[2][mNumEffects] = null;
234
235 6a06a912 Leszek Koltunski
      return addBase(eln);
236
      }
237
      
238
    return -1;
239
    }
240
  
241
///////////////////////////////////////////////////////////////////////////////////////////////////
242 2fce34f4 Leszek Koltunski
// chroma
243
244 8c893ffc Leszek Koltunski
  synchronized long add(EffectNames eln, Data1D level, Data3D color, Data4D region)
245 6a06a912 Leszek Koltunski
    {
246 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
247 6a06a912 Leszek Koltunski
      {
248 2fce34f4 Leszek Koltunski
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
249 a4835695 Leszek Koltunski
250 2fce34f4 Leszek Koltunski
      if( level instanceof Dynamic1D)
251 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = (Dynamic1D)level;
252 2fce34f4 Leszek Koltunski
      else if( level instanceof Static1D )
253 a4835695 Leszek Koltunski
        {
254 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = null;
255 2fce34f4 Leszek Koltunski
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
256 a4835695 Leszek Koltunski
        }
257 2fce34f4 Leszek Koltunski
      else return -1;
258 a4835695 Leszek Koltunski
259 8c893ffc Leszek Koltunski
      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 2fce34f4 Leszek Koltunski
272
      if( region instanceof Dynamic4D)
273 d425545a Leszek Koltunski
        mInter[1][mNumEffects] = (Dynamic4D)region;
274 2fce34f4 Leszek Koltunski
      else if( region instanceof Static4D )
275
        {
276 d425545a Leszek Koltunski
        mInter[1][mNumEffects]  = null;
277 2fce34f4 Leszek Koltunski
        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 a4835695 Leszek Koltunski
        }
282 2fce34f4 Leszek Koltunski
      else return -1;
283 a4835695 Leszek Koltunski
284 6a06a912 Leszek Koltunski
      return addBase(eln); 
285
      }
286
      
287
    return -1;
288
    }
289
  
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291 2fce34f4 Leszek Koltunski
// chroma
292 6a06a912 Leszek Koltunski
293 8c893ffc Leszek Koltunski
  synchronized long add(EffectNames eln, Data1D level, Data3D color)
294 6a06a912 Leszek Koltunski
    {
295 1e438fc7 Leszek Koltunski
    if( mMax[INDEX]>mNumEffects )
296 6a06a912 Leszek Koltunski
      {
297 2fce34f4 Leszek Koltunski
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
298 a4835695 Leszek Koltunski
299 2fce34f4 Leszek Koltunski
      if( level instanceof Dynamic1D)
300 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = (Dynamic1D)level;
301 2fce34f4 Leszek Koltunski
      else if( level instanceof Static1D )
302 a4835695 Leszek Koltunski
        {
303 d425545a Leszek Koltunski
        mInter[0][mNumEffects] = null;
304 2fce34f4 Leszek Koltunski
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)level).getX();
305 a4835695 Leszek Koltunski
        }
306 2fce34f4 Leszek Koltunski
      else return -1;
307 a4835695 Leszek Koltunski
308 8c893ffc Leszek Koltunski
      if( color instanceof Dynamic3D)
309 a4835695 Leszek Koltunski
        {
310 8c893ffc Leszek Koltunski
        mInter[2][mNumEffects] = (Dynamic3D)color;
311 a4835695 Leszek Koltunski
        }
312 8c893ffc Leszek Koltunski
      else if( color instanceof Static3D )
313 a4835695 Leszek Koltunski
        {
314 8c893ffc Leszek Koltunski
        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 a4835695 Leszek Koltunski
        }
319 2fce34f4 Leszek Koltunski
      else return -1;
320 a4835695 Leszek Koltunski
321 d425545a Leszek Koltunski
      mInter[1][mNumEffects]  = null;          //
322 2fce34f4 Leszek Koltunski
      mBuf[4*mNumEffects+2] = 1000*mObjHalfX;  // i.e. null region
323
      mBuf[4*mNumEffects+3] = 1000*mObjHalfY;  //
324 a4835695 Leszek Koltunski
325 6a06a912 Leszek Koltunski
      return addBase(eln);
326
      }
327 8c893ffc Leszek Koltunski
       
328 6a06a912 Leszek Koltunski
    return -1;
329
    }
330 8c893ffc Leszek Koltunski
331 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
332
// end of FragmentEffect   
333
  }