Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / EffectQueueFragment.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.Dynamic4D;
26
import org.distorted.library.type.Static;
27
import org.distorted.library.type.Static1D;
28
import org.distorted.library.type.Static2D;
29
import org.distorted.library.type.Static3D;
30
import org.distorted.library.type.Static4D;
31
import org.distorted.library.type.Static5D;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  static void getUniforms(int mProgramH)
54
    {
55
    mNumEffectsH= GLES30.glGetUniformLocation( mProgramH, "fNumEffects");
56
    mTypeH      = GLES30.glGetUniformLocation( mProgramH, "fType");
57
    mUniformsH  = GLES30.glGetUniformLocation( mProgramH, "fUniforms");
58
    }
59

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

    
72
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
73
        {
74
        for(int j=0; j<mNumListeners; j++)   
75
          EffectMessageSender.newMessage( mListeners.elementAt(j),
76
                                          EffectMessage.EFFECT_FINISHED,
77
                                          (mID[i]<<Effect.LENGTH)+Effect.FRAGMENT,
78
                                          mName[i],
79
                                          mObjectID);
80
      
81
        if( FragmentEffect.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
82
          {
83
          remove(i);
84
          i--;
85
          continue;
86
          }
87
        else mInter[0][i] = null;
88
        }
89

    
90
      if( mInter[1][i]!=null ) mInter[2][i].interpolateMain( mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i], step);
91
      if( mInter[2][i]!=null ) mInter[1][i].interpolateMain( mCache   , NUM_CACHE*i     , mCurrentDuration[i], step);
92
      }
93
   
94
    mTime = currTime;  
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98

    
99
  protected void moveEffect(int index)
100
    {
101
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
102
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
103
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
104
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
105

    
106
    mCache[NUM_CACHE*index  ] = mCache[NUM_CACHE*(index+1)  ];
107
    mCache[NUM_CACHE*index+1] = mCache[NUM_CACHE*(index+1)+1];
108
    mCache[NUM_CACHE*index+2] = mCache[NUM_CACHE*(index+1)+2];
109
    mCache[NUM_CACHE*index+3] = mCache[NUM_CACHE*(index+1)+3];
110
    }
111
  
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113
  
114
  synchronized void send(float halfX, float halfY)
115
    {
116
    GLES30.glUniform1i( mNumEffectsH, mNumEffects);
117

    
118
    if( mNumEffects>0 )
119
      {
120
      for(int i=0; i<mNumEffects; i++)
121
        {
122
        mUniforms[NUM_UNIFORMS*i+4] = mCache[NUM_CACHE*i  ]-halfX;
123
        mUniforms[NUM_UNIFORMS*i+5] =-mCache[NUM_CACHE*i+1]+halfY;
124
        mUniforms[NUM_UNIFORMS*i+6] = mCache[NUM_CACHE*i+2];
125
        mUniforms[NUM_UNIFORMS*i+7] = mCache[NUM_CACHE*i+3];
126
        }
127

    
128
      GLES30.glUniform1iv( mTypeH    ,                 mNumEffects, mName    ,0);
129
      GLES30.glUniform4fv( mUniformsH,(NUM_UNIFORMS/4)*mNumEffects, mUniforms,0);
130
      }  
131
    }
132

    
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134

    
135
  synchronized long add(FragmentEffect fe)
136
    {
137
    if( mMax[INDEX]>mNumEffects )
138
      {
139
      int dim0 = 0;
140

    
141
      if( fe.mDynamic0 != null )
142
        {
143
        mInter[0][mNumEffects] = fe.mDynamic0;
144
        dim0 = fe.mDynamic0.getDimension();
145
        }
146
      else
147
        {
148
        mInter[0][mNumEffects] = null;
149

    
150
        if( fe.mStatic0 != null )
151
          {
152
          Static s = fe.mStatic0;
153
          dim0 = s.getDimension();
154

    
155
          switch( dim0 )
156
            {
157
            case 5 : mUniforms[NUM_UNIFORMS*mNumEffects + 4] = ((Static5D)s).getV();
158
            case 4 : mUniforms[NUM_UNIFORMS*mNumEffects + 3] = ((Static4D)s).getW();
159
            case 3 : mUniforms[NUM_UNIFORMS*mNumEffects + 2] = ((Static3D)s).getZ();
160
            case 2 : mUniforms[NUM_UNIFORMS*mNumEffects + 1] = ((Static2D)s).getY();
161
            case 1 : mUniforms[NUM_UNIFORMS*mNumEffects    ] = ((Static1D)s).getX();
162
            }
163
          }
164
        }
165

    
166
      if( fe.mDynamic1 != null )
167
        {
168
        mInter[1][mNumEffects] = fe.mDynamic1;
169
        }
170
      else
171
        {
172
        mInter[1][mNumEffects] = null;
173

    
174
        if( fe.mStatic1 != null )
175
          {
176
          Static s = fe.mStatic1;
177
          int dim1 = s.getDimension();
178

    
179
          switch( dim1 )
180
            {
181
            case 5 : mUniforms[NUM_UNIFORMS*mNumEffects + dim0 + 4] = ((Static5D)s).getV();
182
            case 4 : mUniforms[NUM_UNIFORMS*mNumEffects + dim0 + 3] = ((Static4D)s).getW();
183
            case 3 : mUniforms[NUM_UNIFORMS*mNumEffects + dim0 + 2] = ((Static3D)s).getZ();
184
            case 2 : mUniforms[NUM_UNIFORMS*mNumEffects + dim0 + 1] = ((Static2D)s).getY();
185
            case 1 : mUniforms[NUM_UNIFORMS*mNumEffects + dim0    ] = ((Static1D)s).getX();
186
            }
187
          }
188
        }
189

    
190
      if( fe.mRegion instanceof Dynamic4D)
191
        {
192
        mInter[2][mNumEffects] = (Dynamic4D)fe.mRegion;
193
        }
194
      else if( fe.mRegion instanceof Static4D )
195
        {
196
        mInter[2][mNumEffects]  = null;
197

    
198
        Static4D s = (Static4D)fe.mRegion;
199
        mCache[NUM_CACHE*mNumEffects  ] = s.getX();
200
        mCache[NUM_CACHE*mNumEffects+1] = s.getY();
201
        mCache[NUM_CACHE*mNumEffects+2] = s.getZ();
202
        mCache[NUM_CACHE*mNumEffects+3] = s.getW();
203
        }
204
      else return -1;
205

    
206
      return addBase(fe);
207
      }
208
      
209
    return -1;
210
    }
211
  }
(4-4/29)