Project

General

Profile

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

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

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.type.Data1D;
26
import org.distorted.library.type.Data2D;
27
import org.distorted.library.type.Data3D;
28
import org.distorted.library.type.Data4D;
29
import org.distorted.library.type.Data5D;
30
import org.distorted.library.type.Static3D;
31

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

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

    
49
  private static long mNextID =0;
50
  private long mID;
51

    
52
  private EffectQueueMatrix      mM;
53
  private EffectQueueFragment    mF;
54
  private EffectQueueVertex      mV;
55
  private EffectQueuePostprocess mP;
56

    
57
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
58

    
59
  static
60
    {
61
    int dataLength = 4;
62

    
63
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
64
    float[] textureData = {  0.0f,  0.0f,   0.0f, 1.0f,  1.0f, 0.0f,  1.0f, 1.0f };
65

    
66
    mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
67
    mQuadPositions.put(positionData).position(0);
68
    mQuadTexture   = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
69
    mQuadTexture.put(textureData).position(0);
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
    
74
  private void initializeEffectLists(DistortedEffects d, int flags)
75
    {
76
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
77
      {
78
      mM = d.mM;
79
      matrixCloned = true;
80
      }
81
    else
82
      {
83
      mM = new EffectQueueMatrix(mID);
84
      matrixCloned = false;
85
      }
86
    
87
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
88
      {
89
      mV = d.mV;
90
      vertexCloned = true;
91
      }
92
    else
93
      {
94
      mV = new EffectQueueVertex(mID);
95
      vertexCloned = false;
96
      }
97
    
98
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
99
      {
100
      mF = d.mF;
101
      fragmentCloned = true;
102
      }
103
    else
104
      {
105
      mF = new EffectQueueFragment(mID);
106
      fragmentCloned = false;
107
      }
108

    
109
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
110
      {
111
      mP = d.mP;
112
      postprocessCloned = true;
113
      }
114
    else
115
      {
116
      mP = new EffectQueuePostprocess(mID);
117
      postprocessCloned = false;
118
      }
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122
   
123
  void drawPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedFramebuffer df, long currTime)
124
    {
125
    mM.compute(currTime);
126
    mV.compute(currTime);
127
    mF.compute(currTime);
128
    mP.compute(currTime);
129

    
130
    float halfZ = halfInputW*mesh.zFactor;
131

    
132
    GLES20.glUseProgram(Distorted.mainProgramH);
133

    
134
    if( mP.mNumEffects==0 )
135
      {
136
      GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
137

    
138
      mM.send(df,halfInputW,halfInputH,halfZ);
139
      mV.send(halfInputW,halfInputH,halfZ);
140
      mF.send(halfInputW,halfInputH);
141

    
142
      mesh.draw();
143
      }
144
    else
145
      {
146
      DistortedFramebuffer buffer = Distorted.getFBO(df.mWidth,df.mHeight);
147

    
148
      GLES20.glViewport(0, 0, buffer.mWidth, buffer.mHeight);
149
      buffer.setAsOutput();
150

    
151
      mM.send(buffer,halfInputW,halfInputH,halfZ);
152
      mV.send(halfInputW,halfInputH,halfZ);
153
      mF.send(halfInputW,halfInputH);
154

    
155
      mesh.draw();
156

    
157
      GLES20.glUseProgram(Distorted.postProgramH);
158
      GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
159
      buffer.setAsInput();
160
      df.setAsOutput();
161
      mP.send(halfInputW,halfInputH);
162

    
163
      GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
164
      GLES20.glVertexAttribPointer(Distorted.mPostProgramAttributes[1], TEX_DATA_SIZE     , GLES20.GL_FLOAT, false, 0, mQuadTexture);
165
      GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
166
      }
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170
   
171
  static void drawNoEffectsPriv(float halfInputW, float halfInputH, MeshObject mesh, DistortedFramebuffer df)
172
    {
173
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
174

    
175
    EffectQueueMatrix.sendZero(df,halfInputW,halfInputH,halfInputW*mesh.zFactor);
176
    EffectQueueVertex.sendZero();
177
    EffectQueueFragment.sendZero();
178
    EffectQueuePostprocess.sendZero(2*halfInputW,2*halfInputH);
179

    
180
    mesh.draw();
181
    }
182
    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
184
   
185
  private void releasePriv()
186
    {
187
    if( !matrixCloned     ) mM.abortAll(false);
188
    if( !vertexCloned     ) mV.abortAll(false);
189
    if( !fragmentCloned   ) mF.abortAll(false);
190
    if( !postprocessCloned) mP.abortAll(false);
191

    
192
    mM = null;
193
    mV = null;
194
    mF = null;
195
    mP = null;
196
    }
197

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  static void onDestroy()
201
    {
202
    mNextID = 0;
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206
// PUBLIC API
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
/**
209
 * Create empty effect queue.
210
 */
211
  public DistortedEffects()
212
    {
213
    mID = mNextID++;
214
    initializeEffectLists(this,0);
215
    }
216

    
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
/**
219
 * Copy constructor.
220
 * <p>
221
 * Whatever we do not clone gets created just like in the default constructor.
222
 *
223
 * @param dc    Source object to create our object from
224
 * @param flags A bitmask of values specifying what to copy.
225
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
226
 */
227
  public DistortedEffects(DistortedEffects dc, int flags)
228
    {
229
    mID = mNextID++;
230
    initializeEffectLists(dc,flags);
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234
/**
235
 * Releases all resources. After this call, the queue should not be used anymore.
236
 */
237
  public synchronized void delete()
238
    {
239
    releasePriv();
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243
/**
244
 * Returns unique ID of this instance.
245
 *
246
 * @return ID of the object.
247
 */
248
  public long getID()
249
      {
250
      return mID;
251
      }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
/**
255
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
256
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
257
 * 
258
 * @param el A class implementing the EffectListener interface that wants to get notifications.
259
 */
260
  public void registerForMessages(EffectListener el)
261
    {
262
    mV.registerForMessages(el);
263
    mF.registerForMessages(el);
264
    mM.registerForMessages(el);
265
    mP.registerForMessages(el);
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269
/**
270
 * Removes the calling class from the list of Listeners.
271
 * 
272
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
273
 */
274
  public void deregisterForMessages(EffectListener el)
275
    {
276
    mV.deregisterForMessages(el);
277
    mF.deregisterForMessages(el);
278
    mM.deregisterForMessages(el);
279
    mP.deregisterForMessages(el);
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283
/**
284
 * Aborts all Effects.
285
 * @return Number of effects aborted.
286
 */
287
  public int abortAllEffects()
288
      {
289
      return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true) + mP.abortAll(true);
290
      }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293
/**
294
 * Aborts all Effects of a given type, for example all MATRIX Effects.
295
 * 
296
 * @param type one of the constants defined in {@link EffectTypes}
297
 * @return Number of effects aborted.
298
 */
299
  public int abortEffects(EffectTypes type)
300
    {
301
    switch(type)
302
      {
303
      case MATRIX     : return mM.abortAll(true);
304
      case VERTEX     : return mV.abortAll(true);
305
      case FRAGMENT   : return mF.abortAll(true);
306
      case POSTPROCESS: return mP.abortAll(true);
307
      default         : return 0;
308
      }
309
    }
310
    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312
/**
313
 * Aborts a single Effect.
314
 * 
315
 * @param id ID of the Effect we want to abort.
316
 * @return number of Effects aborted. Always either 0 or 1.
317
 */
318
  public int abortEffect(long id)
319
    {
320
    int type = (int)(id&EffectTypes.MASK);
321

    
322
    if( type==EffectTypes.MATRIX.type      ) return mM.removeByID(id>>EffectTypes.LENGTH);
323
    if( type==EffectTypes.VERTEX.type      ) return mV.removeByID(id>>EffectTypes.LENGTH);
324
    if( type==EffectTypes.FRAGMENT.type    ) return mF.removeByID(id>>EffectTypes.LENGTH);
325
    if( type==EffectTypes.POSTPROCESS.type ) return mP.removeByID(id>>EffectTypes.LENGTH);
326

    
327
    return 0;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
/**
332
 * Abort all Effects of a given name, for example all rotations.
333
 * 
334
 * @param name one of the constants defined in {@link EffectNames}
335
 * @return number of Effects aborted.
336
 */
337
  public int abortEffects(EffectNames name)
338
    {
339
    switch(name.getType())
340
      {
341
      case MATRIX     : return mM.removeByType(name);
342
      case VERTEX     : return mV.removeByType(name);
343
      case FRAGMENT   : return mF.removeByType(name);
344
      case POSTPROCESS: return mP.removeByType(name);
345
      default         : return 0;
346
      }
347
    }
348
    
349
///////////////////////////////////////////////////////////////////////////////////////////////////
350
/**
351
 * Print some info about a given Effect to Android's standard out. Used for debugging only.
352
 * 
353
 * @param id Effect ID we want to print info about
354
 * @return <code>true</code> if a single Effect of type effectType has been found.
355
 */
356
    
357
  public boolean printEffect(long id)
358
    {
359
    int type = (int)(id&EffectTypes.MASK);
360

    
361
    if( type==EffectTypes.MATRIX.type      )  return mM.printByID(id>>EffectTypes.LENGTH);
362
    if( type==EffectTypes.VERTEX.type      )  return mV.printByID(id>>EffectTypes.LENGTH);
363
    if( type==EffectTypes.FRAGMENT.type    )  return mF.printByID(id>>EffectTypes.LENGTH);
364
    if( type==EffectTypes.POSTPROCESS.type )  return mP.printByID(id>>EffectTypes.LENGTH);
365

    
366
    return false;
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370
/**
371
 * Returns the maximum number of Matrix effects.
372
 *
373
 * @return The maximum number of Matrix effects
374
 */
375
  public static int getMaxMatrix()
376
    {
377
    return EffectQueue.getMax(EffectTypes.MATRIX.ordinal());
378
    }
379

    
380
///////////////////////////////////////////////////////////////////////////////////////////////////
381
/**
382
 * Returns the maximum number of Vertex effects.
383
 *
384
 * @return The maximum number of Vertex effects
385
 */
386
  public static int getMaxVertex()
387
    {
388
    return EffectQueue.getMax(EffectTypes.VERTEX.ordinal());
389
    }
390

    
391
///////////////////////////////////////////////////////////////////////////////////////////////////
392
/**
393
 * Returns the maximum number of Fragment effects.
394
 *
395
 * @return The maximum number of Fragment effects
396
 */
397
  public static int getMaxFragment()
398
    {
399
    return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal());
400
    }
401

    
402
///////////////////////////////////////////////////////////////////////////////////////////////////
403
/**
404
 * Returns the maximum number of Postprocess effects.
405
 *
406
 * @return The maximum number of Postprocess effects
407
 */
408
  public static int getMaxPostprocess()
409
    {
410
    return EffectQueue.getMax(EffectTypes.POSTPROCESS.ordinal());
411
    }
412

    
413
///////////////////////////////////////////////////////////////////////////////////////////////////
414
/**
415
 * Sets the maximum number of Matrix effects that can be stored in a single EffectQueue at one time.
416
 * This can fail if:
417
 * <ul>
418
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
419
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
420
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
421
 *     time only decreasing the value of 'max' is permitted.
422
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
423
 * </ul>
424
 *
425
 * @param max new maximum number of simultaneous Matrix Effects. Has to be a non-negative number not greater
426
 *            than Byte.MAX_VALUE
427
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
428
 */
429
  public static boolean setMaxMatrix(int max)
430
    {
431
    return EffectQueue.setMax(EffectTypes.MATRIX.ordinal(),max);
432
    }
433

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

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

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

    
497
///////////////////////////////////////////////////////////////////////////////////////////////////   
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
// Individual effect functions.
500
///////////////////////////////////////////////////////////////////////////////////////////////////
501
// Matrix-based effects
502
///////////////////////////////////////////////////////////////////////////////////////////////////
503
/**
504
 * Moves the Object by a (possibly changing in time) vector.
505
 * 
506
 * @param vector 3-dimensional Data which at any given time will return a Static3D
507
 *               representing the current coordinates of the vector we want to move the Object with.
508
 * @return       ID of the effect added, or -1 if we failed to add one.
509
 */
510
  public long move(Data3D vector)
511
    {   
512
    return mM.add(EffectNames.MOVE,vector);
513
    }
514

    
515
///////////////////////////////////////////////////////////////////////////////////////////////////
516
/**
517
 * Scales the Object by (possibly changing in time) 3D scale factors.
518
 * 
519
 * @param scale 3-dimensional Data which at any given time returns a Static3D
520
 *              representing the current x- , y- and z- scale factors.
521
 * @return      ID of the effect added, or -1 if we failed to add one.
522
 */
523
  public long scale(Data3D scale)
524
    {   
525
    return mM.add(EffectNames.SCALE,scale);
526
    }
527

    
528
///////////////////////////////////////////////////////////////////////////////////////////////////
529
/**
530
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
531
 *
532
 * @param scale The factor to scale all 3 dimensions with.
533
 * @return      ID of the effect added, or -1 if we failed to add one.
534
 */
535
  public long scale(float scale)
536
    {
537
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
538
    }
539

    
540
///////////////////////////////////////////////////////////////////////////////////////////////////
541
/**
542
 * Rotates the Object by 'angle' degrees around the center.
543
 * Static axis of rotation is given by the last parameter.
544
 *
545
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
546
 * @param axis   Axis of rotation
547
 * @param center Coordinates of the Point we are rotating around.
548
 * @return       ID of the effect added, or -1 if we failed to add one.
549
 */
550
  public long rotate(Data1D angle, Static3D axis, Data3D center )
551
    {   
552
    return mM.add(EffectNames.ROTATE, angle, axis, center);
553
    }
554

    
555
///////////////////////////////////////////////////////////////////////////////////////////////////
556
/**
557
 * Rotates the Object by 'angle' degrees around the center.
558
 * Here both angle and axis can dynamically change.
559
 *
560
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
561
 * @param center    Coordinates of the Point we are rotating around.
562
 * @return          ID of the effect added, or -1 if we failed to add one.
563
 */
564
  public long rotate(Data4D angleaxis, Data3D center)
565
    {
566
    return mM.add(EffectNames.ROTATE, angleaxis, center);
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570
/**
571
 * Rotates the Object by quaternion.
572
 *
573
 * @param quaternion The quaternion describing the rotation.
574
 * @param center     Coordinates of the Point we are rotating around.
575
 * @return           ID of the effect added, or -1 if we failed to add one.
576
 */
577
  public long quaternion(Data4D quaternion, Data3D center )
578
    {
579
    return mM.add(EffectNames.QUATERNION,quaternion,center);
580
    }
581

    
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583
/**
584
 * Shears the Object.
585
 *
586
 * @param shear   The 3-tuple of shear factors. The first controls level
587
 *                of shearing in the X-axis, second - Y-axis and the third -
588
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
589
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
590
 * @param center  Center of shearing, i.e. the point which stays unmoved.
591
 * @return        ID of the effect added, or -1 if we failed to add one.
592
 */
593
  public long shear(Data3D shear, Data3D center)
594
    {
595
    return mM.add(EffectNames.SHEAR, shear, center);
596
    }
597

    
598
///////////////////////////////////////////////////////////////////////////////////////////////////
599
// Fragment-based effects  
600
///////////////////////////////////////////////////////////////////////////////////////////////////
601
/**
602
 * Makes a certain sub-region of the Object smoothly change all three of its RGB components.
603
 *        
604
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
605
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
606
 *               Valid range: <0,1>
607
 * @param color  Color to mix. (1,0,0) is RED.
608
 * @param region Region this Effect is limited to.
609
 * @param smooth If true, the level of 'blend' will smoothly fade out towards the edges of the region.
610
 * @return       ID of the effect added, or -1 if we failed to add one.
611
 */
612
  public long chroma(Data1D blend, Data3D color, Data4D region, boolean smooth)
613
    {
614
    return mF.add( smooth? EffectNames.SMOOTH_CHROMA:EffectNames.CHROMA, blend, color, region);
615
    }
616

    
617
///////////////////////////////////////////////////////////////////////////////////////////////////
618
/**
619
 * Makes the whole Object smoothly change all three of its RGB components.
620
 *
621
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
622
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
623
 *               Valid range: <0,1>
624
 * @param color  Color to mix. (1,0,0) is RED.
625
 * @return       ID of the effect added, or -1 if we failed to add one.
626
 */
627
  public long chroma(Data1D blend, Data3D color)
628
    {
629
    return mF.add(EffectNames.CHROMA, blend, color);
630
    }
631

    
632
///////////////////////////////////////////////////////////////////////////////////////////////////
633
/**
634
 * Makes a certain sub-region of the Object smoothly change its transparency level.
635
 *        
636
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any given
637
 *               moment: pixel.a *= alpha.
638
 *               Valid range: <0,1>
639
 * @param region Region this Effect is limited to. 
640
 * @param smooth If true, the level of 'alpha' 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 alpha(Data1D alpha, Data4D region, boolean smooth)
644
    {
645
    return mF.add( smooth? EffectNames.SMOOTH_ALPHA:EffectNames.ALPHA, alpha, region);
646
    }
647

    
648
///////////////////////////////////////////////////////////////////////////////////////////////////
649
/**
650
 * Makes the whole Object smoothly change its transparency level.
651
 *
652
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any
653
 *               given moment: pixel.a *= alpha.
654
 *               Valid range: <0,1>
655
 * @return       ID of the effect added, or -1 if we failed to add one.
656
 */
657
  public long alpha(Data1D alpha)
658
    {
659
    return mF.add(EffectNames.ALPHA, alpha);
660
    }
661

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

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

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

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

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

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

    
746
///////////////////////////////////////////////////////////////////////////////////////////////////
747
// Vertex-based effects  
748
///////////////////////////////////////////////////////////////////////////////////////////////////
749
/**
750
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
751
 *
752
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
753
 *               currently being dragged with.
754
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
755
 * @param region Region that masks the Effect.
756
 * @return       ID of the effect added, or -1 if we failed to add one.
757
 */
758
  public long distort(Data3D vector, Data3D center, Data4D region)
759
    {  
760
    return mV.add(EffectNames.DISTORT, vector, center, region);
761
    }
762

    
763
///////////////////////////////////////////////////////////////////////////////////////////////////
764
/**
765
 * Distort the whole Object by a (possibly changing in time) vector of force.
766
 *
767
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
768
 *               currently being dragged with.
769
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
770
 * @return       ID of the effect added, or -1 if we failed to add one.
771
 */
772
  public long distort(Data3D vector, Data3D center)
773
    {
774
    return mV.add(EffectNames.DISTORT, vector, center, null);
775
    }
776

    
777
///////////////////////////////////////////////////////////////////////////////////////////////////
778
/**
779
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
780
 * a (possibly changing in time) point on the Object.
781
 *
782
 * @param vector Vector of force that deforms the shape of the whole Object.
783
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
784
 * @param region Region that masks the Effect.
785
 * @return       ID of the effect added, or -1 if we failed to add one.
786
 */
787
  public long deform(Data3D vector, Data3D center, Data4D region)
788
    {
789
    return mV.add(EffectNames.DEFORM, vector, center, region);
790
    }
791

    
792
///////////////////////////////////////////////////////////////////////////////////////////////////
793
/**
794
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
795
 * a (possibly changing in time) point on the Object.
796
 *     
797
 * @param vector Vector of force that deforms the shape of the whole Object.
798
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
799
 * @return       ID of the effect added, or -1 if we failed to add one.
800
 */
801
  public long deform(Data3D vector, Data3D center)
802
    {  
803
    return mV.add(EffectNames.DEFORM, vector, center, null);
804
    }
805

    
806
///////////////////////////////////////////////////////////////////////////////////////////////////  
807
/**
808
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
809
 * away from the center (degree<=1)
810
 *
811
 * @param sink   The current degree of the Effect.
812
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
813
 * @param region Region that masks the Effect.
814
 * @return       ID of the effect added, or -1 if we failed to add one.
815
 */
816
  public long sink(Data1D sink, Data3D center, Data4D region)
817
    {
818
    return mV.add(EffectNames.SINK, sink, center, region);
819
    }
820

    
821
///////////////////////////////////////////////////////////////////////////////////////////////////
822
/**
823
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
824
 * away from the center (degree<=1)
825
 *
826
 * @param sink   The current degree of the Effect.
827
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
828
 * @return       ID of the effect added, or -1 if we failed to add one.
829
 */
830
  public long sink(Data1D sink, Data3D center)
831
    {
832
    return mV.add(EffectNames.SINK, sink, center);
833
    }
834

    
835
///////////////////////////////////////////////////////////////////////////////////////////////////
836
/**
837
 * Pull all points around the center of the Effect towards a line passing through the center
838
 * (that's if degree>=1) or push them away from the line (degree<=1)
839
 *
840
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
841
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
842
 * @param region Region that masks the Effect.
843
 * @return       ID of the effect added, or -1 if we failed to add one.
844
 */
845
  public long pinch(Data2D pinch, Data3D center, Data4D region)
846
    {
847
    return mV.add(EffectNames.PINCH, pinch, center, region);
848
    }
849

    
850
///////////////////////////////////////////////////////////////////////////////////////////////////
851
/**
852
 * Pull all points around the center of the Effect towards a line passing through the center
853
 * (that's if degree>=1) or push them away from the line (degree<=1)
854
 *
855
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
856
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
857
 * @return       ID of the effect added, or -1 if we failed to add one.
858
 */
859
  public long pinch(Data2D pinch, Data3D center)
860
    {
861
    return mV.add(EffectNames.PINCH, pinch, center);
862
    }
863

    
864
///////////////////////////////////////////////////////////////////////////////////////////////////  
865
/**
866
 * Rotate part of the Object around the Center of the Effect by a certain angle.
867
 *
868
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
869
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
870
 * @param region Region that masks the Effect.
871
 * @return       ID of the effect added, or -1 if we failed to add one.
872
 */
873
  public long swirl(Data1D swirl, Data3D center, Data4D region)
874
    {    
875
    return mV.add(EffectNames.SWIRL, swirl, center, region);
876
    }
877

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////
879
/**
880
 * Rotate the whole Object around the Center of the Effect by a certain angle.
881
 *
882
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
883
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
884
 * @return       ID of the effect added, or -1 if we failed to add one.
885
 */
886
  public long swirl(Data1D swirl, Data3D center)
887
    {
888
    return mV.add(EffectNames.SWIRL, swirl, center);
889
    }
890

    
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892
/**
893
 * Directional, sinusoidal wave effect.
894
 *
895
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
896
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
897
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
898
 *               describe the 'direction' of the wave.
899
 *               <p>
900
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
901
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
902
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
903
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
904
 *               <p>
905
 *               <p>
906
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
907
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
908
 *               will be sine shapes.
909
 *               <p>
910
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
911
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
912
 *               YZ-plane with be sine shapes.
913
 *               <p>
914
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
915
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
916
 *               value if sin at this point.
917
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
918
 * @return       ID of the effect added, or -1 if we failed to add one.
919
 */
920
  public long wave(Data5D wave, Data3D center)
921
    {
922
    return mV.add(EffectNames.WAVE, wave, center, null);
923
    }
924

    
925
///////////////////////////////////////////////////////////////////////////////////////////////////
926
/**
927
 * Directional, sinusoidal wave effect.
928
 *
929
 * @param wave   see {@link DistortedEffects#wave(Data5D,Data3D)}
930
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
931
 * @param region Region that masks the Effect.
932
 * @return       ID of the effect added, or -1 if we failed to add one.
933
 */
934
  public long wave(Data5D wave, Data3D center, Data4D region)
935
    {
936
    return mV.add(EffectNames.WAVE, wave, center, region);
937
    }
938

    
939
///////////////////////////////////////////////////////////////////////////////////////////////////
940
// Postprocess-based effects
941
///////////////////////////////////////////////////////////////////////////////////////////////////
942
/**
943
 * Blur the object.
944
 *
945
 * @param radius The 'strength' if the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel,
946
 *               take into account 10 pixels in each direction.
947
 * @param center 2-dimensional Data that, at any given time, returns the Center of the Effect.
948
 * @return ID of the effect added, or -1 if we failed to add one.
949
 */
950
  public long blur(Data1D radius, Data2D center)
951
    {
952
    return mP.add(EffectNames.BLUR, radius, center);
953
    }
954
  }
(2-2/16)