Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueVertex.java @ d07f2950

1
package org.distorted.library;
2

    
3
import android.opengl.GLES20;
4

    
5
///////////////////////////////////////////////////////////////////////////////////////////////////
6

    
7
class EffectQueueVertex extends EffectQueue
8
  { 
9
  private static final int NUM_UNIFORMS = 9;
10
  private static final int INDEX = EffectTypes.VERTEX.ordinal();
11
  private static int mNumEffectsH;
12
  private static int mTypeH;
13
  private static int mUniformsH;
14
  
15
///////////////////////////////////////////////////////////////////////////////////////////////////
16
   
17
  public EffectQueueVertex(DistortedObject obj)
18
    { 
19
    super(obj,NUM_UNIFORMS,INDEX);
20
    }
21
  
22
///////////////////////////////////////////////////////////////////////////////////////////////////
23
// Only max Byte.MAX_VALUE concurrent effects per bitmap.
24
// If you want more, change type of the mNumEffects, mIDIndex and mFreeIndexes variables to shorts.
25
  
26
  static boolean setMax(int m)
27
    {
28
    if( (mCreated==false && !Distorted.isInitialized()) || m<=mMax[INDEX] )
29
      {
30
           if( m<0              ) m = 0;
31
      else if( m>Byte.MAX_VALUE ) m = Byte.MAX_VALUE;
32
      
33
      mMax[INDEX] = m;
34
      return true;
35
      }
36
   
37
    return false;
38
    }
39
 
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41

    
42
  static int getMax()
43
    {
44
    return mMax[INDEX];
45
    }
46

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

    
49
  static void getUniforms(int mProgramH)
50
    {
51
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "vNumEffects");
52
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "vType");
53
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "vUniforms");
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 ) 
69
        {
70
        mInterP[i].interpolateMain(mUniforms, NUM_UNIFORMS*i+7, mCurrentDuration[i]);
71
      
72
        mUniforms[NUM_UNIFORMS*i+7] = mUniforms[NUM_UNIFORMS*i+7]-mObjHalfX;
73
        mUniforms[NUM_UNIFORMS*i+8] =-mUniforms[NUM_UNIFORMS*i+8]+mObjHalfY;
74
        }
75
        
76
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )      
77
        {
78
        for(int j=0; j<mNumListeners; j++)   
79
          EffectMessageSender.newMessage( mListeners.elementAt(j),
80
                                          EffectMessage.EFFECT_FINISHED, 
81
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.VERTEX.type,
82
                                          mType[i], 
83
                                          mBitmapID); 
84
      
85
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
86
          {
87
          remove(i);
88
          i--;
89
          continue;
90
          }
91
        }
92
     
93
      mCurrentDuration[i] += step;
94
      }
95
     
96
    mTime = currTime;  
97
    }  
98
  
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  protected void moveEffect(int index)
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
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
108
    mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
109
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
110
    mUniforms[NUM_UNIFORMS*index+7] = mUniforms[NUM_UNIFORMS*(index+1)+7];
111
    mUniforms[NUM_UNIFORMS*index+8] = mUniforms[NUM_UNIFORMS*(index+1)+8];
112
    }
113
   
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  synchronized void send() 
117
    {
118
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
119
      
120
    if( mNumEffects>0 )
121
      {     
122
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
123
      GLES20.glUniform3fv( mUniformsH,3*mNumEffects, mUniforms,0);
124
      }
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  synchronized void sendZero() 
130
    {
131
    GLES20.glUniform1i( mNumEffectsH, 0);
132
    }
133
  
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// Do various post-processing on already computed effects.
136
// 1) here unlike in the fragment queue, we don't have to multiply the points by ModelView matrix because that gets done in the shader.
137
// 2) in case of swirl, pre-compute the sine and cosine of its rotation angle
138
  
139
  void postprocess()
140
    {
141
    double d;  
142
     
143
    for(int i=0; i<mNumEffects; i++)
144
      {      
145
      if( mType[i]==EffectNames.SWIRL.ordinal() )
146
        {
147
        d = Math.PI*mUniforms[NUM_UNIFORMS*i]/180;  
148
        mUniforms[NUM_UNIFORMS*i+1] = (float)Math.sin(d);
149
        mUniforms[NUM_UNIFORMS*i+2] = (float)Math.cos(d);
150
        }
151
      }
152
    }
153
  
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
  
156
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Interpolator2D point)
157
    {
158
    if( mMax[INDEX]>mNumEffects )
159
      {
160
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
161
      
162
      mInterI[mNumEffects] = inter;
163
      mInterP[mNumEffects] = point;
164

    
165
      return addPriv(eln,region);
166
      }
167
      
168
    return -1;
169
    }
170
   
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
  
173
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, float x, float y)
174
    {
175
    if( mMax[INDEX]>mNumEffects )
176
      {
177
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
178
      
179
      mInterI[mNumEffects] = inter;
180
      mInterP[mNumEffects] = null;
181
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = x-mObjHalfX;
182
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-y+mObjHalfY;  
183
     
184
      return addPriv(eln,region);
185
      }
186
      
187
    return -1;
188
    }
189
 
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
  
192
  synchronized long add(EffectNames eln, float v1, float v2, float v3, Float4D region, float x, float y)
193
    {
194
    if( mMax[INDEX]>mNumEffects )
195
      {
196
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
197
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = v1;
198
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = v2;  
199
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = v3;  
200
     
201
      mInterI[mNumEffects] = null;
202
      mInterP[mNumEffects] = null;
203
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = x-mObjHalfX;
204
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-y+mObjHalfY;  
205
      
206
      return addPriv(eln,region);    
207
      }
208
      
209
    return -1;
210
    }
211
  
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
  
214
  private long addPriv(EffectNames eln, Float4D region)
215
    {    
216
    if( region!=null )
217
      {
218
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = region.x;
219
      mUniforms[NUM_UNIFORMS*mNumEffects+4] =-region.y;   // invert y already
220
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = region.z<=0.0f ? 1000*mObjHalfX : region.z;
221
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = region.w;
222
      }
223
    else
224
      {
225
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = 0.0f;
226
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = 0.0f;
227
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = 1000*mObjHalfX;
228
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = 0.0f;
229
      }
230
    
231
    return addBase(eln);
232
    }
233

    
234
///////////////////////////////////////////////////////////////////////////////////////////////////
235
// end of VertexEffect  
236
  }
(16-16/30)