Project

General

Profile

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

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

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.main;
21

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

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

    
41
import java.io.InputStream;
42
import java.nio.ByteBuffer;
43
import java.nio.ByteOrder;
44
import java.nio.FloatBuffer;
45

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

    
60
  static
61
    {
62
    int len = EffectNames.size();
63

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

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

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

    
83
  /// BLIT DEPTH PROGRAM ///
84
  private static DistortedProgram mBlitDepthProgram;
85
  private static int mBlitDepthTextureH;
86
  private static int mBlitDepthDepthTextureH;
87
  private static int mBlitDepthDepthH;
88

    
89
  /// NORMAL PROGRAM /////
90
  private static DistortedProgram mNormalProgram;
91
  private static int mNormalMVPMatrixH;
92
  /// END PROGRAMS //////
93

    
94
  private static long mNextID =0;
95
  private long mID;
96

    
97
  private EffectQueueMatrix mM;
98
  private EffectQueueFragment mF;
99
  private EffectQueueVertex mV;
100

    
101
  private boolean matrixCloned, vertexCloned, fragmentCloned;
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

    
112
    String mainVertHeader= Distorted.GLSL_VERSION;
113
    String mainFragHeader= Distorted.GLSL_VERSION;
114

    
115
    EffectNames name;
116
    EffectTypes type;
117
    boolean foundF = false;
118
    boolean foundV = false;
119

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

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

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

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

    
146
    String[] feedback = { "v_Position", "v_endPosition" };
147

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

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

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

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

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

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

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

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

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

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

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

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

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

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

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

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

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

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274

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

    
281
    float halfZ = halfW*mesh.zFactor;
282

    
283
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight );
284

    
285
    mMainProgram.useProgram();
286
    GLES30.glUniform1i(mMainTextureH, 0);
287

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

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

    
310
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
311

    
312
    if( mesh.mShowNormals ) displayNormals(mesh);
313
    }
314

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

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

    
328
///////////////////////////////////////////////////////////////////////////////////////////////////
329

    
330
  static void blitDepthPriv(DistortedOutputSurface surface)
331
    {
332
    mBlitDepthProgram.useProgram();
333

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

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

    
350
    mM = null;
351
    mV = null;
352
    mF = null;
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  static void onDestroy()
358
    {
359
    mNextID = 0;
360

    
361
    int len = EffectNames.size();
362

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

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

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

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

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

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

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

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

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

    
486
    if( type==Effect.MATRIX   ) return mM.removeByID(id>>Effect.LENGTH);
487
    if( type==Effect.VERTEX   ) return mV.removeByID(id>>Effect.LENGTH);
488
    if( type==Effect.FRAGMENT ) return mF.removeByID(id>>Effect.LENGTH);
489

    
490
    return 0;
491
    }
492

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

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

    
527
    return false;
528
    }
529

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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