Project

General

Profile

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

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

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

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

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

    
92
      if( mInter[1][i]!=null ) mInter[2][i].interpolateMain( mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i], step);
93
      if( mInter[2][i]!=null ) mInter[1][i].interpolateMain( mCache   , NUM_CACHE*i     , 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

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

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

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

    
135
///////////////////////////////////////////////////////////////////////////////////////////////////
136

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

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

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

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

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

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

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

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

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

    
208
      return addBase(fe);
209
      }
210
      
211
    return -1;
212
    }
213
  }
(18-18/24)