Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffects.java @ 7170e4eb

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
  /// BLIT DEPTH PROGRAM ///
82
  private static DistortedProgram mBlitDepthProgram;
83
  private static int mBlitDepthTextureH;
84
  private static int mBlitDepthDepthTextureH;
85
  private static int mBlitDepthDepthH;
86

    
87
  /// NORMAL PROGRAM /////
88
  private static DistortedProgram mNormalProgram;
89
  private static int mNormalMVPMatrixH;
90
  /// END PROGRAMS //////
91

    
92
  private static long mNextID =0;
93
  private long mID;
94

    
95
  private EffectQueueMatrix   mM;
96
  private EffectQueueFragment mF;
97
  private EffectQueueVertex   mV;
98

    
99
  private boolean matrixCloned, vertexCloned, fragmentCloned;
100

    
101
///////////////////////////////////////////////////////////////////////////////////////////////////
102

    
103
  static void createProgram(Resources resources)
104
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
105
    {
106
    // MAIN PROGRAM ////////////////////////////////////
107
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
108
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
109

    
110
    String mainVertHeader= Distorted.GLSL_VERSION;
111
    String mainFragHeader= Distorted.GLSL_VERSION;
112

    
113
    EffectNames name;
114
    EffectTypes type;
115
    boolean foundF = false;
116
    boolean foundV = false;
117

    
118
    for(int i=0; i<mEffectEnabled.length; i++)
119
      {
120
      if( mEffectEnabled[i] )
121
        {
122
        name = EffectNames.getName(i);
123
        type = EffectNames.getType(i);
124

    
125
        if( type == EffectTypes.VERTEX )
126
          {
127
          mainVertHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
128
          foundV = true;
129
          }
130
        else if( type == EffectTypes.FRAGMENT )
131
          {
132
          mainFragHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
133
          foundF = true;
134
          }
135
        }
136
      }
137

    
138
    mainVertHeader += ("#define NUM_VERTEX "   + ( foundV ? getMaxVertex()   : 0 ) + "\n");
139
    mainFragHeader += ("#define NUM_FRAGMENT " + ( foundF ? getMaxFragment() : 0 ) + "\n");
140

    
141
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
142
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
143

    
144
    String[] feedback = { "v_Position", "v_endPosition" };
145

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

    
148
    int mainProgramH = mMainProgram.getProgramHandle();
149
    EffectQueueFragment.getUniforms(mainProgramH);
150
    EffectQueueVertex.getUniforms(mainProgramH);
151
    EffectQueueMatrix.getUniforms(mainProgramH);
152
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
153

    
154
    // BLIT PROGRAM ////////////////////////////////////
155
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
156
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
157

    
158
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
159
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
160

    
161
    try
162
      {
163
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
164
      }
165
    catch(Exception e)
166
      {
167
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
168
      throw new RuntimeException(e.getMessage());
169
      }
170

    
171
    int blitProgramH = mBlitProgram.getProgramHandle();
172
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
173
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
174

    
175
    // BLIT DEPTH PROGRAM ////////////////////////////////////
176
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
177
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
178

    
179
    try
180
      {
181
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
182
      }
183
    catch(Exception e)
184
      {
185
      android.util.Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
186
      throw new RuntimeException(e.getMessage());
187
      }
188

    
189
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
190
    mBlitDepthTextureH      = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Texture");
191
    mBlitDepthDepthTextureH = GLES30.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
192
    mBlitDepthDepthH        = GLES30.glGetUniformLocation( blitDepthProgramH, "u_Depth");
193

    
194
    // NORMAL PROGRAM //////////////////////////////////////
195
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
196
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
197

    
198
    try
199
      {
200
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
201
      }
202
    catch(Exception e)
203
      {
204
      android.util.Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
205
      throw new RuntimeException(e.getMessage());
206
      }
207

    
208
    int normalProgramH = mNormalProgram.getProgramHandle();
209
    mNormalMVPMatrixH  = GLES30.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213

    
214
  private void initializeEffectLists(DistortedEffects d, int flags)
215
    {
216
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
217
      {
218
      mM = d.mM;
219
      matrixCloned = true;
220
      }
221
    else
222
      {
223
      mM = new EffectQueueMatrix(mID);
224
      matrixCloned = false;
225
      }
226
    
227
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
228
      {
229
      mV = d.mV;
230
      vertexCloned = true;
231
      }
232
    else
233
      {
234
      mV = new EffectQueueVertex(mID);
235
      vertexCloned = false;
236
      }
237
    
238
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
239
      {
240
      mF = d.mF;
241
      fragmentCloned = true;
242
      }
243
    else
244
      {
245
      mF = new EffectQueueFragment(mID);
246
      fragmentCloned = false;
247
      }
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  private void displayNormals(MeshObject mesh)
253
    {
254
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
255
    GLES30.glBeginTransformFeedback( GLES30.GL_POINTS);
256
    DistortedRenderState.switchOffDrawing();
257
    GLES30.glDrawArrays( GLES30.GL_POINTS, 0, mesh.numVertices);
258
    DistortedRenderState.restoreDrawing();
259
    GLES30.glEndTransformFeedback();
260
    GLES30.glBindBufferBase(GLES30.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
261

    
262
    mNormalProgram.useProgram();
263
    GLES30.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
264
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
265
    GLES30.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, 0);
266
    GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
267
    GLES30.glLineWidth(8.0f);
268
    GLES30.glDrawArrays(GLES30.GL_LINES, 0, 2*mesh.numVertices);
269
    }
270

    
271
///////////////////////////////////////////////////////////////////////////////////////////////////
272

    
273
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
274
    {
275
    mM.compute(currTime);
276
    mV.compute(currTime);
277
    mF.compute(currTime);
278

    
279
    float halfZ = halfW*mesh.zFactor;
280

    
281
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
282

    
283
    mMainProgram.useProgram();
284
    GLES30.glUniform1i(mMainTextureH, 0);
285

    
286
    if( Distorted.GLSL >= 300 )
287
      {
288
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
289
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
290
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
291
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
292
      GLES30.glBindBuffer(GLES30.GL_ARRAY_BUFFER, 0);
293
      }
294
    else
295
      {
296
      mesh.mVertAttribs.position(0);
297
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
298
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
299
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
300
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
301
      GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES30.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
302
      }
303

    
304
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
305
    mV.send(halfW,halfH,halfZ);
306
    mF.send(halfW,halfH);
307

    
308
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
309

    
310
    if( mesh.mShowNormals ) displayNormals(mesh);
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
   
315
  static void blitPriv(DistortedOutputSurface surface)
316
    {
317
    mBlitProgram.useProgram();
318

    
319
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
320
    GLES30.glUniform1i(mBlitTextureH, 0);
321
    GLES30.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
322
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
323
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
324
    }
325

    
326
///////////////////////////////////////////////////////////////////////////////////////////////////
327

    
328
  static void blitDepthPriv(DistortedOutputSurface surface)
329
    {
330
    mBlitDepthProgram.useProgram();
331

    
332
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
333
    GLES30.glUniform1i(mBlitDepthTextureH, 0);
334
    GLES30.glUniform1i(mBlitDepthDepthTextureH, 1);
335
    GLES30.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
336
    GLES30.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
337
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
   
342
  private void releasePriv()
343
    {
344
    if( !matrixCloned   ) mM.abortAll(false);
345
    if( !vertexCloned   ) mV.abortAll(false);
346
    if( !fragmentCloned ) mF.abortAll(false);
347

    
348
    mM = null;
349
    mV = null;
350
    mF = null;
351
    }
352

    
353
///////////////////////////////////////////////////////////////////////////////////////////////////
354

    
355
  static void onDestroy()
356
    {
357
    mNextID = 0;
358

    
359
    int len = EffectNames.size();
360

    
361
    for(int i=0; i<len; i++)
362
      {
363
      mEffectEnabled[i] = false;
364
      }
365
    }
366

    
367
///////////////////////////////////////////////////////////////////////////////////////////////////
368
// PUBLIC API
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
/**
371
 * Create empty effect queue.
372
 */
373
  public DistortedEffects()
374
    {
375
    mID = ++mNextID;
376
    initializeEffectLists(this,0);
377
    }
378

    
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
/**
381
 * Copy constructor.
382
 * <p>
383
 * Whatever we do not clone gets created just like in the default constructor.
384
 *
385
 * @param dc    Source object to create our object from
386
 * @param flags A bitmask of values specifying what to copy.
387
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
388
 */
389
  public DistortedEffects(DistortedEffects dc, int flags)
390
    {
391
    mID = ++mNextID;
392
    initializeEffectLists(dc,flags);
393
    }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
/**
397
 * Releases all resources. After this call, the queue should not be used anymore.
398
 */
399
  @SuppressWarnings("unused")
400
  public synchronized void delete()
401
    {
402
    releasePriv();
403
    }
404

    
405
///////////////////////////////////////////////////////////////////////////////////////////////////
406
/**
407
 * Returns unique ID of this instance.
408
 *
409
 * @return ID of the object.
410
 */
411
  public long getID()
412
      {
413
      return mID;
414
      }
415

    
416
///////////////////////////////////////////////////////////////////////////////////////////////////
417
/**
418
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
419
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
420
 * 
421
 * @param el A class implementing the EffectListener interface that wants to get notifications.
422
 */
423
  @SuppressWarnings("unused")
424
  public void registerForMessages(EffectListener el)
425
    {
426
    mV.registerForMessages(el);
427
    mF.registerForMessages(el);
428
    mM.registerForMessages(el);
429
    }
430

    
431
///////////////////////////////////////////////////////////////////////////////////////////////////
432
/**
433
 * Removes the calling class from the list of Listeners.
434
 * 
435
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
436
 */
437
  @SuppressWarnings("unused")
438
  public void deregisterForMessages(EffectListener el)
439
    {
440
    mV.deregisterForMessages(el);
441
    mF.deregisterForMessages(el);
442
    mM.deregisterForMessages(el);
443
    }
444

    
445
///////////////////////////////////////////////////////////////////////////////////////////////////
446
/**
447
 * Aborts all Effects.
448
 * @return Number of effects aborted.
449
 */
450
  public int abortAllEffects()
451
    {
452
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
453
    }
454

    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456
/**
457
 * Aborts all Effects of a given type, for example all MATRIX Effects.
458
 * 
459
 * @param type one of the constants defined in {@link EffectTypes}
460
 * @return Number of effects aborted.
461
 */
462
  public int abortEffects(EffectTypes type)
463
    {
464
    switch(type)
465
      {
466
      case MATRIX     : return mM.abortAll(true);
467
      case VERTEX     : return mV.abortAll(true);
468
      case FRAGMENT   : return mF.abortAll(true);
469
      default         : return 0;
470
      }
471
    }
472
    
473
///////////////////////////////////////////////////////////////////////////////////////////////////
474
/**
475
 * Aborts a single Effect.
476
 * 
477
 * @param id ID of the Effect we want to abort.
478
 * @return number of Effects aborted. Always either 0 or 1.
479
 */
480
  public int abortEffect(long id)
481
    {
482
    int type = (int)(id&EffectTypes.MASK);
483

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

    
488
    return 0;
489
    }
490

    
491
///////////////////////////////////////////////////////////////////////////////////////////////////
492
/**
493
 * Abort all Effects of a given name, for example all rotations.
494
 * 
495
 * @param name one of the constants defined in {@link EffectNames}
496
 * @return number of Effects aborted.
497
 */
498
  public int abortEffects(EffectNames name)
499
    {
500
    switch(name.getType())
501
      {
502
      case MATRIX     : return mM.removeByType(name);
503
      case VERTEX     : return mV.removeByType(name);
504
      case FRAGMENT   : return mF.removeByType(name);
505
      default         : return 0;
506
      }
507
    }
508
    
509
///////////////////////////////////////////////////////////////////////////////////////////////////
510
/**
511
 * Print some info about a given Effect to Android's standard out. Used for debugging only.
512
 * 
513
 * @param id Effect ID we want to print info about
514
 * @return <code>true</code> if a single Effect of type effectType has been found.
515
 */
516
  @SuppressWarnings("unused")
517
  public boolean printEffect(long id)
518
    {
519
    int type = (int)(id&EffectTypes.MASK);
520

    
521
    if( type==EffectTypes.MATRIX.type  )  return mM.printByID(id>>EffectTypes.LENGTH);
522
    if( type==EffectTypes.VERTEX.type  )  return mV.printByID(id>>EffectTypes.LENGTH);
523
    if( type==EffectTypes.FRAGMENT.type)  return mF.printByID(id>>EffectTypes.LENGTH);
524

    
525
    return false;
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Enables a given Effect.
531
 * <p>
532
 * By default, all effects are disabled. One has to explicitly enable each effect one intends to use.
533
 * This needs to be called BEFORE shaders get compiled, i.e. before the call to Distorted.onCreate().
534
 * The point: by enabling only the effects we need, we can optimize the shaders.
535
 *
536
 * @param name Name of the Effect to enable.
537
 */
538
  public static void enableEffect(EffectNames name)
539
    {
540
    mEffectEnabled[name.ordinal()] = true;
541
    }
542

    
543
///////////////////////////////////////////////////////////////////////////////////////////////////
544
/**
545
 * Returns the maximum number of Matrix effects.
546
 *
547
 * @return The maximum number of Matrix effects
548
 */
549
  @SuppressWarnings("unused")
550
  public static int getMaxMatrix()
551
    {
552
    return EffectQueue.getMax(EffectTypes.MATRIX.ordinal());
553
    }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556
/**
557
 * Returns the maximum number of Vertex effects.
558
 *
559
 * @return The maximum number of Vertex effects
560
 */
561
  @SuppressWarnings("unused")
562
  public static int getMaxVertex()
563
    {
564
    return EffectQueue.getMax(EffectTypes.VERTEX.ordinal());
565
    }
566

    
567
///////////////////////////////////////////////////////////////////////////////////////////////////
568
/**
569
 * Returns the maximum number of Fragment effects.
570
 *
571
 * @return The maximum number of Fragment effects
572
 */
573
  @SuppressWarnings("unused")
574
  public static int getMaxFragment()
575
    {
576
    return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal());
577
    }
578

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

    
601
///////////////////////////////////////////////////////////////////////////////////////////////////
602
/**
603
 * Sets the maximum number of Vertex effects that can be stored in a single EffectQueue at one time.
604
 * This can fail if:
605
 * <ul>
606
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
607
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
608
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
609
 *     time only decreasing the value of 'max' is permitted.
610
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
611
 * </ul>
612
 *
613
 * @param max new maximum number of simultaneous Vertex Effects. Has to be a non-negative number not greater
614
 *            than Byte.MAX_VALUE
615
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
616
 */
617
  @SuppressWarnings("unused")
618
  public static boolean setMaxVertex(int max)
619
    {
620
    return EffectQueue.setMax(EffectTypes.VERTEX.ordinal(),max);
621
    }
622

    
623
///////////////////////////////////////////////////////////////////////////////////////////////////
624
/**
625
 * Sets the maximum number of Fragment effects that can be stored in a single EffectQueue at one time.
626
 * This can fail if:
627
 * <ul>
628
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
629
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
630
 *     before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
631
 *     time only decreasing the value of 'max' is permitted.
632
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
633
 * </ul>
634
 *
635
 * @param max new maximum number of simultaneous Fragment Effects. Has to be a non-negative number not greater
636
 *            than Byte.MAX_VALUE
637
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
638
 */
639
  @SuppressWarnings("unused")
640
  public static boolean setMaxFragment(int max)
641
    {
642
    return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max);
643
    }
644

    
645
///////////////////////////////////////////////////////////////////////////////////////////////////   
646
///////////////////////////////////////////////////////////////////////////////////////////////////
647
// Individual effect functions.
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
// Matrix-based effects
650
///////////////////////////////////////////////////////////////////////////////////////////////////
651
/**
652
 * Moves the Object by a (possibly changing in time) vector.
653
 * 
654
 * @param vector 3-dimensional Data which at any given time will return a Static3D
655
 *               representing the current coordinates of the vector we want to move the Object with.
656
 * @return       ID of the effect added, or -1 if we failed to add one.
657
 */
658
  public long move(Data3D vector)
659
    {   
660
    return mM.add(EffectNames.MOVE,vector);
661
    }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664
/**
665
 * Scales the Object by (possibly changing in time) 3D scale factors.
666
 * 
667
 * @param scale 3-dimensional Data which at any given time returns a Static3D
668
 *              representing the current x- , y- and z- scale factors.
669
 * @return      ID of the effect added, or -1 if we failed to add one.
670
 */
671
  public long scale(Data3D scale)
672
    {   
673
    return mM.add(EffectNames.SCALE,scale);
674
    }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677
/**
678
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
679
 *
680
 * @param scale The factor to scale all 3 dimensions with.
681
 * @return      ID of the effect added, or -1 if we failed to add one.
682
 */
683
  public long scale(float scale)
684
    {
685
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
686
    }
687

    
688
///////////////////////////////////////////////////////////////////////////////////////////////////
689
/**
690
 * Rotates the Object by 'angle' degrees around the center.
691
 * Static axis of rotation is given by the last parameter.
692
 *
693
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
694
 * @param axis   Axis of rotation
695
 * @param center Coordinates of the Point we are rotating around.
696
 * @return       ID of the effect added, or -1 if we failed to add one.
697
 */
698
  public long rotate(Data1D angle, Static3D axis, Data3D center )
699
    {   
700
    return mM.add(EffectNames.ROTATE, angle, axis, center);
701
    }
702

    
703
///////////////////////////////////////////////////////////////////////////////////////////////////
704
/**
705
 * Rotates the Object by 'angle' degrees around the center.
706
 * Here both angle and axis can dynamically change.
707
 *
708
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
709
 * @param center    Coordinates of the Point we are rotating around.
710
 * @return          ID of the effect added, or -1 if we failed to add one.
711
 */
712
  public long rotate(Data4D angleaxis, Data3D center)
713
    {
714
    return mM.add(EffectNames.ROTATE, angleaxis, center);
715
    }
716

    
717
///////////////////////////////////////////////////////////////////////////////////////////////////
718
/**
719
 * Rotates the Object by quaternion.
720
 *
721
 * @param quaternion The quaternion describing the rotation.
722
 * @param center     Coordinates of the Point we are rotating around.
723
 * @return           ID of the effect added, or -1 if we failed to add one.
724
 */
725
  public long quaternion(Data4D quaternion, Data3D center )
726
    {
727
    return mM.add(EffectNames.QUATERNION,quaternion,center);
728
    }
729

    
730
///////////////////////////////////////////////////////////////////////////////////////////////////
731
/**
732
 * Shears the Object.
733
 *
734
 * @param shear   The 3-tuple of shear factors. The first controls level
735
 *                of shearing in the X-axis, second - Y-axis and the third -
736
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
737
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
738
 * @param center  Center of shearing, i.e. the point which stays unmoved.
739
 * @return        ID of the effect added, or -1 if we failed to add one.
740
 */
741
  public long shear(Data3D shear, Data3D center)
742
    {
743
    return mM.add(EffectNames.SHEAR, shear, center);
744
    }
745

    
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747
// Fragment-based effects  
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749
/**
750
 * Makes a certain sub-region of the Object smoothly change all three of its RGB components.
751
 *        
752
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
753
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
754
 *               Valid range: <0,1>
755
 * @param color  Color to mix. (1,0,0) is RED.
756
 * @param region Region this Effect is limited to.
757
 * @param smooth If true, the level of 'blend' will smoothly fade out towards the edges of the region.
758
 * @return       ID of the effect added, or -1 if we failed to add one.
759
 */
760
  public long chroma(Data1D blend, Data3D color, Data4D region, boolean smooth)
761
    {
762
    return mF.add( smooth? EffectNames.SMOOTH_CHROMA:EffectNames.CHROMA, blend, color, region);
763
    }
764

    
765
///////////////////////////////////////////////////////////////////////////////////////////////////
766
/**
767
 * Makes the whole Object smoothly change all three of its RGB components.
768
 *
769
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
770
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
771
 *               Valid range: <0,1>
772
 * @param color  Color to mix. (1,0,0) is RED.
773
 * @return       ID of the effect added, or -1 if we failed to add one.
774
 */
775
  public long chroma(Data1D blend, Data3D color)
776
    {
777
    return mF.add(EffectNames.CHROMA, blend, color);
778
    }
779

    
780
///////////////////////////////////////////////////////////////////////////////////////////////////
781
/**
782
 * Makes a certain sub-region of the Object smoothly change its transparency level.
783
 *        
784
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any given
785
 *               moment: pixel.a *= alpha.
786
 *               Valid range: <0,1>
787
 * @param region Region this Effect is limited to. 
788
 * @param smooth If true, the level of 'alpha' will smoothly fade out towards the edges of the region.
789
 * @return       ID of the effect added, or -1 if we failed to add one. 
790
 */
791
  public long alpha(Data1D alpha, Data4D region, boolean smooth)
792
    {
793
    return mF.add( smooth? EffectNames.SMOOTH_ALPHA:EffectNames.ALPHA, alpha, region);
794
    }
795

    
796
///////////////////////////////////////////////////////////////////////////////////////////////////
797
/**
798
 * Makes the whole Object smoothly change its transparency level.
799
 *
800
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any
801
 *               given moment: pixel.a *= alpha.
802
 *               Valid range: <0,1>
803
 * @return       ID of the effect added, or -1 if we failed to add one.
804
 */
805
  public long alpha(Data1D alpha)
806
    {
807
    return mF.add(EffectNames.ALPHA, alpha);
808
    }
809

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

    
825
///////////////////////////////////////////////////////////////////////////////////////////////////
826
/**
827
 * Makes the whole Object smoothly change its brightness level.
828
 *
829
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
830
 *                   at any given moment. Valid range: <0,infinity)
831
 * @return           ID of the effect added, or -1 if we failed to add one.
832
 */
833
  public long brightness(Data1D brightness)
834
    {
835
    return mF.add(EffectNames.BRIGHTNESS, brightness);
836
    }
837

    
838
///////////////////////////////////////////////////////////////////////////////////////////////////
839
/**
840
 * Makes a certain sub-region of the Object smoothly change its contrast level.
841
 *        
842
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
843
 *                 at any given moment. Valid range: <0,infinity)
844
 * @param region   Region this Effect is limited to.
845
 * @param smooth   If true, the level of 'contrast' will smoothly fade out towards the edges of the region.
846
 * @return         ID of the effect added, or -1 if we failed to add one.
847
 */
848
  public long contrast(Data1D contrast, Data4D region, boolean smooth)
849
    {
850
    return mF.add( smooth ? EffectNames.SMOOTH_CONTRAST:EffectNames.CONTRAST, contrast, region);
851
    }
852

    
853
///////////////////////////////////////////////////////////////////////////////////////////////////
854
/**
855
 * Makes the whole Object smoothly change its contrast level.
856
 *
857
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
858
 *                 at any given moment. Valid range: <0,infinity)
859
 * @return         ID of the effect added, or -1 if we failed to add one.
860
 */
861
  public long contrast(Data1D contrast)
862
    {
863
    return mF.add(EffectNames.CONTRAST, contrast);
864
    }
865

    
866
///////////////////////////////////////////////////////////////////////////////////////////////////
867
/**
868
 * Makes a certain sub-region of the Object smoothly change its saturation level.
869
 *        
870
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
871
 *                   at any given moment. Valid range: <0,infinity)
872
 * @param region     Region this Effect is limited to.
873
 * @param smooth     If true, the level of 'saturation' will smoothly fade out towards the edges of the region.
874
 * @return           ID of the effect added, or -1 if we failed to add one.
875
 */
876
  public long saturation(Data1D saturation, Data4D region, boolean smooth)
877
    {
878
    return mF.add( smooth ? EffectNames.SMOOTH_SATURATION:EffectNames.SATURATION, saturation, region);
879
    }
880

    
881
///////////////////////////////////////////////////////////////////////////////////////////////////
882
/**
883
 * Makes the whole Object smoothly change its saturation level.
884
 *
885
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
886
 *                   at any given moment. Valid range: <0,infinity)
887
 * @return           ID of the effect added, or -1 if we failed to add one.
888
 */
889
  public long saturation(Data1D saturation)
890
    {
891
    return mF.add(EffectNames.SATURATION, saturation);
892
    }
893

    
894
///////////////////////////////////////////////////////////////////////////////////////////////////
895
// Vertex-based effects  
896
///////////////////////////////////////////////////////////////////////////////////////////////////
897
/**
898
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
899
 *
900
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
901
 *               currently being dragged with.
902
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
903
 * @param region Region that masks the Effect.
904
 * @return       ID of the effect added, or -1 if we failed to add one.
905
 */
906
  public long distort(Data3D vector, Data3D center, Data4D region)
907
    {  
908
    return mV.add(EffectNames.DISTORT, vector, center, region);
909
    }
910

    
911
///////////////////////////////////////////////////////////////////////////////////////////////////
912
/**
913
 * Distort the whole Object by a (possibly changing in time) vector of force.
914
 *
915
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
916
 *               currently being dragged with.
917
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
918
 * @return       ID of the effect added, or -1 if we failed to add one.
919
 */
920
  public long distort(Data3D vector, Data3D center)
921
    {
922
    return mV.add(EffectNames.DISTORT, vector, center, null);
923
    }
924

    
925
///////////////////////////////////////////////////////////////////////////////////////////////////
926
/**
927
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
928
 * a (possibly changing in time) point on the Object.
929
 *
930
 * @param vector Vector of force that deforms the shape of the whole Object.
931
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
932
 * @param region Region that masks the Effect.
933
 * @return       ID of the effect added, or -1 if we failed to add one.
934
 */
935
  public long deform(Data3D vector, Data3D center, Data4D region)
936
    {
937
    return mV.add(EffectNames.DEFORM, vector, center, region);
938
    }
939

    
940
///////////////////////////////////////////////////////////////////////////////////////////////////
941
/**
942
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
943
 * a (possibly changing in time) point on the Object.
944
 *     
945
 * @param vector Vector of force that deforms the shape of the whole Object.
946
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
947
 * @return       ID of the effect added, or -1 if we failed to add one.
948
 */
949
  public long deform(Data3D vector, Data3D center)
950
    {  
951
    return mV.add(EffectNames.DEFORM, vector, center, null);
952
    }
953

    
954
///////////////////////////////////////////////////////////////////////////////////////////////////  
955
/**
956
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
957
 * away from the center (degree<=1)
958
 *
959
 * @param sink   The current degree of the Effect.
960
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
961
 * @param region Region that masks the Effect.
962
 * @return       ID of the effect added, or -1 if we failed to add one.
963
 */
964
  public long sink(Data1D sink, Data3D center, Data4D region)
965
    {
966
    return mV.add(EffectNames.SINK, sink, center, region);
967
    }
968

    
969
///////////////////////////////////////////////////////////////////////////////////////////////////
970
/**
971
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
972
 * away from the center (degree<=1)
973
 *
974
 * @param sink   The current degree of the Effect.
975
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
976
 * @return       ID of the effect added, or -1 if we failed to add one.
977
 */
978
  public long sink(Data1D sink, Data3D center)
979
    {
980
    return mV.add(EffectNames.SINK, sink, center);
981
    }
982

    
983
///////////////////////////////////////////////////////////////////////////////////////////////////
984
/**
985
 * Pull all points around the center of the Effect towards a line passing through the center
986
 * (that's if degree>=1) or push them away from the line (degree<=1)
987
 *
988
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
989
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
990
 * @param region Region that masks the Effect.
991
 * @return       ID of the effect added, or -1 if we failed to add one.
992
 */
993
  public long pinch(Data2D pinch, Data3D center, Data4D region)
994
    {
995
    return mV.add(EffectNames.PINCH, pinch, center, region);
996
    }
997

    
998
///////////////////////////////////////////////////////////////////////////////////////////////////
999
/**
1000
 * Pull all points around the center of the Effect towards a line passing through the center
1001
 * (that's if degree>=1) or push them away from the line (degree<=1)
1002
 *
1003
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
1004
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1005
 * @return       ID of the effect added, or -1 if we failed to add one.
1006
 */
1007
  public long pinch(Data2D pinch, Data3D center)
1008
    {
1009
    return mV.add(EffectNames.PINCH, pinch, center);
1010
    }
1011

    
1012
///////////////////////////////////////////////////////////////////////////////////////////////////  
1013
/**
1014
 * Rotate part of the Object around the Center of the Effect by a certain angle.
1015
 *
1016
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
1017
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1018
 * @param region Region that masks the Effect.
1019
 * @return       ID of the effect added, or -1 if we failed to add one.
1020
 */
1021
  public long swirl(Data1D swirl, Data3D center, Data4D region)
1022
    {    
1023
    return mV.add(EffectNames.SWIRL, swirl, center, region);
1024
    }
1025

    
1026
///////////////////////////////////////////////////////////////////////////////////////////////////
1027
/**
1028
 * Rotate the whole Object around the Center of the Effect by a certain angle.
1029
 *
1030
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
1031
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1032
 * @return       ID of the effect added, or -1 if we failed to add one.
1033
 */
1034
  public long swirl(Data1D swirl, Data3D center)
1035
    {
1036
    return mV.add(EffectNames.SWIRL, swirl, center);
1037
    }
1038

    
1039
///////////////////////////////////////////////////////////////////////////////////////////////////
1040
/**
1041
 * Directional, sinusoidal wave effect.
1042
 *
1043
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
1044
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
1045
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
1046
 *               describe the 'direction' of the wave.
1047
 *               <p>
1048
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
1049
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
1050
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
1051
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
1052
 *               <p>
1053
 *               <p>
1054
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
1055
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
1056
 *               will be sine shapes.
1057
 *               <p>
1058
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
1059
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
1060
 *               YZ-plane with be sine shapes.
1061
 *               <p>
1062
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
1063
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
1064
 *               value if sin at this point.
1065
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1066
 * @return       ID of the effect added, or -1 if we failed to add one.
1067
 */
1068
  public long wave(Data5D wave, Data3D center)
1069
    {
1070
    return mV.add(EffectNames.WAVE, wave, center, null);
1071
    }
1072

    
1073
///////////////////////////////////////////////////////////////////////////////////////////////////
1074
/**
1075
 * Directional, sinusoidal wave effect.
1076
 *
1077
 * @param wave   see {@link DistortedEffects#wave(Data5D,Data3D)}
1078
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1079
 * @param region Region that masks the Effect.
1080
 * @return       ID of the effect added, or -1 if we failed to add one.
1081
 */
1082
  public long wave(Data5D wave, Data3D center, Data4D region)
1083
    {
1084
    return mV.add(EffectNames.WAVE, wave, center, region);
1085
    }
1086
  }
(2-2/26)