Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / Effect.java @ 2f1f7570

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
///////////////////////////////////////////////////////////////////////////////////////////////////
23 faa3ff56 Leszek Koltunski
/**
24
 * Abstract Effect of any type.
25
 */
26 8eccf334 Leszek Koltunski
public abstract class Effect
27
  {
28 da9b3f07 Leszek Koltunski
  private final static int MAX_UNITY_DIM = 4;
29 c3651001 Leszek Koltunski
  private final static int NUM_EFFECTS = EffectName.LENGTH;
30 da9b3f07 Leszek Koltunski
31 b547aaba leszek
  private final long mID;
32 da9b3f07 Leszek Koltunski
  private final EffectType mType;
33
  private final EffectName mName;
34 b547aaba leszek
  private final int mDimension;
35
  private final boolean mSupportsR;
36
  private final boolean mSupportsC;
37 8eccf334 Leszek Koltunski
38
  private static long mNextID = 0;
39
40 da9b3f07 Leszek Koltunski
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
41
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
42 8eccf334 Leszek Koltunski
43 9af837e8 leszek
  static boolean[] mEnabled = new boolean[NUM_EFFECTS];
44
45
  static
46
    {
47
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
48
    }
49
50 15aa7d94 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
51
52 faa3ff56 Leszek Koltunski
  Effect(EffectName name)
53
    {
54
    mName      = name;
55
    mType      = name.getType();
56
    mDimension = name.getDimension();
57
    mSupportsC = name.supportsCenter();
58
    mSupportsR = name.supportsRegion();
59 15aa7d94 Leszek Koltunski
60 faa3ff56 Leszek Koltunski
    int n = name.ordinal();
61
    float[] u = name.getUnity();
62 2f1f7570 Leszek Koltunski
    int l = u.length;
63 8eccf334 Leszek Koltunski
64 faa3ff56 Leszek Koltunski
    for(int i=0; i<l; i++)
65
      {
66
      mUnity[n*MAX_UNITY_DIM+i] = u[i];
67
      }
68
69
    mUnityDim[n] = l;
70
71
    mID = ((mNextID++)<<EffectType.LENGTH) + mType.ordinal();
72
    }
73
74
///////////////////////////////////////////////////////////////////////////////////////////////////
75
/**
76
 * Only for use by the library itself.
77
 *
78
 * @y.exclude
79
 */
80 b547aaba leszek
  public static void onDestroy()
81 8eccf334 Leszek Koltunski
    {
82 b547aaba leszek
    mNextID = 0;
83 9af837e8 leszek
84
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
85 2f1f7570 Leszek Koltunski
86
    MatrixEffect.destroyStatics();
87
    VertexEffect.destroyStatics();
88
    PostprocessEffect.destroyStatics();
89 8eccf334 Leszek Koltunski
    }
90
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92 faa3ff56 Leszek Koltunski
/**
93
 * Only for use by the library itself.
94
 *
95
 * @y.exclude
96
 */
97
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
98 8eccf334 Leszek Koltunski
99 faa3ff56 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
100
// PUBLIC API
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102
/**
103
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
104
 * null Effect?
105
 */
106 da9b3f07 Leszek Koltunski
  public boolean isUnity(float[] buffer, int index)
107 8eccf334 Leszek Koltunski
    {
108 da9b3f07 Leszek Koltunski
    int name = mName.ordinal();
109
110
    switch(mUnityDim[name])
111
      {
112
      case 0: return true;
113
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
114
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
115
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
116
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
117
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
118
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
119
      case 4: 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
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
123
      }
124
125
    return false;
126 b547aaba leszek
    }
127 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
128 faa3ff56 Leszek Koltunski
/**
129
 * Return the EffectType enum corresponding to this Effect.
130
 *
131
 * @see EffectType
132
 */
133 da9b3f07 Leszek Koltunski
  public EffectType getType()
134 b547aaba leszek
    {
135
    return mType;
136
    }
137 8eccf334 Leszek Koltunski
138 6d62a900 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
139 faa3ff56 Leszek Koltunski
/**
140
 * Return the EffectName enum corresponding to this Effect.
141
 *
142
 * @see EffectName
143
 */
144 da9b3f07 Leszek Koltunski
  public EffectName getName()
145 6d62a900 Leszek Koltunski
    {
146
    return mName;
147
    }
148
149 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
150 faa3ff56 Leszek Koltunski
/**
151
 * Return the unique ID of this Effect.
152
 */
153 b547aaba leszek
  public long getID()
154
    {
155
    return mID;
156 8eccf334 Leszek Koltunski
    }
157
158 6bb59aad Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
159 faa3ff56 Leszek Koltunski
/**
160
 * Return a printable name of this Effect.
161
 */
162 6bb59aad Leszek Koltunski
  public String getString()
163
    {
164 da9b3f07 Leszek Koltunski
    return mName.name();
165 6bb59aad Leszek Koltunski
    }
166
167 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
168 faa3ff56 Leszek Koltunski
/**
169
 * Does this effect have a Center?
170
 */
171 b547aaba leszek
  public boolean supportsCenter()
172 8eccf334 Leszek Koltunski
    {
173 b547aaba leszek
    return mSupportsC;
174 8eccf334 Leszek Koltunski
    }
175
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177 faa3ff56 Leszek Koltunski
/**
178
 * Does this effect support being masked by a Region?
179
 */
180 b547aaba leszek
  public boolean supportsRegion()
181 8eccf334 Leszek Koltunski
    {
182 b547aaba leszek
    return mSupportsR;
183 8eccf334 Leszek Koltunski
    }
184
185
///////////////////////////////////////////////////////////////////////////////////////////////////
186 faa3ff56 Leszek Koltunski
/**
187
 * Return the number of Uniforms needed to describe this effect.
188
 */
189 b547aaba leszek
  public int getDimension()
190 8eccf334 Leszek Koltunski
    {
191 b547aaba leszek
    return mDimension;
192 8eccf334 Leszek Koltunski
    }
193
  }