Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffects.java @ 270c27bc

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

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

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

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

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

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

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

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

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

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

    
321
    int len = EffectNames.size();
322

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

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

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

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

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

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

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

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

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

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

    
450
    return 0;
451
    }
452

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

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

    
487
    return false;
488
    }
489

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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