Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedEffects.java @ 03cb451d

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