Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueVertex.java @ 4fde55a0

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.Data2D;
27
import org.distorted.library.type.Data3D;
28
import org.distorted.library.type.Data4D;
29
import org.distorted.library.type.Dynamic1D;
30
import org.distorted.library.type.Dynamic2D;
31
import org.distorted.library.type.Dynamic3D;
32
import org.distorted.library.type.Dynamic4D;
33
import org.distorted.library.type.Static1D;
34
import org.distorted.library.type.Static2D;
35
import org.distorted.library.type.Static3D;
36
import org.distorted.library.type.Static4D;
37

    
38
///////////////////////////////////////////////////////////////////////////////////////////////////
39

    
40
class EffectQueueVertex extends EffectQueue
41
  { 
42
  private static final int NUM_UNIFORMS = 12;
43
  private static final int INDEX = EffectTypes.VERTEX.ordinal();
44
  private static int mNumEffectsH;
45
  private static int mTypeH;
46
  private static int mUniformsH;
47
  
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49
   
50
  public EffectQueueVertex(DistortedObject obj)
51
    { 
52
    super(obj,NUM_UNIFORMS,INDEX);
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  static void getUniforms(int mProgramH)
58
    {
59
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "vNumEffects");
60
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "vType");
61
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "vUniforms");
62
    }
63

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65
  
66
  synchronized void compute(long currTime) 
67
    {
68
    if( currTime==mTime ) return;
69
    if( mTime==0 ) mTime = currTime;
70
    long step = (currTime-mTime);
71
   
72
    for(int i=0; i<mNumEffects; i++)
73
      {
74
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
75
        {
76
        for(int j=0; j<mNumListeners; j++)
77
          EffectMessageSender.newMessage( mListeners.elementAt(j),
78
                                          EffectMessage.EFFECT_FINISHED,
79
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.VERTEX.type,
80
                                          mName[i],
81
                                          mBitmapID,
82
                                          null);
83

    
84
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
85
          {
86
          remove(i);
87
          i--;
88
          continue;
89
          }
90
        else mInter[0][i] = null;
91
        }
92

    
93
      if( mInter[1][i]!=null )  // region
94
        {
95
        mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+8, mCurrentDuration[i]);
96
        }
97

    
98
      if( mInter[2][i]!=null )  // center
99
        {
100
        mInter[2][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+6, mCurrentDuration[i]);
101

    
102
        mUniforms[NUM_UNIFORMS*i+6] = mUniforms[NUM_UNIFORMS*i+6]-mObjHalfX;
103
        mUniforms[NUM_UNIFORMS*i+7] =-mUniforms[NUM_UNIFORMS*i+7]+mObjHalfY;
104
        }
105

    
106
      mCurrentDuration[i] += step;
107
      }
108
     
109
    mTime = currTime;  
110
    }  
111
  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  protected void moveEffect(int index)
115
    {
116
    mUniforms[NUM_UNIFORMS*index   ] = mUniforms[NUM_UNIFORMS*(index+1)   ];
117
    mUniforms[NUM_UNIFORMS*index+ 1] = mUniforms[NUM_UNIFORMS*(index+1)+ 1];
118
    mUniforms[NUM_UNIFORMS*index+ 2] = mUniforms[NUM_UNIFORMS*(index+1)+ 2];
119
    mUniforms[NUM_UNIFORMS*index+ 3] = mUniforms[NUM_UNIFORMS*(index+1)+ 3];
120
    mUniforms[NUM_UNIFORMS*index+ 4] = mUniforms[NUM_UNIFORMS*(index+1)+ 4];
121
    mUniforms[NUM_UNIFORMS*index+ 5] = mUniforms[NUM_UNIFORMS*(index+1)+ 5];
122
    mUniforms[NUM_UNIFORMS*index+ 6] = mUniforms[NUM_UNIFORMS*(index+1)+ 6];
123
    mUniforms[NUM_UNIFORMS*index+ 7] = mUniforms[NUM_UNIFORMS*(index+1)+ 7];
124
    mUniforms[NUM_UNIFORMS*index+ 8] = mUniforms[NUM_UNIFORMS*(index+1)+ 8];
125
    mUniforms[NUM_UNIFORMS*index+ 9] = mUniforms[NUM_UNIFORMS*(index+1)+ 9];
126
    mUniforms[NUM_UNIFORMS*index+10] = mUniforms[NUM_UNIFORMS*(index+1)+10];
127
    mUniforms[NUM_UNIFORMS*index+11] = mUniforms[NUM_UNIFORMS*(index+1)+11];
128
    }
129
   
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131

    
132
  synchronized void send() 
133
    {
134
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
135
      
136
    if( mNumEffects>0 )
137
      {     
138
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName    ,0);
139
      GLES20.glUniform4fv( mUniformsH,3*mNumEffects, mUniforms,0);
140
      }
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  synchronized void sendZero() 
146
    {
147
    GLES20.glUniform1i( mNumEffectsH, 0);
148
    }
149
  
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// Do various post-processing on already computed effects.
152
// 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.
153
// 2) in case of swirl, pre-compute the sine and cosine of its rotation angle
154
// 3) likewise in case of wave
155
  
156
  void postprocess()
157
    {
158
    double d;  
159
     
160
    for(int i=0; i<mNumEffects; i++)
161
      {      
162
      if( mName[i]==EffectNames.SWIRL.ordinal() )
163
        {
164
        d = Math.PI*mUniforms[NUM_UNIFORMS*i]/180;  
165
        mUniforms[NUM_UNIFORMS*i+4] = (float)Math.sin(d);
166
        mUniforms[NUM_UNIFORMS*i+5] = (float)Math.cos(d);
167
        }
168
      if( mName[i]==EffectNames.WAVE.ordinal() )
169
        {
170
        d = Math.PI*mUniforms[NUM_UNIFORMS*i+1]/180;
171
        mUniforms[NUM_UNIFORMS*i+4] = (float)Math.sin(d);
172
        mUniforms[NUM_UNIFORMS*i+5] = (float)Math.cos(d);
173
        }
174
      }
175
    }
176
  
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
// distort, wave
179

    
180
  synchronized long add(EffectNames eln, Data3D data, Data2D center, Data4D region)
181
    {
182
    if( mMax[INDEX]>mNumEffects )
183
      {
184
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
185

    
186
      if( data instanceof Dynamic3D)
187
        mInter[0][mNumEffects] = (Dynamic3D)data;
188
      else if( data instanceof Static3D)
189
        {
190
        mInter[0][mNumEffects] = null;
191
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)data).getX();
192
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)data).getY();
193
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)data).getZ();
194
        }
195

    
196
      return addPriv(eln,center,region);
197
      }
198
      
199
    return -1;
200
    }
201
   
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203
// deform, distort, wave
204

    
205
  synchronized long add(EffectNames eln, Data3D data, Data2D center)
206
    {
207
    if( mMax[INDEX]>mNumEffects )
208
      {
209
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
210

    
211
      if( data instanceof Dynamic3D)
212
        mInter[0][mNumEffects] = (Dynamic3D)data;
213
      else if( data instanceof Static3D)
214
        {
215
        mInter[0][mNumEffects] = null;
216
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)data).getX();
217
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)data).getY();
218
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)data).getZ();
219
        }
220

    
221
      return addPriv(eln,center,null);
222
      }
223
      
224
    return -1;
225
    }
226
 
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228
// sink, swirl
229

    
230
  synchronized long add(EffectNames eln, Data1D data, Data2D center, Data4D region)
231
    {
232
    if( mMax[INDEX]>mNumEffects )
233
      {
234
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
235

    
236
      if( data instanceof Dynamic1D)
237
        mInter[0][mNumEffects] = (Dynamic1D)data;
238
      else if( data instanceof Static1D)
239
        {
240
        mInter[0][mNumEffects] = null;
241
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
242
        }
243

    
244
      return addPriv(eln,center,region);
245
      }
246
      
247
    return -1;
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
// sink, swirl
252

    
253
  synchronized long add(EffectNames eln, Data1D data, Data2D center)
254
    {
255
    if( mMax[INDEX]>mNumEffects )
256
      {
257
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
258

    
259
      if( data instanceof Dynamic1D)
260
        mInter[0][mNumEffects] = (Dynamic1D)data;
261
      else if( data instanceof Static1D)
262
        {
263
        mInter[0][mNumEffects] = null;
264
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
265
        }
266

    
267
      return addPriv(eln,center,null);
268
      }
269

    
270
    return -1;
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
  
275
  private long addPriv(EffectNames eln, Data2D center, Data4D region)
276
    {
277
    if( region!=null )
278
      {
279
      if( region instanceof Dynamic4D)
280
        {
281
        mInter[1][mNumEffects] = (Dynamic4D)region;
282
        }
283
      else if ( region instanceof Static4D)
284
        {
285
        Static4D tmp = (Static4D)region;
286

    
287
        float z = tmp.getZ();
288

    
289
        mUniforms[NUM_UNIFORMS*mNumEffects+ 8] = tmp.getX();
290
        mUniforms[NUM_UNIFORMS*mNumEffects+ 9] =-tmp.getY();   // invert y already
291
        mUniforms[NUM_UNIFORMS*mNumEffects+10] = z<=0.0f ? 1000*mObjHalfX : z;
292
        mUniforms[NUM_UNIFORMS*mNumEffects+11] = tmp.getW();
293
        mInter[1][mNumEffects] = null;
294
        }
295
      else return -1;
296
      }
297
    else
298
      {
299
      mUniforms[NUM_UNIFORMS*mNumEffects+ 8] = 0.0f;
300
      mUniforms[NUM_UNIFORMS*mNumEffects+ 9] = 0.0f;
301
      mUniforms[NUM_UNIFORMS*mNumEffects+10] = 1000*mObjHalfX;
302
      mUniforms[NUM_UNIFORMS*mNumEffects+11] = 0.0f;
303
      mInter[1][mNumEffects] = null;
304
      }
305

    
306
    if( center instanceof Dynamic2D)
307
      mInter[2][mNumEffects] = (Dynamic2D)center;
308
    else if( center instanceof Static2D)
309
      {
310
      mInter[2][mNumEffects] = null;
311
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static2D)center).getX()-mObjHalfX;
312
      mUniforms[NUM_UNIFORMS*mNumEffects+7] =-((Static2D)center).getY()+mObjHalfY;
313
      }
314

    
315
    return addBase(eln);
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319
// end of VertexEffect  
320
  }
(17-17/18)