Project

General

Profile

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

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

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 )
75
        {
76
        if( mInter[0][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
                                            mName[i],
83
                                            mBitmapID,
84
                                            null);
85

    
86
          if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
87
            {
88
            remove(i);
89
            i--;
90
            continue;
91
            }
92
          else mInter[0][i] = null;
93
          }
94
        else
95
          {
96
          postprocess(i);
97
          }
98
        }
99

    
100
      if( mInter[1][i]!=null )  // region
101
        {
102
        mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+8, mCurrentDuration[i]);
103
        }
104

    
105
      if( mInter[2][i]!=null )  // center
106
        {
107
        mInter[2][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+6, mCurrentDuration[i]);
108

    
109
        mUniforms[NUM_UNIFORMS*i+6] = mUniforms[NUM_UNIFORMS*i+6]-mObjHalfX;
110
        mUniforms[NUM_UNIFORMS*i+7] =-mUniforms[NUM_UNIFORMS*i+7]+mObjHalfY;
111
        }
112

    
113
      mCurrentDuration[i] += step;
114
      }
115
     
116
    mTime = currTime;  
117
    }  
118
  
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120

    
121
  protected void moveEffect(int index)
122
    {
123
    mUniforms[NUM_UNIFORMS*index   ] = mUniforms[NUM_UNIFORMS*(index+1)   ];
124
    mUniforms[NUM_UNIFORMS*index+ 1] = mUniforms[NUM_UNIFORMS*(index+1)+ 1];
125
    mUniforms[NUM_UNIFORMS*index+ 2] = mUniforms[NUM_UNIFORMS*(index+1)+ 2];
126
    mUniforms[NUM_UNIFORMS*index+ 3] = mUniforms[NUM_UNIFORMS*(index+1)+ 3];
127
    mUniforms[NUM_UNIFORMS*index+ 4] = mUniforms[NUM_UNIFORMS*(index+1)+ 4];
128
    mUniforms[NUM_UNIFORMS*index+ 5] = mUniforms[NUM_UNIFORMS*(index+1)+ 5];
129
    mUniforms[NUM_UNIFORMS*index+ 6] = mUniforms[NUM_UNIFORMS*(index+1)+ 6];
130
    mUniforms[NUM_UNIFORMS*index+ 7] = mUniforms[NUM_UNIFORMS*(index+1)+ 7];
131
    mUniforms[NUM_UNIFORMS*index+ 8] = mUniforms[NUM_UNIFORMS*(index+1)+ 8];
132
    mUniforms[NUM_UNIFORMS*index+ 9] = mUniforms[NUM_UNIFORMS*(index+1)+ 9];
133
    mUniforms[NUM_UNIFORMS*index+10] = mUniforms[NUM_UNIFORMS*(index+1)+10];
134
    mUniforms[NUM_UNIFORMS*index+11] = mUniforms[NUM_UNIFORMS*(index+1)+11];
135
    }
136
   
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138

    
139
  synchronized void send() 
140
    {
141
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
142
      
143
    if( mNumEffects>0 )
144
      {     
145
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mName    ,0);
146
      GLES20.glUniform4fv( mUniformsH,3*mNumEffects, mUniforms,0);
147
      }
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  synchronized void sendZero() 
153
    {
154
    GLES20.glUniform1i( mNumEffectsH, 0);
155
    }
156
  
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158
// Do various post-processing on already computed effects.
159
// 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.
160
// 2) in case of swirl, pre-compute the sine and cosine of its rotation angle
161
// 3) likewise in case of wave
162
  
163
  void postprocess(int effect)
164
    {
165
    double d;  
166

    
167
    if( mName[effect]==EffectNames.SWIRL.ordinal() )
168
      {
169
      d = Math.PI*mUniforms[NUM_UNIFORMS*effect]/180;
170
      mUniforms[NUM_UNIFORMS*effect  ] = (float)Math.sin(d);
171
      mUniforms[NUM_UNIFORMS*effect+4] = (float)Math.cos(d);
172
      }
173
    if( mName[effect]==EffectNames.WAVE.ordinal() )
174
      {
175
      d = Math.PI*mUniforms[NUM_UNIFORMS*effect+1]/180;
176
      mUniforms[NUM_UNIFORMS*effect+1] = (float)Math.sin(d);
177
      mUniforms[NUM_UNIFORMS*effect+4] = (float)Math.cos(d);
178
      d = Math.PI*mUniforms[NUM_UNIFORMS*effect+3]/180;
179
      mUniforms[NUM_UNIFORMS*effect+3] = (float)Math.sin(d);
180
      mUniforms[NUM_UNIFORMS*effect+5] = (float)Math.cos(d);
181
      }
182
    if( mName[effect]==EffectNames.DISTORT.ordinal() )
183
      {
184
      mUniforms[NUM_UNIFORMS*effect+1] =-mUniforms[NUM_UNIFORMS*effect+1];
185
      }
186
    }
187

    
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
// wave
190

    
191
  synchronized long add(EffectNames eln, Data4D data, Data2D center, Data4D region)
192
    {
193
    if( mMax[INDEX]>mNumEffects )
194
      {
195
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
196

    
197
      if( data instanceof Dynamic4D)
198
        mInter[0][mNumEffects] = (Dynamic4D)data;
199
      else if( data instanceof Static4D)
200
        {
201
        mInter[0][mNumEffects] = null;
202
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static4D)data).getX();
203
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static4D)data).getY();
204
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static4D)data).getZ();
205
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static4D)data).getW();
206
        }
207

    
208
      return addPriv(eln,center,region);
209
      }
210

    
211
    return -1;
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
// deform,distort
216

    
217
  synchronized long add(EffectNames eln, Data3D data, Data2D center, Data4D region)
218
    {
219
    if( mMax[INDEX]>mNumEffects )
220
      {
221
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
222

    
223
      if( data instanceof Dynamic3D)
224
        mInter[0][mNumEffects] = (Dynamic3D)data;
225
      else if( data instanceof Static3D)
226
        {
227
        mInter[0][mNumEffects] = null;
228
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)data).getX();
229
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)data).getY();
230
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)data).getZ();
231
        }
232

    
233
      return addPriv(eln,center,region);
234
      }
235
      
236
    return -1;
237
    }
238

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240
// sink, swirl
241

    
242
  synchronized long add(EffectNames eln, Data1D data, Data2D center, Data4D region)
243
    {
244
    if( mMax[INDEX]>mNumEffects )
245
      {
246
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
247

    
248
      if( data instanceof Dynamic1D)
249
        mInter[0][mNumEffects] = (Dynamic1D)data;
250
      else if( data instanceof Static1D)
251
        {
252
        mInter[0][mNumEffects] = null;
253
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
254
        }
255

    
256
      return addPriv(eln,center,region);
257
      }
258
      
259
    return -1;
260
    }
261

    
262
///////////////////////////////////////////////////////////////////////////////////////////////////
263
// sink, swirl
264

    
265
  synchronized long add(EffectNames eln, Data1D data, Data2D center)
266
    {
267
    if( mMax[INDEX]>mNumEffects )
268
      {
269
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
270

    
271
      if( data instanceof Dynamic1D)
272
        mInter[0][mNumEffects] = (Dynamic1D)data;
273
      else if( data instanceof Static1D)
274
        {
275
        mInter[0][mNumEffects] = null;
276
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
277
        }
278

    
279
      return addPriv(eln,center,null);
280
      }
281

    
282
    return -1;
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286
  
287
  private long addPriv(EffectNames eln, Data2D center, Data4D region)
288
    {
289
    if( region!=null )
290
      {
291
      if( region instanceof Dynamic4D)
292
        {
293
        mInter[1][mNumEffects] = (Dynamic4D)region;
294
        }
295
      else if ( region instanceof Static4D)
296
        {
297
        Static4D tmp = (Static4D)region;
298

    
299
        float z = tmp.getZ();
300

    
301
        mUniforms[NUM_UNIFORMS*mNumEffects+ 8] = tmp.getX();
302
        mUniforms[NUM_UNIFORMS*mNumEffects+ 9] =-tmp.getY();   // invert y already
303
        mUniforms[NUM_UNIFORMS*mNumEffects+10] = z<=0.0f ? 1000*mObjHalfX : z;
304
        mUniforms[NUM_UNIFORMS*mNumEffects+11] = tmp.getW();
305
        mInter[1][mNumEffects] = null;
306
        }
307
      else return -1;
308
      }
309
    else
310
      {
311
      mUniforms[NUM_UNIFORMS*mNumEffects+ 8] = 0.0f;
312
      mUniforms[NUM_UNIFORMS*mNumEffects+ 9] = 0.0f;
313
      mUniforms[NUM_UNIFORMS*mNumEffects+10] = 1000*mObjHalfX;
314
      mUniforms[NUM_UNIFORMS*mNumEffects+11] = 0.0f;
315
      mInter[1][mNumEffects] = null;
316
      }
317

    
318
    if( center instanceof Dynamic2D)
319
      mInter[2][mNumEffects] = (Dynamic2D)center;
320
    else if( center instanceof Static2D)
321
      {
322
      mInter[2][mNumEffects] = null;
323
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static2D)center).getX()-mObjHalfX;
324
      mUniforms[NUM_UNIFORMS*mNumEffects+7] =-((Static2D)center).getY()+mObjHalfY;
325
      }
326

    
327
    long ret= addBase(eln);
328

    
329
    postprocess(mNumEffects-1); //addBase just incremented mNumEffects
330

    
331
    return ret;
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
// end of VertexEffect  
336
  }
(17-17/18)