Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffects.java @ 55c14a19

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.opengl.GLES20;
23

    
24
import org.distorted.library.message.EffectListener;
25
import org.distorted.library.program.DistortedProgram;
26
import org.distorted.library.type.Data1D;
27
import org.distorted.library.type.Data2D;
28
import org.distorted.library.type.Data3D;
29
import org.distorted.library.type.Data4D;
30
import org.distorted.library.type.Data5D;
31
import org.distorted.library.type.Static3D;
32

    
33
import java.nio.ByteBuffer;
34
import java.nio.ByteOrder;
35
import java.nio.FloatBuffer;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
/**
39
 * Class containing {@link EffectTypes#LENGTH} queues, each a class derived from EffectQueue.
40
 * <p>
41
 * The queues hold actual effects to be applied to a given (DistortedTexture,MeshObject) combo.
42
 */
43
public class DistortedEffects
44
  {
45
  private static final int BYTES_PER_FLOAT   = 4; //
46
  private static final int MAIN_POSITION_DATA_SIZE= 3; // Size of the position data in elements
47
  private static final int MAIN_NORMAL_DATA_SIZE  = 3; // Size of the position data in elements
48
  private static final int MAIN_TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
49
  private static final int POST_POSITION_DATA_SIZE= 2; // Size of the position data in elements
50
  private static final int POST_TEX_DATA_SIZE     = 2; // Size of the texture coordinate data in elements.
51

    
52
  private static final FloatBuffer mQuadPositions, mQuadTexture;
53
  private static DistortedProgram mMainProgram, mPostProgram;
54

    
55
  private static long mNextID =0;
56
  private long mID;
57

    
58
  private static DistortedFramebuffer mBufferFBO;
59

    
60
  private EffectQueueMatrix      mM;
61
  private EffectQueueFragment    mF;
62
  private EffectQueueVertex      mV;
63
  private EffectQueuePostprocess mP;
64

    
65
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
66

    
67
  static
68
    {
69
    int dataLength = 4;
70

    
71
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
72
    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
73

    
74
    mQuadPositions = ByteBuffer.allocateDirect(POST_POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
75
    mQuadPositions.put(positionData).position(0);
76
    mQuadTexture   = ByteBuffer.allocateDirect(POST_TEX_DATA_SIZE     *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
77
    mQuadTexture.put(textureData).position(0);
78

    
79
    mBufferFBO = new DistortedFramebuffer(1,1);
80
    }
81

    
82
///////////////////////////////////////////////////////////////////////////////////////////////////
83

    
84
  static void setPrograms(DistortedProgram main, DistortedProgram post)
85
    {
86
    mMainProgram = main;
87
    mPostProgram = post;
88
    }
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  private void initializeEffectLists(DistortedEffects d, int flags)
93
    {
94
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
95
      {
96
      mM = d.mM;
97
      matrixCloned = true;
98
      }
99
    else
100
      {
101
      mM = new EffectQueueMatrix(mID);
102
      matrixCloned = false;
103
      }
104
    
105
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
106
      {
107
      mV = d.mV;
108
      vertexCloned = true;
109
      }
110
    else
111
      {
112
      mV = new EffectQueueVertex(mID);
113
      vertexCloned = false;
114
      }
115
    
116
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
117
      {
118
      mF = d.mF;
119
      fragmentCloned = true;
120
      }
121
    else
122
      {
123
      mF = new EffectQueueFragment(mID);
124
      fragmentCloned = false;
125
      }
126

    
127
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
128
      {
129
      mP = d.mP;
130
      postprocessCloned = true;
131
      }
132
    else
133
      {
134
      mP = new EffectQueuePostprocess(mID);
135
      postprocessCloned = false;
136
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
   
141
  void drawPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedFramebuffer df, long currTime)
142
    {
143
    mM.compute(currTime);
144
    mV.compute(currTime);
145
    mF.compute(currTime);
146
    mP.compute(currTime);
147

    
148
    float halfZ = halfInputW*mesh.zFactor;
149
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
150

    
151
    if( mP.mNumEffects==0 )
152
      {
153
      mMainProgram.useProgram();
154
      mM.send(df,halfInputW,halfInputH,halfZ);
155
      mV.send(halfInputW,halfInputH,halfZ);
156
      mF.send(halfInputW,halfInputH);
157
      GLES20.glVertexAttribPointer(mMainProgram.mAttribute[0], MAIN_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
158
      GLES20.glVertexAttribPointer(mMainProgram.mAttribute[1], MAIN_NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
159
      GLES20.glVertexAttribPointer(mMainProgram.mAttribute[2], MAIN_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
160
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
161
      }
162
    else
163
      {
164
      if( mV.mNumEffects>0 || mF.mNumEffects>0 )
165
        {
166
        mMainProgram.useProgram();
167
        mBufferFBO.resizeFast(df.mWidth, df.mHeight);
168
        mBufferFBO.setAsOutput();
169
        mM.send(mBufferFBO,halfInputW,halfInputH,halfZ);
170
        mV.send(halfInputW,halfInputH,halfZ);
171
        mF.send(halfInputW,halfInputH);
172
        GLES20.glVertexAttribPointer(mMainProgram.mAttribute[0], MAIN_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
173
        GLES20.glVertexAttribPointer(mMainProgram.mAttribute[1], MAIN_NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
174
        GLES20.glVertexAttribPointer(mMainProgram.mAttribute[2], MAIN_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
175
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
176

    
177
        mPostProgram.useProgram();
178
        mBufferFBO.setAsInput();
179
        df.setAsOutput();
180
        mP.send(df);
181
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[0], POST_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
182
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[1], POST_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
183
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
184
        }
185
      else
186
        {
187
        mPostProgram.useProgram();
188
        mM.constructMatrices(df,halfInputW,halfInputH);
189
        mP.send(2*halfInputW,2*halfInputH, mM.getMVP() );
190
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[0], POST_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
191
        GLES20.glVertexAttribPointer(mPostProgram.mAttribute[1], POST_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
192
        GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
193
        }
194
      }
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198
   
199
  static void drawNoEffectsPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedFramebuffer df)
200
    {
201
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
202

    
203
    EffectQueueMatrix.sendZero(df,halfInputW,halfInputH,halfInputW*mesh.zFactor);
204
    EffectQueueVertex.sendZero();
205
    EffectQueueFragment.sendZero();
206
    EffectQueuePostprocess.sendZero(df);
207

    
208
    GLES20.glVertexAttribPointer(mMainProgram.mAttribute[0], MAIN_POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mesh.mMeshPositions);
209
    GLES20.glVertexAttribPointer(mMainProgram.mAttribute[1], MAIN_NORMAL_DATA_SIZE  , GLES20.GL_FLOAT, false, 0, mesh.mMeshNormals);
210
    GLES20.glVertexAttribPointer(mMainProgram.mAttribute[2], MAIN_TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mesh.mMeshTexture);
211
    GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
212
    }
213
    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
   
216
  private void releasePriv()
217
    {
218
    if( !matrixCloned     ) mM.abortAll(false);
219
    if( !vertexCloned     ) mV.abortAll(false);
220
    if( !fragmentCloned   ) mF.abortAll(false);
221
    if( !postprocessCloned) mP.abortAll(false);
222

    
223
    mM = null;
224
    mV = null;
225
    mF = null;
226
    mP = null;
227
    }
228

    
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230

    
231
  static void onDestroy()
232
    {
233
    mNextID = 0;
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237
// PUBLIC API
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Create empty effect queue.
241
 */
242
  public DistortedEffects()
243
    {
244
    mID = mNextID++;
245
    initializeEffectLists(this,0);
246
    }
247

    
248
///////////////////////////////////////////////////////////////////////////////////////////////////
249
/**
250
 * Copy constructor.
251
 * <p>
252
 * Whatever we do not clone gets created just like in the default constructor.
253
 *
254
 * @param dc    Source object to create our object from
255
 * @param flags A bitmask of values specifying what to copy.
256
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
257
 */
258
  public DistortedEffects(DistortedEffects dc, int flags)
259
    {
260
    mID = mNextID++;
261
    initializeEffectLists(dc,flags);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
/**
266
 * Releases all resources. After this call, the queue should not be used anymore.
267
 */
268
  public synchronized void delete()
269
    {
270
    releasePriv();
271
    }
272

    
273
///////////////////////////////////////////////////////////////////////////////////////////////////
274
/**
275
 * Returns unique ID of this instance.
276
 *
277
 * @return ID of the object.
278
 */
279
  public long getID()
280
      {
281
      return mID;
282
      }
283

    
284
///////////////////////////////////////////////////////////////////////////////////////////////////
285
/**
286
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
287
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
288
 * 
289
 * @param el A class implementing the EffectListener interface that wants to get notifications.
290
 */
291
  public void registerForMessages(EffectListener el)
292
    {
293
    mV.registerForMessages(el);
294
    mF.registerForMessages(el);
295
    mM.registerForMessages(el);
296
    mP.registerForMessages(el);
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300
/**
301
 * Removes the calling class from the list of Listeners.
302
 * 
303
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
304
 */
305
  public void deregisterForMessages(EffectListener el)
306
    {
307
    mV.deregisterForMessages(el);
308
    mF.deregisterForMessages(el);
309
    mM.deregisterForMessages(el);
310
    mP.deregisterForMessages(el);
311
    }
312

    
313
///////////////////////////////////////////////////////////////////////////////////////////////////
314
/**
315
 * Aborts all Effects.
316
 * @return Number of effects aborted.
317
 */
318
  public int abortAllEffects()
319
      {
320
      return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true) + mP.abortAll(true);
321
      }
322

    
323
///////////////////////////////////////////////////////////////////////////////////////////////////
324
/**
325
 * Aborts all Effects of a given type, for example all MATRIX Effects.
326
 * 
327
 * @param type one of the constants defined in {@link EffectTypes}
328
 * @return Number of effects aborted.
329
 */
330
  public int abortEffects(EffectTypes type)
331
    {
332
    switch(type)
333
      {
334
      case MATRIX     : return mM.abortAll(true);
335
      case VERTEX     : return mV.abortAll(true);
336
      case FRAGMENT   : return mF.abortAll(true);
337
      case POSTPROCESS: return mP.abortAll(true);
338
      default         : return 0;
339
      }
340
    }
341
    
342
///////////////////////////////////////////////////////////////////////////////////////////////////
343
/**
344
 * Aborts a single Effect.
345
 * 
346
 * @param id ID of the Effect we want to abort.
347
 * @return number of Effects aborted. Always either 0 or 1.
348
 */
349
  public int abortEffect(long id)
350
    {
351
    int type = (int)(id&EffectTypes.MASK);
352

    
353
    if( type==EffectTypes.MATRIX.type      ) return mM.removeByID(id>>EffectTypes.LENGTH);
354
    if( type==EffectTypes.VERTEX.type      ) return mV.removeByID(id>>EffectTypes.LENGTH);
355
    if( type==EffectTypes.FRAGMENT.type    ) return mF.removeByID(id>>EffectTypes.LENGTH);
356
    if( type==EffectTypes.POSTPROCESS.type ) return mP.removeByID(id>>EffectTypes.LENGTH);
357

    
358
    return 0;
359
    }
360

    
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362
/**
363
 * Abort all Effects of a given name, for example all rotations.
364
 * 
365
 * @param name one of the constants defined in {@link EffectNames}
366
 * @return number of Effects aborted.
367
 */
368
  public int abortEffects(EffectNames name)
369
    {
370
    switch(name.getType())
371
      {
372
      case MATRIX     : return mM.removeByType(name);
373
      case VERTEX     : return mV.removeByType(name);
374
      case FRAGMENT   : return mF.removeByType(name);
375
      case POSTPROCESS: return mP.removeByType(name);
376
      default         : return 0;
377
      }
378
    }
379
    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
/**
382
 * Print some info about a given Effect to Android's standard out. Used for debugging only.
383
 * 
384
 * @param id Effect ID we want to print info about
385
 * @return <code>true</code> if a single Effect of type effectType has been found.
386
 */
387
    
388
  public boolean printEffect(long id)
389
    {
390
    int type = (int)(id&EffectTypes.MASK);
391

    
392
    if( type==EffectTypes.MATRIX.type      )  return mM.printByID(id>>EffectTypes.LENGTH);
393
    if( type==EffectTypes.VERTEX.type      )  return mV.printByID(id>>EffectTypes.LENGTH);
394
    if( type==EffectTypes.FRAGMENT.type    )  return mF.printByID(id>>EffectTypes.LENGTH);
395
    if( type==EffectTypes.POSTPROCESS.type )  return mP.printByID(id>>EffectTypes.LENGTH);
396

    
397
    return false;
398
    }
399

    
400
///////////////////////////////////////////////////////////////////////////////////////////////////
401
/**
402
 * Returns the maximum number of Matrix effects.
403
 *
404
 * @return The maximum number of Matrix effects
405
 */
406
  public static int getMaxMatrix()
407
    {
408
    return EffectQueue.getMax(EffectTypes.MATRIX.ordinal());
409
    }
410

    
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
/**
413
 * Returns the maximum number of Vertex effects.
414
 *
415
 * @return The maximum number of Vertex effects
416
 */
417
  public static int getMaxVertex()
418
    {
419
    return EffectQueue.getMax(EffectTypes.VERTEX.ordinal());
420
    }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423
/**
424
 * Returns the maximum number of Fragment effects.
425
 *
426
 * @return The maximum number of Fragment effects
427
 */
428
  public static int getMaxFragment()
429
    {
430
    return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal());
431
    }
432

    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434
/**
435
 * Returns the maximum number of Postprocess effects.
436
 *
437
 * @return The maximum number of Postprocess effects
438
 */
439
  public static int getMaxPostprocess()
440
    {
441
    return EffectQueue.getMax(EffectTypes.POSTPROCESS.ordinal());
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445
/**
446
 * Sets the maximum number of Matrix effects that can be stored in a single EffectQueue at one time.
447
 * This can fail if:
448
 * <ul>
449
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
450
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
451
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
452
 *     time only decreasing the value of 'max' is permitted.
453
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
454
 * </ul>
455
 *
456
 * @param max new maximum number of simultaneous Matrix Effects. Has to be a non-negative number not greater
457
 *            than Byte.MAX_VALUE
458
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
459
 */
460
  public static boolean setMaxMatrix(int max)
461
    {
462
    return EffectQueue.setMax(EffectTypes.MATRIX.ordinal(),max);
463
    }
464

    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466
/**
467
 * Sets the maximum number of Vertex effects that can be stored in a single EffectQueue at one time.
468
 * This can fail if:
469
 * <ul>
470
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
471
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
472
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
473
 *     time only decreasing the value of 'max' is permitted.
474
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
475
 * </ul>
476
 *
477
 * @param max new maximum number of simultaneous Vertex Effects. Has to be a non-negative number not greater
478
 *            than Byte.MAX_VALUE
479
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
480
 */
481
  public static boolean setMaxVertex(int max)
482
    {
483
    return EffectQueue.setMax(EffectTypes.VERTEX.ordinal(),max);
484
    }
485

    
486
///////////////////////////////////////////////////////////////////////////////////////////////////
487
/**
488
 * Sets the maximum number of Fragment effects that can be stored in a single EffectQueue at one time.
489
 * This can fail if:
490
 * <ul>
491
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
492
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
493
 *     before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
494
 *     time only decreasing the value of 'max' is permitted.
495
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
496
 * </ul>
497
 *
498
 * @param max new maximum number of simultaneous Fragment Effects. Has to be a non-negative number not greater
499
 *            than Byte.MAX_VALUE
500
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
501
 */
502
  public static boolean setMaxFragment(int max)
503
    {
504
    return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max);
505
    }
506

    
507
///////////////////////////////////////////////////////////////////////////////////////////////////
508
/**
509
 * Sets the maximum number of Postprocess effects that can be stored in a single EffectQueue at one time.
510
 * This can fail if:
511
 * <ul>
512
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
513
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
514
 *     before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
515
 *     time only decreasing the value of 'max' is permitted.
516
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
517
 * </ul>
518
 *
519
 * @param max new maximum number of simultaneous Postprocess Effects. Has to be a non-negative number not greater
520
 *            than Byte.MAX_VALUE
521
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
522
 */
523
  public static boolean setMaxPostprocess(int max)
524
    {
525
    return EffectQueue.setMax(EffectTypes.POSTPROCESS.ordinal(),max);
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////   
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
// Individual effect functions.
531
///////////////////////////////////////////////////////////////////////////////////////////////////
532
// Matrix-based effects
533
///////////////////////////////////////////////////////////////////////////////////////////////////
534
/**
535
 * Moves the Object by a (possibly changing in time) vector.
536
 * 
537
 * @param vector 3-dimensional Data which at any given time will return a Static3D
538
 *               representing the current coordinates of the vector we want to move the Object with.
539
 * @return       ID of the effect added, or -1 if we failed to add one.
540
 */
541
  public long move(Data3D vector)
542
    {   
543
    return mM.add(EffectNames.MOVE,vector);
544
    }
545

    
546
///////////////////////////////////////////////////////////////////////////////////////////////////
547
/**
548
 * Scales the Object by (possibly changing in time) 3D scale factors.
549
 * 
550
 * @param scale 3-dimensional Data which at any given time returns a Static3D
551
 *              representing the current x- , y- and z- scale factors.
552
 * @return      ID of the effect added, or -1 if we failed to add one.
553
 */
554
  public long scale(Data3D scale)
555
    {   
556
    return mM.add(EffectNames.SCALE,scale);
557
    }
558

    
559
///////////////////////////////////////////////////////////////////////////////////////////////////
560
/**
561
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
562
 *
563
 * @param scale The factor to scale all 3 dimensions with.
564
 * @return      ID of the effect added, or -1 if we failed to add one.
565
 */
566
  public long scale(float scale)
567
    {
568
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
569
    }
570

    
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572
/**
573
 * Rotates the Object by 'angle' degrees around the center.
574
 * Static axis of rotation is given by the last parameter.
575
 *
576
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
577
 * @param axis   Axis of rotation
578
 * @param center Coordinates of the Point we are rotating around.
579
 * @return       ID of the effect added, or -1 if we failed to add one.
580
 */
581
  public long rotate(Data1D angle, Static3D axis, Data3D center )
582
    {   
583
    return mM.add(EffectNames.ROTATE, angle, axis, center);
584
    }
585

    
586
///////////////////////////////////////////////////////////////////////////////////////////////////
587
/**
588
 * Rotates the Object by 'angle' degrees around the center.
589
 * Here both angle and axis can dynamically change.
590
 *
591
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
592
 * @param center    Coordinates of the Point we are rotating around.
593
 * @return          ID of the effect added, or -1 if we failed to add one.
594
 */
595
  public long rotate(Data4D angleaxis, Data3D center)
596
    {
597
    return mM.add(EffectNames.ROTATE, angleaxis, center);
598
    }
599

    
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601
/**
602
 * Rotates the Object by quaternion.
603
 *
604
 * @param quaternion The quaternion describing the rotation.
605
 * @param center     Coordinates of the Point we are rotating around.
606
 * @return           ID of the effect added, or -1 if we failed to add one.
607
 */
608
  public long quaternion(Data4D quaternion, Data3D center )
609
    {
610
    return mM.add(EffectNames.QUATERNION,quaternion,center);
611
    }
612

    
613
///////////////////////////////////////////////////////////////////////////////////////////////////
614
/**
615
 * Shears the Object.
616
 *
617
 * @param shear   The 3-tuple of shear factors. The first controls level
618
 *                of shearing in the X-axis, second - Y-axis and the third -
619
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
620
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
621
 * @param center  Center of shearing, i.e. the point which stays unmoved.
622
 * @return        ID of the effect added, or -1 if we failed to add one.
623
 */
624
  public long shear(Data3D shear, Data3D center)
625
    {
626
    return mM.add(EffectNames.SHEAR, shear, center);
627
    }
628

    
629
///////////////////////////////////////////////////////////////////////////////////////////////////
630
// Fragment-based effects  
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632
/**
633
 * Makes a certain sub-region of the Object smoothly change all three of its RGB components.
634
 *        
635
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
636
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
637
 *               Valid range: <0,1>
638
 * @param color  Color to mix. (1,0,0) is RED.
639
 * @param region Region this Effect is limited to.
640
 * @param smooth If true, the level of 'blend' will smoothly fade out towards the edges of the region.
641
 * @return       ID of the effect added, or -1 if we failed to add one.
642
 */
643
  public long chroma(Data1D blend, Data3D color, Data4D region, boolean smooth)
644
    {
645
    return mF.add( smooth? EffectNames.SMOOTH_CHROMA:EffectNames.CHROMA, blend, color, region);
646
    }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
/**
650
 * Makes the whole Object smoothly change all three of its RGB components.
651
 *
652
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
653
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
654
 *               Valid range: <0,1>
655
 * @param color  Color to mix. (1,0,0) is RED.
656
 * @return       ID of the effect added, or -1 if we failed to add one.
657
 */
658
  public long chroma(Data1D blend, Data3D color)
659
    {
660
    return mF.add(EffectNames.CHROMA, blend, color);
661
    }
662

    
663
///////////////////////////////////////////////////////////////////////////////////////////////////
664
/**
665
 * Makes a certain sub-region of the Object smoothly change its transparency level.
666
 *        
667
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any given
668
 *               moment: pixel.a *= alpha.
669
 *               Valid range: <0,1>
670
 * @param region Region this Effect is limited to. 
671
 * @param smooth If true, the level of 'alpha' will smoothly fade out towards the edges of the region.
672
 * @return       ID of the effect added, or -1 if we failed to add one. 
673
 */
674
  public long alpha(Data1D alpha, Data4D region, boolean smooth)
675
    {
676
    return mF.add( smooth? EffectNames.SMOOTH_ALPHA:EffectNames.ALPHA, alpha, region);
677
    }
678

    
679
///////////////////////////////////////////////////////////////////////////////////////////////////
680
/**
681
 * Makes the whole Object smoothly change its transparency level.
682
 *
683
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any
684
 *               given moment: pixel.a *= alpha.
685
 *               Valid range: <0,1>
686
 * @return       ID of the effect added, or -1 if we failed to add one.
687
 */
688
  public long alpha(Data1D alpha)
689
    {
690
    return mF.add(EffectNames.ALPHA, alpha);
691
    }
692

    
693
///////////////////////////////////////////////////////////////////////////////////////////////////
694
/**
695
 * Makes a certain sub-region of the Object smoothly change its brightness level.
696
 *        
697
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
698
 *                   at any given moment. Valid range: <0,infinity)
699
 * @param region     Region this Effect is limited to.
700
 * @param smooth     If true, the level of 'brightness' will smoothly fade out towards the edges of the region.
701
 * @return           ID of the effect added, or -1 if we failed to add one.
702
 */
703
  public long brightness(Data1D brightness, Data4D region, boolean smooth)
704
    {
705
    return mF.add( smooth ? EffectNames.SMOOTH_BRIGHTNESS: EffectNames.BRIGHTNESS, brightness, region);
706
    }
707

    
708
///////////////////////////////////////////////////////////////////////////////////////////////////
709
/**
710
 * Makes the whole Object smoothly change its brightness level.
711
 *
712
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
713
 *                   at any given moment. Valid range: <0,infinity)
714
 * @return           ID of the effect added, or -1 if we failed to add one.
715
 */
716
  public long brightness(Data1D brightness)
717
    {
718
    return mF.add(EffectNames.BRIGHTNESS, brightness);
719
    }
720

    
721
///////////////////////////////////////////////////////////////////////////////////////////////////
722
/**
723
 * Makes a certain sub-region of the Object smoothly change its contrast level.
724
 *        
725
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
726
 *                 at any given moment. Valid range: <0,infinity)
727
 * @param region   Region this Effect is limited to.
728
 * @param smooth   If true, the level of 'contrast' will smoothly fade out towards the edges of the region.
729
 * @return         ID of the effect added, or -1 if we failed to add one.
730
 */
731
  public long contrast(Data1D contrast, Data4D region, boolean smooth)
732
    {
733
    return mF.add( smooth ? EffectNames.SMOOTH_CONTRAST:EffectNames.CONTRAST, contrast, region);
734
    }
735

    
736
///////////////////////////////////////////////////////////////////////////////////////////////////
737
/**
738
 * Makes the whole Object smoothly change its contrast level.
739
 *
740
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
741
 *                 at any given moment. Valid range: <0,infinity)
742
 * @return         ID of the effect added, or -1 if we failed to add one.
743
 */
744
  public long contrast(Data1D contrast)
745
    {
746
    return mF.add(EffectNames.CONTRAST, contrast);
747
    }
748

    
749
///////////////////////////////////////////////////////////////////////////////////////////////////
750
/**
751
 * Makes a certain sub-region of the Object smoothly change its saturation level.
752
 *        
753
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
754
 *                   at any given moment. Valid range: <0,infinity)
755
 * @param region     Region this Effect is limited to.
756
 * @param smooth     If true, the level of 'saturation' will smoothly fade out towards the edges of the region.
757
 * @return           ID of the effect added, or -1 if we failed to add one.
758
 */
759
  public long saturation(Data1D saturation, Data4D region, boolean smooth)
760
    {
761
    return mF.add( smooth ? EffectNames.SMOOTH_SATURATION:EffectNames.SATURATION, saturation, region);
762
    }
763

    
764
///////////////////////////////////////////////////////////////////////////////////////////////////
765
/**
766
 * Makes the whole Object smoothly change its saturation level.
767
 *
768
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
769
 *                   at any given moment. Valid range: <0,infinity)
770
 * @return           ID of the effect added, or -1 if we failed to add one.
771
 */
772
  public long saturation(Data1D saturation)
773
    {
774
    return mF.add(EffectNames.SATURATION, saturation);
775
    }
776

    
777
///////////////////////////////////////////////////////////////////////////////////////////////////
778
// Vertex-based effects  
779
///////////////////////////////////////////////////////////////////////////////////////////////////
780
/**
781
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
782
 *
783
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
784
 *               currently being dragged with.
785
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
786
 * @param region Region that masks the Effect.
787
 * @return       ID of the effect added, or -1 if we failed to add one.
788
 */
789
  public long distort(Data3D vector, Data3D center, Data4D region)
790
    {  
791
    return mV.add(EffectNames.DISTORT, vector, center, region);
792
    }
793

    
794
///////////////////////////////////////////////////////////////////////////////////////////////////
795
/**
796
 * Distort the whole Object by a (possibly changing in time) vector of force.
797
 *
798
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
799
 *               currently being dragged with.
800
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
801
 * @return       ID of the effect added, or -1 if we failed to add one.
802
 */
803
  public long distort(Data3D vector, Data3D center)
804
    {
805
    return mV.add(EffectNames.DISTORT, vector, center, null);
806
    }
807

    
808
///////////////////////////////////////////////////////////////////////////////////////////////////
809
/**
810
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
811
 * a (possibly changing in time) point on the Object.
812
 *
813
 * @param vector Vector of force that deforms the shape of the whole Object.
814
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
815
 * @param region Region that masks the Effect.
816
 * @return       ID of the effect added, or -1 if we failed to add one.
817
 */
818
  public long deform(Data3D vector, Data3D center, Data4D region)
819
    {
820
    return mV.add(EffectNames.DEFORM, vector, center, region);
821
    }
822

    
823
///////////////////////////////////////////////////////////////////////////////////////////////////
824
/**
825
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
826
 * a (possibly changing in time) point on the Object.
827
 *     
828
 * @param vector Vector of force that deforms the shape of the whole Object.
829
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
830
 * @return       ID of the effect added, or -1 if we failed to add one.
831
 */
832
  public long deform(Data3D vector, Data3D center)
833
    {  
834
    return mV.add(EffectNames.DEFORM, vector, center, null);
835
    }
836

    
837
///////////////////////////////////////////////////////////////////////////////////////////////////  
838
/**
839
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
840
 * away from the center (degree<=1)
841
 *
842
 * @param sink   The current degree of the Effect.
843
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
844
 * @param region Region that masks the Effect.
845
 * @return       ID of the effect added, or -1 if we failed to add one.
846
 */
847
  public long sink(Data1D sink, Data3D center, Data4D region)
848
    {
849
    return mV.add(EffectNames.SINK, sink, center, region);
850
    }
851

    
852
///////////////////////////////////////////////////////////////////////////////////////////////////
853
/**
854
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
855
 * away from the center (degree<=1)
856
 *
857
 * @param sink   The current degree of the Effect.
858
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
859
 * @return       ID of the effect added, or -1 if we failed to add one.
860
 */
861
  public long sink(Data1D sink, Data3D center)
862
    {
863
    return mV.add(EffectNames.SINK, sink, center);
864
    }
865

    
866
///////////////////////////////////////////////////////////////////////////////////////////////////
867
/**
868
 * Pull all points around the center of the Effect towards a line passing through the center
869
 * (that's if degree>=1) or push them away from the line (degree<=1)
870
 *
871
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
872
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
873
 * @param region Region that masks the Effect.
874
 * @return       ID of the effect added, or -1 if we failed to add one.
875
 */
876
  public long pinch(Data2D pinch, Data3D center, Data4D region)
877
    {
878
    return mV.add(EffectNames.PINCH, pinch, center, region);
879
    }
880

    
881
///////////////////////////////////////////////////////////////////////////////////////////////////
882
/**
883
 * Pull all points around the center of the Effect towards a line passing through the center
884
 * (that's if degree>=1) or push them away from the line (degree<=1)
885
 *
886
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
887
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
888
 * @return       ID of the effect added, or -1 if we failed to add one.
889
 */
890
  public long pinch(Data2D pinch, Data3D center)
891
    {
892
    return mV.add(EffectNames.PINCH, pinch, center);
893
    }
894

    
895
///////////////////////////////////////////////////////////////////////////////////////////////////  
896
/**
897
 * Rotate part of the Object around the Center of the Effect by a certain angle.
898
 *
899
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
900
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
901
 * @param region Region that masks the Effect.
902
 * @return       ID of the effect added, or -1 if we failed to add one.
903
 */
904
  public long swirl(Data1D swirl, Data3D center, Data4D region)
905
    {    
906
    return mV.add(EffectNames.SWIRL, swirl, center, region);
907
    }
908

    
909
///////////////////////////////////////////////////////////////////////////////////////////////////
910
/**
911
 * Rotate the whole Object around the Center of the Effect by a certain angle.
912
 *
913
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
914
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
915
 * @return       ID of the effect added, or -1 if we failed to add one.
916
 */
917
  public long swirl(Data1D swirl, Data3D center)
918
    {
919
    return mV.add(EffectNames.SWIRL, swirl, center);
920
    }
921

    
922
///////////////////////////////////////////////////////////////////////////////////////////////////
923
/**
924
 * Directional, sinusoidal wave effect.
925
 *
926
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
927
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
928
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
929
 *               describe the 'direction' of the wave.
930
 *               <p>
931
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
932
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
933
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
934
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
935
 *               <p>
936
 *               <p>
937
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
938
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
939
 *               will be sine shapes.
940
 *               <p>
941
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
942
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
943
 *               YZ-plane with be sine shapes.
944
 *               <p>
945
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
946
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
947
 *               value if sin at this point.
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 wave(Data5D wave, Data3D center)
952
    {
953
    return mV.add(EffectNames.WAVE, wave, center, null);
954
    }
955

    
956
///////////////////////////////////////////////////////////////////////////////////////////////////
957
/**
958
 * Directional, sinusoidal wave effect.
959
 *
960
 * @param wave   see {@link DistortedEffects#wave(Data5D,Data3D)}
961
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
962
 * @param region Region that masks the Effect.
963
 * @return       ID of the effect added, or -1 if we failed to add one.
964
 */
965
  public long wave(Data5D wave, Data3D center, Data4D region)
966
    {
967
    return mV.add(EffectNames.WAVE, wave, center, region);
968
    }
969

    
970
///////////////////////////////////////////////////////////////////////////////////////////////////
971
// Postprocess-based effects
972
///////////////////////////////////////////////////////////////////////////////////////////////////
973
/**
974
 * Blur the object.
975
 *
976
 * @param radius The 'strength' if the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel,
977
 *               take into account 10 pixels in each direction.
978
 * @param center 2-dimensional Data that, at any given time, returns the Center of the Effect.
979
 * @return ID of the effect added, or -1 if we failed to add one.
980
 */
981
  public long blur(Data1D radius, Data2D center)
982
    {
983
    return mP.add(EffectNames.BLUR, radius, center);
984
    }
985
  }
(2-2/16)