Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / EffectQueueVertex.java @ 310e14fb

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.effect;
21

    
22
import android.opengl.GLES30;
23

    
24
import org.distorted.library.message.EffectMessage;
25
import org.distorted.library.type.Dynamic3D;
26
import org.distorted.library.type.Dynamic4D;
27
import org.distorted.library.type.Static;
28
import org.distorted.library.type.Static1D;
29
import org.distorted.library.type.Static2D;
30
import org.distorted.library.type.Static3D;
31
import org.distorted.library.type.Static4D;
32
import org.distorted.library.type.Static5D;
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35

    
36
class EffectQueueVertex extends EffectQueue
37
  { 
38
  private static final int NUM_UNIFORMS = 12;
39
  private static final int NUM_CACHE    =  3;
40
  private static final int INDEX = Effect.VERTEX;
41
  private static int mNumEffectsH;
42
  private static int mTypeH;
43
  private static int mUniformsH;
44
  
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
   
47
  EffectQueueVertex(long id)
48
    { 
49
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX);
50
    }
51

    
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53

    
54
  static void getUniforms(int mProgramH)
55
    {
56
    mNumEffectsH= GLES30.glGetUniformLocation( mProgramH, "vNumEffects");
57
    mTypeH      = GLES30.glGetUniformLocation( mProgramH, "vType");
58
    mUniformsH  = GLES30.glGetUniformLocation( mProgramH, "vUniforms");
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62
  
63
  synchronized void compute(long currTime) 
64
    {
65
    if( currTime==mTime ) return;
66
    if( mTime==0 ) mTime = currTime;
67
    long step = (currTime-mTime);
68
   
69
    for(int i=0; i<mNumEffects; i++)
70
      {
71
      mCurrentDuration[i] += step;
72

    
73
      if( mInter[0][i]!=null )
74
        {
75
        if( mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
76
          {
77
          postprocess(i);
78

    
79
          for(int j=0; j<mNumListeners; j++)
80
            EffectMessageSender.newMessage( mListeners.elementAt(j),
81
                                            EffectMessage.EFFECT_FINISHED,
82
                                           (mID[i]<<Effect.LENGTH)+Effect.VERTEX,
83
                                            mName[i],
84
                                            mObjectID);
85

    
86
          if( VertexEffect.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 ) mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+8, mCurrentDuration[i], step);
101
      if( mInter[2][i]!=null ) mInter[2][i].interpolateMain(mCache   , NUM_CACHE*i     , mCurrentDuration[i], step);
102
      }
103
     
104
    mTime = currTime;  
105
    }  
106
  
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  protected void moveEffect(int index)
110
    {
111
    mUniforms[NUM_UNIFORMS*index   ] = mUniforms[NUM_UNIFORMS*(index+1)   ];
112
    mUniforms[NUM_UNIFORMS*index+ 1] = mUniforms[NUM_UNIFORMS*(index+1)+ 1];
113
    mUniforms[NUM_UNIFORMS*index+ 2] = mUniforms[NUM_UNIFORMS*(index+1)+ 2];
114
    mUniforms[NUM_UNIFORMS*index+ 3] = mUniforms[NUM_UNIFORMS*(index+1)+ 3];
115
    mUniforms[NUM_UNIFORMS*index+ 4] = mUniforms[NUM_UNIFORMS*(index+1)+ 4];
116

    
117
    mCache[NUM_CACHE*index  ] = mCache[NUM_CACHE*(index+1)  ];
118
    mCache[NUM_CACHE*index+1] = mCache[NUM_CACHE*(index+1)+1];
119
    mCache[NUM_CACHE*index+2] = mCache[NUM_CACHE*(index+1)+2];
120

    
121
    mUniforms[NUM_UNIFORMS*index+ 8] = mUniforms[NUM_UNIFORMS*(index+1)+ 8];
122
    mUniforms[NUM_UNIFORMS*index+ 9] = mUniforms[NUM_UNIFORMS*(index+1)+ 9];
123
    mUniforms[NUM_UNIFORMS*index+10] = mUniforms[NUM_UNIFORMS*(index+1)+10];
124
    mUniforms[NUM_UNIFORMS*index+11] = mUniforms[NUM_UNIFORMS*(index+1)+11];
125
    }
126
   
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  synchronized void send(float halfX, float halfY, float halfZ)
130
    {
131
    GLES30.glUniform1i( mNumEffectsH, mNumEffects);
132
      
133
    if( mNumEffects>0 )
134
      {
135
      for(int i=0; i<mNumEffects; i++)
136
        {
137
        mUniforms[NUM_UNIFORMS*i+5] = mCache[NUM_CACHE*i  ]-halfX;
138
        mUniforms[NUM_UNIFORMS*i+6] =-mCache[NUM_CACHE*i+1]+halfY;
139
        mUniforms[NUM_UNIFORMS*i+7] = mCache[NUM_CACHE*i+2]-halfZ;
140
        }
141

    
142
      GLES30.glUniform1iv( mTypeH    ,                 mNumEffects, mName    ,0);
143
      GLES30.glUniform4fv( mUniformsH,(NUM_UNIFORMS/4)*mNumEffects, mUniforms,0);
144
      }
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
// Do various post-processing on already computed effects.
149
// 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.
150
// 2) in case of SWIRL, switch the angles from degrees to radians
151
// 3) likewise in case of WAVE and PINCH
152
// 4) In case of DISTORT, invert the Y-axis
153
  
154
  private void postprocess(int effect)
155
    {
156
    if( mName[effect]==VertexEffect.SWIRL )
157
      {
158
      mUniforms[NUM_UNIFORMS*effect  ] = (float)(Math.PI*mUniforms[NUM_UNIFORMS*effect  ]/180);
159
      }
160
    if( mName[effect]==VertexEffect.PINCH )
161
      {
162
      mUniforms[NUM_UNIFORMS*effect+1] = (float)(Math.PI*mUniforms[NUM_UNIFORMS*effect+1]/180);
163
      }
164
    if( mName[effect]==VertexEffect.WAVE )
165
      {
166
      mUniforms[NUM_UNIFORMS*effect+2] = (float)(Math.PI*mUniforms[NUM_UNIFORMS*effect+2]/180);
167
      mUniforms[NUM_UNIFORMS*effect+3] = (float)(Math.PI*mUniforms[NUM_UNIFORMS*effect+3]/180);
168
      mUniforms[NUM_UNIFORMS*effect+4] = (float)(Math.PI*mUniforms[NUM_UNIFORMS*effect+4]/180);
169
      }
170
    if( mName[effect]==VertexEffect.DISTORT )
171
      {
172
      mUniforms[NUM_UNIFORMS*effect+1] =-mUniforms[NUM_UNIFORMS*effect+1];
173
      }
174
    }
175

    
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177

    
178
  synchronized long add(VertexEffect ve)
179
    {
180
    if( mMax[INDEX]>mNumEffects )
181
      {
182
      if( ve.mDynamic0 != null )
183
        {
184
        mInter[0][mNumEffects] = ve.mDynamic0;
185
        }
186
      else
187
        {
188
        mInter[0][mNumEffects] = null;
189

    
190
        if( ve.mStatic0 != null )
191
          {
192
          Static s = ve.mStatic0;
193

    
194
          switch( s.getDimension() )
195
            {
196
            case 5 : mUniforms[NUM_UNIFORMS*mNumEffects + 4] = ((Static5D)s).getV();
197
            case 4 : mUniforms[NUM_UNIFORMS*mNumEffects + 3] = ((Static4D)s).getW();
198
            case 3 : mUniforms[NUM_UNIFORMS*mNumEffects + 2] = ((Static3D)s).getZ();
199
            case 2 : mUniforms[NUM_UNIFORMS*mNumEffects + 1] = ((Static2D)s).getY();
200
            case 1 : mUniforms[NUM_UNIFORMS*mNumEffects    ] = ((Static1D)s).getX();
201
            }
202
          }
203
        }
204

    
205
      if( ve.mRegion!=null )
206
        {
207
        if( ve.mRegion instanceof Dynamic4D)
208
          {
209
          mInter[1][mNumEffects] = (Dynamic4D)ve.mRegion;
210
          }
211
        else if ( ve.mRegion instanceof Static4D)
212
          {
213
          Static4D tmp = (Static4D)ve.mRegion;
214

    
215
          float z = tmp.getZ();
216

    
217
          mUniforms[NUM_UNIFORMS*mNumEffects+ 8] = tmp.getX();
218
          mUniforms[NUM_UNIFORMS*mNumEffects+ 9] =-tmp.getY();   // invert y already
219
          mUniforms[NUM_UNIFORMS*mNumEffects+10] = z<=0.0f ? Float.MAX_VALUE : z;
220
          mUniforms[NUM_UNIFORMS*mNumEffects+11] = tmp.getW();
221
          mInter[1][mNumEffects] = null;
222
          }
223
        else return -1;
224
        }
225
      else
226
        {
227
        mUniforms[NUM_UNIFORMS*mNumEffects+ 8] = 0.0f;
228
        mUniforms[NUM_UNIFORMS*mNumEffects+ 9] = 0.0f;
229
        mUniforms[NUM_UNIFORMS*mNumEffects+10] = Float.MAX_VALUE;
230
        mUniforms[NUM_UNIFORMS*mNumEffects+11] = 0.0f;
231
        mInter[1][mNumEffects] = null;
232
        }
233

    
234
      if( ve.mCenter instanceof Dynamic3D)
235
        {
236
        mInter[2][mNumEffects] = (Dynamic3D)ve.mCenter;
237
        }
238
      else if( ve.mCenter instanceof Static3D)
239
        {
240
        mInter[2][mNumEffects] = null;
241
        mCache[NUM_CACHE*mNumEffects  ] = ((Static3D)ve.mCenter).getX();
242
        mCache[NUM_CACHE*mNumEffects+1] = ((Static3D)ve.mCenter).getY();
243
        mCache[NUM_CACHE*mNumEffects+2] = ((Static3D)ve.mCenter).getZ();
244
        }
245

    
246
      long ret= addBase(ve);
247

    
248
      postprocess(mNumEffects-1); //addBase just incremented mNumEffects
249

    
250
      return ret;
251
      }
252

    
253
    return -1;
254
    }
255
  }
(7-7/29)