Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffects.java @ b3120b1b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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;
21

    
22
import android.content.res.Resources;
23
import android.opengl.GLES30;
24

    
25
import org.distorted.library.message.EffectListener;
26
import org.distorted.library.program.DistortedProgram;
27
import org.distorted.library.program.FragmentCompilationException;
28
import org.distorted.library.program.FragmentUniformsException;
29
import org.distorted.library.program.LinkingException;
30
import org.distorted.library.program.VertexCompilationException;
31
import org.distorted.library.program.VertexUniformsException;
32
import org.distorted.library.type.Data1D;
33
import org.distorted.library.type.Data2D;
34
import org.distorted.library.type.Data3D;
35
import org.distorted.library.type.Data4D;
36
import org.distorted.library.type.Data5D;
37
import org.distorted.library.type.Static3D;
38

    
39
import java.io.InputStream;
40
import java.nio.ByteBuffer;
41
import java.nio.ByteOrder;
42
import java.nio.FloatBuffer;
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45
/**
46
 * Class containing Matrix,Vertex and Fragment effect queues. Postprocessing queue is held in a separate
47
 * class.
48
 * <p>
49
 * The queues hold actual effects to be applied to a given (DistortedTexture,MeshObject) combo.
50
 */
51
public class DistortedEffects
52
  {
53
  /// MAIN PROGRAM ///
54
  private static DistortedProgram mMainProgram;
55
  private static int mMainTextureH;
56
  private static boolean[] mEffectEnabled = new boolean[EffectNames.size()];
57

    
58
  static
59
    {
60
    int len = EffectNames.size();
61

    
62
    for(int i=0; i<len; i++)
63
      {
64
      mEffectEnabled[i] = false;
65
      }
66
    }
67

    
68
  /// BLIT PROGRAM ///
69
  private static DistortedProgram mBlitProgram;
70
  private static int mBlitTextureH;
71
  private static int mBlitDepthH;
72
  private static final FloatBuffer mQuadPositions;
73

    
74
  static
75
    {
76
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
77
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
78
    mQuadPositions.put(positionData).position(0);
79
    }
80

    
81
  /// NORMAL PROGRAM /////
82
  private static DistortedProgram mNormalProgram;
83
  private static int mNormalMVPMatrixH;
84
  /// END PROGRAMS //////
85

    
86
  private static long mNextID =0;
87
  private long mID;
88

    
89
  private EffectQueueMatrix   mM;
90
  private EffectQueueFragment mF;
91
  private EffectQueueVertex   mV;
92

    
93
  private boolean matrixCloned, vertexCloned, fragmentCloned;
94

    
95
///////////////////////////////////////////////////////////////////////////////////////////////////
96

    
97
  static void createProgram(Resources resources)
98
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
99
    {
100
    // MAIN PROGRAM ////////////////////////////////////
101
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
102
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
103

    
104
    String mainVertHeader= Distorted.GLSL_VERSION;
105
    String mainFragHeader= Distorted.GLSL_VERSION;
106

    
107
    EffectNames name;
108
    EffectTypes type;
109
    boolean foundF = false;
110
    boolean foundV = false;
111

    
112
    for(int i=0; i<mEffectEnabled.length; i++)
113
      {
114
      if( mEffectEnabled[i] )
115
        {
116
        name = EffectNames.getName(i);
117
        type = EffectNames.getType(i);
118

    
119
        if( type == EffectTypes.VERTEX )
120
          {
121
          mainVertHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
122
          foundV = true;
123
          }
124
        else if( type == EffectTypes.FRAGMENT )
125
          {
126
          mainFragHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
127
          foundF = true;
128
          }
129
        }
130
      }
131

    
132
    mainVertHeader += ("#define NUM_VERTEX "   + ( foundV ? getMaxVertex()   : 0 ) + "\n");
133
    mainFragHeader += ("#define NUM_FRAGMENT " + ( foundF ? getMaxFragment() : 0 ) + "\n");
134

    
135
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
136
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
137

    
138
    String[] feedback = { "v_Position", "v_endPosition" };
139

    
140
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader, Distorted.GLSL, feedback);
141

    
142
    int mainProgramH = mMainProgram.getProgramHandle();
143
    EffectQueueFragment.getUniforms(mainProgramH);
144
    EffectQueueVertex.getUniforms(mainProgramH);
145
    EffectQueueMatrix.getUniforms(mainProgramH);
146
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
147

    
148
    // BLIT PROGRAM ////////////////////////////////////
149
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
150
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
151

    
152
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
153
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
154

    
155
    try
156
      {
157
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
158
      }
159
    catch(Exception e)
160
      {
161
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
162
      throw new RuntimeException(e.getMessage());
163
      }
164

    
165
    int blitProgramH = mBlitProgram.getProgramHandle();
166
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
167
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
168

    
169
    // NORMAL PROGRAM //////////////////////////////////////
170
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
171
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
172

    
173
    try
174
      {
175
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
176
      }
177
    catch(Exception e)
178
      {
179
      android.util.Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
180
      throw new RuntimeException(e.getMessage());
181
      }
182

    
183
    int normalProgramH = mNormalProgram.getProgramHandle();
184
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  private void initializeEffectLists(DistortedEffects d, int flags)
190
    {
191
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
192
      {
193
      mM = d.mM;
194
      matrixCloned = true;
195
      }
196
    else
197
      {
198
      mM = new EffectQueueMatrix(mID);
199
      matrixCloned = false;
200
      }
201
    
202
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
203
      {
204
      mV = d.mV;
205
      vertexCloned = true;
206
      }
207
    else
208
      {
209
      mV = new EffectQueueVertex(mID);
210
      vertexCloned = false;
211
      }
212
    
213
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
214
      {
215
      mF = d.mF;
216
      fragmentCloned = true;
217
      }
218
    else
219
      {
220
      mF = new EffectQueueFragment(mID);
221
      fragmentCloned = false;
222
      }
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226

    
227
  private void displayNormals(MeshObject mesh)
228
    {
229
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
230
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
231
    DistortedRenderState.switchOffDrawing();
232
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, mesh.numVertices);
233
    DistortedRenderState.restoreDrawing();
234
    GLES30.glEndTransformFeedback();
235
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
236

    
237
    mNormalProgram.useProgram();
238
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
239
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
240
    GLES30.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
241
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
242
    GLES30.glLineWidth(8.0f);
243
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*mesh.numVertices);
244
    }
245

    
246
///////////////////////////////////////////////////////////////////////////////////////////////////
247

    
248
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
249
    {
250
    mM.compute(currTime);
251
    mV.compute(currTime);
252
    mF.compute(currTime);
253

    
254
    float halfZ = halfW*mesh.zFactor;
255

    
256
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
257

    
258
    mMainProgram.useProgram();
259
    GLES30.glUniform1i(mMainTextureH, 0);
260

    
261
    if( Distorted.GLSL >= 300 )
262
      {
263
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
264
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
265
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
266
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
267
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
268
      }
269
    else
270
      {
271
      mesh.mVertAttribs.position(0);
272
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
273
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
274
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
275
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
276
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
277
      }
278

    
279
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
280
    mV.send(halfW,halfH,halfZ);
281
    mF.send(halfW,halfH);
282

    
283
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
284

    
285
    if( mesh.mShowNormals ) displayNormals(mesh);
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
   
290
  static void blitPriv(DistortedOutputSurface surface)
291
    {
292
    mBlitProgram.useProgram();
293

    
294
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
295
    GLES30.glUniform1i(mBlitTextureH, 0);
296
    GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
297
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
298
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
299
    }
300
    
301
///////////////////////////////////////////////////////////////////////////////////////////////////
302
   
303
  private void releasePriv()
304
    {
305
    if( !matrixCloned   ) mM.abortAll(false);
306
    if( !vertexCloned   ) mV.abortAll(false);
307
    if( !fragmentCloned ) mF.abortAll(false);
308

    
309
    mM = null;
310
    mV = null;
311
    mF = null;
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315

    
316
  static void onDestroy()
317
    {
318
    mNextID = 0;
319

    
320
    int len = EffectNames.size();
321

    
322
    for(int i=0; i<len; i++)
323
      {
324
      mEffectEnabled[i] = false;
325
      }
326
    }
327

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329
// PUBLIC API
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
/**
332
 * Create empty effect queue.
333
 */
334
  public DistortedEffects()
335
    {
336
    mID = ++mNextID;
337
    initializeEffectLists(this,0);
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
/**
342
 * Copy constructor.
343
 * <p>
344
 * Whatever we do not clone gets created just like in the default constructor.
345
 *
346
 * @param dc    Source object to create our object from
347
 * @param flags A bitmask of values specifying what to copy.
348
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
349
 */
350
  public DistortedEffects(DistortedEffects dc, int flags)
351
    {
352
    mID = ++mNextID;
353
    initializeEffectLists(dc,flags);
354
    }
355

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
/**
358
 * Releases all resources. After this call, the queue should not be used anymore.
359
 */
360
  @SuppressWarnings("unused")
361
  public synchronized void delete()
362
    {
363
    releasePriv();
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367
/**
368
 * Returns unique ID of this instance.
369
 *
370
 * @return ID of the object.
371
 */
372
  public long getID()
373
      {
374
      return mID;
375
      }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378
/**
379
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
380
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
381
 * 
382
 * @param el A class implementing the EffectListener interface that wants to get notifications.
383
 */
384
  @SuppressWarnings("unused")
385
  public void registerForMessages(EffectListener el)
386
    {
387
    mV.registerForMessages(el);
388
    mF.registerForMessages(el);
389
    mM.registerForMessages(el);
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393
/**
394
 * Removes the calling class from the list of Listeners.
395
 * 
396
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
397
 */
398
  @SuppressWarnings("unused")
399
  public void deregisterForMessages(EffectListener el)
400
    {
401
    mV.deregisterForMessages(el);
402
    mF.deregisterForMessages(el);
403
    mM.deregisterForMessages(el);
404
    }
405

    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
/**
408
 * Aborts all Effects.
409
 * @return Number of effects aborted.
410
 */
411
  public int abortAllEffects()
412
    {
413
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
414
    }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
/**
418
 * Aborts all Effects of a given type, for example all MATRIX Effects.
419
 * 
420
 * @param type one of the constants defined in {@link EffectTypes}
421
 * @return Number of effects aborted.
422
 */
423
  public int abortEffects(EffectTypes type)
424
    {
425
    switch(type)
426
      {
427
      case MATRIX     : return mM.abortAll(true);
428
      case VERTEX     : return mV.abortAll(true);
429
      case FRAGMENT   : return mF.abortAll(true);
430
      default         : return 0;
431
      }
432
    }
433
    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
/**
436
 * Aborts a single Effect.
437
 * 
438
 * @param id ID of the Effect we want to abort.
439
 * @return number of Effects aborted. Always either 0 or 1.
440
 */
441
  public int abortEffect(long id)
442
    {
443
    int type = (int)(id&EffectTypes.MASK);
444

    
445
    if( type==EffectTypes.MATRIX.type      ) return mM.removeByID(id>>EffectTypes.LENGTH);
446
    if( type==EffectTypes.VERTEX.type      ) return mV.removeByID(id>>EffectTypes.LENGTH);
447
    if( type==EffectTypes.FRAGMENT.type    ) return mF.removeByID(id>>EffectTypes.LENGTH);
448

    
449
    return 0;
450
    }
451

    
452
///////////////////////////////////////////////////////////////////////////////////////////////////
453
/**
454
 * Abort all Effects of a given name, for example all rotations.
455
 * 
456
 * @param name one of the constants defined in {@link EffectNames}
457
 * @return number of Effects aborted.
458
 */
459
  public int abortEffects(EffectNames name)
460
    {
461
    switch(name.getType())
462
      {
463
      case MATRIX     : return mM.removeByType(name);
464
      case VERTEX     : return mV.removeByType(name);
465
      case FRAGMENT   : return mF.removeByType(name);
466
      default         : return 0;
467
      }
468
    }
469
    
470
///////////////////////////////////////////////////////////////////////////////////////////////////
471
/**
472
 * Print some info about a given Effect to Android's standard out. Used for debugging only.
473
 * 
474
 * @param id Effect ID we want to print info about
475
 * @return <code>true</code> if a single Effect of type effectType has been found.
476
 */
477
  @SuppressWarnings("unused")
478
  public boolean printEffect(long id)
479
    {
480
    int type = (int)(id&EffectTypes.MASK);
481

    
482
    if( type==EffectTypes.MATRIX.type  )  return mM.printByID(id>>EffectTypes.LENGTH);
483
    if( type==EffectTypes.VERTEX.type  )  return mV.printByID(id>>EffectTypes.LENGTH);
484
    if( type==EffectTypes.FRAGMENT.type)  return mF.printByID(id>>EffectTypes.LENGTH);
485

    
486
    return false;
487
    }
488

    
489
///////////////////////////////////////////////////////////////////////////////////////////////////
490
/**
491
 * Enables a given Effect.
492
 * <p>
493
 * By default, all effects are disabled. One has to explicitly enable each effect one intends to use.
494
 * This needs to be called BEFORE shaders get compiled, i.e. before the call to Distorted.onCreate().
495
 * The point: by enabling only the effects we need, we can optimize the shaders.
496
 *
497
 * @param name Name of the Effect to enable.
498
 */
499
  public static void enableEffect(EffectNames name)
500
    {
501
    mEffectEnabled[name.ordinal()] = true;
502
    }
503

    
504
///////////////////////////////////////////////////////////////////////////////////////////////////
505
/**
506
 * Returns the maximum number of Matrix effects.
507
 *
508
 * @return The maximum number of Matrix effects
509
 */
510
  @SuppressWarnings("unused")
511
  public static int getMaxMatrix()
512
    {
513
    return EffectQueue.getMax(EffectTypes.MATRIX.ordinal());
514
    }
515

    
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518
 * Returns the maximum number of Vertex effects.
519
 *
520
 * @return The maximum number of Vertex effects
521
 */
522
  @SuppressWarnings("unused")
523
  public static int getMaxVertex()
524
    {
525
    return EffectQueue.getMax(EffectTypes.VERTEX.ordinal());
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Returns the maximum number of Fragment effects.
531
 *
532
 * @return The maximum number of Fragment effects
533
 */
534
  @SuppressWarnings("unused")
535
  public static int getMaxFragment()
536
    {
537
    return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal());
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541
/**
542
 * Sets the maximum number of Matrix effects that can be stored in a single EffectQueue at one time.
543
 * This can fail if:
544
 * <ul>
545
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
546
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
547
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
548
 *     time only decreasing the value of 'max' is permitted.
549
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
550
 * </ul>
551
 *
552
 * @param max new maximum number of simultaneous Matrix Effects. Has to be a non-negative number not greater
553
 *            than Byte.MAX_VALUE
554
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
555
 */
556
  @SuppressWarnings("unused")
557
  public static boolean setMaxMatrix(int max)
558
    {
559
    return EffectQueue.setMax(EffectTypes.MATRIX.ordinal(),max);
560
    }
561

    
562
///////////////////////////////////////////////////////////////////////////////////////////////////
563
/**
564
 * Sets the maximum number of Vertex effects that can be stored in a single EffectQueue at one time.
565
 * This can fail if:
566
 * <ul>
567
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
568
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
569
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
570
 *     time only decreasing the value of 'max' is permitted.
571
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
572
 * </ul>
573
 *
574
 * @param max new maximum number of simultaneous Vertex Effects. Has to be a non-negative number not greater
575
 *            than Byte.MAX_VALUE
576
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
577
 */
578
  @SuppressWarnings("unused")
579
  public static boolean setMaxVertex(int max)
580
    {
581
    return EffectQueue.setMax(EffectTypes.VERTEX.ordinal(),max);
582
    }
583

    
584
///////////////////////////////////////////////////////////////////////////////////////////////////
585
/**
586
 * Sets the maximum number of Fragment effects that can be stored in a single EffectQueue at one time.
587
 * This can fail if:
588
 * <ul>
589
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
590
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
591
 *     before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
592
 *     time only decreasing the value of 'max' is permitted.
593
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
594
 * </ul>
595
 *
596
 * @param max new maximum number of simultaneous Fragment Effects. Has to be a non-negative number not greater
597
 *            than Byte.MAX_VALUE
598
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
599
 */
600
  @SuppressWarnings("unused")
601
  public static boolean setMaxFragment(int max)
602
    {
603
    return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max);
604
    }
605

    
606
///////////////////////////////////////////////////////////////////////////////////////////////////   
607
///////////////////////////////////////////////////////////////////////////////////////////////////
608
// Individual effect functions.
609
///////////////////////////////////////////////////////////////////////////////////////////////////
610
// Matrix-based effects
611
///////////////////////////////////////////////////////////////////////////////////////////////////
612
/**
613
 * Moves the Object by a (possibly changing in time) vector.
614
 * 
615
 * @param vector 3-dimensional Data which at any given time will return a Static3D
616
 *               representing the current coordinates of the vector we want to move the Object with.
617
 * @return       ID of the effect added, or -1 if we failed to add one.
618
 */
619
  public long move(Data3D vector)
620
    {   
621
    return mM.add(EffectNames.MOVE,vector);
622
    }
623

    
624
///////////////////////////////////////////////////////////////////////////////////////////////////
625
/**
626
 * Scales the Object by (possibly changing in time) 3D scale factors.
627
 * 
628
 * @param scale 3-dimensional Data which at any given time returns a Static3D
629
 *              representing the current x- , y- and z- scale factors.
630
 * @return      ID of the effect added, or -1 if we failed to add one.
631
 */
632
  public long scale(Data3D scale)
633
    {   
634
    return mM.add(EffectNames.SCALE,scale);
635
    }
636

    
637
///////////////////////////////////////////////////////////////////////////////////////////////////
638
/**
639
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
640
 *
641
 * @param scale The factor to scale all 3 dimensions with.
642
 * @return      ID of the effect added, or -1 if we failed to add one.
643
 */
644
  public long scale(float scale)
645
    {
646
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
647
    }
648

    
649
///////////////////////////////////////////////////////////////////////////////////////////////////
650
/**
651
 * Rotates the Object by 'angle' degrees around the center.
652
 * Static axis of rotation is given by the last parameter.
653
 *
654
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
655
 * @param axis   Axis of rotation
656
 * @param center Coordinates of the Point we are rotating around.
657
 * @return       ID of the effect added, or -1 if we failed to add one.
658
 */
659
  public long rotate(Data1D angle, Static3D axis, Data3D center )
660
    {   
661
    return mM.add(EffectNames.ROTATE, angle, axis, center);
662
    }
663

    
664
///////////////////////////////////////////////////////////////////////////////////////////////////
665
/**
666
 * Rotates the Object by 'angle' degrees around the center.
667
 * Here both angle and axis can dynamically change.
668
 *
669
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
670
 * @param center    Coordinates of the Point we are rotating around.
671
 * @return          ID of the effect added, or -1 if we failed to add one.
672
 */
673
  public long rotate(Data4D angleaxis, Data3D center)
674
    {
675
    return mM.add(EffectNames.ROTATE, angleaxis, center);
676
    }
677

    
678
///////////////////////////////////////////////////////////////////////////////////////////////////
679
/**
680
 * Rotates the Object by quaternion.
681
 *
682
 * @param quaternion The quaternion describing the rotation.
683
 * @param center     Coordinates of the Point we are rotating around.
684
 * @return           ID of the effect added, or -1 if we failed to add one.
685
 */
686
  public long quaternion(Data4D quaternion, Data3D center )
687
    {
688
    return mM.add(EffectNames.QUATERNION,quaternion,center);
689
    }
690

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692
/**
693
 * Shears the Object.
694
 *
695
 * @param shear   The 3-tuple of shear factors. The first controls level
696
 *                of shearing in the X-axis, second - Y-axis and the third -
697
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
698
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
699
 * @param center  Center of shearing, i.e. the point which stays unmoved.
700
 * @return        ID of the effect added, or -1 if we failed to add one.
701
 */
702
  public long shear(Data3D shear, Data3D center)
703
    {
704
    return mM.add(EffectNames.SHEAR, shear, center);
705
    }
706

    
707
///////////////////////////////////////////////////////////////////////////////////////////////////
708
// Fragment-based effects  
709
///////////////////////////////////////////////////////////////////////////////////////////////////
710
/**
711
 * Makes a certain sub-region of the Object smoothly change all three of its RGB components.
712
 *        
713
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
714
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
715
 *               Valid range: <0,1>
716
 * @param color  Color to mix. (1,0,0) is RED.
717
 * @param region Region this Effect is limited to.
718
 * @param smooth If true, the level of 'blend' will smoothly fade out towards the edges of the region.
719
 * @return       ID of the effect added, or -1 if we failed to add one.
720
 */
721
  public long chroma(Data1D blend, Data3D color, Data4D region, boolean smooth)
722
    {
723
    return mF.add( smooth? EffectNames.SMOOTH_CHROMA:EffectNames.CHROMA, blend, color, region);
724
    }
725

    
726
///////////////////////////////////////////////////////////////////////////////////////////////////
727
/**
728
 * Makes the whole Object smoothly change all three of its RGB components.
729
 *
730
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
731
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
732
 *               Valid range: <0,1>
733
 * @param color  Color to mix. (1,0,0) is RED.
734
 * @return       ID of the effect added, or -1 if we failed to add one.
735
 */
736
  public long chroma(Data1D blend, Data3D color)
737
    {
738
    return mF.add(EffectNames.CHROMA, blend, color);
739
    }
740

    
741
///////////////////////////////////////////////////////////////////////////////////////////////////
742
/**
743
 * Makes a certain sub-region of the Object smoothly change its transparency level.
744
 *        
745
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any given
746
 *               moment: pixel.a *= alpha.
747
 *               Valid range: <0,1>
748
 * @param region Region this Effect is limited to. 
749
 * @param smooth If true, the level of 'alpha' will smoothly fade out towards the edges of the region.
750
 * @return       ID of the effect added, or -1 if we failed to add one. 
751
 */
752
  public long alpha(Data1D alpha, Data4D region, boolean smooth)
753
    {
754
    return mF.add( smooth? EffectNames.SMOOTH_ALPHA:EffectNames.ALPHA, alpha, region);
755
    }
756

    
757
///////////////////////////////////////////////////////////////////////////////////////////////////
758
/**
759
 * Makes the whole Object smoothly change its transparency level.
760
 *
761
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any
762
 *               given moment: pixel.a *= alpha.
763
 *               Valid range: <0,1>
764
 * @return       ID of the effect added, or -1 if we failed to add one.
765
 */
766
  public long alpha(Data1D alpha)
767
    {
768
    return mF.add(EffectNames.ALPHA, alpha);
769
    }
770

    
771
///////////////////////////////////////////////////////////////////////////////////////////////////
772
/**
773
 * Makes a certain sub-region of the Object smoothly change its brightness level.
774
 *        
775
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
776
 *                   at any given moment. Valid range: <0,infinity)
777
 * @param region     Region this Effect is limited to.
778
 * @param smooth     If true, the level of 'brightness' will smoothly fade out towards the edges of the region.
779
 * @return           ID of the effect added, or -1 if we failed to add one.
780
 */
781
  public long brightness(Data1D brightness, Data4D region, boolean smooth)
782
    {
783
    return mF.add( smooth ? EffectNames.SMOOTH_BRIGHTNESS: EffectNames.BRIGHTNESS, brightness, region);
784
    }
785

    
786
///////////////////////////////////////////////////////////////////////////////////////////////////
787
/**
788
 * Makes the whole Object smoothly change its brightness level.
789
 *
790
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
791
 *                   at any given moment. Valid range: <0,infinity)
792
 * @return           ID of the effect added, or -1 if we failed to add one.
793
 */
794
  public long brightness(Data1D brightness)
795
    {
796
    return mF.add(EffectNames.BRIGHTNESS, brightness);
797
    }
798

    
799
///////////////////////////////////////////////////////////////////////////////////////////////////
800
/**
801
 * Makes a certain sub-region of the Object smoothly change its contrast level.
802
 *        
803
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
804
 *                 at any given moment. Valid range: <0,infinity)
805
 * @param region   Region this Effect is limited to.
806
 * @param smooth   If true, the level of 'contrast' will smoothly fade out towards the edges of the region.
807
 * @return         ID of the effect added, or -1 if we failed to add one.
808
 */
809
  public long contrast(Data1D contrast, Data4D region, boolean smooth)
810
    {
811
    return mF.add( smooth ? EffectNames.SMOOTH_CONTRAST:EffectNames.CONTRAST, contrast, region);
812
    }
813

    
814
///////////////////////////////////////////////////////////////////////////////////////////////////
815
/**
816
 * Makes the whole Object smoothly change its contrast level.
817
 *
818
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
819
 *                 at any given moment. Valid range: <0,infinity)
820
 * @return         ID of the effect added, or -1 if we failed to add one.
821
 */
822
  public long contrast(Data1D contrast)
823
    {
824
    return mF.add(EffectNames.CONTRAST, contrast);
825
    }
826

    
827
///////////////////////////////////////////////////////////////////////////////////////////////////
828
/**
829
 * Makes a certain sub-region of the Object smoothly change its saturation level.
830
 *        
831
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
832
 *                   at any given moment. Valid range: <0,infinity)
833
 * @param region     Region this Effect is limited to.
834
 * @param smooth     If true, the level of 'saturation' will smoothly fade out towards the edges of the region.
835
 * @return           ID of the effect added, or -1 if we failed to add one.
836
 */
837
  public long saturation(Data1D saturation, Data4D region, boolean smooth)
838
    {
839
    return mF.add( smooth ? EffectNames.SMOOTH_SATURATION:EffectNames.SATURATION, saturation, region);
840
    }
841

    
842
///////////////////////////////////////////////////////////////////////////////////////////////////
843
/**
844
 * Makes the whole Object smoothly change its saturation level.
845
 *
846
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
847
 *                   at any given moment. Valid range: <0,infinity)
848
 * @return           ID of the effect added, or -1 if we failed to add one.
849
 */
850
  public long saturation(Data1D saturation)
851
    {
852
    return mF.add(EffectNames.SATURATION, saturation);
853
    }
854

    
855
///////////////////////////////////////////////////////////////////////////////////////////////////
856
// Vertex-based effects  
857
///////////////////////////////////////////////////////////////////////////////////////////////////
858
/**
859
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
860
 *
861
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
862
 *               currently being dragged with.
863
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
864
 * @param region Region that masks the Effect.
865
 * @return       ID of the effect added, or -1 if we failed to add one.
866
 */
867
  public long distort(Data3D vector, Data3D center, Data4D region)
868
    {  
869
    return mV.add(EffectNames.DISTORT, vector, center, region);
870
    }
871

    
872
///////////////////////////////////////////////////////////////////////////////////////////////////
873
/**
874
 * Distort the whole Object by a (possibly changing in time) vector of force.
875
 *
876
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
877
 *               currently being dragged with.
878
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
879
 * @return       ID of the effect added, or -1 if we failed to add one.
880
 */
881
  public long distort(Data3D vector, Data3D center)
882
    {
883
    return mV.add(EffectNames.DISTORT, vector, center, null);
884
    }
885

    
886
///////////////////////////////////////////////////////////////////////////////////////////////////
887
/**
888
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
889
 * a (possibly changing in time) point on the Object.
890
 *
891
 * @param vector Vector of force that deforms the shape of the whole Object.
892
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
893
 * @param region Region that masks the Effect.
894
 * @return       ID of the effect added, or -1 if we failed to add one.
895
 */
896
  public long deform(Data3D vector, Data3D center, Data4D region)
897
    {
898
    return mV.add(EffectNames.DEFORM, vector, center, region);
899
    }
900

    
901
///////////////////////////////////////////////////////////////////////////////////////////////////
902
/**
903
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
904
 * a (possibly changing in time) point on the Object.
905
 *     
906
 * @param vector Vector of force that deforms the shape of the whole Object.
907
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
908
 * @return       ID of the effect added, or -1 if we failed to add one.
909
 */
910
  public long deform(Data3D vector, Data3D center)
911
    {  
912
    return mV.add(EffectNames.DEFORM, vector, center, null);
913
    }
914

    
915
///////////////////////////////////////////////////////////////////////////////////////////////////  
916
/**
917
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
918
 * away from the center (degree<=1)
919
 *
920
 * @param sink   The current degree of the Effect.
921
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
922
 * @param region Region that masks the Effect.
923
 * @return       ID of the effect added, or -1 if we failed to add one.
924
 */
925
  public long sink(Data1D sink, Data3D center, Data4D region)
926
    {
927
    return mV.add(EffectNames.SINK, sink, center, region);
928
    }
929

    
930
///////////////////////////////////////////////////////////////////////////////////////////////////
931
/**
932
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
933
 * away from the center (degree<=1)
934
 *
935
 * @param sink   The current degree of the Effect.
936
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
937
 * @return       ID of the effect added, or -1 if we failed to add one.
938
 */
939
  public long sink(Data1D sink, Data3D center)
940
    {
941
    return mV.add(EffectNames.SINK, sink, center);
942
    }
943

    
944
///////////////////////////////////////////////////////////////////////////////////////////////////
945
/**
946
 * Pull all points around the center of the Effect towards a line passing through the center
947
 * (that's if degree>=1) or push them away from the line (degree<=1)
948
 *
949
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
950
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
951
 * @param region Region that masks the Effect.
952
 * @return       ID of the effect added, or -1 if we failed to add one.
953
 */
954
  public long pinch(Data2D pinch, Data3D center, Data4D region)
955
    {
956
    return mV.add(EffectNames.PINCH, pinch, center, region);
957
    }
958

    
959
///////////////////////////////////////////////////////////////////////////////////////////////////
960
/**
961
 * Pull all points around the center of the Effect towards a line passing through the center
962
 * (that's if degree>=1) or push them away from the line (degree<=1)
963
 *
964
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
965
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
966
 * @return       ID of the effect added, or -1 if we failed to add one.
967
 */
968
  public long pinch(Data2D pinch, Data3D center)
969
    {
970
    return mV.add(EffectNames.PINCH, pinch, center);
971
    }
972

    
973
///////////////////////////////////////////////////////////////////////////////////////////////////  
974
/**
975
 * Rotate part of the Object around the Center of the Effect by a certain angle.
976
 *
977
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
978
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
979
 * @param region Region that masks the Effect.
980
 * @return       ID of the effect added, or -1 if we failed to add one.
981
 */
982
  public long swirl(Data1D swirl, Data3D center, Data4D region)
983
    {    
984
    return mV.add(EffectNames.SWIRL, swirl, center, region);
985
    }
986

    
987
///////////////////////////////////////////////////////////////////////////////////////////////////
988
/**
989
 * Rotate the whole Object around the Center of the Effect by a certain angle.
990
 *
991
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
992
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
993
 * @return       ID of the effect added, or -1 if we failed to add one.
994
 */
995
  public long swirl(Data1D swirl, Data3D center)
996
    {
997
    return mV.add(EffectNames.SWIRL, swirl, center);
998
    }
999

    
1000
///////////////////////////////////////////////////////////////////////////////////////////////////
1001
/**
1002
 * Directional, sinusoidal wave effect.
1003
 *
1004
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
1005
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
1006
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
1007
 *               describe the 'direction' of the wave.
1008
 *               <p>
1009
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
1010
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
1011
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
1012
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
1013
 *               <p>
1014
 *               <p>
1015
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
1016
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
1017
 *               will be sine shapes.
1018
 *               <p>
1019
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
1020
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
1021
 *               YZ-plane with be sine shapes.
1022
 *               <p>
1023
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
1024
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
1025
 *               value if sin at this point.
1026
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1027
 * @return       ID of the effect added, or -1 if we failed to add one.
1028
 */
1029
  public long wave(Data5D wave, Data3D center)
1030
    {
1031
    return mV.add(EffectNames.WAVE, wave, center, null);
1032
    }
1033

    
1034
///////////////////////////////////////////////////////////////////////////////////////////////////
1035
/**
1036
 * Directional, sinusoidal wave effect.
1037
 *
1038
 * @param wave   see {@link DistortedEffects#wave(Data5D,Data3D)}
1039
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1040
 * @param region Region that masks the Effect.
1041
 * @return       ID of the effect added, or -1 if we failed to add one.
1042
 */
1043
  public long wave(Data5D wave, Data3D center, Data4D region)
1044
    {
1045
    return mV.add(EffectNames.WAVE, wave, center, region);
1046
    }
1047
  }
(2-2/26)