Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueVertex.java @ 568b29d8

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.Dynamic;
26
import org.distorted.library.type.Dynamic2D;
27
import org.distorted.library.type.Static2D;
28
import org.distorted.library.type.Static4D;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

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

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

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

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57
  
58
  synchronized void compute(long currTime) 
59
    {
60
    if( currTime==mTime ) return;
61
    if( mTime==0 ) mTime = currTime;
62
    long step = (currTime-mTime);
63
   
64
    for(int i=0; i<mNumEffects; i++)
65
      {
66
      if( mInterI[i]==null ) continue;    
67
      
68
      if( mInterP[i]!=null ) 
69
        {
70
        mInterP[i].interpolateMain(mUniforms, NUM_UNIFORMS*i+7, mCurrentDuration[i]);
71
      
72
        mUniforms[NUM_UNIFORMS*i+7] = mUniforms[NUM_UNIFORMS*i+7]-mObjHalfX;
73
        mUniforms[NUM_UNIFORMS*i+8] =-mUniforms[NUM_UNIFORMS*i+8]+mObjHalfY;
74
        }
75
        
76
      if( mInterI[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
                                          mType[i], 
83
                                          mBitmapID,
84
                                          null);
85
      
86
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i) )
87
          {
88
          remove(i);
89
          i--;
90
          continue;
91
          }
92
        }
93
     
94
      mCurrentDuration[i] += step;
95
      }
96
     
97
    mTime = currTime;  
98
    }  
99
  
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

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

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

    
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129

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

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

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

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
// end of VertexEffect  
239
  }
(16-16/17)