Project

General

Profile

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

library / src / main / java / org / distorted / library / effect / VertexEffect.java @ 8c57d77b

1 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 cc62b34a Leszek Koltunski
// Copyright 2017 Leszek Koltunski  leszek@koltunski.pl                                          //
3 8eccf334 Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 8eccf334 Leszek Koltunski
//                                                                                               //
6 cc62b34a Leszek Koltunski
// 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 8eccf334 Leszek Koltunski
//                                                                                               //
11 cc62b34a Leszek Koltunski
// This library is distributed in the hope that it will be useful,                               //
12 8eccf334 Leszek Koltunski
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13 cc62b34a Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 8eccf334 Leszek Koltunski
//                                                                                               //
16 cc62b34a Leszek Koltunski
// 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 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
package org.distorted.library.effect;
22
23 43814a57 Leszek Koltunski
import org.distorted.library.effectqueue.EffectQueue;
24 8c57d77b Leszek Koltunski
import org.distorted.library.main.DistortedLibrary;
25 e5f796bc Leszek Koltunski
import org.distorted.library.type.Static1D;
26
import org.distorted.library.type.Static3D;
27 dd89c7f4 Leszek Koltunski
import org.distorted.library.type.Static4D;
28 e5f796bc Leszek Koltunski
import org.distorted.library.type.Static5D;
29 dd89c7f4 Leszek Koltunski
30 f046b159 Leszek Koltunski
import java.lang.reflect.Method;
31 43814a57 Leszek Koltunski
import java.util.ArrayList;
32 f046b159 Leszek Koltunski
33 8eccf334 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
34 faa3ff56 Leszek Koltunski
/**
35 a20f274f Leszek Koltunski
 * Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
36 faa3ff56 Leszek Koltunski
 */
37 8eccf334 Leszek Koltunski
public abstract class VertexEffect extends Effect
38
  {
39 96e3b88a Leszek Koltunski
  /**
40
   * 12: 5 per-effect interpolated values, 3-dimensional center, 4-dimensional Region
41
   */
42
  public static final int NUM_FLOAT_UNIFORMS = 12;
43
  /**
44 24804c15 Leszek Koltunski
   * 4: name, AND association, reserved, EQU Association
45 96e3b88a Leszek Koltunski
   */
46 24804c15 Leszek Koltunski
  public static final int NUM_INT_UNIFORMS = 4;
47 96e3b88a Leszek Koltunski
48 b24e4719 Leszek Koltunski
  static final int VALUES_OFFSET = 0;
49
  static final int CENTER_OFFSET = 5;
50
  static final int REGION_OFFSET = 8;
51 7cd24173 leszek
  private static String mGLSL = "";
52
  private static int mNumEnabled = 0;
53 15aa7d94 Leszek Koltunski
54 f046b159 Leszek Koltunski
  private static String mFullGLSL = "";
55
  private static int mFullEnabled = 0;
56
  private static boolean mFullPrepared = false;
57
58 dd89c7f4 Leszek Koltunski
  final static Static4D MAX_REGION = new Static4D(0,0,0,1000000);
59
60 43814a57 Leszek Koltunski
  ArrayList<EffectQueue> mQueues;
61
62 b547aaba leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 a0d5e302 Leszek Koltunski
  VertexEffect(EffectName name)
65 b547aaba leszek
    {
66 9d0d8530 leszek
    super(name);
67 b547aaba leszek
    }
68 7cd24173 leszek
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 f046b159 Leszek Koltunski
  private static String retSection(int effect, String code)
72 7cd24173 leszek
    {
73 f046b159 Leszek Koltunski
    return
74 7cd24173 leszek
75 24804c15 Leszek Koltunski
        "if( vProperties[i].x =="+effect+" )\n" +
76 7cd24173 leszek
          "{\n" +
77
           code +"\n" +
78
          "}\n" +
79
        "else\n";
80
    }
81
82 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
83
// prepare the code to be injected into the Full program, i.e. code of ALL vertex effects.
84
85
  private static void prepareFull()
86
    {
87
    Method method;
88
89
    for(EffectName name: EffectName.values())
90
      {
91
      if( name.getType() == EffectType.VERTEX )
92
        {
93
        Class<? extends Effect> cls = name.getEffectClass();
94
95
        try
96
          {
97
          method = cls.getDeclaredMethod("code");
98
          }
99
        catch(NoSuchMethodException ex)
100
          {
101 8c57d77b Leszek Koltunski
          DistortedLibrary.logMessage("VertexEffect: exception getting method: "+ex.getMessage());
102 f046b159 Leszek Koltunski
          method = null;
103
          }
104
105
        try
106
          {
107
          if( method!=null )
108
            {
109
            Object value = method.invoke(null);
110
            String code = (String)value;
111
            mFullGLSL += retSection(name.ordinal(),code);
112
            mFullEnabled++;
113
            }
114
          }
115
        catch(Exception ex)
116
          {
117 8c57d77b Leszek Koltunski
          DistortedLibrary.logMessage("VertexEffect: exception invoking method: "+ex.getMessage());
118 f046b159 Leszek Koltunski
          }
119
        }
120
      }
121
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
// prepare code to be injected into the 'main_vertex_shader' main() function.
125
126
  static void addEffect(EffectName name, String code)
127
    {
128
    int effect = name.ordinal();
129
130
    if( !mEnabled[effect] )
131
      {
132
      mEnabled[effect] = true;
133
      mNumEnabled ++;
134
      mGLSL += retSection(effect,code);
135
      }
136
    }
137
138 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
139 faa3ff56 Leszek Koltunski
/**
140
 * Only for use by the library itself.
141
 *
142
 * @y.exclude
143
 */
144 7cd24173 leszek
  public static String getGLSL()
145
    {
146
    return mGLSL + "{}";
147
    }
148
149 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
150
/**
151
 * Only for use by the library itself.
152
 *
153
 * @y.exclude
154
 */
155
  public static String getAllGLSL()
156
    {
157
    if( !mFullPrepared )
158
      {
159
      prepareFull();
160
      mFullPrepared = true;
161
      }
162
163
    return mFullGLSL + "{}";
164
    }
165
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167
/**
168
 * Only for use by the library itself.
169
 *
170
 * @y.exclude
171
 */
172
  public static int getAllEnabled()
173
    {
174
    if( !mFullPrepared )
175
      {
176
      prepareFull();
177
      mFullPrepared = true;
178
      }
179
180
    return mFullEnabled;
181
    }
182
183 7cd24173 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
184 2f1f7570 Leszek Koltunski
185
  static void destroyStatics()
186 7cd24173 leszek
    {
187 faa3ff56 Leszek Koltunski
    mNumEnabled = 0;
188
    mGLSL = "";
189 f046b159 Leszek Koltunski
    mFullEnabled= 0;
190
    mFullGLSL = "";
191
    mFullPrepared = false;
192 7cd24173 leszek
    }
193
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195 faa3ff56 Leszek Koltunski
// PUBLIC API
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197
/**
198 6b816678 Leszek Koltunski
 * Return the number of Vertex effects enabled.
199 faa3ff56 Leszek Koltunski
 */
200
  public static int getNumEnabled()
201 7cd24173 leszek
    {
202 faa3ff56 Leszek Koltunski
    return mNumEnabled;
203 7cd24173 leszek
    }
204 bc208a9c Leszek Koltunski
205 43814a57 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
206
/**
207
 * Only for use by the library itself.
208
 *
209
 * @y.exclude
210
 */
211
  public void addQueue(EffectQueue queue)
212
    {
213
    if( mQueues==null ) mQueues = new ArrayList<>();
214
215
    mQueues.add(queue);
216
    }
217
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
/**
220
 * Only for use by the library itself.
221
 *
222
 * @y.exclude
223
 */
224
  public void remQueue(EffectQueue queue)
225
    {
226
    if( mQueues==null ) mQueues = new ArrayList<>();
227
228
    mQueues.remove(queue);
229
    }
230
231 bc208a9c Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
232
/**
233 1e672c1d Leszek Koltunski
 * Set Mesh association.
234 bc208a9c Leszek Koltunski
 *
235 2aeb75aa Leszek Koltunski
 * This creates an association between a Component of a Mesh and this Vertex Effect.
236
 * One can set two types of associations - an 'logical and' and a 'equal' associations and the Effect
237
 * will only be active on vertices of Components such that
238 36d65d88 Leszek Koltunski
 *
239 2aeb75aa Leszek Koltunski
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
240 36d65d88 Leszek Koltunski
 *
241
 * (see main_vertex_shader)
242 bc208a9c Leszek Koltunski
 *
243
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
244 1e672c1d Leszek Koltunski
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
245 bc208a9c Leszek Koltunski
 */
246 2aeb75aa Leszek Koltunski
  public void setMeshAssociation(int andAssociation, int equAssociation)
247 bc208a9c Leszek Koltunski
    {
248 2aeb75aa Leszek Koltunski
    mAndAssociation = andAssociation;
249
    mEquAssociation = equAssociation;
250 9becf30e Leszek Koltunski
251 43814a57 Leszek Koltunski
    long id = getID();
252
    int numQueues = mQueues==null ? 0: mQueues.size();
253
254
    for(int i=0; i<numQueues; i++)
255
      {
256
      EffectQueue queue = mQueues.get(i);
257
      queue.setAssociation(id);
258
      }
259 bc208a9c Leszek Koltunski
    }
260 e5f796bc Leszek Koltunski
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
/**
263
 * Return a constructed VertexEffect based on its name, the 5 variables, center & region.
264
 */
265
  public static VertexEffect constructEffect(String name,float[] vars, float[] center, float[] region)
266
    {
267
    Static3D staCenter = new Static3D(center[0],center[1],center[2]);
268
    Static4D staRegion = new Static4D(region[0],region[1],region[2],region[3]);
269
270
    if( name.equals(EffectName.DISTORT.name()) )
271
      {
272
      Static3D staVars = new Static3D(vars[2],vars[3],vars[4]);
273
      return new VertexEffectDistort(staVars,staCenter,staRegion);
274
      }
275
    if( name.equals(EffectName.DEFORM.name()) )
276
      {
277
      Static3D staVars   = new Static3D(vars[1],vars[2],vars[3]);
278
      Static1D staRadius = new Static1D(vars[4]);
279
      return new VertexEffectDeform(staVars,staRadius,staCenter,staRegion);
280
      }
281
    if( name.equals(EffectName.SINK.name()) )
282
      {
283
      Static1D staSink = new Static1D(vars[4]);
284
      return new VertexEffectSink(staSink,staCenter,staRegion);
285
      }
286
    if( name.equals(EffectName.PINCH.name()) )
287
      {
288
      Static3D staVars = new Static3D(vars[2],vars[3],vars[4]);
289
      return new VertexEffectPinch(staVars,staCenter,staRegion);
290
      }
291
    if( name.equals(EffectName.SWIRL.name()) )
292
      {
293
      Static1D staSwirl = new Static1D(vars[4]);
294
      return new VertexEffectSwirl(staSwirl,staCenter,staRegion);
295
      }
296
    if( name.equals(EffectName.WAVE.name()) )
297
      {
298
      Static5D staWave = new Static5D(vars[0],vars[1],vars[2],vars[3],vars[4]);
299
      return new VertexEffectWave(staWave,staCenter,staRegion);
300
      }
301
    if( name.equals(EffectName.DISAPPEAR.name()) )
302
      {
303
      return new VertexEffectDisappear();
304
      }
305
    if( name.equals(EffectName.VERTEX_MOVE.name()) )
306
      {
307
      Static3D staVector = new Static3D(vars[2],vars[3],vars[4]);
308
      return new VertexEffectMove(staVector);
309
      }
310
    if( name.equals(EffectName.VERTEX_QUATERNION.name()) )
311
      {
312
      Static4D staQuat = new Static4D(vars[1],vars[2],vars[3],vars[4]);
313
      return new VertexEffectQuaternion(staQuat,staCenter);
314
      }
315
    if( name.equals(EffectName.VERTEX_ROTATE.name()) )
316
      {
317
      Static1D staAngle = new Static1D(vars[1]);
318
      Static3D staAxis  = new Static3D(vars[2],vars[3],vars[4]);
319
      return new VertexEffectRotate(staAngle,staAxis,staCenter);
320
      }
321
    if( name.equals(EffectName.VERTEX_SCALE.name()) )
322
      {
323
      Static3D staScale = new Static3D(vars[2],vars[3],vars[4]);
324
      return new VertexEffectScale(staScale);
325
      }
326
    if( name.equals(EffectName.VERTEX_SHEAR.name()) )
327
      {
328
      Static3D staShear = new Static3D(vars[2],vars[3],vars[4]);
329
      return new VertexEffectShear(staShear,staCenter);
330
      }
331
332
    return null;
333
    }
334 8eccf334 Leszek Koltunski
  }