Project

General

Profile

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

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

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
///////////////////////////////////////////////////////////////////////////////////////////////////
25

    
26
class EffectQueueFragment extends EffectQueue
27
  {
28
  private static final int NUM_UNIFORMS = 9;
29
  private static final int INDEX = EffectTypes.FRAGMENT.ordinal();
30
  private float[] mBuf;
31
  private static int mNumEffectsH;
32
  private static int mTypeH;
33
  private static int mUniformsH;
34
  
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
   
37
  public EffectQueueFragment(DistortedObject obj)
38
    { 
39
    super(obj,NUM_UNIFORMS,INDEX);
40
   
41
    if( mMax[INDEX]>0 )
42
      {
43
      mBuf= new float[4*mMax[INDEX]];
44
      }
45
    }
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  static void getUniforms(int mProgramH)
50
    {
51
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "fNumEffects");
52
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "fType");
53
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "fUniforms");
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
  
58
  synchronized void compute(long currTime) 
59
    { 
60
    if( currTime==mTime ) return;
61
    if( mTime==0 ) mTime = currTime;
62
    long step = (currTime-mTime);
63
   
64
    for(int i=0; i<mNumEffects; i++)
65
      {
66
      if( mInterI[i]==null ) continue;    
67
      
68
      if( mInterP[i]!=null ) mInterP[i].interpolateMain(mBuf, 4*i, mCurrentDuration[i]);
69
        
70
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )      
71
        {
72
        for(int j=0; j<mNumListeners; j++)   
73
          EffectMessageSender.newMessage( mListeners.elementAt(j),
74
                                          EffectMessage.EFFECT_FINISHED, 
75
                                          (mID[i]<<EffectTypes.LENGTH)+EffectTypes.FRAGMENT.type,
76
                                          mType[i], 
77
                                          mBitmapID,
78
                                          null);
79
      
80
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
81
          {
82
          remove(i);
83
          i--;
84
          continue;
85
          }
86
        }
87
           
88
      mCurrentDuration[i] += step;
89
      }
90
   
91
    mTime = currTime;  
92
    }
93

    
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95

    
96
  protected void moveEffect(int index)
97
    {
98
    mBuf[4*index  ] = mBuf[4*index+4];
99
    mBuf[4*index+1] = mBuf[4*index+5];
100
    mBuf[4*index+2] = mBuf[4*index+6];
101
    mBuf[4*index+3] = mBuf[4*index+7];
102
              
103
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
104
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
105
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];  
106
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];  
107
    }
108
  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
  
111
  synchronized void send() 
112
    {
113
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
114
      
115
    if( mNumEffects>0 )
116
      {     
117
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
118
      GLES20.glUniform3fv( mUniformsH,3*mNumEffects, mUniforms,0);
119
      }  
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  synchronized void sendZero() 
125
    {
126
    GLES20.glUniform1i( mNumEffectsH, 0);
127
    }
128
    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// Do various post-processing on already computed effects.
131
// 1) move all Points and scale all Region radii by a ModelView matrix
132
// 2) in case of macroblock, pre-compute some values so that we don't have to do it in the fragment shader.
133
  
134
  void postprocess(float[] MVmatrix)
135
    {
136
    if( mNumEffects>0 )
137
      {
138
      float tx,ty;   
139
      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
140
      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.
141
   
142
      for(int i=0; i<mNumEffects; i++)
143
        {   
144
        tx = mBuf[4*i  ]-mObjHalfX; // we have to invert y and move everything by (half of bmp width, half of bmp height)
145
        ty =-mBuf[4*i+1]+mObjHalfY; //
146
      
147
        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
148
        mUniforms[NUM_UNIFORMS*i+5] = h*mBuf[4*i+3];                                  // 
149
     // mUniforms[NUM_UNIFORMS*i+6] =                                                 // this value is not used in Fragment Shader   
150
        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.
151
        mUniforms[NUM_UNIFORMS*i+8] = MVmatrix[1]*tx + MVmatrix[5]*ty + MVmatrix[13]; //  
152
        
153
        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
154
          {
155
          mUniforms[NUM_UNIFORMS*i+1] = 2.0f*mObjHalfX/mUniforms[NUM_UNIFORMS*i];
156
          mUniforms[NUM_UNIFORMS*i+2] = 2.0f*mObjHalfY/mUniforms[NUM_UNIFORMS*i];
157
          }
158
        }
159
      }
160
    }
161
  
162
///////////////////////////////////////////////////////////////////////////////////////////////////
163
       
164
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Interpolator2D point)
165
    {
166
    if( mMax[INDEX]>mNumEffects )
167
      {
168
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
169
      mInterI[mNumEffects] = inter;
170
      mInterP[mNumEffects] = point;
171
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
172
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
173
   
174
      return addBase(eln); 
175
      }
176
      
177
    return -1;
178
    }
179
  
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, float x, float y)
183
    {
184
    if( mMax[INDEX]>mNumEffects )
185
      {
186
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
187
      mInterI[mNumEffects] = inter;
188
      mInterP[mNumEffects] = null;
189
      mBuf[4*mNumEffects  ] = x;
190
      mBuf[4*mNumEffects+1] = y;
191
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
192
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
193
   
194
      return addBase(eln);
195
      }
196
      
197
    return -1;
198
    }
199
  
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201
       
202
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, Interpolator2D point)
203
    {
204
    if( mMax[INDEX]>mNumEffects )
205
      {
206
      mInterI[mNumEffects] = inter;
207
      mInterP[mNumEffects] = point;
208
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
209
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
210
   
211
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
212
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
213
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
214
     
215
      return addBase(eln); 
216
      }
217
      
218
    return -1;
219
    }
220
  
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  synchronized long add(EffectNames eln, Interpolator1D inter, Float3D c, Float4D region, float x, float y)
224
    {
225
    if( mMax[INDEX]>mNumEffects )
226
      {
227
      mInterI[mNumEffects] = inter;
228
      mInterP[mNumEffects] = null;
229
      mBuf[4*mNumEffects  ] = x;
230
      mBuf[4*mNumEffects+1] = y;
231
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
232
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
233
      
234
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
235
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
236
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
237
   
238
      return addBase(eln);
239
      }
240
       
241
    return -1;
242
    }
243
  
244
///////////////////////////////////////////////////////////////////////////////////////////////////
245
       
246
  synchronized long add(EffectNames eln, float t, Float3D c, Float4D region, Interpolator2D point)
247
    {
248
    if( mMax[INDEX]>mNumEffects )
249
      {
250
      mInterI[mNumEffects] = null;
251
      mInterP[mNumEffects] = point;
252
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
253
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
254
   
255
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
256
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
257
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
258
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
259
     
260
      return addBase(eln); 
261
      }
262
      
263
    return -1;
264
    }
265
  
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267

    
268
  synchronized long add(EffectNames eln, float t, Float3D c, Float4D region, float x, float y)
269
    {
270
    if( mMax[INDEX]>mNumEffects )
271
      {
272
      mInterI[mNumEffects] = null;
273
      mInterP[mNumEffects] = null;
274
      mBuf[4*mNumEffects  ] = x;
275
      mBuf[4*mNumEffects+1] = y;
276
      mBuf[4*mNumEffects+2] = (region==null || region.z<=0.0f) ? 1000*mObjHalfX : region.z;
277
      mBuf[4*mNumEffects+3] = (region==null || region.w<=0.0f) ? 1000*mObjHalfY : region.w;
278
      
279
      mUniforms[NUM_UNIFORMS*mNumEffects+0] = t;
280
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = c.x;
281
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = c.y;
282
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = c.z;
283
   
284
      return addBase(eln);
285
      }
286
      
287
    return -1;
288
    }
289
    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
// end of FragmentEffect   
292
  }
(13-13/30)