Project

General

Profile

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

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

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22 c90b9e01 Leszek Koltunski
import android.content.res.Resources;
23 194ab46f Leszek Koltunski
import android.opengl.GLES30;
24 8fa96e69 Leszek Koltunski
import android.opengl.Matrix;
25 6a06a912 Leszek Koltunski
26 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectListener;
27 55c14a19 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
28 c90b9e01 Leszek Koltunski
import org.distorted.library.program.FragmentCompilationException;
29
import org.distorted.library.program.FragmentUniformsException;
30
import org.distorted.library.program.LinkingException;
31
import org.distorted.library.program.VertexCompilationException;
32
import org.distorted.library.program.VertexUniformsException;
33 568b29d8 Leszek Koltunski
import org.distorted.library.type.Data1D;
34 f2fe7e28 Leszek Koltunski
import org.distorted.library.type.Data2D;
35 568b29d8 Leszek Koltunski
import org.distorted.library.type.Data3D;
36
import org.distorted.library.type.Data4D;
37 350cc2f5 Leszek Koltunski
import org.distorted.library.type.Data5D;
38 568b29d8 Leszek Koltunski
import org.distorted.library.type.Static3D;
39 a4835695 Leszek Koltunski
40 c90b9e01 Leszek Koltunski
import java.io.InputStream;
41 c638c1b0 Leszek Koltunski
import java.nio.ByteBuffer;
42
import java.nio.ByteOrder;
43
import java.nio.FloatBuffer;
44
45 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
46 b329f352 Leszek Koltunski
/**
47 4e2382f3 Leszek Koltunski
 * Class containing {@link EffectTypes#LENGTH} queues, each a class derived from EffectQueue.
48 b73dcaa7 Leszek Koltunski
 * <p>
49 26df012c Leszek Koltunski
 * The queues hold actual effects to be applied to a given (DistortedTexture,MeshObject) combo.
50 b329f352 Leszek Koltunski
 */
51 421c2728 Leszek Koltunski
public class DistortedEffects
52 d425545a Leszek Koltunski
  {
53 8fa96e69 Leszek Koltunski
  private static final int POSITION_DATA_SIZE= 3; // Main Program: size of the position data in elements
54
  private static final int NORMAL_DATA_SIZE  = 3; // Main Program: size of the normal data in elements
55
  private static final int TEX_DATA_SIZE     = 2; // Main Program: size of the texture coordinate data in elements.
56 55c14a19 Leszek Koltunski
57 c1e24646 leszek
  /// MAIN PROGRAM ///
58
  private static DistortedProgram mMainProgram;
59
  private static int mMainTextureH;
60 03cb451d Leszek Koltunski
  private static boolean[] mEffectEnabled = new boolean[EffectNames.size()];
61
62
  static
63
    {
64
    int len = EffectNames.size();
65
66
    for(int i=0; i<len; i++)
67
      {
68
      mEffectEnabled[i] = false;
69
      }
70
    }
71 8fa96e69 Leszek Koltunski
72 c1e24646 leszek
  /// BLIT PROGRAM ///
73
  private static DistortedProgram mBlitProgram;
74
  private static int mBlitTextureH;
75 c2c08950 leszek
  private static int mBlitDepthH;
76 c90b9e01 Leszek Koltunski
  private static final FloatBuffer mQuadPositions;
77
78
  static
79
    {
80
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
81
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
82
    mQuadPositions.put(positionData).position(0);
83
    }
84
85 c1e24646 leszek
  /// DEBUG ONLY /////
86
  private static DistortedProgram mDebugProgram;
87
88
  private static int mDebugObjDH;
89
  private static int mDebugMVPMatrixH;
90 c90b9e01 Leszek Koltunski
  /// END DEBUG //////
91
92 8fa96e69 Leszek Koltunski
  private static float[] mMVPMatrix = new float[16];
93
  private static float[] mTmpMatrix = new float[16];
94 c638c1b0 Leszek Koltunski
95 8e34674e Leszek Koltunski
  private static long mNextID =0;
96 4e2382f3 Leszek Koltunski
  private long mID;
97 8e34674e Leszek Koltunski
98 d6e94c84 Leszek Koltunski
  private EffectQueueMatrix      mM;
99
  private EffectQueueFragment    mF;
100
  private EffectQueueVertex      mV;
101
  private EffectQueuePostprocess mP;
102 3d590d8d Leszek Koltunski
103 d6e94c84 Leszek Koltunski
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
104 985ea9c5 Leszek Koltunski
105 9361b337 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
106 55c14a19 Leszek Koltunski
107 c90b9e01 Leszek Koltunski
  static void createProgram(Resources resources)
108
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
109 55c14a19 Leszek Koltunski
    {
110 03cb451d Leszek Koltunski
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
111
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
112
113
    String mainVertHeader= ("#version 100\n");
114
    String mainFragHeader= ("#version 100\n");
115 c90b9e01 Leszek Koltunski
116 03cb451d Leszek Koltunski
    EffectNames name;
117
    EffectTypes type;
118
    boolean foundF = false;
119
    boolean foundV = false;
120 c90b9e01 Leszek Koltunski
121 03cb451d Leszek Koltunski
    for(int i=0; i<mEffectEnabled.length; i++)
122 c90b9e01 Leszek Koltunski
      {
123 03cb451d Leszek Koltunski
      if( mEffectEnabled[i] )
124
        {
125
        name = EffectNames.getName(i);
126
        type = EffectNames.getType(i);
127
128
        if( type == EffectTypes.VERTEX )
129
          {
130
          mainVertHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
131
          foundV = true;
132
          }
133
        else if( type == EffectTypes.FRAGMENT )
134
          {
135
          mainFragHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
136
          foundF = true;
137
          }
138
        }
139 c90b9e01 Leszek Koltunski
      }
140
141 03cb451d Leszek Koltunski
    mainVertHeader += ("#define NUM_VERTEX "   + ( foundV ? getMaxVertex()   : 0 ) + "\n");
142
    mainFragHeader += ("#define NUM_FRAGMENT " + ( foundF ? getMaxFragment() : 0 ) + "\n");
143 c90b9e01 Leszek Koltunski
144 03cb451d Leszek Koltunski
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
145
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
146 c90b9e01 Leszek Koltunski
147 c1e24646 leszek
    mMainProgram = new DistortedProgram(mainVertStream,mainFragStream, mainVertHeader, mainFragHeader);
148 c90b9e01 Leszek Koltunski
149 c1e24646 leszek
    int mainProgramH = mMainProgram.getProgramHandle();
150 c90b9e01 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainProgramH);
151
    EffectQueueVertex.getUniforms(mainProgramH);
152
    EffectQueueMatrix.getUniforms(mainProgramH);
153 c1e24646 leszek
    mMainTextureH= GLES30.glGetUniformLocation( mainProgramH, "u_Texture");
154
155
    // BLIT PROGRAM ////////////////////////////////////
156
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
157
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
158
159
    String blitVertHeader= ("#version 100\n#define NUM_VERTEX 0\n"  );
160
    String blitFragHeader= ("#version 100\n#define NUM_FRAGMENT 0\n");
161
162
    mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader);
163
164
    int blitProgramH = mBlitProgram.getProgramHandle();
165
    mBlitTextureH  = GLES30.glGetUniformLocation( blitProgramH, "u_Texture");
166 c2c08950 leszek
    mBlitDepthH    = GLES30.glGetUniformLocation( blitProgramH, "u_Depth");
167 c90b9e01 Leszek Koltunski
168
    // DEBUG ONLY //////////////////////////////////////
169
    final InputStream debugVertexStream   = resources.openRawResource(R.raw.test_vertex_shader);
170
    final InputStream debugFragmentStream = resources.openRawResource(R.raw.test_fragment_shader);
171
172
    mDebugProgram = new DistortedProgram(debugVertexStream,debugFragmentStream, "#version 100\n", "#version 100\n");
173
174
    int debugProgramH = mDebugProgram.getProgramHandle();
175 c1e24646 leszek
    mDebugObjDH = GLES30.glGetUniformLocation( debugProgramH, "u_objD");
176
    mDebugMVPMatrixH = GLES30.glGetUniformLocation( debugProgramH, "u_MVPMatrix");
177 c90b9e01 Leszek Koltunski
    // END DEBUG  //////////////////////////////////////
178 55c14a19 Leszek Koltunski
    }
179
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
182 421c2728 Leszek Koltunski
  private void initializeEffectLists(DistortedEffects d, int flags)
183 d425545a Leszek Koltunski
    {
184 015642fb Leszek Koltunski
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
185 6a06a912 Leszek Koltunski
      {
186 d425545a Leszek Koltunski
      mM = d.mM;
187
      matrixCloned = true;
188
      }
189
    else
190
      {
191 4e2382f3 Leszek Koltunski
      mM = new EffectQueueMatrix(mID);
192 d425545a Leszek Koltunski
      matrixCloned = false;
193
      }
194 6a06a912 Leszek Koltunski
    
195 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
196
      {
197
      mV = d.mV;
198
      vertexCloned = true;
199
      }
200
    else
201
      {
202 4e2382f3 Leszek Koltunski
      mV = new EffectQueueVertex(mID);
203 d425545a Leszek Koltunski
      vertexCloned = false;
204
      }
205 6a06a912 Leszek Koltunski
    
206 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
207
      {
208
      mF = d.mF;
209
      fragmentCloned = true;
210 6a06a912 Leszek Koltunski
      }
211 d425545a Leszek Koltunski
    else
212
      {
213 4e2382f3 Leszek Koltunski
      mF = new EffectQueueFragment(mID);
214 d425545a Leszek Koltunski
      fragmentCloned = false;
215
      }
216 d6e94c84 Leszek Koltunski
217
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
218
      {
219
      mP = d.mP;
220
      postprocessCloned = true;
221
      }
222
    else
223
      {
224
      mP = new EffectQueuePostprocess(mID);
225
      postprocessCloned = false;
226
      }
227 d425545a Leszek Koltunski
    }
228 6a06a912 Leszek Koltunski
229
///////////////////////////////////////////////////////////////////////////////////////////////////
230 c90b9e01 Leszek Koltunski
// DEBUG ONLY
231
232 e8b2f311 Leszek Koltunski
  @SuppressWarnings("unused")
233 c5369f1b leszek
  private void displayBoundingRect(float halfX, float halfY, float halfZ, DistortedOutputSurface surface, float[] mvp, float[] vertices)
234 c90b9e01 Leszek Koltunski
    {
235
    int len  = vertices.length/3;
236
    int minx = Integer.MAX_VALUE;
237
    int maxx = Integer.MIN_VALUE;
238
    int miny = Integer.MAX_VALUE;
239
    int maxy = Integer.MIN_VALUE;
240
    int wx,wy;
241
242
    float x,y,z, X,Y,W, ndcX,ndcY;
243
244
    for(int i=0; i<len; i++)
245
      {
246
      x = 2*halfX*vertices[3*i  ];
247
      y = 2*halfY*vertices[3*i+1];
248
      z = 2*halfZ*vertices[3*i+2];
249
250
      X = mvp[ 0]*x + mvp[ 4]*y + mvp[ 8]*z + mvp[12];
251
      Y = mvp[ 1]*x + mvp[ 5]*y + mvp[ 9]*z + mvp[13];
252
      W = mvp[ 3]*x + mvp[ 7]*y + mvp[11]*z + mvp[15];
253
254
      ndcX = X/W;
255
      ndcY = Y/W;
256
257 af4cc5db Leszek Koltunski
      wx = (int)(surface.mWidth *(ndcX+1)/2);
258
      wy = (int)(surface.mHeight*(ndcY+1)/2);
259 c90b9e01 Leszek Koltunski
260 e8b2f311 Leszek Koltunski
      if( wx<minx ) minx = wx;
261
      if( wx>maxx ) maxx = wx;
262
      if( wy<miny ) miny = wy;
263
      if( wy>maxy ) maxy = wy;
264 c90b9e01 Leszek Koltunski
      }
265
266
    mDebugProgram.useProgram();
267 c5369f1b leszek
    surface.setAsOutput();
268 c90b9e01 Leszek Koltunski
269 c318fb14 Leszek Koltunski
    Matrix.setIdentityM( mTmpMatrix, 0);
270 af4cc5db Leszek Koltunski
    Matrix.translateM  ( mTmpMatrix, 0, minx-surface.mWidth/2, maxy-surface.mHeight/2, -surface.mDistance);
271 c318fb14 Leszek Koltunski
    Matrix.scaleM      ( mTmpMatrix, 0, (float)(maxx-minx)/(2*halfX), (float)(maxy-miny)/(2*halfY), 1.0f);
272
    Matrix.translateM  ( mTmpMatrix, 0, halfX,-halfY, 0);
273 af4cc5db Leszek Koltunski
    Matrix.multiplyMM  ( mMVPMatrix, 0, surface.mProjectionMatrix, 0, mTmpMatrix, 0);
274 c90b9e01 Leszek Koltunski
275 c1e24646 leszek
    GLES30.glUniform2f(mDebugObjDH, 2*halfX, 2*halfY);
276
    GLES30.glUniformMatrix4fv(mDebugMVPMatrixH, 1, false, mMVPMatrix , 0);
277 c90b9e01 Leszek Koltunski
278 194ab46f Leszek Koltunski
    GLES30.glVertexAttribPointer(mDebugProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
279
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
280 c90b9e01 Leszek Koltunski
    }
281
282 b9798977 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
283
284
  DistortedFramebuffer getPostprocessingBuffer()
285
    {
286
    return mP.mNumEffects==0 ? null : mP.mMainBuffer;
287
    }
288
289 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
290
291 c1e24646 leszek
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime)
292 d425545a Leszek Koltunski
    {
293
    mM.compute(currTime);
294
    mV.compute(currTime);
295
    mF.compute(currTime);
296 d6e94c84 Leszek Koltunski
    mP.compute(currTime);
297 a56bc359 Leszek Koltunski
298 c1e24646 leszek
    float halfZ = halfW*mesh.zFactor;
299 af4cc5db Leszek Koltunski
    GLES30.glViewport(0, 0, surface.mWidth, surface.mHeight);
300 d6e94c84 Leszek Koltunski
301 b9798977 leszek
    mMainProgram.useProgram();
302
    GLES30.glUniform1i(mMainTextureH, 0);
303
    surface.setAsOutput();
304
    mM.send(surface,halfW,halfH,halfZ);
305
    mV.send(halfW,halfH,halfZ);
306
    mF.send(halfW,halfH);
307
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[0], POSITION_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mesh.mMeshPositions);
308
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[1], NORMAL_DATA_SIZE  , GLES30.GL_FLOAT, false, 0, mesh.mMeshNormals);
309
    GLES30.glVertexAttribPointer(mMainProgram.mAttribute[2], TEX_DATA_SIZE     , GLES30.GL_FLOAT, false, 0, mesh.mMeshTexture);
310
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, mesh.dataLength);
311 c90b9e01 Leszek Koltunski
312
    /// DEBUG ONLY //////
313 dbe8ea80 Leszek Koltunski
    // displayBoundingRect(halfInputW, halfInputH, halfZ, df, mM.getMVP(), mesh.getBoundingVertices() );
314 c90b9e01 Leszek Koltunski
    /// END DEBUG ///////
315 d425545a Leszek Koltunski
    }
316 6a06a912 Leszek Koltunski
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318
   
319 c1e24646 leszek
  static void blitPriv(DistortedOutputSurface projection)
320 d425545a Leszek Koltunski
    {
321 c1e24646 leszek
    mBlitProgram.useProgram();
322
323 c2c08950 leszek
    GLES30.glViewport(0, 0, projection.mWidth, projection.mHeight);
324 c1e24646 leszek
    GLES30.glUniform1i(mBlitTextureH, 0);
325 c2c08950 leszek
    GLES30.glUniform1f( mBlitDepthH , 1.0f-projection.mNear);
326 c1e24646 leszek
    GLES30.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES30.GL_FLOAT, false, 0, mQuadPositions);
327
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
328 d425545a Leszek Koltunski
    }
329 6a06a912 Leszek Koltunski
    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
   
332 8e34674e Leszek Koltunski
  private void releasePriv()
333 d425545a Leszek Koltunski
    {
334 d6e94c84 Leszek Koltunski
    if( !matrixCloned     ) mM.abortAll(false);
335
    if( !vertexCloned     ) mV.abortAll(false);
336
    if( !fragmentCloned   ) mF.abortAll(false);
337
    if( !postprocessCloned) mP.abortAll(false);
338 d425545a Leszek Koltunski
339 4e2382f3 Leszek Koltunski
    mM = null;
340
    mV = null;
341
    mF = null;
342 d6e94c84 Leszek Koltunski
    mP = null;
343 8e34674e Leszek Koltunski
    }
344
345 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
346
347 7b8086eb Leszek Koltunski
  static void onDestroy()
348 1942537e Leszek Koltunski
    {
349
    mNextID = 0;
350 03cb451d Leszek Koltunski
351
    int len = EffectNames.size();
352
353
    for(int i=0; i<len; i++)
354
      {
355
      mEffectEnabled[i] = false;
356
      }
357 1942537e Leszek Koltunski
    }
358
359 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
360
// PUBLIC API
361 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
362 d425545a Leszek Koltunski
/**
363 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
364 d425545a Leszek Koltunski
 */
365 421c2728 Leszek Koltunski
  public DistortedEffects()
366 d425545a Leszek Koltunski
    {
367 c7da4e65 leszek
    mID = ++mNextID;
368 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
369 d425545a Leszek Koltunski
    }
370 ada90d33 Leszek Koltunski
371
///////////////////////////////////////////////////////////////////////////////////////////////////
372 d425545a Leszek Koltunski
/**
373 4e2382f3 Leszek Koltunski
 * Copy constructor.
374 d425545a Leszek Koltunski
 * <p>
375
 * Whatever we do not clone gets created just like in the default constructor.
376
 *
377
 * @param dc    Source object to create our object from
378
 * @param flags A bitmask of values specifying what to copy.
379 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
380 d425545a Leszek Koltunski
 */
381 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
382 d425545a Leszek Koltunski
    {
383 c7da4e65 leszek
    mID = ++mNextID;
384 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
385 d425545a Leszek Koltunski
    }
386 ada90d33 Leszek Koltunski
387 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
388
/**
389 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
390 6a06a912 Leszek Koltunski
 */
391 8e34674e Leszek Koltunski
  public synchronized void delete()
392 d425545a Leszek Koltunski
    {
393
    releasePriv();
394
    }
395 6a06a912 Leszek Koltunski
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397
/**
398 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
399
 *
400
 * @return ID of the object.
401 6a06a912 Leszek Koltunski
 */
402 4e2382f3 Leszek Koltunski
  public long getID()
403 d425545a Leszek Koltunski
      {
404 4e2382f3 Leszek Koltunski
      return mID;
405 d425545a Leszek Koltunski
      }
406 4e2382f3 Leszek Koltunski
407 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
408
/**
409
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
410 07305c87 Leszek Koltunski
 * to one of the Effects in those queues. Nothing will happen if 'el' is already in the list.
411 6a06a912 Leszek Koltunski
 * 
412
 * @param el A class implementing the EffectListener interface that wants to get notifications.
413
 */
414 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
415 d425545a Leszek Koltunski
    {
416 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
417
    mF.registerForMessages(el);
418
    mM.registerForMessages(el);
419 d6e94c84 Leszek Koltunski
    mP.registerForMessages(el);
420 d425545a Leszek Koltunski
    }
421 6a06a912 Leszek Koltunski
422
///////////////////////////////////////////////////////////////////////////////////////////////////
423
/**
424
 * Removes the calling class from the list of Listeners.
425
 * 
426
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
427
 */
428 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
429 d425545a Leszek Koltunski
    {
430 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
431
    mF.deregisterForMessages(el);
432
    mM.deregisterForMessages(el);
433 d6e94c84 Leszek Koltunski
    mP.deregisterForMessages(el);
434 d425545a Leszek Koltunski
    }
435 6a06a912 Leszek Koltunski
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
/**
438 d07f2950 Leszek Koltunski
 * Aborts all Effects.
439
 * @return Number of effects aborted.
440 6a06a912 Leszek Koltunski
 */
441 d425545a Leszek Koltunski
  public int abortAllEffects()
442 6a06a912 Leszek Koltunski
      {
443 d6e94c84 Leszek Koltunski
      return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true) + mP.abortAll(true);
444 6a06a912 Leszek Koltunski
      }
445
446
///////////////////////////////////////////////////////////////////////////////////////////////////
447
/**
448 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
449 6a06a912 Leszek Koltunski
 * 
450 d07f2950 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectTypes}
451
 * @return Number of effects aborted.
452 6a06a912 Leszek Koltunski
 */
453 d425545a Leszek Koltunski
  public int abortEffects(EffectTypes type)
454
    {
455
    switch(type)
456 6a06a912 Leszek Koltunski
      {
457 d6e94c84 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
458
      case VERTEX     : return mV.abortAll(true);
459
      case FRAGMENT   : return mF.abortAll(true);
460
      case POSTPROCESS: return mP.abortAll(true);
461
      default         : return 0;
462 6a06a912 Leszek Koltunski
      }
463 d425545a Leszek Koltunski
    }
464 6a06a912 Leszek Koltunski
    
465
///////////////////////////////////////////////////////////////////////////////////////////////////
466
/**
467
 * Aborts a single Effect.
468
 * 
469
 * @param id ID of the Effect we want to abort.
470 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
471 6a06a912 Leszek Koltunski
 */
472 d425545a Leszek Koltunski
  public int abortEffect(long id)
473
    {
474
    int type = (int)(id&EffectTypes.MASK);
475 1e438fc7 Leszek Koltunski
476 d6e94c84 Leszek Koltunski
    if( type==EffectTypes.MATRIX.type      ) return mM.removeByID(id>>EffectTypes.LENGTH);
477
    if( type==EffectTypes.VERTEX.type      ) return mV.removeByID(id>>EffectTypes.LENGTH);
478
    if( type==EffectTypes.FRAGMENT.type    ) return mF.removeByID(id>>EffectTypes.LENGTH);
479
    if( type==EffectTypes.POSTPROCESS.type ) return mP.removeByID(id>>EffectTypes.LENGTH);
480 1e438fc7 Leszek Koltunski
481 d425545a Leszek Koltunski
    return 0;
482
    }
483 6a06a912 Leszek Koltunski
484
///////////////////////////////////////////////////////////////////////////////////////////////////
485
/**
486 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
487 6a06a912 Leszek Koltunski
 * 
488 d07f2950 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectNames}
489 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
490 6a06a912 Leszek Koltunski
 */
491 d425545a Leszek Koltunski
  public int abortEffects(EffectNames name)
492
    {
493
    switch(name.getType())
494 6a06a912 Leszek Koltunski
      {
495 d6e94c84 Leszek Koltunski
      case MATRIX     : return mM.removeByType(name);
496
      case VERTEX     : return mV.removeByType(name);
497
      case FRAGMENT   : return mF.removeByType(name);
498
      case POSTPROCESS: return mP.removeByType(name);
499
      default         : return 0;
500 6a06a912 Leszek Koltunski
      }
501 d425545a Leszek Koltunski
    }
502 6a06a912 Leszek Koltunski
    
503
///////////////////////////////////////////////////////////////////////////////////////////////////
504
/**
505
 * Print some info about a given Effect to Android's standard out. Used for debugging only.
506
 * 
507
 * @param id Effect ID we want to print info about
508
 * @return <code>true</code> if a single Effect of type effectType has been found.
509
 */
510
    
511 d425545a Leszek Koltunski
  public boolean printEffect(long id)
512
    {
513
    int type = (int)(id&EffectTypes.MASK);
514 1e438fc7 Leszek Koltunski
515 d6e94c84 Leszek Koltunski
    if( type==EffectTypes.MATRIX.type      )  return mM.printByID(id>>EffectTypes.LENGTH);
516
    if( type==EffectTypes.VERTEX.type      )  return mV.printByID(id>>EffectTypes.LENGTH);
517
    if( type==EffectTypes.FRAGMENT.type    )  return mF.printByID(id>>EffectTypes.LENGTH);
518
    if( type==EffectTypes.POSTPROCESS.type )  return mP.printByID(id>>EffectTypes.LENGTH);
519 1e438fc7 Leszek Koltunski
520 d425545a Leszek Koltunski
    return false;
521
    }
522 432442f9 Leszek Koltunski
523 03cb451d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
524
/**
525
 * Enables a given Effect.
526
 * <p>
527
 * By default, all effects are disabled. One has to explicitly enable each effect one intends to use.
528
 * This needs to be called BEFORE shaders get compiled, i.e. before the call to Distorted.onCreate().
529
 * The point: by enabling only the effects we need, we can optimize the shaders.
530
 *
531
 * @param name Name of the Effect to enable.
532
 */
533
  public static void enableEffect(EffectNames name)
534
    {
535
    mEffectEnabled[name.ordinal()] = true;
536
    }
537
538 432442f9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
539
/**
540
 * Returns the maximum number of Matrix effects.
541
 *
542
 * @return The maximum number of Matrix effects
543
 */
544
  public static int getMaxMatrix()
545
    {
546
    return EffectQueue.getMax(EffectTypes.MATRIX.ordinal());
547
    }
548
549
///////////////////////////////////////////////////////////////////////////////////////////////////
550
/**
551
 * Returns the maximum number of Vertex effects.
552
 *
553
 * @return The maximum number of Vertex effects
554
 */
555
  public static int getMaxVertex()
556
    {
557
    return EffectQueue.getMax(EffectTypes.VERTEX.ordinal());
558
    }
559
560
///////////////////////////////////////////////////////////////////////////////////////////////////
561
/**
562
 * Returns the maximum number of Fragment effects.
563
 *
564
 * @return The maximum number of Fragment effects
565
 */
566
  public static int getMaxFragment()
567
    {
568
    return EffectQueue.getMax(EffectTypes.FRAGMENT.ordinal());
569
    }
570
571
///////////////////////////////////////////////////////////////////////////////////////////////////
572
/**
573
 * Returns the maximum number of Postprocess effects.
574
 *
575
 * @return The maximum number of Postprocess effects
576
 */
577
  public static int getMaxPostprocess()
578
    {
579
    return EffectQueue.getMax(EffectTypes.POSTPROCESS.ordinal());
580
    }
581
582
///////////////////////////////////////////////////////////////////////////////////////////////////
583
/**
584
 * Sets the maximum number of Matrix effects that can be stored in a single EffectQueue at one time.
585
 * This can fail if:
586
 * <ul>
587
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
588
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
589
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
590
 *     time only decreasing the value of 'max' is permitted.
591
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
592
 * </ul>
593
 *
594
 * @param max new maximum number of simultaneous Matrix Effects. Has to be a non-negative number not greater
595
 *            than Byte.MAX_VALUE
596
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
597
 */
598
  public static boolean setMaxMatrix(int max)
599
    {
600
    return EffectQueue.setMax(EffectTypes.MATRIX.ordinal(),max);
601
    }
602
603
///////////////////////////////////////////////////////////////////////////////////////////////////
604
/**
605
 * Sets the maximum number of Vertex effects that can be stored in a single EffectQueue at one time.
606
 * This can fail if:
607
 * <ul>
608
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
609
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
610
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
611
 *     time only decreasing the value of 'max' is permitted.
612
* <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
613
 * </ul>
614
 *
615
 * @param max new maximum number of simultaneous Vertex Effects. Has to be a non-negative number not greater
616
 *            than Byte.MAX_VALUE
617
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
618
 */
619
  public static boolean setMaxVertex(int max)
620
    {
621
    return EffectQueue.setMax(EffectTypes.VERTEX.ordinal(),max);
622
    }
623
624
///////////////////////////////////////////////////////////////////////////////////////////////////
625
/**
626
 * Sets the maximum number of Fragment effects that can be stored in a single EffectQueue at one time.
627
 * This can fail if:
628
 * <ul>
629
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
630
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
631
 *     before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
632
 *     time only decreasing the value of 'max' is permitted.
633
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
634
 * </ul>
635
 *
636
 * @param max new maximum number of simultaneous Fragment Effects. Has to be a non-negative number not greater
637
 *            than Byte.MAX_VALUE
638
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
639
 */
640
  public static boolean setMaxFragment(int max)
641
    {
642
    return EffectQueue.setMax(EffectTypes.FRAGMENT.ordinal(),max);
643
    }
644
645
///////////////////////////////////////////////////////////////////////////////////////////////////
646
/**
647
 * Sets the maximum number of Postprocess effects that can be stored in a single EffectQueue at one time.
648
 * This can fail if:
649
 * <ul>
650
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
651
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
652
 *     before the Fragment Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
653
 *     time only decreasing the value of 'max' is permitted.
654
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
655
 * </ul>
656
 *
657
 * @param max new maximum number of simultaneous Postprocess Effects. Has to be a non-negative number not greater
658
 *            than Byte.MAX_VALUE
659
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
660
 */
661
  public static boolean setMaxPostprocess(int max)
662
    {
663
    return EffectQueue.setMax(EffectTypes.POSTPROCESS.ordinal(),max);
664
    }
665
666 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////   
667
///////////////////////////////////////////////////////////////////////////////////////////////////
668
// Individual effect functions.
669
///////////////////////////////////////////////////////////////////////////////////////////////////
670
// Matrix-based effects
671
///////////////////////////////////////////////////////////////////////////////////////////////////
672
/**
673 e8c81a8e Leszek Koltunski
 * Moves the Object by a (possibly changing in time) vector.
674 6a06a912 Leszek Koltunski
 * 
675 568b29d8 Leszek Koltunski
 * @param vector 3-dimensional Data which at any given time will return a Static3D
676 e0a16874 Leszek Koltunski
 *               representing the current coordinates of the vector we want to move the Object with.
677
 * @return       ID of the effect added, or -1 if we failed to add one.
678 6a06a912 Leszek Koltunski
 */
679 568b29d8 Leszek Koltunski
  public long move(Data3D vector)
680 6a06a912 Leszek Koltunski
    {   
681 e0a16874 Leszek Koltunski
    return mM.add(EffectNames.MOVE,vector);
682 6a06a912 Leszek Koltunski
    }
683
684
///////////////////////////////////////////////////////////////////////////////////////////////////
685
/**
686 e8c81a8e Leszek Koltunski
 * Scales the Object by (possibly changing in time) 3D scale factors.
687 6a06a912 Leszek Koltunski
 * 
688 d1e740c5 Leszek Koltunski
 * @param scale 3-dimensional Data which at any given time returns a Static3D
689 e0a16874 Leszek Koltunski
 *              representing the current x- , y- and z- scale factors.
690
 * @return      ID of the effect added, or -1 if we failed to add one.
691 6a06a912 Leszek Koltunski
 */
692 568b29d8 Leszek Koltunski
  public long scale(Data3D scale)
693 6a06a912 Leszek Koltunski
    {   
694 e0a16874 Leszek Koltunski
    return mM.add(EffectNames.SCALE,scale);
695 6a06a912 Leszek Koltunski
    }
696
697 2fce34f4 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
698
/**
699 e8c81a8e Leszek Koltunski
 * Scales the Object by one uniform, constant factor in all 3 dimensions. Convenience function.
700 2fce34f4 Leszek Koltunski
 *
701
 * @param scale The factor to scale all 3 dimensions with.
702
 * @return      ID of the effect added, or -1 if we failed to add one.
703
 */
704 d425545a Leszek Koltunski
  public long scale(float scale)
705
    {
706
    return mM.add(EffectNames.SCALE, new Static3D(scale,scale,scale));
707
    }
708 2fce34f4 Leszek Koltunski
709 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
710
/**
711 568b29d8 Leszek Koltunski
 * Rotates the Object by 'angle' degrees around the center.
712
 * Static axis of rotation is given by the last parameter.
713
 *
714 9351ad55 Leszek Koltunski
 * @param angle  Angle that we want to rotate the Object to. Unit: degrees
715 568b29d8 Leszek Koltunski
 * @param axis   Axis of rotation
716 0df17fad Leszek Koltunski
 * @param center Coordinates of the Point we are rotating around.
717 e0a16874 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
718
 */
719 0df17fad Leszek Koltunski
  public long rotate(Data1D angle, Static3D axis, Data3D center )
720 6a06a912 Leszek Koltunski
    {   
721 0df17fad Leszek Koltunski
    return mM.add(EffectNames.ROTATE, angle, axis, center);
722 6a06a912 Leszek Koltunski
    }
723
724
///////////////////////////////////////////////////////////////////////////////////////////////////
725
/**
726 568b29d8 Leszek Koltunski
 * Rotates the Object by 'angle' degrees around the center.
727 2fce34f4 Leszek Koltunski
 * Here both angle and axis can dynamically change.
728 568b29d8 Leszek Koltunski
 *
729
 * @param angleaxis Combined 4-tuple representing the (angle,axisX,axisY,axisZ).
730 0df17fad Leszek Koltunski
 * @param center    Coordinates of the Point we are rotating around.
731 568b29d8 Leszek Koltunski
 * @return          ID of the effect added, or -1 if we failed to add one.
732 e0a16874 Leszek Koltunski
 */
733 0df17fad Leszek Koltunski
  public long rotate(Data4D angleaxis, Data3D center)
734 568b29d8 Leszek Koltunski
    {
735 0df17fad Leszek Koltunski
    return mM.add(EffectNames.ROTATE, angleaxis, center);
736 6a06a912 Leszek Koltunski
    }
737
738
///////////////////////////////////////////////////////////////////////////////////////////////////
739
/**
740 568b29d8 Leszek Koltunski
 * Rotates the Object by quaternion.
741 0df17fad Leszek Koltunski
 *
742 568b29d8 Leszek Koltunski
 * @param quaternion The quaternion describing the rotation.
743 0df17fad Leszek Koltunski
 * @param center     Coordinates of the Point we are rotating around.
744 568b29d8 Leszek Koltunski
 * @return           ID of the effect added, or -1 if we failed to add one.
745 6a06a912 Leszek Koltunski
 */
746 0df17fad Leszek Koltunski
  public long quaternion(Data4D quaternion, Data3D center )
747 568b29d8 Leszek Koltunski
    {
748 0df17fad Leszek Koltunski
    return mM.add(EffectNames.QUATERNION,quaternion,center);
749 6a06a912 Leszek Koltunski
    }
750
751
///////////////////////////////////////////////////////////////////////////////////////////////////
752
/**
753 568b29d8 Leszek Koltunski
 * Shears the Object.
754 6a06a912 Leszek Koltunski
 *
755 8c3cdec5 Leszek Koltunski
 * @param shear   The 3-tuple of shear factors. The first controls level
756
 *                of shearing in the X-axis, second - Y-axis and the third -
757
 *                Z-axis. Each is the tangens of the shear angle, i.e 0 -
758
 *                no shear, 1 - shear by 45 degrees (tan(45deg)=1) etc.
759 0df17fad Leszek Koltunski
 * @param center  Center of shearing, i.e. the point which stays unmoved.
760 e0a16874 Leszek Koltunski
 * @return        ID of the effect added, or -1 if we failed to add one.
761 6a06a912 Leszek Koltunski
 */
762 0df17fad Leszek Koltunski
  public long shear(Data3D shear, Data3D center)
763 6a06a912 Leszek Koltunski
    {
764 0df17fad Leszek Koltunski
    return mM.add(EffectNames.SHEAR, shear, center);
765 6a06a912 Leszek Koltunski
    }
766
767
///////////////////////////////////////////////////////////////////////////////////////////////////
768
// Fragment-based effects  
769
///////////////////////////////////////////////////////////////////////////////////////////////////
770
/**
771 e0a16874 Leszek Koltunski
 * Makes a certain sub-region of the Object smoothly change all three of its RGB components.
772 6a06a912 Leszek Koltunski
 *        
773 2fce34f4 Leszek Koltunski
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
774 e4878781 Leszek Koltunski
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
775
 *               Valid range: <0,1>
776 b1e91f2c Leszek Koltunski
 * @param color  Color to mix. (1,0,0) is RED.
777 2fce34f4 Leszek Koltunski
 * @param region Region this Effect is limited to.
778
 * @param smooth If true, the level of 'blend' will smoothly fade out towards the edges of the region.
779
 * @return       ID of the effect added, or -1 if we failed to add one.
780 6a06a912 Leszek Koltunski
 */
781 8c893ffc Leszek Koltunski
  public long chroma(Data1D blend, Data3D color, Data4D region, boolean smooth)
782 6a06a912 Leszek Koltunski
    {
783 2fce34f4 Leszek Koltunski
    return mF.add( smooth? EffectNames.SMOOTH_CHROMA:EffectNames.CHROMA, blend, color, region);
784 6a06a912 Leszek Koltunski
    }
785
786
///////////////////////////////////////////////////////////////////////////////////////////////////
787
/**
788 2fce34f4 Leszek Koltunski
 * Makes the whole Object smoothly change all three of its RGB components.
789
 *
790
 * @param blend  1-dimensional Data that returns the level of blend a given pixel will be
791 e4878781 Leszek Koltunski
 *               mixed with the next parameter 'color': pixel = (1-level)*pixel + level*color.
792
 *               Valid range: <0,1>
793 b1e91f2c Leszek Koltunski
 * @param color  Color to mix. (1,0,0) is RED.
794 2fce34f4 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
795 6a06a912 Leszek Koltunski
 */
796 8c893ffc Leszek Koltunski
  public long chroma(Data1D blend, Data3D color)
797 6a06a912 Leszek Koltunski
    {
798 2fce34f4 Leszek Koltunski
    return mF.add(EffectNames.CHROMA, blend, color);
799 6a06a912 Leszek Koltunski
    }
800
801
///////////////////////////////////////////////////////////////////////////////////////////////////
802
/**
803 e0a16874 Leszek Koltunski
 * Makes a certain sub-region of the Object smoothly change its transparency level.
804 6a06a912 Leszek Koltunski
 *        
805 2fce34f4 Leszek Koltunski
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any given
806 e4878781 Leszek Koltunski
 *               moment: pixel.a *= alpha.
807
 *               Valid range: <0,1>
808 d7bbef2f Leszek Koltunski
 * @param region Region this Effect is limited to. 
809 2fce34f4 Leszek Koltunski
 * @param smooth If true, the level of 'alpha' will smoothly fade out towards the edges of the region.
810 d7bbef2f Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one. 
811 6a06a912 Leszek Koltunski
 */
812 2fce34f4 Leszek Koltunski
  public long alpha(Data1D alpha, Data4D region, boolean smooth)
813 6a06a912 Leszek Koltunski
    {
814 2fce34f4 Leszek Koltunski
    return mF.add( smooth? EffectNames.SMOOTH_ALPHA:EffectNames.ALPHA, alpha, region);
815 6a06a912 Leszek Koltunski
    }
816
817
///////////////////////////////////////////////////////////////////////////////////////////////////
818
/**
819 2fce34f4 Leszek Koltunski
 * Makes the whole Object smoothly change its transparency level.
820
 *
821
 * @param alpha  1-dimensional Data that returns the level of transparency we want to have at any
822 e4878781 Leszek Koltunski
 *               given moment: pixel.a *= alpha.
823
 *               Valid range: <0,1>
824 2fce34f4 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
825 6a06a912 Leszek Koltunski
 */
826 2fce34f4 Leszek Koltunski
  public long alpha(Data1D alpha)
827 6a06a912 Leszek Koltunski
    {
828 2fce34f4 Leszek Koltunski
    return mF.add(EffectNames.ALPHA, alpha);
829 6a06a912 Leszek Koltunski
    }
830
831
///////////////////////////////////////////////////////////////////////////////////////////////////
832
/**
833 e0a16874 Leszek Koltunski
 * Makes a certain sub-region of the Object smoothly change its brightness level.
834 6a06a912 Leszek Koltunski
 *        
835 2fce34f4 Leszek Koltunski
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
836 e4878781 Leszek Koltunski
 *                   at any given moment. Valid range: <0,infinity)
837 e0a16874 Leszek Koltunski
 * @param region     Region this Effect is limited to.
838 2fce34f4 Leszek Koltunski
 * @param smooth     If true, the level of 'brightness' will smoothly fade out towards the edges of the region.
839 e0a16874 Leszek Koltunski
 * @return           ID of the effect added, or -1 if we failed to add one.
840 6a06a912 Leszek Koltunski
 */
841 2fce34f4 Leszek Koltunski
  public long brightness(Data1D brightness, Data4D region, boolean smooth)
842 6a06a912 Leszek Koltunski
    {
843 2fce34f4 Leszek Koltunski
    return mF.add( smooth ? EffectNames.SMOOTH_BRIGHTNESS: EffectNames.BRIGHTNESS, brightness, region);
844 6a06a912 Leszek Koltunski
    }
845
846
///////////////////////////////////////////////////////////////////////////////////////////////////
847
/**
848 2fce34f4 Leszek Koltunski
 * Makes the whole Object smoothly change its brightness level.
849
 *
850
 * @param brightness 1-dimensional Data that returns the level of brightness we want to have
851 e4878781 Leszek Koltunski
 *                   at any given moment. Valid range: <0,infinity)
852 e0a16874 Leszek Koltunski
 * @return           ID of the effect added, or -1 if we failed to add one.
853 6a06a912 Leszek Koltunski
 */
854 2fce34f4 Leszek Koltunski
  public long brightness(Data1D brightness)
855 6a06a912 Leszek Koltunski
    {
856 2fce34f4 Leszek Koltunski
    return mF.add(EffectNames.BRIGHTNESS, brightness);
857 6a06a912 Leszek Koltunski
    }
858
859
///////////////////////////////////////////////////////////////////////////////////////////////////
860
/**
861 e0a16874 Leszek Koltunski
 * Makes a certain sub-region of the Object smoothly change its contrast level.
862 6a06a912 Leszek Koltunski
 *        
863 2fce34f4 Leszek Koltunski
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
864 e4878781 Leszek Koltunski
 *                 at any given moment. Valid range: <0,infinity)
865 e0a16874 Leszek Koltunski
 * @param region   Region this Effect is limited to.
866 2fce34f4 Leszek Koltunski
 * @param smooth   If true, the level of 'contrast' will smoothly fade out towards the edges of the region.
867 e0a16874 Leszek Koltunski
 * @return         ID of the effect added, or -1 if we failed to add one.
868 6a06a912 Leszek Koltunski
 */
869 2fce34f4 Leszek Koltunski
  public long contrast(Data1D contrast, Data4D region, boolean smooth)
870 6a06a912 Leszek Koltunski
    {
871 2fce34f4 Leszek Koltunski
    return mF.add( smooth ? EffectNames.SMOOTH_CONTRAST:EffectNames.CONTRAST, contrast, region);
872 6a06a912 Leszek Koltunski
    }
873
874
///////////////////////////////////////////////////////////////////////////////////////////////////
875
/**
876 2fce34f4 Leszek Koltunski
 * Makes the whole Object smoothly change its contrast level.
877
 *
878
 * @param contrast 1-dimensional Data that returns the level of contrast we want to have
879 e4878781 Leszek Koltunski
 *                 at any given moment. Valid range: <0,infinity)
880 e0a16874 Leszek Koltunski
 * @return         ID of the effect added, or -1 if we failed to add one.
881 6a06a912 Leszek Koltunski
 */
882 2fce34f4 Leszek Koltunski
  public long contrast(Data1D contrast)
883 6a06a912 Leszek Koltunski
    {
884 2fce34f4 Leszek Koltunski
    return mF.add(EffectNames.CONTRAST, contrast);
885 6a06a912 Leszek Koltunski
    }
886
887
///////////////////////////////////////////////////////////////////////////////////////////////////
888
/**
889 e0a16874 Leszek Koltunski
 * Makes a certain sub-region of the Object smoothly change its saturation level.
890 6a06a912 Leszek Koltunski
 *        
891 2fce34f4 Leszek Koltunski
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
892 e4878781 Leszek Koltunski
 *                   at any given moment. Valid range: <0,infinity)
893 e0a16874 Leszek Koltunski
 * @param region     Region this Effect is limited to.
894 2fce34f4 Leszek Koltunski
 * @param smooth     If true, the level of 'saturation' will smoothly fade out towards the edges of the region.
895 e0a16874 Leszek Koltunski
 * @return           ID of the effect added, or -1 if we failed to add one.
896 6a06a912 Leszek Koltunski
 */
897 2fce34f4 Leszek Koltunski
  public long saturation(Data1D saturation, Data4D region, boolean smooth)
898 6a06a912 Leszek Koltunski
    {
899 2fce34f4 Leszek Koltunski
    return mF.add( smooth ? EffectNames.SMOOTH_SATURATION:EffectNames.SATURATION, saturation, region);
900 6a06a912 Leszek Koltunski
    }
901
902
///////////////////////////////////////////////////////////////////////////////////////////////////
903
/**
904 2fce34f4 Leszek Koltunski
 * Makes the whole Object smoothly change its saturation level.
905
 *
906
 * @param saturation 1-dimensional Data that returns the level of saturation we want to have
907 e4878781 Leszek Koltunski
 *                   at any given moment. Valid range: <0,infinity)
908 e0a16874 Leszek Koltunski
 * @return           ID of the effect added, or -1 if we failed to add one.
909 6a06a912 Leszek Koltunski
 */
910 2fce34f4 Leszek Koltunski
  public long saturation(Data1D saturation)
911 6a06a912 Leszek Koltunski
    {
912 2fce34f4 Leszek Koltunski
    return mF.add(EffectNames.SATURATION, saturation);
913 6a06a912 Leszek Koltunski
    }
914
915
///////////////////////////////////////////////////////////////////////////////////////////////////
916
// Vertex-based effects  
917
///////////////////////////////////////////////////////////////////////////////////////////////////
918
/**
919 e0a16874 Leszek Koltunski
 * Distort a (possibly changing in time) part of the Object by a (possibly changing in time) vector of force.
920 f2fe7e28 Leszek Koltunski
 *
921
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
922
 *               currently being dragged with.
923 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
924 f2fe7e28 Leszek Koltunski
 * @param region Region that masks the Effect.
925
 * @return       ID of the effect added, or -1 if we failed to add one.
926 6a06a912 Leszek Koltunski
 */
927 fa6c352d Leszek Koltunski
  public long distort(Data3D vector, Data3D center, Data4D region)
928 6a06a912 Leszek Koltunski
    {  
929 f2fe7e28 Leszek Koltunski
    return mV.add(EffectNames.DISTORT, vector, center, region);
930 6a06a912 Leszek Koltunski
    }
931
932
///////////////////////////////////////////////////////////////////////////////////////////////////
933
/**
934 e0a16874 Leszek Koltunski
 * Distort the whole Object by a (possibly changing in time) vector of force.
935 f2fe7e28 Leszek Koltunski
 *
936
 * @param vector 3-dimensional Vector which represents the force the Center of the Effect is
937
 *               currently being dragged with.
938 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
939 e0a16874 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
940 6a06a912 Leszek Koltunski
 */
941 fa6c352d Leszek Koltunski
  public long distort(Data3D vector, Data3D center)
942 d425545a Leszek Koltunski
    {
943 02ef26bc Leszek Koltunski
    return mV.add(EffectNames.DISTORT, vector, center, null);
944 d425545a Leszek Koltunski
    }
945 6a06a912 Leszek Koltunski
946 6ebdbbf1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
947
/**
948
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
949
 * a (possibly changing in time) point on the Object.
950
 *
951
 * @param vector Vector of force that deforms the shape of the whole Object.
952
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
953
 * @param region Region that masks the Effect.
954
 * @return       ID of the effect added, or -1 if we failed to add one.
955
 */
956
  public long deform(Data3D vector, Data3D center, Data4D region)
957
    {
958
    return mV.add(EffectNames.DEFORM, vector, center, region);
959
    }
960
961 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
962
/**
963 e0a16874 Leszek Koltunski
 * Deform the shape of the whole Object with a (possibly changing in time) vector of force applied to
964 9351ad55 Leszek Koltunski
 * a (possibly changing in time) point on the Object.
965 6a06a912 Leszek Koltunski
 *     
966 f2fe7e28 Leszek Koltunski
 * @param vector Vector of force that deforms the shape of the whole Object.
967 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
968 e0a16874 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
969 6a06a912 Leszek Koltunski
 */
970 fa6c352d Leszek Koltunski
  public long deform(Data3D vector, Data3D center)
971 6a06a912 Leszek Koltunski
    {  
972 02ef26bc Leszek Koltunski
    return mV.add(EffectNames.DEFORM, vector, center, null);
973 6a06a912 Leszek Koltunski
    }
974
975
///////////////////////////////////////////////////////////////////////////////////////////////////  
976
/**
977 f2fe7e28 Leszek Koltunski
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
978 6a06a912 Leszek Koltunski
 * away from the center (degree<=1)
979 f2fe7e28 Leszek Koltunski
 *
980
 * @param sink   The current degree of the Effect.
981 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
982 f2fe7e28 Leszek Koltunski
 * @param region Region that masks the Effect.
983
 * @return       ID of the effect added, or -1 if we failed to add one.
984 6a06a912 Leszek Koltunski
 */
985 fa6c352d Leszek Koltunski
  public long sink(Data1D sink, Data3D center, Data4D region)
986 6a06a912 Leszek Koltunski
    {
987 f2fe7e28 Leszek Koltunski
    return mV.add(EffectNames.SINK, sink, center, region);
988 6a06a912 Leszek Koltunski
    }
989
990
///////////////////////////////////////////////////////////////////////////////////////////////////
991
/**
992 f2fe7e28 Leszek Koltunski
 * Pull all points around the center of the Effect towards the center (if degree>=1) or push them
993
 * away from the center (degree<=1)
994
 *
995
 * @param sink   The current degree of the Effect.
996 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
997 f2fe7e28 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
998 6a06a912 Leszek Koltunski
 */
999 fa6c352d Leszek Koltunski
  public long sink(Data1D sink, Data3D center)
1000 d425545a Leszek Koltunski
    {
1001
    return mV.add(EffectNames.SINK, sink, center);
1002
    }
1003 6a06a912 Leszek Koltunski
1004 82ee855a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
1005
/**
1006
 * Pull all points around the center of the Effect towards a line passing through the center
1007
 * (that's if degree>=1) or push them away from the line (degree<=1)
1008
 *
1009
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
1010
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1011
 * @param region Region that masks the Effect.
1012
 * @return       ID of the effect added, or -1 if we failed to add one.
1013
 */
1014
  public long pinch(Data2D pinch, Data3D center, Data4D region)
1015
    {
1016
    return mV.add(EffectNames.PINCH, pinch, center, region);
1017
    }
1018
1019
///////////////////////////////////////////////////////////////////////////////////////////////////
1020
/**
1021
 * Pull all points around the center of the Effect towards a line passing through the center
1022
 * (that's if degree>=1) or push them away from the line (degree<=1)
1023
 *
1024
 * @param pinch  The current degree of the Effect + angle the line forms with X-axis
1025
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1026
 * @return       ID of the effect added, or -1 if we failed to add one.
1027
 */
1028
  public long pinch(Data2D pinch, Data3D center)
1029
    {
1030
    return mV.add(EffectNames.PINCH, pinch, center);
1031
    }
1032
1033 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////  
1034
/**
1035 f2fe7e28 Leszek Koltunski
 * Rotate part of the Object around the Center of the Effect by a certain angle.
1036
 *
1037 4fde55a0 Leszek Koltunski
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
1038 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1039 f2fe7e28 Leszek Koltunski
 * @param region Region that masks the Effect.
1040
 * @return       ID of the effect added, or -1 if we failed to add one.
1041 6a06a912 Leszek Koltunski
 */
1042 fa6c352d Leszek Koltunski
  public long swirl(Data1D swirl, Data3D center, Data4D region)
1043 6a06a912 Leszek Koltunski
    {    
1044 f2fe7e28 Leszek Koltunski
    return mV.add(EffectNames.SWIRL, swirl, center, region);
1045 6a06a912 Leszek Koltunski
    }
1046
1047
///////////////////////////////////////////////////////////////////////////////////////////////////
1048
/**
1049 f2fe7e28 Leszek Koltunski
 * Rotate the whole Object around the Center of the Effect by a certain angle.
1050
 *
1051 4fde55a0 Leszek Koltunski
 * @param swirl  The angle of Swirl (in degrees). Positive values swirl clockwise.
1052 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1053 f2fe7e28 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
1054 6a06a912 Leszek Koltunski
 */
1055 fa6c352d Leszek Koltunski
  public long swirl(Data1D swirl, Data3D center)
1056 d425545a Leszek Koltunski
    {
1057
    return mV.add(EffectNames.SWIRL, swirl, center);
1058
    }
1059 4fde55a0 Leszek Koltunski
1060
///////////////////////////////////////////////////////////////////////////////////////////////////
1061
/**
1062
 * Directional, sinusoidal wave effect.
1063
 *
1064 350cc2f5 Leszek Koltunski
 * @param wave   A 5-dimensional data structure describing the wave: first member is the amplitude,
1065 ea16dc89 Leszek Koltunski
 *               second is the wave length, third is the phase (i.e. when phase = PI/2, the sine
1066 350cc2f5 Leszek Koltunski
 *               wave at the center does not start from sin(0), but from sin(PI/2) ) and the next two
1067
 *               describe the 'direction' of the wave.
1068 3695d6fa Leszek Koltunski
 *               <p>
1069 d0c902b8 Leszek Koltunski
 *               Wave direction is defined to be a 3D vector of length 1. To define such vectors, we
1070
 *               need 2 floats: thus the third member is the angle Alpha (in degrees) which the vector
1071
 *               forms with the XY-plane, and the fourth is the angle Beta (again in degrees) which
1072
 *               the projection of the vector to the XY-plane forms with the Y-axis (counterclockwise).
1073 3695d6fa Leszek Koltunski
 *               <p>
1074
 *               <p>
1075 d0c902b8 Leszek Koltunski
 *               Example1: if Alpha = 90, Beta = 90, (then V=(0,0,1) ) and the wave acts 'vertically'
1076
 *               in the X-direction, i.e. cross-sections of the resulting surface with the XZ-plane
1077
 *               will be sine shapes.
1078 3695d6fa Leszek Koltunski
 *               <p>
1079 d0c902b8 Leszek Koltunski
 *               Example2: if Alpha = 90, Beta = 0, the again V=(0,0,1) and the wave is 'vertical',
1080
 *               but this time it waves in the Y-direction, i.e. cross sections of the surface and the
1081
 *               YZ-plane with be sine shapes.
1082 3695d6fa Leszek Koltunski
 *               <p>
1083 d0c902b8 Leszek Koltunski
 *               Example3: if Alpha = 0 and Beta = 45, then V=(sqrt(2)/2, -sqrt(2)/2, 0) and the wave
1084 350cc2f5 Leszek Koltunski
 *               is entirely 'horizontal' and moves point (x,y,0) in direction V by whatever is the
1085
 *               value if sin at this point.
1086 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1087 4fde55a0 Leszek Koltunski
 * @return       ID of the effect added, or -1 if we failed to add one.
1088
 */
1089 fa6c352d Leszek Koltunski
  public long wave(Data5D wave, Data3D center)
1090 4fde55a0 Leszek Koltunski
    {
1091 02ef26bc Leszek Koltunski
    return mV.add(EffectNames.WAVE, wave, center, null);
1092 4fde55a0 Leszek Koltunski
    }
1093
1094
///////////////////////////////////////////////////////////////////////////////////////////////////
1095
/**
1096
 * Directional, sinusoidal wave effect.
1097
 *
1098 421c2728 Leszek Koltunski
 * @param wave   see {@link DistortedEffects#wave(Data5D,Data3D)}
1099 fa6c352d Leszek Koltunski
 * @param center 3-dimensional Data that, at any given time, returns the Center of the Effect.
1100 4fde55a0 Leszek Koltunski
 * @param region Region that masks the Effect.
1101
 * @return       ID of the effect added, or -1 if we failed to add one.
1102
 */
1103 fa6c352d Leszek Koltunski
  public long wave(Data5D wave, Data3D center, Data4D region)
1104 4fde55a0 Leszek Koltunski
    {
1105
    return mV.add(EffectNames.WAVE, wave, center, region);
1106
    }
1107 d6e94c84 Leszek Koltunski
1108
///////////////////////////////////////////////////////////////////////////////////////////////////
1109
// Postprocess-based effects
1110
///////////////////////////////////////////////////////////////////////////////////////////////////
1111
/**
1112
 * Blur the object.
1113
 *
1114
 * @param radius The 'strength' if the effect, in pixels. 0 = no blur, 10 = when blurring a given pixel,
1115
 *               take into account 10 pixels in each direction.
1116
 * @return ID of the effect added, or -1 if we failed to add one.
1117
 */
1118 85cbbc5e Leszek Koltunski
  public long blur(Data1D radius)
1119 d6e94c84 Leszek Koltunski
    {
1120 85cbbc5e Leszek Koltunski
    return mP.add(EffectNames.BLUR, radius);
1121 d6e94c84 Leszek Koltunski
    }
1122 f2fe7e28 Leszek Koltunski
  }