Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffects.java @ 60c1c622

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