Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueVertex.java @ 02ef26bc

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
    }
183

    
184
///////////////////////////////////////////////////////////////////////////////////////////////////
185
// wave
186

    
187
  synchronized long add(EffectNames eln, Data4D data, Data2D center, Data4D region)
188
    {
189
    if( mMax[INDEX]>mNumEffects )
190
      {
191
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
192

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

    
204
      return addPriv(eln,center,region);
205
      }
206

    
207
    return -1;
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
// deform,distort
212

    
213
  synchronized long add(EffectNames eln, Data3D data, Data2D center, Data4D region)
214
    {
215
    if( mMax[INDEX]>mNumEffects )
216
      {
217
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
218

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

    
229
      return addPriv(eln,center,region);
230
      }
231
      
232
    return -1;
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
// sink, swirl
237

    
238
  synchronized long add(EffectNames eln, Data1D data, Data2D center, Data4D region)
239
    {
240
    if( mMax[INDEX]>mNumEffects )
241
      {
242
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
243

    
244
      if( data instanceof Dynamic1D)
245
        mInter[0][mNumEffects] = (Dynamic1D)data;
246
      else if( data instanceof Static1D)
247
        {
248
        mInter[0][mNumEffects] = null;
249
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
250
        }
251

    
252
      return addPriv(eln,center,region);
253
      }
254
      
255
    return -1;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
// sink, swirl
260

    
261
  synchronized long add(EffectNames eln, Data1D data, Data2D center)
262
    {
263
    if( mMax[INDEX]>mNumEffects )
264
      {
265
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
266

    
267
      if( data instanceof Dynamic1D)
268
        mInter[0][mNumEffects] = (Dynamic1D)data;
269
      else if( data instanceof Static1D)
270
        {
271
        mInter[0][mNumEffects] = null;
272
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
273
        }
274

    
275
      return addPriv(eln,center,null);
276
      }
277

    
278
    return -1;
279
    }
280

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

    
295
        float z = tmp.getZ();
296

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

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

    
323
    long ret= addBase(eln);
324

    
325
    postprocess(mNumEffects-1); //addBase just incremented mNumEffects
326

    
327
    return ret;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
// end of VertexEffect  
332
  }
(17-17/18)