Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueVertex.java @ 9351ad55

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
///////////////////////////////////////////////////////////////////////////////////////////////////
25

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

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42

    
43
  static void getUniforms(int mProgramH)
44
    {
45
    mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "vNumEffects");
46
    mTypeH      = GLES20.glGetUniformLocation( mProgramH, "vType");
47
    mUniformsH  = GLES20.glGetUniformLocation( mProgramH, "vUniforms");
48
    }
49

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

    
96
  protected void moveEffect(int index)
97
    {
98
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
99
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
100
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
101
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
102
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
103
    mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
104
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
105
    mUniforms[NUM_UNIFORMS*index+7] = mUniforms[NUM_UNIFORMS*(index+1)+7];
106
    mUniforms[NUM_UNIFORMS*index+8] = mUniforms[NUM_UNIFORMS*(index+1)+8];
107
    }
108
   
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110

    
111
  synchronized void send() 
112
    {
113
    GLES20.glUniform1i( mNumEffectsH, mNumEffects);
114
      
115
    if( mNumEffects>0 )
116
      {     
117
      GLES20.glUniform1iv( mTypeH    ,  mNumEffects, mType    ,0);
118
      GLES20.glUniform3fv( mUniformsH,3*mNumEffects, mUniforms,0);
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

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

    
160
      return addPriv(eln,region);
161
      }
162
      
163
    return -1;
164
    }
165
   
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
  
168
  synchronized long add(EffectNames eln, Interpolator inter, Float4D region, Float2D point)
169
    {
170
    if( mMax[INDEX]>mNumEffects )
171
      {
172
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects);    
173
      
174
      mInterI[mNumEffects] = inter;
175
      mInterP[mNumEffects] = null;
176
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.x-mObjHalfX;
177
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.y+mObjHalfY;
178
     
179
      return addPriv(eln,region);
180
      }
181
      
182
    return -1;
183
    }
184
 
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186
  
187
  synchronized long add(EffectNames eln, float v1, float v2, float v3, Float4D region, Float2D point)
188
    {
189
    if( mMax[INDEX]>mNumEffects )
190
      {
191
      EffectNames.fillWithUnities(eln.ordinal(), mUniforms, NUM_UNIFORMS*mNumEffects); 
192
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = v1;
193
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = v2;  
194
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = v3;  
195
     
196
      mInterI[mNumEffects] = null;
197
      mInterP[mNumEffects] = null;
198
      mUniforms[NUM_UNIFORMS*mNumEffects+7] = point.x-mObjHalfX;
199
      mUniforms[NUM_UNIFORMS*mNumEffects+8] =-point.y+mObjHalfY;
200
      
201
      return addPriv(eln,region);    
202
      }
203
      
204
    return -1;
205
    }
206
  
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
  
209
  private long addPriv(EffectNames eln, Float4D region)
210
    {    
211
    if( region!=null )
212
      {
213
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = region.x;
214
      mUniforms[NUM_UNIFORMS*mNumEffects+4] =-region.y;   // invert y already
215
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = region.z<=0.0f ? 1000*mObjHalfX : region.z;
216
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = region.w;
217
      }
218
    else
219
      {
220
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = 0.0f;
221
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = 0.0f;
222
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = 1000*mObjHalfX;
223
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = 0.0f;
224
      }
225
    
226
    return addBase(eln);
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230
// end of VertexEffect  
231
  }
(16-16/30)