Project

General

Profile

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

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

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