Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffect.java @ e4577791

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.effect;
22

    
23
import org.distorted.library.effectqueue.EffectQueue;
24
import org.distorted.library.type.Static1D;
25
import org.distorted.library.type.Static3D;
26
import org.distorted.library.type.Static4D;
27
import org.distorted.library.type.Static5D;
28

    
29
import java.lang.reflect.Method;
30
import java.util.ArrayList;
31

    
32
///////////////////////////////////////////////////////////////////////////////////////////////////
33
/**
34
 * Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
35
 */
36
public abstract class VertexEffect extends Effect
37
  {
38
  /**
39
   * 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
40
   */
41
  public static final int NUM_FLOAT_UNIFORMS = 12;
42
  /**
43
   * 4: name, AND association, reserved, EQU Association
44
   */
45
  public static final int NUM_INT_UNIFORMS = 4;
46

    
47
  static final int VALUES_OFFSET = 0;
48
  static final int CENTER_OFFSET = 5;
49
  static final int REGION_OFFSET = 8;
50
  private static String mGLSL = "";
51
  private static int mNumEnabled = 0;
52

    
53
  private static String mFullGLSL = "";
54
  private static int mFullEnabled = 0;
55
  private static boolean mFullPrepared = false;
56

    
57
  final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
58

    
59
  ArrayList<EffectQueue> mQueues;
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  VertexEffect(EffectName name)
64
    {
65
    super(name);
66
    }
67

    
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69

    
70
  private static String retSection(int effect, String code)
71
    {
72
    return
73

    
74
        "if( vProperties[i].x =="+effect+" )\n" +
75
          "{\n" +
76
           code +"\n" +
77
          "}\n" +
78
        "else\n";
79
    }
80

    
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
83

    
84
  private static void prepareFull()
85
    {
86
    Method method;
87

    
88
    for(EffectName name: EffectName.values())
89
      {
90
      if( name.getType() == EffectType.VERTEX )
91
        {
92
        Class<? extends Effect> cls = name.getEffectClass();
93

    
94
        try
95
          {
96
          method = cls.getDeclaredMethod("code");
97
          }
98
        catch(NoSuchMethodException ex)
99
          {
100
          android.util.Log.e("Effect", "exception getting method: "+ex.getMessage());
101
          method = null;
102
          }
103

    
104
        try
105
          {
106
          if( method!=null )
107
            {
108
            Object value = method.invoke(null);
109
            String code = (String)value;
110
            mFullGLSL += retSection(name.ordinal(),code);
111
            mFullEnabled++;
112
            }
113
          }
114
        catch(Exception ex)
115
          {
116
          android.util.Log.e("Effect", "exception invoking method: "+ex.getMessage());
117
          }
118
        }
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// prepare code to be injected into the 'main_vertex_shader' main() function.
124

    
125
  static void addEffect(EffectName name, String code)
126
    {
127
    int effect = name.ordinal();
128

    
129
    if( !mEnabled[effect] )
130
      {
131
      mEnabled[effect] = true;
132
      mNumEnabled ++;
133
      mGLSL += retSection(effect,code);
134
      }
135
    }
136

    
137
///////////////////////////////////////////////////////////////////////////////////////////////////
138
/**
139
 * Only for use by the library itself.
140
 *
141
 * @y.exclude
142
 */
143
  public static String getGLSL()
144
    {
145
    return mGLSL + "{}";
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
/**
150
 * Only for use by the library itself.
151
 *
152
 * @y.exclude
153
 */
154
  public static String getAllGLSL()
155
    {
156
    if( !mFullPrepared )
157
      {
158
      prepareFull();
159
      mFullPrepared = true;
160
      }
161

    
162
    return mFullGLSL + "{}";
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
/**
167
 * Only for use by the library itself.
168
 *
169
 * @y.exclude
170
 */
171
  public static int getAllEnabled()
172
    {
173
    if( !mFullPrepared )
174
      {
175
      prepareFull();
176
      mFullPrepared = true;
177
      }
178

    
179
    return mFullEnabled;
180
    }
181

    
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183

    
184
  static void destroyStatics()
185
    {
186
    mNumEnabled = 0;
187
    mGLSL = "";
188
    mFullEnabled= 0;
189
    mFullGLSL = "";
190
    mFullPrepared = false;
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194
// PUBLIC API
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
/**
197
 * Return the number of Vertex effects enabled.
198
 */
199
  public static int getNumEnabled()
200
    {
201
    return mNumEnabled;
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
/**
206
 * Only for use by the library itself.
207
 *
208
 * @y.exclude
209
 */
210
  public void addQueue(EffectQueue queue)
211
    {
212
    if( mQueues==null ) mQueues = new ArrayList<>();
213

    
214
    mQueues.add(queue);
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
/**
219
 * Only for use by the library itself.
220
 *
221
 * @y.exclude
222
 */
223
  public void remQueue(EffectQueue queue)
224
    {
225
    if( mQueues==null ) mQueues = new ArrayList<>();
226

    
227
    mQueues.remove(queue);
228
    }
229

    
230
///////////////////////////////////////////////////////////////////////////////////////////////////
231
/**
232
 * Set Mesh association.
233
 *
234
 * This creates an association between a Component of a Mesh and this Vertex Effect.
235
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
236
 * will only be active on vertices of Components such that
237
 *
238
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
239
 *
240
 * (see main_vertex_shader)
241
 *
242
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
243
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
244
 */
245
  public void setMeshAssociation(int andAssociation, int equAssociation)
246
    {
247
    mAndAssociation = andAssociation;
248
    mEquAssociation = equAssociation;
249

    
250
    long id = getID();
251
    int numQueues = mQueues==null ? 0: mQueues.size();
252

    
253
    for(int i=0; i<numQueues; i++)
254
      {
255
      EffectQueue queue = mQueues.get(i);
256
      queue.setAssociation(id);
257
      }
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
/**
262
 * Return a constructed VertexEffect based on its name, the 5 variables, center & region.
263
 */
264
  public static VertexEffect constructEffect(String name,float[] vars, float[] center, float[] region)
265
    {
266
    Static3D staCenter = new Static3D(center[0],center[1],center[2]);
267
    Static4D staRegion = new Static4D(region[0],region[1],region[2],region[3]);
268

    
269
    if( name.equals(EffectName.DISTORT.name()) )
270
      {
271
      Static3D staVars = new Static3D(vars[2],vars[3],vars[4]);
272
      return new VertexEffectDistort(staVars,staCenter,staRegion);
273
      }
274
    if( name.equals(EffectName.DEFORM.name()) )
275
      {
276
      Static3D staVars   = new Static3D(vars[1],vars[2],vars[3]);
277
      Static1D staRadius = new Static1D(vars[4]);
278
      return new VertexEffectDeform(staVars,staRadius,staCenter,staRegion);
279
      }
280
    if( name.equals(EffectName.SINK.name()) )
281
      {
282
      Static1D staSink = new Static1D(vars[4]);
283
      return new VertexEffectSink(staSink,staCenter,staRegion);
284
      }
285
    if( name.equals(EffectName.PINCH.name()) )
286
      {
287
      Static3D staVars = new Static3D(vars[2],vars[3],vars[4]);
288
      return new VertexEffectPinch(staVars,staCenter,staRegion);
289
      }
290
    if( name.equals(EffectName.SWIRL.name()) )
291
      {
292
      Static1D staSwirl = new Static1D(vars[4]);
293
      return new VertexEffectSwirl(staSwirl,staCenter,staRegion);
294
      }
295
    if( name.equals(EffectName.WAVE.name()) )
296
      {
297
      Static5D staWave = new Static5D(vars[0],vars[1],vars[2],vars[3],vars[4]);
298
      return new VertexEffectWave(staWave,staCenter,staRegion);
299
      }
300
    if( name.equals(EffectName.DISAPPEAR.name()) )
301
      {
302
      return new VertexEffectDisappear();
303
      }
304
    if( name.equals(EffectName.VERTEX_MOVE.name()) )
305
      {
306
      Static3D staVector = new Static3D(vars[2],vars[3],vars[4]);
307
      return new VertexEffectMove(staVector);
308
      }
309
    if( name.equals(EffectName.VERTEX_QUATERNION.name()) )
310
      {
311
      Static4D staQuat = new Static4D(vars[1],vars[2],vars[3],vars[4]);
312
      return new VertexEffectQuaternion(staQuat,staCenter);
313
      }
314
    if( name.equals(EffectName.VERTEX_ROTATE.name()) )
315
      {
316
      Static1D staAngle = new Static1D(vars[1]);
317
      Static3D staAxis  = new Static3D(vars[2],vars[3],vars[4]);
318
      return new VertexEffectRotate(staAngle,staAxis,staCenter);
319
      }
320
    if( name.equals(EffectName.VERTEX_SCALE.name()) )
321
      {
322
      Static3D staScale = new Static3D(vars[2],vars[3],vars[4]);
323
      return new VertexEffectScale(staScale);
324
      }
325
    if( name.equals(EffectName.VERTEX_SHEAR.name()) )
326
      {
327
      Static3D staShear = new Static3D(vars[2],vars[3],vars[4]);
328
      return new VertexEffectShear(staShear,staCenter);
329
      }
330

    
331
    return null;
332
    }
333
  }
(22-22/34)