Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedObject.java @ 8e34674e

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.graphics.Bitmap;
23
import android.graphics.Matrix;
24
import android.opengl.GLES20;
25
import android.opengl.GLUtils;
26

    
27
import org.distorted.library.message.EffectListener;
28
import org.distorted.library.type.Data1D;
29
import org.distorted.library.type.Data2D;
30
import org.distorted.library.type.Data3D;
31
import org.distorted.library.type.Data4D;
32
import org.distorted.library.type.Data5D;
33
import org.distorted.library.type.Static3D;
34

    
35
import java.util.HashMap;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38
/**
39
 * All Objects to which Distorted Graphics effects can be applied need to be extended from here.
40
 *
41
 * Just like in DistortedNode and DistortedFramebuffer, we need to have a static list of all
42
 * DistortedObjects currently created by the application so that we can implement the 'mark for
43
 * deletion now - actually delete on next render' thing.
44
 * We need to be able to quickly retrieve an Object by its ID, thus a HashMap.
45
 */
46
public abstract class DistortedObject 
47
  {
48
  private static long mNextID =0;
49
  private static HashMap<Long,DistortedObject> mObjects = new HashMap<>();
50

    
51
  private EffectQueueMatrix    mM;
52
  private EffectQueueFragment  mF;
53
  private EffectQueueVertex    mV;
54

    
55
  private boolean matrixCloned, vertexCloned, fragmentCloned;
56
  private long mID;
57
  private int mSizeX, mSizeY, mSizeZ; // in screen space
58

    
59
  protected DistortedObjectGrid mGrid = null;
60

    
61
  private Bitmap[] mBmp= null; //
62
  int[] mTextureDataH;         // have to be shared among all the cloned Objects
63
  boolean[] mBitmapSet;        //
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
// We have to flip vertically every single Bitmap that we get fed with.
67
//
68
// Reason: textures read from files are the only objects in OpenGL which have their origins at the
69
// upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
70
// The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
71
// i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
72
// then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
73
// created from FBO have their origins in the lower-left... Mindfuck!
74

    
75
  private static Bitmap flipBitmap(Bitmap src)
76
    {
77
    Matrix matrix = new Matrix();
78
    matrix.preScale(1.0f,-1.0f);
79

    
80
    return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
81
    }
82

    
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84

    
85
  protected abstract DistortedObject deepCopy(int flags);
86

    
87
///////////////////////////////////////////////////////////////////////////////////////////////////
88

    
89
  protected void initializeData(int x, int y, int z)
90
    {
91
    mSizeX= x;
92
    mSizeY= y;
93
    mSizeZ= z;
94

    
95
    mID = mNextID++;
96
    mObjects.put(mID,this);
97

    
98
    mTextureDataH   = new int[1];
99
    mTextureDataH[0]= 0;
100
    mBmp            = new Bitmap[1];
101
    mBmp[0]         = null;
102
    mBitmapSet      = new boolean[1];
103
    mBitmapSet[0]   = false;
104
      
105
    initializeEffectLists(this,0);
106
      
107
    if( Distorted.isInitialized() ) resetTexture();
108
    }
109
    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111
    
112
  private void initializeEffectLists(DistortedObject d, int flags)
113
    {
114
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
115
      {
116
      mM = d.mM;
117
      matrixCloned = true;
118
      }
119
    else
120
      {
121
      mM = new EffectQueueMatrix(d);
122
      matrixCloned = false;
123
      }
124
    
125
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
126
      {
127
      mV = d.mV;
128
      vertexCloned = true;
129
      }
130
    else
131
      {
132
      mV = new EffectQueueVertex(d);
133
      vertexCloned = false;
134
      }
135
    
136
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
137
      {
138
      mF = d.mF;
139
      fragmentCloned = true;
140
      }
141
    else
142
      {
143
      mF = new EffectQueueFragment(d);
144
      fragmentCloned = false;
145
      }
146
    }
147
    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149
// this will be called on startup and every time OpenGL context has been lost
150
// also call this from the constructor if the OpenGL context has been created already.
151
    
152
  private void resetTexture()
153
    {
154
    if( mTextureDataH!=null )
155
      {
156
      if( mTextureDataH[0]==0 ) GLES20.glGenTextures(1, mTextureDataH, 0);
157

    
158
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
159
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR );
160
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR );
161
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
162
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );
163
       
164
      if( mBmp!=null && mBmp[0]!=null)
165
        {
166
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(mBmp[0]), 0);
167
        mBmp[0] = null;
168
        }
169
      }
170
    }
171
  
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173
   
174
  void drawPriv(long currTime, DistortedFramebuffer df)
175
    {
176
    DistortedFramebuffer.deleteAllMarked();
177

    
178
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
179
      
180
    mM.compute(currTime);
181
    mM.send(df);
182
      
183
    mV.compute(currTime);
184
    mV.send();
185
        
186
    mF.compute(currTime);
187
    mF.send();
188
       
189
    mGrid.draw();
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
   
194
  void drawNoEffectsPriv(DistortedFramebuffer df)
195
    {
196
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
197
    mM.sendZero(df);
198
    mV.sendZero();
199
    mF.sendZero();
200
    mGrid.draw();
201
    }
202
    
203
///////////////////////////////////////////////////////////////////////////////////////////////////
204
   
205
  private void releasePriv()
206
    {
207
    if( !matrixCloned  ) mM.abortAll(false);
208
    if( !vertexCloned  ) mV.abortAll(false);
209
    if( !fragmentCloned) mF.abortAll(false);
210

    
211
    mBmp          = null;
212
    mGrid         = null;
213
    mM            = null;
214
    mV            = null;
215
    mF            = null;
216
    mTextureDataH = null;
217
    }
218
 
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

    
221
  long getBitmapID()
222
    {
223
    return mBmp==null ? 0 : mBmp.hashCode();
224
    }
225

    
226
///////////////////////////////////////////////////////////////////////////////////////////////////
227

    
228
  static synchronized void reset()
229
    {
230
    for(long id: mObjects.keySet())
231
      {
232
      mObjects.get(id).resetTexture();
233
      }
234
    }
235

    
236
///////////////////////////////////////////////////////////////////////////////////////////////////
237

    
238
  static synchronized void release()
239
    {
240
    for(long id: mObjects.keySet())
241
      {
242
      mObjects.get(id).releasePriv();
243
      }
244

    
245
    mObjects.clear();
246
    mNextID = 0;
247
    }
248

    
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250
// PUBLIC API
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252
/**
253
 * Default empty constructor so that derived classes can call it
254
 */
255
  public DistortedObject()
256
    {
257

    
258
    }
259

    
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
/**
262
 * Copy constructor used to create a DistortedObject based on various parts of another object.
263
 * <p>
264
 * Whatever we do not clone gets created just like in the default constructor.
265
 * We only call this from the descendant's classes' constructors where we have to pay attention
266
 * to give it the appropriate type of a DistortedObject!
267
 *
268
 * @param dc    Source object to create our object from
269
 * @param flags A bitmask of values specifying what to copy.
270
 *              For example, CLONE_BITMAP | CLONE_MATRIX.
271
 */
272
  public DistortedObject(DistortedObject dc, int flags)
273
    {
274
    initializeEffectLists(dc,flags);
275

    
276
    mID = mNextID++;
277
    mObjects.put(mID,this);
278

    
279
    mSizeX = dc.mSizeX;
280
    mSizeY = dc.mSizeY;
281
    mSizeZ = dc.mSizeZ;
282
    mGrid  = dc.mGrid;
283

    
284
    if( (flags & Distorted.CLONE_BITMAP) != 0 )
285
      {
286
      mTextureDataH = dc.mTextureDataH;
287
      mBmp          = dc.mBmp;
288
      mBitmapSet    = dc.mBitmapSet;
289
      }
290
    else
291
      {
292
      mTextureDataH   = new int[1];
293
      mTextureDataH[0]= 0;
294
      mBitmapSet      = new boolean[1];
295
      mBitmapSet[0]   = false;
296
      mBmp            = new Bitmap[1];
297
      mBmp[0]         = null;
298

    
299
      if( Distorted.isInitialized() ) resetTexture();
300
      }
301
    }
302

    
303
///////////////////////////////////////////////////////////////////////////////////////////////////
304
/**
305
 * Draw the DistortedObject to the location specified by current Matrix effects.    
306
 *     
307
 * @param currTime current time, in milliseconds, as returned by System.currentTimeMillis().
308
 *        This gets passed on to Dynamics inside the Effects that are currently applied to the
309
 *        Object.
310
 */
311
  public void draw(long currTime)
312
    {
313
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
314
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
315
    GLES20.glUniform1i(Distorted.mTextureUniformH, 0);
316
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
317

    
318
    drawPriv(currTime, Distorted.mFramebuffer);
319
    }
320
 
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
/**
323
 * Releases all resources.
324
 */
325
  public synchronized void delete()
326
    {
327
    releasePriv();
328
    mObjects.remove(this);
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332
/**
333
 * Sets the underlying android.graphics.Bitmap object and uploads it to the GPU. 
334
 * <p>
335
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call 
336
 * to onSurfaceCreated) because only after this point can the Library upload it to the GPU!
337
 * 
338
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
339
 */
340
   
341
  public void setBitmap(Bitmap bmp)
342
    {
343
    mBitmapSet[0] = true;
344
      
345
    if( Distorted.isInitialized() )
346
      {
347
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
348
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
349
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(bmp), 0);
350
      }
351
    else
352
      {
353
      mBmp[0] = bmp;
354
      }
355
    }
356
    
357
///////////////////////////////////////////////////////////////////////////////////////////////////
358
/**
359
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
360
 * to one of the Effects that are currently applied to the DistortedObject.
361
 * 
362
 * @param el A class implementing the EffectListener interface that wants to get notifications.
363
 */
364
  public void addEventListener(EffectListener el)
365
    {
366
    mV.addListener(el);
367
    mF.addListener(el);
368
    mM.addListener(el);
369
    }
370

    
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372
/**
373
 * Removes the calling class from the list of Listeners.
374
 * 
375
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
376
 */
377
  public void removeEventListener(EffectListener el)
378
    {
379
    mV.removeListener(el);
380
    mF.removeListener(el);
381
    mM.removeListener(el);
382
    }
383
   
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
/**
386
 * Returns the height of the DistortedObject.
387
 *    
388
 * @return height of the object, in pixels.
389
 */
390
  public int getWidth()
391
     {
392
     return mSizeX;   
393
     }
394

    
395
///////////////////////////////////////////////////////////////////////////////////////////////////
396
/**
397
 * Returns the width of the DistortedObject.
398
 * 
399
 * @return width of the Object, in pixels.
400
 */
401
  public int getHeight()
402
      {
403
      return mSizeY;  
404
      }
405
    
406
///////////////////////////////////////////////////////////////////////////////////////////////////
407
/**
408
 * Returns the depth of the DistortedObject.
409
 * 
410
 * @return depth of the Object, in pixels.
411
 */
412
  public int getDepth()
413
      {
414
      return mSizeZ;  
415
      }
416
        
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418
/**
419
 * Returns unique ID of this instance.
420
 * 
421
 * @return ID of the object.
422
 */
423
  public long getID()
424
      {
425
      return mID;  
426
      }
427
    
428
///////////////////////////////////////////////////////////////////////////////////////////////////
429
/**
430
 * Aborts all Effects.
431
 * @return Number of effects aborted.
432
 */
433
  public int abortAllEffects()
434
      {
435
      return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
436
      }
437

    
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439
/**
440
 * Aborts all Effects of a given type, for example all MATRIX Effects.
441
 * 
442
 * @param type one of the constants defined in {@link EffectTypes}
443
 * @return Number of effects aborted.
444
 */
445
  public int abortEffects(EffectTypes type)
446
    {
447
    switch(type)
448
      {
449
      case MATRIX  : return mM.abortAll(true);
450
      case VERTEX  : return mV.abortAll(true);
451
      case FRAGMENT: return mF.abortAll(true);
452
      default      : return 0;
453
      }
454
    }
455
    
456
///////////////////////////////////////////////////////////////////////////////////////////////////
457
/**
458
 * Aborts a single Effect.
459
 * 
460
 * @param id ID of the Effect we want to abort.
461
 * @return number of Effects aborted. Always either 0 or 1.
462
 */
463
  public int abortEffect(long id)
464
    {
465
    int type = (int)(id&EffectTypes.MASK);
466

    
467
    if( type==EffectTypes.MATRIX.type   ) return mM.removeByID(id>>EffectTypes.LENGTH);
468
    if( type==EffectTypes.VERTEX.type   ) return mV.removeByID(id>>EffectTypes.LENGTH);
469
    if( type==EffectTypes.FRAGMENT.type ) return mF.removeByID(id>>EffectTypes.LENGTH);
470

    
471
    return 0;
472
    }
473

    
474
///////////////////////////////////////////////////////////////////////////////////////////////////
475
/**
476
 * Abort all Effects of a given name, for example all rotations.
477
 * 
478
 * @param name one of the constants defined in {@link EffectNames}
479
 * @return number of Effects aborted.
480
 */
481
  public int abortEffects(EffectNames name)
482
    {
483
    switch(name.getType())
484
      {
485
      case MATRIX  : return mM.removeByType(name);
486
      case VERTEX  : return mV.removeByType(name);
487
      case FRAGMENT: return mF.removeByType(name);
488
      default      : return 0;
489
      }
490
    }
491
    
492
///////////////////////////////////////////////////////////////////////////////////////////////////
493
/**
494
 * Print some info about a given Effect to Android's standard out. Used for debugging only.
495
 * 
496
 * @param id Effect ID we want to print info about
497
 * @return <code>true</code> if a single Effect of type effectType has been found.
498
 */
499
    
500
  public boolean printEffect(long id)
501
    {
502
    int type = (int)(id&EffectTypes.MASK);
503

    
504
    if( type==EffectTypes.MATRIX.type   )  return mM.printByID(id>>EffectTypes.LENGTH);
505
    if( type==EffectTypes.VERTEX.type   )  return mV.printByID(id>>EffectTypes.LENGTH);
506
    if( type==EffectTypes.FRAGMENT.type )  return mF.printByID(id>>EffectTypes.LENGTH);
507

    
508
    return false;
509
    }
510
   
511
///////////////////////////////////////////////////////////////////////////////////////////////////   
512
///////////////////////////////////////////////////////////////////////////////////////////////////
513
// Individual effect functions.
514
///////////////////////////////////////////////////////////////////////////////////////////////////
515
// Matrix-based effects
516
///////////////////////////////////////////////////////////////////////////////////////////////////
517
/**
518
 * Moves the Object by a (possibly changing in time) vector.
519
 * 
520
 * @param vector 3-dimensional Data which at any given time will return a Static3D
521
 *               representing the current coordinates of the vector we want to move the Object with.
522
 * @return       ID of the effect added, or -1 if we failed to add one.
523
 */
524
  public long move(Data3D vector)
525
    {   
526
    return mM.add(EffectNames.MOVE,vector);
527
    }
528

    
529
///////////////////////////////////////////////////////////////////////////////////////////////////
530
/**
531
 * Scales the Object by (possibly changing in time) 3D scale factors.
532
 * 
533
 * @param scale 3-dimensional Dynamic which at any given time returns a Static3D
534
 *              representing the current x- , y- and z- scale factors.
535
 * @return      ID of the effect added, or -1 if we failed to add one.
536
 */
537
  public long scale(Data3D scale)
538
    {   
539
    return mM.add(EffectNames.SCALE,scale);
540
    }
541

    
542
///////////////////////////////////////////////////////////////////////////////////////////////////
543
/**
544
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
545
 *
546
 * @param scale The factor to scale all 3 dimensions with.
547
 * @return      ID of the effect added, or -1 if we failed to add one.
548
 */
549
  public long scale(float scale)
550
    {
551
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
552
    }
553

    
554
///////////////////////////////////////////////////////////////////////////////////////////////////
555
/**
556
 * Rotates the Object by 'angle' degrees around the center.
557
 * Static axis of rotation is given by the last parameter.
558
 *
559
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
560
 * @param axis   Axis of rotation
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(Data1D angle, Static3D axis, Data3D center )
565
    {   
566
    return mM.add(EffectNames.ROTATE, angle, axis, center);
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570
/**
571
 * Rotates the Object by 'angle' degrees around the center.
572
 * Here both angle and axis can dynamically change.
573
 *
574
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
575
 * @param center    Coordinates of the Point we are rotating around.
576
 * @return          ID of the effect added, or -1 if we failed to add one.
577
 */
578
  public long rotate(Data4D angleaxis, Data3D center)
579
    {
580
    return mM.add(EffectNames.ROTATE, angleaxis, center);
581
    }
582

    
583
///////////////////////////////////////////////////////////////////////////////////////////////////
584
/**
585
 * Rotates the Object by quaternion.
586
 *
587
 * @param quaternion The quaternion describing the rotation.
588
 * @param center     Coordinates of the Point we are rotating around.
589
 * @return           ID of the effect added, or -1 if we failed to add one.
590
 */
591
  public long quaternion(Data4D quaternion, Data3D center )
592
    {
593
    return mM.add(EffectNames.QUATERNION,quaternion,center);
594
    }
595

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597
/**
598
 * Shears the Object.
599
 *
600
 * @param shear   The 3-tuple of shear factors. The first controls level
601
 *                of shearing in the X-axis, second - Y-axis and the third -
602
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
603
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
604
 * @param center  Center of shearing, i.e. the point which stays unmoved.
605
 * @return        ID of the effect added, or -1 if we failed to add one.
606
 */
607
  public long shear(Data3D shear, Data3D center)
608
    {
609
    return mM.add(EffectNames.SHEAR, shear, center);
610
    }
611

    
612
///////////////////////////////////////////////////////////////////////////////////////////////////
613
// Fragment-based effects  
614
///////////////////////////////////////////////////////////////////////////////////////////////////
615
/**
616
 * Makes a certain sub-region of the Object smoothly change all three of its RGB components.
617
 *        
618
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
619
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
620
 *               Valid range: <0,1>
621
 * @param color  Color to mix. (1,0,0) is RED.
622
 * @param region Region this Effect is limited to.
623
 * @param smooth If true, the level of 'blend' will smoothly fade out towards the edges of the region.
624
 * @return       ID of the effect added, or -1 if we failed to add one.
625
 */
626
  public long chroma(Data1D blend, Data3D color, Data4D region, boolean smooth)
627
    {
628
    return mF.add( smooth? EffectNames.SMOOTH_CHROMA:EffectNames.CHROMA, blend, color, region);
629
    }
630

    
631
///////////////////////////////////////////////////////////////////////////////////////////////////
632
/**
633
 * Makes the whole 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
 * @return       ID of the effect added, or -1 if we failed to add one.
640
 */
641
  public long chroma(Data1D blend, Data3D color)
642
    {
643
    return mF.add(EffectNames.CHROMA, blend, color);
644
    }
645

    
646
///////////////////////////////////////////////////////////////////////////////////////////////////
647
/**
648
 * Makes a certain sub-region of the Object smoothly change its transparency level.
649
 *        
650
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any given
651
 *               moment: pixel.a *= alpha.
652
 *               Valid range: <0,1>
653
 * @param region Region this Effect is limited to. 
654
 * @param smooth If true, the level of 'alpha' will smoothly fade out towards the edges of the region.
655
 * @return       ID of the effect added, or -1 if we failed to add one. 
656
 */
657
  public long alpha(Data1D alpha, Data4D region, boolean smooth)
658
    {
659
    return mF.add( smooth? EffectNames.SMOOTH_ALPHA:EffectNames.ALPHA, alpha, region);
660
    }
661

    
662
///////////////////////////////////////////////////////////////////////////////////////////////////
663
/**
664
 * Makes the whole Object smoothly change its transparency level.
665
 *
666
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any
667
 *               given moment: pixel.a *= alpha.
668
 *               Valid range: <0,1>
669
 * @return       ID of the effect added, or -1 if we failed to add one.
670
 */
671
  public long alpha(Data1D alpha)
672
    {
673
    return mF.add(EffectNames.ALPHA, alpha);
674
    }
675

    
676
///////////////////////////////////////////////////////////////////////////////////////////////////
677
/**
678
 * Makes a certain sub-region of the Object smoothly change its brightness level.
679
 *        
680
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
681
 *                   at any given moment. Valid range: <0,infinity)
682
 * @param region     Region this Effect is limited to.
683
 * @param smooth     If true, the level of 'brightness' will smoothly fade out towards the edges of the region.
684
 * @return           ID of the effect added, or -1 if we failed to add one.
685
 */
686
  public long brightness(Data1D brightness, Data4D region, boolean smooth)
687
    {
688
    return mF.add( smooth ? EffectNames.SMOOTH_BRIGHTNESS: EffectNames.BRIGHTNESS, brightness, region);
689
    }
690

    
691
///////////////////////////////////////////////////////////////////////////////////////////////////
692
/**
693
 * Makes the whole Object smoothly change its brightness level.
694
 *
695
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
696
 *                   at any given moment. Valid range: <0,infinity)
697
 * @return           ID of the effect added, or -1 if we failed to add one.
698
 */
699
  public long brightness(Data1D brightness)
700
    {
701
    return mF.add(EffectNames.BRIGHTNESS, brightness);
702
    }
703

    
704
///////////////////////////////////////////////////////////////////////////////////////////////////
705
/**
706
 * Makes a certain sub-region of the Object smoothly change its contrast level.
707
 *        
708
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
709
 *                 at any given moment. Valid range: <0,infinity)
710
 * @param region   Region this Effect is limited to.
711
 * @param smooth   If true, the level of 'contrast' will smoothly fade out towards the edges of the region.
712
 * @return         ID of the effect added, or -1 if we failed to add one.
713
 */
714
  public long contrast(Data1D contrast, Data4D region, boolean smooth)
715
    {
716
    return mF.add( smooth ? EffectNames.SMOOTH_CONTRAST:EffectNames.CONTRAST, contrast, region);
717
    }
718

    
719
///////////////////////////////////////////////////////////////////////////////////////////////////
720
/**
721
 * Makes the whole Object smoothly change its contrast level.
722
 *
723
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
724
 *                 at any given moment. Valid range: <0,infinity)
725
 * @return         ID of the effect added, or -1 if we failed to add one.
726
 */
727
  public long contrast(Data1D contrast)
728
    {
729
    return mF.add(EffectNames.CONTRAST, contrast);
730
    }
731

    
732
///////////////////////////////////////////////////////////////////////////////////////////////////
733
/**
734
 * Makes a certain sub-region of the Object smoothly change its saturation level.
735
 *        
736
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
737
 *                   at any given moment. Valid range: <0,infinity)
738
 * @param region     Region this Effect is limited to.
739
 * @param smooth     If true, the level of 'saturation' will smoothly fade out towards the edges of the region.
740
 * @return           ID of the effect added, or -1 if we failed to add one.
741
 */
742
  public long saturation(Data1D saturation, Data4D region, boolean smooth)
743
    {
744
    return mF.add( smooth ? EffectNames.SMOOTH_SATURATION:EffectNames.SATURATION, saturation, region);
745
    }
746

    
747
///////////////////////////////////////////////////////////////////////////////////////////////////
748
/**
749
 * Makes the whole Object smoothly change its saturation level.
750
 *
751
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
752
 *                   at any given moment. Valid range: <0,infinity)
753
 * @return           ID of the effect added, or -1 if we failed to add one.
754
 */
755
  public long saturation(Data1D saturation)
756
    {
757
    return mF.add(EffectNames.SATURATION, saturation);
758
    }
759

    
760
///////////////////////////////////////////////////////////////////////////////////////////////////
761
// Vertex-based effects  
762
///////////////////////////////////////////////////////////////////////////////////////////////////
763
/**
764
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
765
 *
766
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
767
 *               currently being dragged with.
768
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
769
 * @param region Region that masks 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, Data4D region)
773
    {  
774
    return mV.add(EffectNames.DISTORT, vector, center, region);
775
    }
776

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

    
791
///////////////////////////////////////////////////////////////////////////////////////////////////
792
/**
793
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
794
 * a (possibly changing in time) point on the Object.
795
 *
796
 * @param vector Vector of force that deforms the shape of the whole Object.
797
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
798
 * @param region Region that masks 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, Data4D region)
802
    {
803
    return mV.add(EffectNames.DEFORM, vector, center, region);
804
    }
805

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

    
820
///////////////////////////////////////////////////////////////////////////////////////////////////  
821
/**
822
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
823
 * away from the center (degree<=1)
824
 *
825
 * @param sink   The current degree of the Effect.
826
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
827
 * @param region Region that masks 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, Data4D region)
831
    {
832
    return mV.add(EffectNames.SINK, sink, center, region);
833
    }
834

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

    
849
///////////////////////////////////////////////////////////////////////////////////////////////////
850
/**
851
 * Pull all points around the center of the Effect towards a line passing through the center
852
 * (that's if degree>=1) or push them away from the line (degree<=1)
853
 *
854
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
855
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
856
 * @param region Region that masks 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, Data4D region)
860
    {
861
    return mV.add(EffectNames.PINCH, pinch, center, region);
862
    }
863

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

    
878
///////////////////////////////////////////////////////////////////////////////////////////////////  
879
/**
880
 * Rotate part of the 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
 * @param region Region that masks the Effect.
885
 * @return       ID of the effect added, or -1 if we failed to add one.
886
 */
887
  public long swirl(Data1D swirl, Data3D center, Data4D region)
888
    {    
889
    return mV.add(EffectNames.SWIRL, swirl, center, region);
890
    }
891

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

    
905
///////////////////////////////////////////////////////////////////////////////////////////////////
906
/**
907
 * Directional, sinusoidal wave effect.
908
 *
909
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
910
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
911
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
912
 *               describe the 'direction' of the wave.
913
 *               <p>
914
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
915
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
916
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
917
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
918
 *               <p>
919
 *               <p>
920
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
921
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
922
 *               will be sine shapes.
923
 *               <p>
924
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
925
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
926
 *               YZ-plane with be sine shapes.
927
 *               <p>
928
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
929
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
930
 *               value if sin at this point.
931
 * @param center 3-dimensional Data that, at any given time, returns the Center of 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)
935
    {
936
    return mV.add(EffectNames.WAVE, wave, center, null);
937
    }
938

    
939
///////////////////////////////////////////////////////////////////////////////////////////////////
940
/**
941
 * Directional, sinusoidal wave effect.
942
 *
943
 * @param wave   see {@link DistortedObject#wave(Data5D,Data3D)}
944
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
945
 * @param region Region that masks the Effect.
946
 * @return       ID of the effect added, or -1 if we failed to add one.
947
 */
948
  public long wave(Data5D wave, Data3D center, Data4D region)
949
    {
950
    return mV.add(EffectNames.WAVE, wave, center, region);
951
    }
952
  }
(9-9/17)