Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectListVertex.java @ 6a06a912

1
package org.distorted.library;
2

    
3
import android.opengl.GLES20;
4

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

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

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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  static void getUniforms(int mProgramH)
49
    {
50
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "vNumEffects");
51
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "vType");
52
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "vUniforms");
53
    }
54

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

    
100
  protected void moveEffect(int index)
101
    {
102
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
103
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
104
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
105
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
106
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
107
    mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
108
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
109
    mUniforms[NUM_UNIFORMS*index+7] = mUniforms[NUM_UNIFORMS*(index+1)+7];
110
    mUniforms[NUM_UNIFORMS*index+8] = mUniforms[NUM_UNIFORMS*(index+1)+8];
111
    }
112
   
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114

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

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  synchronized void sendZero() 
129
    {
130
    GLES20.glUniform1i( mNumEffectsH, 0);
131
    }
132
  
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134
// Do various post-processing on already computed effects.
135
// 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.
136
// 2) in case of swirl, pre-compute the sine and cosine of its rotation angle
137
  
138
  void postprocess()
139
    {
140
    double d;  
141
     
142
    for(int i=0; i<mNumEffects; i++)
143
      {      
144
      if( mType[i]==EffectNames.SWIRL.ordinal() )
145
        {
146
        d = Math.PI*mUniforms[NUM_UNIFORMS*i]/180;  
147
        mUniforms[NUM_UNIFORMS*i+1] = (float)Math.sin(d);
148
        mUniforms[NUM_UNIFORMS*i+2] = (float)Math.cos(d);
149
        }
150
      }
151
    }
152
  
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
  
155
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Interpolator2D point)
156
    {
157
    if( mMax[VERTEX]>mNumEffects )
158
      {
159
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
160
      
161
      mInterI[mNumEffects] = inter;
162
      mInterP[mNumEffects] = point;
163

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

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
// end of VertexEffect  
235
  }
(11-11/28)