Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
/**
24
 * Abstract Effect of any type.
25
 */
26
public abstract class Effect
27
  {
28
  private final static int MAX_UNITY_DIM = 4;
29
  private final static int NUM_EFFECTS = EffectName.LENGTH;
30

    
31
  private final long mID;
32
  private final EffectType mType;
33
  private final EffectName mName;
34
  private final int mDimension;
35
  private final int mRegionDim;
36
  private final int mCenterDim;
37

    
38
  private static long mNextID = 0;
39

    
40
  private final static float[] mUnity= new float[MAX_UNITY_DIM*NUM_EFFECTS];
41
  private final static int[]   mUnityDim = new int[NUM_EFFECTS];
42

    
43
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
51

    
52
  Effect(EffectName name)
53
    {
54
    mName      = name;
55
    mType      = name.getType();
56
    mDimension = name.getEffectDimension();
57
    mCenterDim = name.getCenterDimension();
58
    mRegionDim = name.getRegionDimension();
59

    
60
    int n = name.ordinal();
61
    float[] u = name.getUnity();
62
    int l = u.length;
63

    
64
    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
  public static void onDestroy()
81
    {
82
    mNextID = 0;
83

    
84
    for(int i=0; i<NUM_EFFECTS; i++) mEnabled[i] = false;
85

    
86
    MatrixEffect.destroyStatics();
87
    VertexEffect.destroyStatics();
88
    FragmentEffect.destroyStatics();
89
    PostprocessEffect.destroyStatics();
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
/**
94
 * Only for use by the library itself.
95
 *
96
 * @y.exclude
97
 */
98
  public abstract boolean compute(float[] uniforms, int index, long currentDuration, long step );
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
// PUBLIC API
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
/**
104
 * Do the set of Uniforms written in buffer[index], buffer[index+1], etc represent a Unity, i.e a
105
 * null Effect?
106
 */
107
  public boolean isUnity(float[] buffer, int index)
108
    {
109
    int name = mName.ordinal();
110

    
111
    switch(mUnityDim[name])
112
      {
113
      case 0: return true;
114
      case 1: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ];
115
      case 2: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
116
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1];
117
      case 3: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
118
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
119
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2];
120
      case 4: return buffer[index  ]==mUnity[MAX_UNITY_DIM*name  ] &&
121
                     buffer[index+1]==mUnity[MAX_UNITY_DIM*name+1] &&
122
                     buffer[index+2]==mUnity[MAX_UNITY_DIM*name+2] &&
123
                     buffer[index+3]==mUnity[MAX_UNITY_DIM*name+3];
124
      }
125

    
126
    return false;
127
    }
128
///////////////////////////////////////////////////////////////////////////////////////////////////
129
/**
130
 * Return the EffectType enum corresponding to this Effect.
131
 *
132
 * @see EffectType
133
 */
134
  public EffectType getType()
135
    {
136
    return mType;
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
/**
141
 * Return the EffectName enum corresponding to this Effect.
142
 *
143
 * @see EffectName
144
 */
145
  public EffectName getName()
146
    {
147
    return mName;
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152
 * Return the unique ID of this Effect.
153
 */
154
  public long getID()
155
    {
156
    return mID;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
/**
161
 * Return a printable name of this Effect.
162
 */
163
  public String getString()
164
    {
165
    return mName.name();
166
    }
167

    
168
///////////////////////////////////////////////////////////////////////////////////////////////////
169
/**
170
 * Return the dimension of the Center supported by this effect (0- no center supported at all).
171
 */
172
  public int getCenterDimension()
173
    {
174
    return mCenterDim;
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178
/**
179
 * Return the dimension of the Region supported by this effect (0- no region supported at all).
180
 */
181
  public int getRegionDimension()
182
    {
183
    return mRegionDim;
184
    }
185

    
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
/**
188
 * Return the number of Uniforms needed to describe this effect.
189
 */
190
  public int getEffectDimension()
191
    {
192
    return mDimension;
193
    }
194
  }
(1-1/26)