Project

General

Profile

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

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

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.main.DistortedLibrary;
25
import org.distorted.library.type.Static1D;
26
import org.distorted.library.type.Static3D;
27
import org.distorted.library.type.Static4D;
28
import org.distorted.library.type.Static5D;
29

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

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Abstract class that represents an Effect that works by injecting certain code into the main Vertex shader.
36
 */
37
public abstract class VertexEffect extends Effect
38
  {
39
  /**
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
   * 4: name, AND association, reserved, EQU Association
45
   */
46
  public static final int NUM_INT_UNIFORMS = 4;
47

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

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

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

    
60
  ArrayList<EffectQueue> mQueues;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

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

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
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
          DistortedLibrary.logMessage("VertexEffect: exception getting method: "+ex.getMessage());
102
          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
          DistortedLibrary.logMessage("VertexEffect: exception invoking method: "+ex.getMessage());
118
          }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
139
/**
140
 * Only for use by the library itself.
141
 *
142
 * @y.exclude
143
 */
144
  public static String getGLSL()
145
    {
146
    return mGLSL + "{}";
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
184

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

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

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
232
/**
233
 * Set Mesh association.
234
 *
235
 * 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
 *
239
 * (effect andAssoc) & (component andAssoc) != 0 || (effect equAssoc) == (mesh equAssoc)
240
 *
241
 * (see main_vertex_shader)
242
 *
243
 * The point: this way we can configure the system so that each Vertex Effect acts only on a certain
244
 * subset of a Mesh, thus potentially significantly reducing the number of render calls.
245
 */
246
  public void setMeshAssociation(int andAssociation, int equAssociation)
247
    {
248
    mAndAssociation = andAssociation;
249
    mEquAssociation = equAssociation;
250

    
251
    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
    }
260

    
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
  }
(22-22/34)