Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueVertex.java @ 6ddda212

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+4] = (float)Math.sin(d);
171
      mUniforms[NUM_UNIFORMS*effect+5] = (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+4] = (float)Math.sin(d);
177
      mUniforms[NUM_UNIFORMS*effect+5] = (float)Math.cos(d);
178
      }
179
    }
180
  
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// distort, wave
183

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

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

    
200
      return addPriv(eln,center,region);
201
      }
202
      
203
    return -1;
204
    }
205
   
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
// deform, distort, wave
208

    
209
  synchronized long add(EffectNames eln, Data3D data, Data2D center)
210
    {
211
    if( mMax[INDEX]>mNumEffects )
212
      {
213
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
214

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

    
225
      return addPriv(eln,center,null);
226
      }
227
      
228
    return -1;
229
    }
230
 
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232
// sink, swirl
233

    
234
  synchronized long add(EffectNames eln, Data1D data, Data2D center, Data4D region)
235
    {
236
    if( mMax[INDEX]>mNumEffects )
237
      {
238
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
239

    
240
      if( data instanceof Dynamic1D)
241
        mInter[0][mNumEffects] = (Dynamic1D)data;
242
      else if( data instanceof Static1D)
243
        {
244
        mInter[0][mNumEffects] = null;
245
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
246
        }
247

    
248
      return addPriv(eln,center,region);
249
      }
250
      
251
    return -1;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255
// sink, swirl
256

    
257
  synchronized long add(EffectNames eln, Data1D data, Data2D center)
258
    {
259
    if( mMax[INDEX]>mNumEffects )
260
      {
261
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);
262

    
263
      if( data instanceof Dynamic1D)
264
        mInter[0][mNumEffects] = (Dynamic1D)data;
265
      else if( data instanceof Static1D)
266
        {
267
        mInter[0][mNumEffects] = null;
268
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)data).getX();
269
        }
270

    
271
      return addPriv(eln,center,null);
272
      }
273

    
274
    return -1;
275
    }
276

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

    
291
        float z = tmp.getZ();
292

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

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

    
319
    long ret= addBase(eln);
320

    
321
    postprocess(mNumEffects-1); //addBase just incremented mNumEffects
322

    
323
    return ret;
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327
// end of VertexEffect  
328
  }
(17-17/18)