Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffects.java @ 3fc9327a

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)
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
    surface.setAsOutput(currTime);
260
    GLES30.glUniform1i(mMainTextureH, 0);
261

    
262
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
263
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
264
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
265
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
266
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0 );
267

    
268
    mM.send(surface,halfW,halfH,halfZ);
269
    mV.send(halfW,halfH,halfZ);
270
    mF.send(halfW,halfH);
271

    
272
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
273

    
274
    if( mesh.mShowNormals ) displayNormals(mesh);
275
    }
276

    
277
///////////////////////////////////////////////////////////////////////////////////////////////////
278
   
279
  static void blitPriv(DistortedOutputSurface surface)
280
    {
281
    mBlitProgram.useProgram();
282

    
283
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
284
    GLES30.glUniform1i(mBlitTextureH, 0);
285
    GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
286
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
287
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
288
    }
289
    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
291
   
292
  private void releasePriv()
293
    {
294
    if( !matrixCloned     ) mM.abortAll(false);
295
    if( !vertexCloned     ) mV.abortAll(false);
296
    if( !fragmentCloned   ) mF.abortAll(false);
297

    
298
    mM = null;
299
    mV = null;
300
    mF = null;
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304

    
305
  static void onDestroy()
306
    {
307
    mNextID = 0;
308

    
309
    int len = EffectNames.size();
310

    
311
    for(int i=0; i<len; i++)
312
      {
313
      mEffectEnabled[i] = false;
314
      }
315
    }
316

    
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318
// PUBLIC API
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
/**
321
 * Create empty effect queue.
322
 */
323
  public DistortedEffects()
324
    {
325
    mID = ++mNextID;
326
    initializeEffectLists(this,0);
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
/**
331
 * Copy constructor.
332
 * <p>
333
 * Whatever we do not clone gets created just like in the default constructor.
334
 *
335
 * @param dc    Source object to create our object from
336
 * @param flags A bitmask of values specifying what to copy.
337
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
338
 */
339
  public DistortedEffects(DistortedEffects dc, int flags)
340
    {
341
    mID = ++mNextID;
342
    initializeEffectLists(dc,flags);
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346
/**
347
 * Releases all resources. After this call, the queue should not be used anymore.
348
 */
349
  @SuppressWarnings("unused")
350
  public synchronized void delete()
351
    {
352
    releasePriv();
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356
/**
357
 * Returns unique ID of this instance.
358
 *
359
 * @return ID of the object.
360
 */
361
  public long getID()
362
      {
363
      return mID;
364
      }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367
/**
368
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
369
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
370
 * 
371
 * @param el A class implementing the EffectListener interface that wants to get notifications.
372
 */
373
  @SuppressWarnings("unused")
374
  public void registerForMessages(EffectListener el)
375
    {
376
    mV.registerForMessages(el);
377
    mF.registerForMessages(el);
378
    mM.registerForMessages(el);
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
/**
383
 * Removes the calling class from the list of Listeners.
384
 * 
385
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
386
 */
387
  @SuppressWarnings("unused")
388
  public void deregisterForMessages(EffectListener el)
389
    {
390
    mV.deregisterForMessages(el);
391
    mF.deregisterForMessages(el);
392
    mM.deregisterForMessages(el);
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
/**
397
 * Aborts all Effects.
398
 * @return Number of effects aborted.
399
 */
400
  public int abortAllEffects()
401
    {
402
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
/**
407
 * Aborts all Effects of a given type, for example all MATRIX Effects.
408
 * 
409
 * @param type one of the constants defined in {@link EffectTypes}
410
 * @return Number of effects aborted.
411
 */
412
  public int abortEffects(EffectTypes type)
413
    {
414
    switch(type)
415
      {
416
      case MATRIX     : return mM.abortAll(true);
417
      case VERTEX     : return mV.abortAll(true);
418
      case FRAGMENT   : return mF.abortAll(true);
419
      default         : return 0;
420
      }
421
    }
422
    
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424
/**
425
 * Aborts a single Effect.
426
 * 
427
 * @param id ID of the Effect we want to abort.
428
 * @return number of Effects aborted. Always either 0 or 1.
429
 */
430
  public int abortEffect(long id)
431
    {
432
    int type = (int)(id&EffectTypes.MASK);
433

    
434
    if( type==EffectTypes.MATRIX.type      ) return mM.removeByID(id>>EffectTypes.LENGTH);
435
    if( type==EffectTypes.VERTEX.type      ) return mV.removeByID(id>>EffectTypes.LENGTH);
436
    if( type==EffectTypes.FRAGMENT.type    ) return mF.removeByID(id>>EffectTypes.LENGTH);
437

    
438
    return 0;
439
    }
440

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

    
471
    if( type==EffectTypes.MATRIX.type  )  return mM.printByID(id>>EffectTypes.LENGTH);
472
    if( type==EffectTypes.VERTEX.type  )  return mV.printByID(id>>EffectTypes.LENGTH);
473
    if( type==EffectTypes.FRAGMENT.type)  return mF.printByID(id>>EffectTypes.LENGTH);
474

    
475
    return false;
476
    }
477

    
478
///////////////////////////////////////////////////////////////////////////////////////////////////
479
/**
480
 * Enables a given Effect.
481
 * <p>
482
 * By default, all effects are disabled. One has to explicitly enable each effect one intends to use.
483
 * This needs to be called BEFORE shaders get compiled, i.e. before the call to Distorted.onCreate().
484
 * The point: by enabling only the effects we need, we can optimize the shaders.
485
 *
486
 * @param name Name of the Effect to enable.
487
 */
488
  public static void enableEffect(EffectNames name)
489
    {
490
    mEffectEnabled[name.ordinal()] = true;
491
    }
492

    
493
///////////////////////////////////////////////////////////////////////////////////////////////////
494
/**
495
 * Returns the maximum number of Matrix effects.
496
 *
497
 * @return The maximum number of Matrix effects
498
 */
499
  @SuppressWarnings("unused")
500
  public static int getMaxMatrix()
501
    {
502
    return EffectQueue.getMax(EffectTypes.MATRIX.ordinal());
503
    }
504

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

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

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

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

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

    
595
///////////////////////////////////////////////////////////////////////////////////////////////////   
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597
// Individual effect functions.
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599
// Matrix-based effects
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601
/**
602
 * Moves the Object by a (possibly changing in time) vector.
603
 * 
604
 * @param vector 3-dimensional Data which at any given time will return a Static3D
605
 *               representing the current coordinates of the vector we want to move the Object with.
606
 * @return       ID of the effect added, or -1 if we failed to add one.
607
 */
608
  public long move(Data3D vector)
609
    {   
610
    return mM.add(EffectNames.MOVE,vector);
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614
/**
615
 * Scales the Object by (possibly changing in time) 3D scale factors.
616
 * 
617
 * @param scale 3-dimensional Data which at any given time returns a Static3D
618
 *              representing the current x- , y- and z- scale factors.
619
 * @return      ID of the effect added, or -1 if we failed to add one.
620
 */
621
  public long scale(Data3D scale)
622
    {   
623
    return mM.add(EffectNames.SCALE,scale);
624
    }
625

    
626
///////////////////////////////////////////////////////////////////////////////////////////////////
627
/**
628
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
629
 *
630
 * @param scale The factor to scale all 3 dimensions with.
631
 * @return      ID of the effect added, or -1 if we failed to add one.
632
 */
633
  public long scale(float scale)
634
    {
635
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
636
    }
637

    
638
///////////////////////////////////////////////////////////////////////////////////////////////////
639
/**
640
 * Rotates the Object by 'angle' degrees around the center.
641
 * Static axis of rotation is given by the last parameter.
642
 *
643
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
644
 * @param axis   Axis of rotation
645
 * @param center Coordinates of the Point we are rotating around.
646
 * @return       ID of the effect added, or -1 if we failed to add one.
647
 */
648
  public long rotate(Data1D angle, Static3D axis, Data3D center )
649
    {   
650
    return mM.add(EffectNames.ROTATE, angle, axis, center);
651
    }
652

    
653
///////////////////////////////////////////////////////////////////////////////////////////////////
654
/**
655
 * Rotates the Object by 'angle' degrees around the center.
656
 * Here both angle and axis can dynamically change.
657
 *
658
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
659
 * @param center    Coordinates of the Point we are rotating around.
660
 * @return          ID of the effect added, or -1 if we failed to add one.
661
 */
662
  public long rotate(Data4D angleaxis, Data3D center)
663
    {
664
    return mM.add(EffectNames.ROTATE, angleaxis, center);
665
    }
666

    
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668
/**
669
 * Rotates the Object by quaternion.
670
 *
671
 * @param quaternion The quaternion describing the rotation.
672
 * @param center     Coordinates of the Point we are rotating around.
673
 * @return           ID of the effect added, or -1 if we failed to add one.
674
 */
675
  public long quaternion(Data4D quaternion, Data3D center )
676
    {
677
    return mM.add(EffectNames.QUATERNION,quaternion,center);
678
    }
679

    
680
///////////////////////////////////////////////////////////////////////////////////////////////////
681
/**
682
 * Shears the Object.
683
 *
684
 * @param shear   The 3-tuple of shear factors. The first controls level
685
 *                of shearing in the X-axis, second - Y-axis and the third -
686
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
687
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
688
 * @param center  Center of shearing, i.e. the point which stays unmoved.
689
 * @return        ID of the effect added, or -1 if we failed to add one.
690
 */
691
  public long shear(Data3D shear, Data3D center)
692
    {
693
    return mM.add(EffectNames.SHEAR, shear, center);
694
    }
695

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

    
715
///////////////////////////////////////////////////////////////////////////////////////////////////
716
/**
717
 * Makes the whole Object smoothly change all three of its RGB components.
718
 *
719
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
720
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
721
 *               Valid range: <0,1>
722
 * @param color  Color to mix. (1,0,0) is RED.
723
 * @return       ID of the effect added, or -1 if we failed to add one.
724
 */
725
  public long chroma(Data1D blend, Data3D color)
726
    {
727
    return mF.add(EffectNames.CHROMA, blend, color);
728
    }
729

    
730
///////////////////////////////////////////////////////////////////////////////////////////////////
731
/**
732
 * Makes a certain sub-region of the Object smoothly change its transparency level.
733
 *        
734
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any given
735
 *               moment: pixel.a *= alpha.
736
 *               Valid range: <0,1>
737
 * @param region Region this Effect is limited to. 
738
 * @param smooth If true, the level of 'alpha' will smoothly fade out towards the edges of the region.
739
 * @return       ID of the effect added, or -1 if we failed to add one. 
740
 */
741
  public long alpha(Data1D alpha, Data4D region, boolean smooth)
742
    {
743
    return mF.add( smooth? EffectNames.SMOOTH_ALPHA:EffectNames.ALPHA, alpha, region);
744
    }
745

    
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747
/**
748
 * Makes the whole Object smoothly change its transparency level.
749
 *
750
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any
751
 *               given moment: pixel.a *= alpha.
752
 *               Valid range: <0,1>
753
 * @return       ID of the effect added, or -1 if we failed to add one.
754
 */
755
  public long alpha(Data1D alpha)
756
    {
757
    return mF.add(EffectNames.ALPHA, alpha);
758
    }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761
/**
762
 * Makes a certain sub-region of the Object smoothly change its brightness level.
763
 *        
764
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
765
 *                   at any given moment. Valid range: <0,infinity)
766
 * @param region     Region this Effect is limited to.
767
 * @param smooth     If true, the level of 'brightness' will smoothly fade out towards the edges of the region.
768
 * @return           ID of the effect added, or -1 if we failed to add one.
769
 */
770
  public long brightness(Data1D brightness, Data4D region, boolean smooth)
771
    {
772
    return mF.add( smooth ? EffectNames.SMOOTH_BRIGHTNESS: EffectNames.BRIGHTNESS, brightness, region);
773
    }
774

    
775
///////////////////////////////////////////////////////////////////////////////////////////////////
776
/**
777
 * Makes the whole Object smoothly change its brightness level.
778
 *
779
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
780
 *                   at any given moment. Valid range: <0,infinity)
781
 * @return           ID of the effect added, or -1 if we failed to add one.
782
 */
783
  public long brightness(Data1D brightness)
784
    {
785
    return mF.add(EffectNames.BRIGHTNESS, brightness);
786
    }
787

    
788
///////////////////////////////////////////////////////////////////////////////////////////////////
789
/**
790
 * Makes a certain sub-region of the Object smoothly change its contrast level.
791
 *        
792
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
793
 *                 at any given moment. Valid range: <0,infinity)
794
 * @param region   Region this Effect is limited to.
795
 * @param smooth   If true, the level of 'contrast' will smoothly fade out towards the edges of the region.
796
 * @return         ID of the effect added, or -1 if we failed to add one.
797
 */
798
  public long contrast(Data1D contrast, Data4D region, boolean smooth)
799
    {
800
    return mF.add( smooth ? EffectNames.SMOOTH_CONTRAST:EffectNames.CONTRAST, contrast, region);
801
    }
802

    
803
///////////////////////////////////////////////////////////////////////////////////////////////////
804
/**
805
 * Makes the whole Object smoothly change its contrast level.
806
 *
807
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
808
 *                 at any given moment. Valid range: <0,infinity)
809
 * @return         ID of the effect added, or -1 if we failed to add one.
810
 */
811
  public long contrast(Data1D contrast)
812
    {
813
    return mF.add(EffectNames.CONTRAST, contrast);
814
    }
815

    
816
///////////////////////////////////////////////////////////////////////////////////////////////////
817
/**
818
 * Makes a certain sub-region of the Object smoothly change its saturation level.
819
 *        
820
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
821
 *                   at any given moment. Valid range: <0,infinity)
822
 * @param region     Region this Effect is limited to.
823
 * @param smooth     If true, the level of 'saturation' will smoothly fade out towards the edges of the region.
824
 * @return           ID of the effect added, or -1 if we failed to add one.
825
 */
826
  public long saturation(Data1D saturation, Data4D region, boolean smooth)
827
    {
828
    return mF.add( smooth ? EffectNames.SMOOTH_SATURATION:EffectNames.SATURATION, saturation, region);
829
    }
830

    
831
///////////////////////////////////////////////////////////////////////////////////////////////////
832
/**
833
 * Makes the whole Object smoothly change its saturation level.
834
 *
835
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
836
 *                   at any given moment. Valid range: <0,infinity)
837
 * @return           ID of the effect added, or -1 if we failed to add one.
838
 */
839
  public long saturation(Data1D saturation)
840
    {
841
    return mF.add(EffectNames.SATURATION, saturation);
842
    }
843

    
844
///////////////////////////////////////////////////////////////////////////////////////////////////
845
// Vertex-based effects  
846
///////////////////////////////////////////////////////////////////////////////////////////////////
847
/**
848
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
849
 *
850
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
851
 *               currently being dragged with.
852
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
853
 * @param region Region that masks the Effect.
854
 * @return       ID of the effect added, or -1 if we failed to add one.
855
 */
856
  public long distort(Data3D vector, Data3D center, Data4D region)
857
    {  
858
    return mV.add(EffectNames.DISTORT, vector, center, region);
859
    }
860

    
861
///////////////////////////////////////////////////////////////////////////////////////////////////
862
/**
863
 * Distort the whole Object by a (possibly changing in time) vector of force.
864
 *
865
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
866
 *               currently being dragged with.
867
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
868
 * @return       ID of the effect added, or -1 if we failed to add one.
869
 */
870
  public long distort(Data3D vector, Data3D center)
871
    {
872
    return mV.add(EffectNames.DISTORT, vector, center, null);
873
    }
874

    
875
///////////////////////////////////////////////////////////////////////////////////////////////////
876
/**
877
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
878
 * a (possibly changing in time) point on the Object.
879
 *
880
 * @param vector Vector of force that deforms the shape of the whole Object.
881
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
882
 * @param region Region that masks the Effect.
883
 * @return       ID of the effect added, or -1 if we failed to add one.
884
 */
885
  public long deform(Data3D vector, Data3D center, Data4D region)
886
    {
887
    return mV.add(EffectNames.DEFORM, vector, center, region);
888
    }
889

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

    
904
///////////////////////////////////////////////////////////////////////////////////////////////////  
905
/**
906
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
907
 * away from the center (degree<=1)
908
 *
909
 * @param sink   The current degree of the Effect.
910
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
911
 * @param region Region that masks the Effect.
912
 * @return       ID of the effect added, or -1 if we failed to add one.
913
 */
914
  public long sink(Data1D sink, Data3D center, Data4D region)
915
    {
916
    return mV.add(EffectNames.SINK, sink, center, region);
917
    }
918

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

    
933
///////////////////////////////////////////////////////////////////////////////////////////////////
934
/**
935
 * Pull all points around the center of the Effect towards a line passing through the center
936
 * (that's if degree>=1) or push them away from the line (degree<=1)
937
 *
938
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
939
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
940
 * @param region Region that masks the Effect.
941
 * @return       ID of the effect added, or -1 if we failed to add one.
942
 */
943
  public long pinch(Data2D pinch, Data3D center, Data4D region)
944
    {
945
    return mV.add(EffectNames.PINCH, pinch, center, region);
946
    }
947

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

    
962
///////////////////////////////////////////////////////////////////////////////////////////////////  
963
/**
964
 * Rotate part of the Object around the Center of the Effect by a certain angle.
965
 *
966
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
967
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
968
 * @param region Region that masks the Effect.
969
 * @return       ID of the effect added, or -1 if we failed to add one.
970
 */
971
  public long swirl(Data1D swirl, Data3D center, Data4D region)
972
    {    
973
    return mV.add(EffectNames.SWIRL, swirl, center, region);
974
    }
975

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

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

    
1023
///////////////////////////////////////////////////////////////////////////////////////////////////
1024
/**
1025
 * Directional, sinusoidal wave effect.
1026
 *
1027
 * @param wave   see {@link DistortedEffects#wave(Data5D,Data3D)}
1028
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1029
 * @param region Region that masks the Effect.
1030
 * @return       ID of the effect added, or -1 if we failed to add one.
1031
 */
1032
  public long wave(Data5D wave, Data3D center, Data4D region)
1033
    {
1034
    return mV.add(EffectNames.WAVE, wave, center, region);
1035
    }
1036
  }
(2-2/26)