Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedObject.java @ d1e740c5

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
 * <p>
41
 * General idea is as follows:
42
 * <ul>
43
 * <li> Create an instance of (some class descended from) DistortedObject
44
 * <li> Paint something onto the Bitmap that's backing it up
45
 * <li> Apply some effects
46
 * <li> Draw it!
47
 * </ul>
48
 * <p>
49
 * The effects we can apply fall into three general categories:
50
 * <ul>
51
 * <li> Matrix Effects, i.e. ones that change the Bitmap's ModelView Matrix (moves, scales, rotations)
52
 * <li> Vertex Effects, i.e. effects that are implemented in the Vertex Shader. Those typically change
53
 *      the shape of (some sub-Region of) the Bitmap in some way (deforms, distortions, sinks)
54
 * <li> Fragment Effects, i.e. effects that change (some of) the pixels of the Bitmap (transparency, macroblock)
55
 * </ul>
56
 * <p>
57
 * Just like in DistortedNode and DistortedFramebuffer, we need to have a static list of all
58
 * DistortedObjects currently created by the application so that we can implement the 'mark for
59
 * deletion now - actually delete on next render' thing.
60
 * We need to be able to quickly retrieve an Object by its ID, thus a HashMap.
61
 */
62
public abstract class DistortedObject 
63
  {
64
  private static long mNextID =0;
65
  private static HashMap<Long,DistortedObject> mObjects = new HashMap<>();
66

    
67
  private EffectQueueMatrix    mM;
68
  private EffectQueueFragment  mF;
69
  private EffectQueueVertex    mV;
70

    
71
  private boolean matrixCloned, vertexCloned, fragmentCloned;
72
  private long mID;
73
  private int mSizeX, mSizeY, mSizeZ; // in screen space
74

    
75
  protected DistortedObjectGrid mGrid = null;
76

    
77
  private Bitmap[] mBmp= null; //
78
  int[] mTextureDataH;         // have to be shared among all the cloned Objects
79
  boolean[] mBitmapSet;        //
80

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

    
91
  private static Bitmap flipBitmap(Bitmap src)
92
    {
93
    Matrix matrix = new Matrix();
94
    matrix.preScale(1.0f,-1.0f);
95

    
96
    return Bitmap.createBitmap(src,0,0,src.getWidth(),src.getHeight(), matrix,true);
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  protected abstract DistortedObject deepCopy(int flags);
102

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

    
105
  protected void initializeData(int x, int y, int z)
106
    {
107
    mSizeX= x;
108
    mSizeY= y;
109
    mSizeZ= z;
110

    
111
    mID = mNextID++;
112
    mObjects.put(mID,this);
113

    
114
    mTextureDataH   = new int[1];
115
    mTextureDataH[0]= 0;
116
    mBmp            = new Bitmap[1];
117
    mBmp[0]         = null;
118
    mBitmapSet      = new boolean[1];
119
    mBitmapSet[0]   = false;
120
      
121
    initializeEffectLists(this,0);
122
      
123
    if( Distorted.isInitialized() ) resetTexture();
124
    }
125
    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
    
128
  private void initializeEffectLists(DistortedObject d, int flags)
129
    {
130
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
131
      {
132
      mM = d.mM;
133
      matrixCloned = true;
134
      }
135
    else
136
      {
137
      mM = new EffectQueueMatrix(d);
138
      matrixCloned = false;
139
      }
140
    
141
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
142
      {
143
      mV = d.mV;
144
      vertexCloned = true;
145
      }
146
    else
147
      {
148
      mV = new EffectQueueVertex(d);
149
      vertexCloned = false;
150
      }
151
    
152
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
153
      {
154
      mF = d.mF;
155
      fragmentCloned = true;
156
      }
157
    else
158
      {
159
      mF = new EffectQueueFragment(d);
160
      fragmentCloned = false;
161
      }
162
    }
163
    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165
// this will be called on startup and every time OpenGL context has been lost
166
// also call this from the constructor if the OpenGL context has been created already.
167
    
168
  private void resetTexture()
169
    {
170
    if( mTextureDataH!=null )
171
      {
172
      if( mTextureDataH[0]==0 ) GLES20.glGenTextures(1, mTextureDataH, 0);
173

    
174
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
175
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR );
176
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR );
177
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE );
178
      GLES20.glTexParameteri ( GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE );
179
       
180
      if( mBmp!=null && mBmp[0]!=null)
181
        {
182
        GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(mBmp[0]), 0);
183
        mBmp[0] = null;
184
        }
185
      }
186
    }
187
  
188
///////////////////////////////////////////////////////////////////////////////////////////////////
189
   
190
  void drawPriv(long currTime, DistortedFramebuffer df)
191
    {
192
    DistortedFramebuffer.deleteAllMarked();
193

    
194
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
195
      
196
    mM.compute(currTime);
197
    mM.send(df);
198
      
199
    mV.compute(currTime);
200
    mV.send();
201
        
202
    mF.compute(currTime);
203
    mF.send();
204
       
205
    mGrid.draw();
206
    }
207

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209
   
210
  void drawNoEffectsPriv(DistortedFramebuffer df)
211
    {
212
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
213
    mM.sendZero(df);
214
    mV.sendZero();
215
    mF.sendZero();
216
    mGrid.draw();
217
    }
218
    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
   
221
  private void releasePriv()
222
    {
223
    if( !matrixCloned  ) mM.abortAll(false);
224
    if( !vertexCloned  ) mV.abortAll(false);
225
    if( !fragmentCloned) mF.abortAll(false);
226

    
227
    mBmp          = null;
228
    mGrid         = null;
229
    mM            = null;
230
    mV            = null;
231
    mF            = null;
232
    mTextureDataH = null;
233
    }
234
 
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  long getBitmapID()
238
    {
239
    return mBmp==null ? 0 : mBmp.hashCode();
240
    }
241

    
242
///////////////////////////////////////////////////////////////////////////////////////////////////
243

    
244
  static synchronized void reset()
245
    {
246
    for(long id: mObjects.keySet())
247
      {
248
      mObjects.get(id).resetTexture();
249
      }
250
    }
251

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

    
254
  static synchronized void release()
255
    {
256
    for(long id: mObjects.keySet())
257
      {
258
      mObjects.get(id).releasePriv();
259
      }
260

    
261
    mObjects.clear();
262
    mNextID = 0;
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
// PUBLIC API
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
/**
269
 * Default empty constructor so that derived classes can call it
270
 */
271
  public DistortedObject()
272
    {
273

    
274
    }
275

    
276
///////////////////////////////////////////////////////////////////////////////////////////////////
277
/**
278
 * Copy constructor used to create a DistortedObject based on various parts of another object.
279
 * <p>
280
 * Whatever we do not clone gets created just like in the default constructor.
281
 * We only call this from the descendant's classes' constructors where we have to pay attention
282
 * to give it the appropriate type of a DistortedObject!
283
 *
284
 * @param dc    Source object to create our object from
285
 * @param flags A bitmask of values specifying what to copy.
286
 *              For example, CLONE_BITMAP | CLONE_MATRIX.
287
 */
288
  public DistortedObject(DistortedObject dc, int flags)
289
    {
290
    initializeEffectLists(dc,flags);
291

    
292
    mID = mNextID++;
293
    mObjects.put(mID,this);
294

    
295
    mSizeX = dc.mSizeX;
296
    mSizeY = dc.mSizeY;
297
    mSizeZ = dc.mSizeZ;
298
    mGrid  = dc.mGrid;
299

    
300
    if( (flags & Distorted.CLONE_BITMAP) != 0 )
301
      {
302
      mTextureDataH = dc.mTextureDataH;
303
      mBmp          = dc.mBmp;
304
      mBitmapSet    = dc.mBitmapSet;
305
      }
306
    else
307
      {
308
      mTextureDataH   = new int[1];
309
      mTextureDataH[0]= 0;
310
      mBitmapSet      = new boolean[1];
311
      mBitmapSet[0]   = false;
312
      mBmp            = new Bitmap[1];
313
      mBmp[0]         = null;
314

    
315
      if( Distorted.isInitialized() ) resetTexture();
316
      }
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320
/**
321
 * Draw the DistortedObject to the location specified by current Matrix effects.    
322
 *     
323
 * @param currTime current time, in milliseconds.
324
 *        This gets passed on to Dynamics inside the Effects that are currently applied to the
325
 *        Object.
326
 */
327
  public void draw(long currTime)
328
    {
329
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
330
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
331
    drawPriv(currTime, Distorted.mFramebuffer);
332
    }
333

    
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
/**
336
 * Draw the DistortedObject to the Framebuffer passed.
337
 *
338
 * @param currTime Current time, in milliseconds.
339
 * @param df       Framebuffer to render this to.
340
 */
341
  public void draw(long currTime, DistortedFramebuffer df)
342
    {
343
    df.setAsOutput();
344
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
345
    drawPriv(currTime,df);
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349
/**
350
 * Releases all resources.
351
 */
352
  public synchronized void delete()
353
    {
354
    releasePriv();
355
    mObjects.remove(this);
356
    }
357

    
358
///////////////////////////////////////////////////////////////////////////////////////////////////
359
/**
360
 * Sets the underlying android.graphics.Bitmap object and uploads it to the GPU. 
361
 * <p>
362
 * You can only recycle() the passed Bitmap once the OpenGL context gets created (i.e. after call 
363
 * to onSurfaceCreated) because only after this point can the Library upload it to the GPU!
364
 * 
365
 * @param bmp The android.graphics.Bitmap object to apply effects to and display.
366
 */
367
   
368
  public void setBitmap(Bitmap bmp)
369
    {
370
    mBitmapSet[0] = true;
371
      
372
    if( Distorted.isInitialized() )
373
      {
374
      GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
375
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureDataH[0]);
376
      GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, flipBitmap(bmp), 0);
377
      }
378
    else
379
      {
380
      mBmp[0] = bmp;
381
      }
382
    }
383
    
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
/**
386
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
387
 * to one of the Effects that are currently applied to the DistortedObject.
388
 * 
389
 * @param el A class implementing the EffectListener interface that wants to get notifications.
390
 */
391
  public void addEventListener(EffectListener el)
392
    {
393
    mV.addListener(el);
394
    mF.addListener(el);
395
    mM.addListener(el);
396
    }
397

    
398
///////////////////////////////////////////////////////////////////////////////////////////////////
399
/**
400
 * Removes the calling class from the list of Listeners.
401
 * 
402
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
403
 */
404
  public void removeEventListener(EffectListener el)
405
    {
406
    mV.removeListener(el);
407
    mF.removeListener(el);
408
    mM.removeListener(el);
409
    }
410
   
411
///////////////////////////////////////////////////////////////////////////////////////////////////
412
/**
413
 * Returns the height of the DistortedObject.
414
 *    
415
 * @return height of the object, in pixels.
416
 */
417
  public int getWidth()
418
     {
419
     return mSizeX;   
420
     }
421

    
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423
/**
424
 * Returns the width of the DistortedObject.
425
 * 
426
 * @return width of the Object, in pixels.
427
 */
428
  public int getHeight()
429
      {
430
      return mSizeY;  
431
      }
432
    
433
///////////////////////////////////////////////////////////////////////////////////////////////////
434
/**
435
 * Returns the depth of the DistortedObject.
436
 * 
437
 * @return depth of the Object, in pixels.
438
 */
439
  public int getDepth()
440
      {
441
      return mSizeZ;  
442
      }
443
        
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445
/**
446
 * Returns unique ID of this instance.
447
 * 
448
 * @return ID of the object.
449
 */
450
  public long getID()
451
      {
452
      return mID;  
453
      }
454
    
455
///////////////////////////////////////////////////////////////////////////////////////////////////
456
/**
457
 * Aborts all Effects.
458
 * @return Number of effects aborted.
459
 */
460
  public int abortAllEffects()
461
      {
462
      return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
463
      }
464

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

    
494
    if( type==EffectTypes.MATRIX.type   ) return mM.removeByID(id>>EffectTypes.LENGTH);
495
    if( type==EffectTypes.VERTEX.type   ) return mV.removeByID(id>>EffectTypes.LENGTH);
496
    if( type==EffectTypes.FRAGMENT.type ) return mF.removeByID(id>>EffectTypes.LENGTH);
497

    
498
    return 0;
499
    }
500

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

    
531
    if( type==EffectTypes.MATRIX.type   )  return mM.printByID(id>>EffectTypes.LENGTH);
532
    if( type==EffectTypes.VERTEX.type   )  return mV.printByID(id>>EffectTypes.LENGTH);
533
    if( type==EffectTypes.FRAGMENT.type )  return mF.printByID(id>>EffectTypes.LENGTH);
534

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

    
556
///////////////////////////////////////////////////////////////////////////////////////////////////
557
/**
558
 * Scales the Object by (possibly changing in time) 3D scale factors.
559
 * 
560
 * @param scale 3-dimensional Data which at any given time returns a Static3D
561
 *              representing the current x- , y- and z- scale factors.
562
 * @return      ID of the effect added, or -1 if we failed to add one.
563
 */
564
  public long scale(Data3D scale)
565
    {   
566
    return mM.add(EffectNames.SCALE,scale);
567
    }
568

    
569
///////////////////////////////////////////////////////////////////////////////////////////////////
570
/**
571
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
572
 *
573
 * @param scale The factor to scale all 3 dimensions with.
574
 * @return      ID of the effect added, or -1 if we failed to add one.
575
 */
576
  public long scale(float scale)
577
    {
578
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
579
    }
580

    
581
///////////////////////////////////////////////////////////////////////////////////////////////////
582
/**
583
 * Rotates the Object by 'angle' degrees around the center.
584
 * Static axis of rotation is given by the last parameter.
585
 *
586
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
587
 * @param axis   Axis of 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 rotate(Data1D angle, Static3D axis, Data3D center )
592
    {   
593
    return mM.add(EffectNames.ROTATE, angle, axis, center);
594
    }
595

    
596
///////////////////////////////////////////////////////////////////////////////////////////////////
597
/**
598
 * Rotates the Object by 'angle' degrees around the center.
599
 * Here both angle and axis can dynamically change.
600
 *
601
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
602
 * @param center    Coordinates of the Point we are rotating around.
603
 * @return          ID of the effect added, or -1 if we failed to add one.
604
 */
605
  public long rotate(Data4D angleaxis, Data3D center)
606
    {
607
    return mM.add(EffectNames.ROTATE, angleaxis, center);
608
    }
609

    
610
///////////////////////////////////////////////////////////////////////////////////////////////////
611
/**
612
 * Rotates the Object by quaternion.
613
 *
614
 * @param quaternion The quaternion describing the rotation.
615
 * @param center     Coordinates of the Point we are rotating around.
616
 * @return           ID of the effect added, or -1 if we failed to add one.
617
 */
618
  public long quaternion(Data4D quaternion, Data3D center )
619
    {
620
    return mM.add(EffectNames.QUATERNION,quaternion,center);
621
    }
622

    
623
///////////////////////////////////////////////////////////////////////////////////////////////////
624
/**
625
 * Shears the Object.
626
 *
627
 * @param shear   The 3-tuple of shear factors. The first controls level
628
 *                of shearing in the X-axis, second - Y-axis and the third -
629
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
630
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
631
 * @param center  Center of shearing, i.e. the point which stays unmoved.
632
 * @return        ID of the effect added, or -1 if we failed to add one.
633
 */
634
  public long shear(Data3D shear, Data3D center)
635
    {
636
    return mM.add(EffectNames.SHEAR, shear, center);
637
    }
638

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

    
658
///////////////////////////////////////////////////////////////////////////////////////////////////
659
/**
660
 * Makes the whole Object smoothly change all three of its RGB components.
661
 *
662
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
663
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
664
 *               Valid range: <0,1>
665
 * @param color  Color to mix. (1,0,0) is RED.
666
 * @return       ID of the effect added, or -1 if we failed to add one.
667
 */
668
  public long chroma(Data1D blend, Data3D color)
669
    {
670
    return mF.add(EffectNames.CHROMA, blend, color);
671
    }
672

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

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

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

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

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

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

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

    
774
///////////////////////////////////////////////////////////////////////////////////////////////////
775
/**
776
 * Makes the whole Object smoothly change its saturation level.
777
 *
778
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
779
 *                   at any given moment. Valid range: <0,infinity)
780
 * @return           ID of the effect added, or -1 if we failed to add one.
781
 */
782
  public long saturation(Data1D saturation)
783
    {
784
    return mF.add(EffectNames.SATURATION, saturation);
785
    }
786

    
787
///////////////////////////////////////////////////////////////////////////////////////////////////
788
// Vertex-based effects  
789
///////////////////////////////////////////////////////////////////////////////////////////////////
790
/**
791
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
792
 *
793
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
794
 *               currently being dragged with.
795
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
796
 * @param region Region that masks the Effect.
797
 * @return       ID of the effect added, or -1 if we failed to add one.
798
 */
799
  public long distort(Data3D vector, Data3D center, Data4D region)
800
    {  
801
    return mV.add(EffectNames.DISTORT, vector, center, region);
802
    }
803

    
804
///////////////////////////////////////////////////////////////////////////////////////////////////
805
/**
806
 * Distort the whole Object by a (possibly changing in time) vector of force.
807
 *
808
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
809
 *               currently being dragged with.
810
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
811
 * @return       ID of the effect added, or -1 if we failed to add one.
812
 */
813
  public long distort(Data3D vector, Data3D center)
814
    {
815
    return mV.add(EffectNames.DISTORT, vector, center, null);
816
    }
817

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

    
833
///////////////////////////////////////////////////////////////////////////////////////////////////
834
/**
835
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
836
 * a (possibly changing in time) point on the Object.
837
 *     
838
 * @param vector Vector of force that deforms the shape of the whole Object.
839
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
840
 * @return       ID of the effect added, or -1 if we failed to add one.
841
 */
842
  public long deform(Data3D vector, Data3D center)
843
    {  
844
    return mV.add(EffectNames.DEFORM, vector, center, null);
845
    }
846

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

    
862
///////////////////////////////////////////////////////////////////////////////////////////////////
863
/**
864
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
865
 * away from the center (degree<=1)
866
 *
867
 * @param sink   The current degree of the Effect.
868
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
869
 * @return       ID of the effect added, or -1 if we failed to add one.
870
 */
871
  public long sink(Data1D sink, Data3D center)
872
    {
873
    return mV.add(EffectNames.SINK, sink, center);
874
    }
875

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

    
891
///////////////////////////////////////////////////////////////////////////////////////////////////
892
/**
893
 * Pull all points around the center of the Effect towards a line passing through the center
894
 * (that's if degree>=1) or push them away from the line (degree<=1)
895
 *
896
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
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 pinch(Data2D pinch, Data3D center)
901
    {
902
    return mV.add(EffectNames.PINCH, pinch, center);
903
    }
904

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

    
919
///////////////////////////////////////////////////////////////////////////////////////////////////
920
/**
921
 * Rotate the whole Object around the Center of the Effect by a certain angle.
922
 *
923
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
924
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
925
 * @return       ID of the effect added, or -1 if we failed to add one.
926
 */
927
  public long swirl(Data1D swirl, Data3D center)
928
    {
929
    return mV.add(EffectNames.SWIRL, swirl, center);
930
    }
931

    
932
///////////////////////////////////////////////////////////////////////////////////////////////////
933
/**
934
 * Directional, sinusoidal wave effect.
935
 *
936
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
937
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
938
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
939
 *               describe the 'direction' of the wave.
940
 *               <p>
941
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
942
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
943
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
944
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
945
 *               <p>
946
 *               <p>
947
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
948
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
949
 *               will be sine shapes.
950
 *               <p>
951
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
952
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
953
 *               YZ-plane with be sine shapes.
954
 *               <p>
955
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
956
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
957
 *               value if sin at this point.
958
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
959
 * @return       ID of the effect added, or -1 if we failed to add one.
960
 */
961
  public long wave(Data5D wave, Data3D center)
962
    {
963
    return mV.add(EffectNames.WAVE, wave, center, null);
964
    }
965

    
966
///////////////////////////////////////////////////////////////////////////////////////////////////
967
/**
968
 * Directional, sinusoidal wave effect.
969
 *
970
 * @param wave   see {@link DistortedObject#wave(Data5D,Data3D)}
971
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
972
 * @param region Region that masks the Effect.
973
 * @return       ID of the effect added, or -1 if we failed to add one.
974
 */
975
  public long wave(Data5D wave, Data3D center, Data4D region)
976
    {
977
    return mV.add(EffectNames.WAVE, wave, center, region);
978
    }
979
  }
(9-9/17)