Project

General

Profile

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

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

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.type.Float2D;
25
import org.distorted.library.type.Float4D;
26
import org.distorted.library.type.Interpolator;
27
import org.distorted.library.type.Interpolator2D;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class EffectQueueVertex extends EffectQueue
32
  { 
33
  private static final int NUM_UNIFORMS = 9;
34
  private static final int INDEX = EffectTypes.VERTEX.ordinal();
35
  private static int mNumEffectsH;
36
  private static int mTypeH;
37
  private static int mUniformsH;
38
  
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
  public EffectQueueVertex(DistortedObject obj)
42
    { 
43
    super(obj,NUM_UNIFORMS,INDEX);
44
    }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  static void getUniforms(int mProgramH)
49
    {
50
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "vNumEffects");
51
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "vType");
52
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "vUniforms");
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
  
57
  synchronized void compute(long currTime) 
58
    {
59
    if( currTime==mTime ) return;
60
    if( mTime==0 ) mTime = currTime;
61
    long step = (currTime-mTime);
62
   
63
    for(int i=0; i<mNumEffects; i++)
64
      {
65
      if( mInterI[i]==null ) continue;    
66
      
67
      if( mInterP[i]!=null ) 
68
        {
69
        mInterP[i].interpolateMain(mUniforms, NUM_UNIFORMS*i+7, mCurrentDuration[i]);
70
      
71
        mUniforms[NUM_UNIFORMS*i+7] = mUniforms[NUM_UNIFORMS*i+7]-mObjHalfX;
72
        mUniforms[NUM_UNIFORMS*i+8] =-mUniforms[NUM_UNIFORMS*i+8]+mObjHalfY;
73
        }
74
        
75
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )      
76
        {
77
        for(int j=0; j<mNumListeners; j++)   
78
          EffectMessageSender.newMessage( mListeners.elementAt(j),
79
                                          EffectMessage.EFFECT_FINISHED, 
80
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.VERTEX.type,
81
                                          mType[i], 
82
                                          mBitmapID,
83
                                          null);
84
      
85
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
86
          {
87
          remove(i);
88
          i--;
89
          continue;
90
          }
91
        }
92
     
93
      mCurrentDuration[i] += step;
94
      }
95
     
96
    mTime = currTime;  
97
    }  
98
  
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  protected void moveEffect(int index)
102
    {
103
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
104
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
105
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
106
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
107
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
108
    mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
109
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
110
    mUniforms[NUM_UNIFORMS*index+7] = mUniforms[NUM_UNIFORMS*(index+1)+7];
111
    mUniforms[NUM_UNIFORMS*index+8] = mUniforms[NUM_UNIFORMS*(index+1)+8];
112
    }
113
   
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  synchronized void send() 
117
    {
118
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
119
      
120
    if( mNumEffects>0 )
121
      {     
122
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
123
      GLES20.glUniform3fv( mUniformsH,3*mNumEffects, mUniforms,0);
124
      }
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  synchronized void sendZero() 
130
    {
131
    GLES20.glUniform1i( mNumEffectsH, 0);
132
    }
133
  
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135
// Do various post-processing on already computed effects.
136
// 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.
137
// 2) in case of swirl, pre-compute the sine and cosine of its rotation angle
138
  
139
  void postprocess()
140
    {
141
    double d;  
142
     
143
    for(int i=0; i<mNumEffects; i++)
144
      {      
145
      if( mType[i]==EffectNames.SWIRL.ordinal() )
146
        {
147
        d = Math.PI*mUniforms[NUM_UNIFORMS*i]/180;  
148
        mUniforms[NUM_UNIFORMS*i+1] = (float)Math.sin(d);
149
        mUniforms[NUM_UNIFORMS*i+2] = (float)Math.cos(d);
150
        }
151
      }
152
    }
153
  
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155
  
156
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Interpolator2D point)
157
    {
158
    if( mMax[INDEX]>mNumEffects )
159
      {
160
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
161
      
162
      mInterI[mNumEffects] = inter;
163
      mInterP[mNumEffects] = point;
164

    
165
      return addPriv(eln,region);
166
      }
167
      
168
    return -1;
169
    }
170
   
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172
  
173
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Float2D point)
174
    {
175
    if( mMax[INDEX]>mNumEffects )
176
      {
177
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
178
      
179
      mInterI[mNumEffects] = inter;
180
      mInterP[mNumEffects] = null;
181
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.getX()-mObjHalfX;
182
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.getY()+mObjHalfY;
183
     
184
      return addPriv(eln,region);
185
      }
186
      
187
    return -1;
188
    }
189
 
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191
  
192
  synchronized long add(EffectNames eln, float v1, float v2, float v3, Float4D region, Float2D point)
193
    {
194
    if( mMax[INDEX]>mNumEffects )
195
      {
196
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
197
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = v1;
198
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = v2;  
199
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = v3;  
200
     
201
      mInterI[mNumEffects] = null;
202
      mInterP[mNumEffects] = null;
203
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.getX()-mObjHalfX;
204
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.getY()+mObjHalfY;
205
      
206
      return addPriv(eln,region);    
207
      }
208
      
209
    return -1;
210
    }
211
  
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
  
214
  private long addPriv(EffectNames eln, Float4D region)
215
    {    
216
    if( region!=null )
217
      {
218
      float z = region.getZ();
219

    
220
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = region.getX();
221
      mUniforms[NUM_UNIFORMS*mNumEffects+4] =-region.getY();   // invert y already
222
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = z<=0.0f ? 1000*mObjHalfX : z;
223
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = region.getW();
224
      }
225
    else
226
      {
227
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = 0.0f;
228
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = 0.0f;
229
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = 1000*mObjHalfX;
230
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = 0.0f;
231
      }
232
    
233
    return addBase(eln);
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// end of VertexEffect  
238
  }
(16-16/20)