Project

General

Profile

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

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

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