Project

General

Profile

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

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

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

    
22
import android.opengl.GLES30;
23

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

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

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

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

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

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

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

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

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

    
102
      if( mInter[1][i]!=null ) mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+8, mCurrentDuration[i], step);
103
      if( mInter[2][i]!=null ) mInter[2][i].interpolateMain(mCache   , NUM_CACHE*i     , mCurrentDuration[i], step);
104
      }
105
     
106
    mTime = currTime;  
107
    }  
108
  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

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

    
119
    mCache[NUM_CACHE*index  ] = mCache[NUM_CACHE*(index+1)  ];
120
    mCache[NUM_CACHE*index+1] = mCache[NUM_CACHE*(index+1)+1];
121
    mCache[NUM_CACHE*index+2] = mCache[NUM_CACHE*(index+1)+2];
122

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

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

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

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

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179

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

    
192
        if( ve.mStatic0 != null )
193
          {
194
          Static s = ve.mStatic0;
195

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

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

    
217
          float z = tmp.getZ();
218

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

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

    
248
      long ret= addBase(ve);
249

    
250
      postprocess(mNumEffects-1); //addBase just incremented mNumEffects
251

    
252
      return ret;
253
      }
254

    
255
    return -1;
256
    }
257
  }
(21-21/24)