Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ b82a9ac9

1 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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 b82a9ac9 Leszek Koltunski
import java.lang.reflect.Method;
23
24 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
25 faa3ff56 Leszek Koltunski
/**
26
 * Abstract Effect of any type.
27
 */
28 8eccf334 Leszek Koltunski
public abstract class Effect
29
  {
30 da9b3f07 Leszek Koltunski
  private final static int MAX_UNITY_DIM = 4;
31 c3651001 Leszek Koltunski
  private final static int NUM_EFFECTS = EffectName.LENGTH;
32 da9b3f07 Leszek Koltunski
33 b547aaba leszek
  private final long mID;
34 da9b3f07 Leszek Koltunski
  private final EffectType mType;
35
  private final EffectName mName;
36 b547aaba leszek
  private final int mDimension;
37 b24e4719 Leszek Koltunski
  private final int mRegionDim;
38
  private final int mCenterDim;
39 8eccf334 Leszek Koltunski
40
  private static long mNextID = 0;
41
42 da9b3f07 Leszek Koltunski
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
43
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
44 8eccf334 Leszek Koltunski
45 9af837e8 leszek
  static boolean[] mEnabled = new boolean[NUM_EFFECTS];
46
47
  static
48
    {
49
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
50
    }
51
52 15aa7d94 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 faa3ff56 Leszek Koltunski
  Effect(EffectName name)
55
    {
56
    mName      = name;
57
    mType      = name.getType();
58 b24e4719 Leszek Koltunski
    mDimension = name.getEffectDimension();
59
    mCenterDim = name.getCenterDimension();
60
    mRegionDim = name.getRegionDimension();
61 15aa7d94 Leszek Koltunski
62 faa3ff56 Leszek Koltunski
    int n = name.ordinal();
63
    float[] u = name.getUnity();
64 2f1f7570 Leszek Koltunski
    int l = u.length;
65 8eccf334 Leszek Koltunski
66 faa3ff56 Leszek Koltunski
    for(int i=0; i<l; i++)
67
      {
68
      mUnity[n*MAX_UNITY_DIM+i] = u[i];
69
      }
70
71
    mUnityDim[n] = l;
72
73
    mID = ((mNextID++)<<EffectType.LENGTH) + mType.ordinal();
74
    }
75
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
/**
78
 * Only for use by the library itself.
79
 *
80
 * @y.exclude
81
 */
82 b547aaba leszek
  public static void onDestroy()
83 8eccf334 Leszek Koltunski
    {
84 b547aaba leszek
    mNextID = 0;
85 9af837e8 leszek
86
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
87 2f1f7570 Leszek Koltunski
88
    MatrixEffect.destroyStatics();
89
    VertexEffect.destroyStatics();
90 143095f7 Leszek Koltunski
    FragmentEffect.destroyStatics();
91 2f1f7570 Leszek Koltunski
    PostprocessEffect.destroyStatics();
92 8eccf334 Leszek Koltunski
    }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95 faa3ff56 Leszek Koltunski
/**
96
 * Only for use by the library itself.
97
 *
98
 * @y.exclude
99
 */
100
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
101 8eccf334 Leszek Koltunski
102 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
103
// PUBLIC API
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105
/**
106
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
107
 * null Effect?
108
 */
109 da9b3f07 Leszek Koltunski
  public boolean isUnity(float[] buffer, int index)
110 8eccf334 Leszek Koltunski
    {
111 da9b3f07 Leszek Koltunski
    int name = mName.ordinal();
112
113
    switch(mUnityDim[name])
114
      {
115
      case 0: return true;
116
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
117
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
118
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
119
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
120
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
121
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
122
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
123
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
124
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
125
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
126
      }
127
128
    return false;
129 b547aaba leszek
    }
130 b82a9ac9 Leszek Koltunski
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
// this will enable() all Fragment Effects twice (once for smooth variant, once for non-smooth)
133
// but this shouldn't matter.
134
/**
135
 * Enable all effects of a given type.
136
 *
137
 * @param type EffectType to enable.
138
 */
139
  public static void enableEffects(EffectType type)
140
    {
141
    Method method=null;
142
143
    for(EffectName name: EffectName.values())
144
      {
145
      if( name.getType() == type )
146
        {
147
        Class<? extends Effect> cls = name.getEffectClass();
148
149
        try
150
          {
151
          method = cls.getMethod("enable");
152
          }
153
        catch(NoSuchMethodException ex)
154
          {
155
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
156
          }
157
158
        try
159
          {
160
          method.invoke(null);
161
          }
162
        catch(Exception ex)
163
          {
164
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
165
          }
166
        }
167
      }
168
    }
169
170 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
171 faa3ff56 Leszek Koltunski
/**
172
 * Return the EffectType enum corresponding to this Effect.
173
 *
174
 * @see EffectType
175
 */
176 da9b3f07 Leszek Koltunski
  public EffectType getType()
177 b547aaba leszek
    {
178
    return mType;
179
    }
180 8eccf334 Leszek Koltunski
181 6d62a900 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
182 faa3ff56 Leszek Koltunski
/**
183
 * Return the EffectName enum corresponding to this Effect.
184
 *
185
 * @see EffectName
186
 */
187 da9b3f07 Leszek Koltunski
  public EffectName getName()
188 6d62a900 Leszek Koltunski
    {
189
    return mName;
190
    }
191
192 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
193 faa3ff56 Leszek Koltunski
/**
194
 * Return the unique ID of this Effect.
195
 */
196 b547aaba leszek
  public long getID()
197
    {
198
    return mID;
199 8eccf334 Leszek Koltunski
    }
200
201 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
202 faa3ff56 Leszek Koltunski
/**
203
 * Return a printable name of this Effect.
204
 */
205 6bb59aad Leszek Koltunski
  public String getString()
206
    {
207 da9b3f07 Leszek Koltunski
    return mName.name();
208 6bb59aad Leszek Koltunski
    }
209
210 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
211 faa3ff56 Leszek Koltunski
/**
212 b24e4719 Leszek Koltunski
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
213 faa3ff56 Leszek Koltunski
 */
214 b24e4719 Leszek Koltunski
  public int getCenterDimension()
215 8eccf334 Leszek Koltunski
    {
216 b24e4719 Leszek Koltunski
    return mCenterDim;
217 8eccf334 Leszek Koltunski
    }
218
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220 faa3ff56 Leszek Koltunski
/**
221 b24e4719 Leszek Koltunski
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
222 faa3ff56 Leszek Koltunski
 */
223 b24e4719 Leszek Koltunski
  public int getRegionDimension()
224 8eccf334 Leszek Koltunski
    {
225 b24e4719 Leszek Koltunski
    return mRegionDim;
226 8eccf334 Leszek Koltunski
    }
227
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229 faa3ff56 Leszek Koltunski
/**
230
 * Return the number of Uniforms needed to describe this effect.
231
 */
232 b24e4719 Leszek Koltunski
  public int getEffectDimension()
233 8eccf334 Leszek Koltunski
    {
234 b547aaba leszek
    return mDimension;
235 8eccf334 Leszek Koltunski
    }
236
  }