Project

General

Profile

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

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

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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 c90b9e01 Leszek Koltunski
import android.content.res.Resources;
23 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
24
import android.util.Log;
25 6a06a912 Leszek Koltunski
26 fe82a979 Leszek Koltunski
import org.distorted.library.R;
27
import org.distorted.library.effect.Effect;
28 da9b3f07 Leszek Koltunski
import org.distorted.library.effect.EffectName;
29
import org.distorted.library.effect.EffectType;
30 7cd24173 leszek
import org.distorted.library.effect.FragmentEffect;
31
import org.distorted.library.effect.VertexEffect;
32 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectListener;
33 55c14a19 Leszek Koltunski
import org.distorted.library.program.DistortedProgram;
34 c90b9e01 Leszek Koltunski
import org.distorted.library.program.FragmentCompilationException;
35
import org.distorted.library.program.FragmentUniformsException;
36
import org.distorted.library.program.LinkingException;
37
import org.distorted.library.program.VertexCompilationException;
38
import org.distorted.library.program.VertexUniformsException;
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 faa3ff56 Leszek Koltunski
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
48 b73dcaa7 Leszek Koltunski
 * <p>
49 faa3ff56 Leszek Koltunski
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
50 b329f352 Leszek Koltunski
 */
51 86d322b5 Leszek Koltunski
public class DistortedEffects
52 d425545a Leszek Koltunski
  {
53 1aedf874 leszek
  /// MAIN PROGRAM ///
54
  private static DistortedProgram mMainProgram;
55
  private static int mMainTextureH;
56 12f45260 Leszek Koltunski
  private static int mCountIndexH;
57 8fa96e69 Leszek Koltunski
58 c1e24646 leszek
  /// BLIT PROGRAM ///
59
  private static DistortedProgram mBlitProgram;
60
  private static int mBlitTextureH;
61 c2c08950 leszek
  private static int mBlitDepthH;
62 c90b9e01 Leszek Koltunski
  private static final FloatBuffer mQuadPositions;
63
64
  static
65
    {
66
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
67
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
68
    mQuadPositions.put(positionData).position(0);
69
    }
70
71 7170e4eb leszek
  /// BLIT DEPTH PROGRAM ///
72
  private static DistortedProgram mBlitDepthProgram;
73
  private static int mBlitDepthTextureH;
74
  private static int mBlitDepthDepthTextureH;
75
  private static int mBlitDepthDepthH;
76 ae2802b1 Leszek Koltunski
  private static int mBlitDepthTexCorrH;
77 7170e4eb leszek
78 3fc9327a Leszek Koltunski
  /// NORMAL PROGRAM /////
79
  private static DistortedProgram mNormalProgram;
80
  private static int mNormalMVPMatrixH;
81
  /// END PROGRAMS //////
82 c638c1b0 Leszek Koltunski
83 8e34674e Leszek Koltunski
  private static long mNextID =0;
84 4e2382f3 Leszek Koltunski
  private long mID;
85 8e34674e Leszek Koltunski
86 310e14fb leszek
  private EffectQueueMatrix mM;
87 3fc9327a Leszek Koltunski
  private EffectQueueFragment mF;
88 310e14fb leszek
  private EffectQueueVertex mV;
89 80b3acf6 Leszek Koltunski
  private EffectQueuePostprocess mP;
90
91
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
92 3d590d8d Leszek Koltunski
93 9361b337 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
94 55c14a19 Leszek Koltunski
95 c90b9e01 Leszek Koltunski
  static void createProgram(Resources resources)
96
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
97 55c14a19 Leszek Koltunski
    {
98 cab7c165 Leszek Koltunski
    // MAIN PROGRAM ////////////////////////////////////
99 1aedf874 leszek
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
100
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
101
102 7cd24173 leszek
    int numF = FragmentEffect.getNumEnabled();
103
    int numV = VertexEffect.getNumEnabled();
104 c90b9e01 Leszek Koltunski
105 7cd24173 leszek
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
106
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
107
    String enabledEffectV= VertexEffect.getGLSL();
108
    String enabledEffectF= FragmentEffect.getGLSL();
109 c90b9e01 Leszek Koltunski
110 03cb451d Leszek Koltunski
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
111
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
112 7cd24173 leszek
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
113
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
114 c90b9e01 Leszek Koltunski
115 3fc9327a Leszek Koltunski
    String[] feedback = { "v_Position", "v_endPosition" };
116 cab7c165 Leszek Koltunski
117 7cd24173 leszek
    mMainProgram = new DistortedProgram( mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
118
                                         enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
119 c90b9e01 Leszek Koltunski
120 c1e24646 leszek
    int mainProgramH = mMainProgram.getProgramHandle();
121 c90b9e01 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainProgramH);
122 1aedf874 leszek
    EffectQueueVertex.getUniforms(mainProgramH);
123
    EffectQueueMatrix.getUniforms(mainProgramH);
124 e6519ac8 Leszek Koltunski
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
125 12f45260 Leszek Koltunski
    mCountIndexH = GLES31.glGetUniformLocation( mainProgramH, "u_currentIndex");
126 c1e24646 leszek
127 1aedf874 leszek
    // BLIT PROGRAM ////////////////////////////////////
128 c1e24646 leszek
    final InputStream blitVertStream = resources.openRawResource(R.raw.blit_vertex_shader);
129
    final InputStream blitFragStream = resources.openRawResource(R.raw.blit_fragment_shader);
130
131 94f6d472 Leszek Koltunski
    String blitVertHeader= (Distorted.GLSL_VERSION + "#define NUM_VERTEX 0\n"  );
132
    String blitFragHeader= (Distorted.GLSL_VERSION + "#define NUM_FRAGMENT 0\n");
133 c1e24646 leszek
134 f2367b75 Leszek Koltunski
    try
135
      {
136 94f6d472 Leszek Koltunski
      mBlitProgram = new DistortedProgram(blitVertStream,blitFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
137 f2367b75 Leszek Koltunski
      }
138
    catch(Exception e)
139
      {
140 e6519ac8 Leszek Koltunski
      Log.e("EFFECTS", "exception trying to compile BLIT program: "+e.getMessage());
141 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
142
      }
143 c1e24646 leszek
144
    int blitProgramH = mBlitProgram.getProgramHandle();
145 e6519ac8 Leszek Koltunski
    mBlitTextureH  = GLES31.glGetUniformLocation( blitProgramH, "u_Texture");
146
    mBlitDepthH    = GLES31.glGetUniformLocation( blitProgramH, "u_Depth");
147 c90b9e01 Leszek Koltunski
148 7170e4eb leszek
    // BLIT DEPTH PROGRAM ////////////////////////////////////
149
    final InputStream blitDepthVertStream = resources.openRawResource(R.raw.blit_depth_vertex_shader);
150
    final InputStream blitDepthFragStream = resources.openRawResource(R.raw.blit_depth_fragment_shader);
151
152
    try
153
      {
154
      mBlitDepthProgram = new DistortedProgram(blitDepthVertStream,blitDepthFragStream,blitVertHeader,blitFragHeader, Distorted.GLSL);
155
      }
156
    catch(Exception e)
157
      {
158 e6519ac8 Leszek Koltunski
      Log.e("EFFECTS", "exception trying to compile BLIT DEPTH program: "+e.getMessage());
159 7170e4eb leszek
      throw new RuntimeException(e.getMessage());
160
      }
161
162
    int blitDepthProgramH   = mBlitDepthProgram.getProgramHandle();
163 e6519ac8 Leszek Koltunski
    mBlitDepthTextureH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Texture");
164
    mBlitDepthDepthTextureH = GLES31.glGetUniformLocation( blitDepthProgramH, "u_DepthTexture");
165
    mBlitDepthDepthH        = GLES31.glGetUniformLocation( blitDepthProgramH, "u_Depth");
166 ae2802b1 Leszek Koltunski
    mBlitDepthTexCorrH      = GLES31.glGetUniformLocation( blitDepthProgramH, "u_TexCorr");
167 7170e4eb leszek
168 3fc9327a Leszek Koltunski
    // NORMAL PROGRAM //////////////////////////////////////
169
    final InputStream normalVertexStream   = resources.openRawResource(R.raw.normal_vertex_shader);
170
    final InputStream normalFragmentStream = resources.openRawResource(R.raw.normal_fragment_shader);
171 c90b9e01 Leszek Koltunski
172 f2367b75 Leszek Koltunski
    try
173
      {
174 3fc9327a Leszek Koltunski
      mNormalProgram = new DistortedProgram(normalVertexStream,normalFragmentStream, Distorted.GLSL_VERSION, Distorted.GLSL_VERSION, Distorted.GLSL);
175 f2367b75 Leszek Koltunski
      }
176
    catch(Exception e)
177
      {
178 e6519ac8 Leszek Koltunski
      Log.e("EFFECTS", "exception trying to compile NORMAL program: "+e.getMessage());
179 f2367b75 Leszek Koltunski
      throw new RuntimeException(e.getMessage());
180
      }
181 c90b9e01 Leszek Koltunski
182 3fc9327a Leszek Koltunski
    int normalProgramH = mNormalProgram.getProgramHandle();
183 e6519ac8 Leszek Koltunski
    mNormalMVPMatrixH  = GLES31.glGetUniformLocation( normalProgramH, "u_MVPMatrix");
184 55c14a19 Leszek Koltunski
    }
185
186
///////////////////////////////////////////////////////////////////////////////////////////////////
187
188 421c2728 Leszek Koltunski
  private void initializeEffectLists(DistortedEffects d, int flags)
189 d425545a Leszek Koltunski
    {
190 015642fb Leszek Koltunski
    if( (flags & Distorted.CLONE_MATRIX) != 0 )
191 6a06a912 Leszek Koltunski
      {
192 d425545a Leszek Koltunski
      mM = d.mM;
193
      matrixCloned = true;
194
      }
195
    else
196
      {
197 4e2382f3 Leszek Koltunski
      mM = new EffectQueueMatrix(mID);
198 d425545a Leszek Koltunski
      matrixCloned = false;
199
      }
200 6a06a912 Leszek Koltunski
    
201 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_VERTEX) != 0 )
202
      {
203
      mV = d.mV;
204
      vertexCloned = true;
205
      }
206
    else
207
      {
208 4e2382f3 Leszek Koltunski
      mV = new EffectQueueVertex(mID);
209 d425545a Leszek Koltunski
      vertexCloned = false;
210
      }
211 6a06a912 Leszek Koltunski
    
212 d425545a Leszek Koltunski
    if( (flags & Distorted.CLONE_FRAGMENT) != 0 )
213
      {
214
      mF = d.mF;
215
      fragmentCloned = true;
216 6a06a912 Leszek Koltunski
      }
217 d425545a Leszek Koltunski
    else
218
      {
219 4e2382f3 Leszek Koltunski
      mF = new EffectQueueFragment(mID);
220 d425545a Leszek Koltunski
      fragmentCloned = false;
221
      }
222 80b3acf6 Leszek Koltunski
223
    if( (flags & Distorted.CLONE_POSTPROCESS) != 0 )
224
      {
225
      mP = d.mP;
226
      postprocessCloned = true;
227
      }
228
    else
229
      {
230
      mP = new EffectQueuePostprocess(mID);
231
      postprocessCloned = false;
232
      }
233
    }
234
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236
237 70b6a155 Leszek Koltunski
  EffectQueuePostprocess getPostprocess()
238 80b3acf6 Leszek Koltunski
    {
239 70b6a155 Leszek Koltunski
    return mP;
240 80b3acf6 Leszek Koltunski
    }
241
242 26a4e5f6 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
243
244
  void newNode(DistortedNode node)
245
    {
246
    mM.newNode(node);
247
    mF.newNode(node);
248
    mV.newNode(node);
249
    mP.newNode(node);
250
    }
251
252 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
253 c90b9e01 Leszek Koltunski
254 3fc9327a Leszek Koltunski
  private void displayNormals(MeshObject mesh)
255 c90b9e01 Leszek Koltunski
    {
256 e6519ac8 Leszek Koltunski
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, mesh.mAttTFO[0]);
257
    GLES31.glBeginTransformFeedback( GLES31.GL_POINTS);
258 604b2899 Leszek Koltunski
    DistortedRenderState.switchOffDrawing();
259 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays( GLES31.GL_POINTS, 0, mesh.numVertices);
260 604b2899 Leszek Koltunski
    DistortedRenderState.restoreDrawing();
261 e6519ac8 Leszek Koltunski
    GLES31.glEndTransformFeedback();
262
    GLES31.glBindBufferBase(GLES31.GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
263 604b2899 Leszek Koltunski
264 3fc9327a Leszek Koltunski
    mNormalProgram.useProgram();
265 e6519ac8 Leszek Koltunski
    GLES31.glUniformMatrix4fv(mNormalMVPMatrixH, 1, false, mM.getMVP() , 0);
266
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttTFO[0]);
267
    GLES31.glVertexAttribPointer(mNormalProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, 0);
268
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
269
    GLES31.glLineWidth(8.0f);
270
    GLES31.glDrawArrays(GLES31.GL_LINES, 0, 2*mesh.numVertices);
271 c90b9e01 Leszek Koltunski
    }
272 1aedf874 leszek
273 c90b9e01 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
274
275 270c27bc Leszek Koltunski
  void drawPriv(float halfW, float halfH, MeshObject mesh, DistortedOutputSurface surface, long currTime, float marginInPixels)
276 d425545a Leszek Koltunski
    {
277 c1e24646 leszek
    float halfZ = halfW*mesh.zFactor;
278 638b5b5c leszek
279 8fbd0237 Leszek Koltunski
    mM.compute(currTime);
280
    mV.compute(currTime,halfW,halfH,halfZ);
281
    mF.compute(currTime,halfW,halfH);
282 1149be8f leszek
    mP.compute(currTime);
283 8fbd0237 Leszek Koltunski
284 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
285 d6e94c84 Leszek Koltunski
286 b9798977 leszek
    mMainProgram.useProgram();
287 e6519ac8 Leszek Koltunski
    GLES31.glUniform1i(mMainTextureH, 0);
288 8a57da61 Leszek Koltunski
    GLES31.glUniform1i(mCountIndexH, surface.getNewCounter() );
289 12f45260 Leszek Koltunski
290 420836fc leszek
    if( Distorted.GLSL >= 300 )
291
      {
292 e6519ac8 Leszek Koltunski
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
293
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
294
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
295
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
296
      GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
297 420836fc leszek
      }
298
    else
299
      {
300
      mesh.mVertAttribs.position(0);
301 e6519ac8 Leszek Koltunski
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
302 420836fc leszek
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE);
303 e6519ac8 Leszek Koltunski
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
304 420836fc leszek
      mesh.mVertAttribs.position(MeshObject.POS_DATA_SIZE+MeshObject.NOR_DATA_SIZE);
305 e6519ac8 Leszek Koltunski
      GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, mesh.mVertAttribs);
306 420836fc leszek
      }
307 cab7c165 Leszek Koltunski
308 270c27bc Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
309 8fbd0237 Leszek Koltunski
    mV.send();
310
    mF.send();
311 42571056 Leszek Koltunski
312 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
313 c90b9e01 Leszek Koltunski
314 3fc9327a Leszek Koltunski
    if( mesh.mShowNormals ) displayNormals(mesh);
315 d425545a Leszek Koltunski
    }
316 6a06a912 Leszek Koltunski
317
///////////////////////////////////////////////////////////////////////////////////////////////////
318 7266d8ef Leszek Koltunski
/**
319
 * Only for use by the library itself.
320
 *
321
 * @y.exclude
322
 */
323
  public static void blitPriv(DistortedOutputSurface surface)
324 d425545a Leszek Koltunski
    {
325 c1e24646 leszek
    mBlitProgram.useProgram();
326
327 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
328
    GLES31.glUniform1i(mBlitTextureH, 0);
329
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
330
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
331
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
332 d425545a Leszek Koltunski
    }
333 7170e4eb leszek
334
///////////////////////////////////////////////////////////////////////////////////////////////////
335
336 ae2802b1 Leszek Koltunski
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
337 7170e4eb leszek
    {
338
    mBlitDepthProgram.useProgram();
339
340 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
341
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
342
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
343 ae2802b1 Leszek Koltunski
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
344 e6519ac8 Leszek Koltunski
    GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
345
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
346
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
347 7170e4eb leszek
    }
348
349 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
350
   
351 8e34674e Leszek Koltunski
  private void releasePriv()
352 d425545a Leszek Koltunski
    {
353 80b3acf6 Leszek Koltunski
    if( !matrixCloned   )   mM.abortAll(false);
354
    if( !vertexCloned   )   mV.abortAll(false);
355
    if( !fragmentCloned )   mF.abortAll(false);
356
    if( !postprocessCloned) mP.abortAll(false);
357 d425545a Leszek Koltunski
358 4e2382f3 Leszek Koltunski
    mM = null;
359
    mV = null;
360
    mF = null;
361 80b3acf6 Leszek Koltunski
    mP = null;
362 8e34674e Leszek Koltunski
    }
363
364 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
365
366 7b8086eb Leszek Koltunski
  static void onDestroy()
367 1942537e Leszek Koltunski
    {
368
    mNextID = 0;
369
    }
370
371 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
372
// PUBLIC API
373 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
374 d425545a Leszek Koltunski
/**
375 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
376 d425545a Leszek Koltunski
 */
377 421c2728 Leszek Koltunski
  public DistortedEffects()
378 d425545a Leszek Koltunski
    {
379 c7da4e65 leszek
    mID = ++mNextID;
380 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
381 d425545a Leszek Koltunski
    }
382 ada90d33 Leszek Koltunski
383
///////////////////////////////////////////////////////////////////////////////////////////////////
384 d425545a Leszek Koltunski
/**
385 4e2382f3 Leszek Koltunski
 * Copy constructor.
386 d425545a Leszek Koltunski
 * <p>
387
 * Whatever we do not clone gets created just like in the default constructor.
388
 *
389
 * @param dc    Source object to create our object from
390
 * @param flags A bitmask of values specifying what to copy.
391 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
392 d425545a Leszek Koltunski
 */
393 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
394 d425545a Leszek Koltunski
    {
395 c7da4e65 leszek
    mID = ++mNextID;
396 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
397 d425545a Leszek Koltunski
    }
398 ada90d33 Leszek Koltunski
399 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
400
/**
401 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
402 6a06a912 Leszek Koltunski
 */
403 13687207 leszek
  @SuppressWarnings("unused")
404 8e34674e Leszek Koltunski
  public synchronized void delete()
405 d425545a Leszek Koltunski
    {
406
    releasePriv();
407
    }
408 6a06a912 Leszek Koltunski
409
///////////////////////////////////////////////////////////////////////////////////////////////////
410
/**
411 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
412
 *
413
 * @return ID of the object.
414 6a06a912 Leszek Koltunski
 */
415 4e2382f3 Leszek Koltunski
  public long getID()
416 d425545a Leszek Koltunski
      {
417 4e2382f3 Leszek Koltunski
      return mID;
418 d425545a Leszek Koltunski
      }
419 4e2382f3 Leszek Koltunski
420 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
421
/**
422
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
423 faa3ff56 Leszek Koltunski
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
424 6a06a912 Leszek Koltunski
 * 
425
 * @param el A class implementing the EffectListener interface that wants to get notifications.
426
 */
427 13687207 leszek
  @SuppressWarnings("unused")
428 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
429 d425545a Leszek Koltunski
    {
430 26a4e5f6 leszek
    mM.registerForMessages(el);
431 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
432
    mF.registerForMessages(el);
433 80b3acf6 Leszek Koltunski
    mP.registerForMessages(el);
434 d425545a Leszek Koltunski
    }
435 6a06a912 Leszek Koltunski
436
///////////////////////////////////////////////////////////////////////////////////////////////////
437
/**
438 faa3ff56 Leszek Koltunski
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
439 6a06a912 Leszek Koltunski
 * 
440
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
441
 */
442 13687207 leszek
  @SuppressWarnings("unused")
443 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
444 d425545a Leszek Koltunski
    {
445 26a4e5f6 leszek
    mM.deregisterForMessages(el);
446 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
447
    mF.deregisterForMessages(el);
448 80b3acf6 Leszek Koltunski
    mP.deregisterForMessages(el);
449 d425545a Leszek Koltunski
    }
450 6a06a912 Leszek Koltunski
451
///////////////////////////////////////////////////////////////////////////////////////////////////
452
/**
453 d07f2950 Leszek Koltunski
 * Aborts all Effects.
454
 * @return Number of effects aborted.
455 6a06a912 Leszek Koltunski
 */
456 d425545a Leszek Koltunski
  public int abortAllEffects()
457 13687207 leszek
    {
458
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
459
    }
460 6a06a912 Leszek Koltunski
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
/**
463 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
464 6a06a912 Leszek Koltunski
 * 
465 da9b3f07 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectType}
466 d07f2950 Leszek Koltunski
 * @return Number of effects aborted.
467 6a06a912 Leszek Koltunski
 */
468 da9b3f07 Leszek Koltunski
  public int abortByType(EffectType type)
469 d425545a Leszek Koltunski
    {
470
    switch(type)
471 6a06a912 Leszek Koltunski
      {
472 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
473
      case VERTEX     : return mV.abortAll(true);
474
      case FRAGMENT   : return mF.abortAll(true);
475 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.abortAll(true);
476 da9b3f07 Leszek Koltunski
      default         : return 0;
477 6a06a912 Leszek Koltunski
      }
478 d425545a Leszek Koltunski
    }
479 47316d20 leszek
480
///////////////////////////////////////////////////////////////////////////////////////////////////
481
/**
482 faa3ff56 Leszek Koltunski
 * Aborts an Effect by its ID.
483 47316d20 leszek
 *
484
 * @param id the Id of the Effect to be removed, as returned by getID().
485
 * @return Number of effects aborted.
486
 */
487
  public int abortById(long id)
488
    {
489
    long type = id&EffectType.MASK;
490
491 2ef5dd9e leszek
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
492
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
493
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
494 80b3acf6 Leszek Koltunski
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
495 47316d20 leszek
496
    return 0;
497
    }
498
499 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
500
/**
501
 * Aborts a single Effect.
502
 * 
503 6bb59aad Leszek Koltunski
 * @param effect the Effect we want to abort.
504 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
505 6a06a912 Leszek Koltunski
 */
506 6bb59aad Leszek Koltunski
  public int abortEffect(Effect effect)
507 d425545a Leszek Koltunski
    {
508 6bb59aad Leszek Koltunski
    switch(effect.getType())
509
      {
510 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeEffect(effect);
511
      case VERTEX     : return mV.removeEffect(effect);
512
      case FRAGMENT   : return mF.removeEffect(effect);
513 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeEffect(effect);
514 da9b3f07 Leszek Koltunski
      default         : return 0;
515 6bb59aad Leszek Koltunski
      }
516 d425545a Leszek Koltunski
    }
517 6a06a912 Leszek Koltunski
518
///////////////////////////////////////////////////////////////////////////////////////////////////
519
/**
520 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
521 6a06a912 Leszek Koltunski
 * 
522 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
523 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
524 6a06a912 Leszek Koltunski
 */
525 da9b3f07 Leszek Koltunski
  public int abortByName(EffectName name)
526 d425545a Leszek Koltunski
    {
527 da9b3f07 Leszek Koltunski
    switch(name.getType())
528 6a06a912 Leszek Koltunski
      {
529 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeByName(name);
530
      case VERTEX     : return mV.removeByName(name);
531
      case FRAGMENT   : return mF.removeByName(name);
532 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeByName(name);
533 6bb59aad Leszek Koltunski
      default                : return 0;
534 6a06a912 Leszek Koltunski
      }
535 d425545a Leszek Koltunski
    }
536 432442f9 Leszek Koltunski
537
///////////////////////////////////////////////////////////////////////////////////////////////////
538
/**
539 faa3ff56 Leszek Koltunski
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
540
 * single (InputSurface,MeshObject) combo.
541 432442f9 Leszek Koltunski
 *
542 fe6fe99a leszek
 * @param type {@link EffectType}
543
 * @return The maximum number of effects of a given type.
544 432442f9 Leszek Koltunski
 */
545 13687207 leszek
  @SuppressWarnings("unused")
546 fe6fe99a leszek
  public static int getMax(EffectType type)
547 432442f9 Leszek Koltunski
    {
548 fe6fe99a leszek
    return EffectQueue.getMax(type.ordinal());
549 432442f9 Leszek Koltunski
    }
550
551
///////////////////////////////////////////////////////////////////////////////////////////////////
552
/**
553 fe6fe99a leszek
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
554 432442f9 Leszek Koltunski
 * This can fail if:
555
 * <ul>
556
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
557
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
558
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
559
 *     time only decreasing the value of 'max' is permitted.
560
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
561
 * </ul>
562
 *
563 fe6fe99a leszek
 * @param type {@link EffectType}
564
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
565 80b3acf6 Leszek Koltunski
 *            than Byte.MAX_VALUE
566
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
567
 */
568
  @SuppressWarnings("unused")
569 fe6fe99a leszek
  public static boolean setMax(EffectType type, int max)
570 80b3acf6 Leszek Koltunski
    {
571 fe6fe99a leszek
    return EffectQueue.setMax(type.ordinal(),max);
572 80b3acf6 Leszek Koltunski
    }
573
574 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
575
/**
576 6bb59aad Leszek Koltunski
 * Add a new Effect to our queue.
577 f2fe7e28 Leszek Koltunski
 *
578 6bb59aad Leszek Koltunski
 * @param effect The Effect to add.
579 ae77d55e Leszek Koltunski
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
580 6a06a912 Leszek Koltunski
 */
581 ae77d55e Leszek Koltunski
  public boolean apply(Effect effect)
582 d425545a Leszek Koltunski
    {
583 6bb59aad Leszek Koltunski
    switch(effect.getType())
584
      {
585 26a4e5f6 leszek
      case MATRIX      : return mM.add(effect);
586 ae77d55e Leszek Koltunski
      case VERTEX      : return mV.add(effect);
587
      case FRAGMENT    : return mF.add(effect);
588 80b3acf6 Leszek Koltunski
      case POSTPROCESS : return mP.add(effect);
589 6bb59aad Leszek Koltunski
      }
590 ae77d55e Leszek Koltunski
591
    return false;
592 4fde55a0 Leszek Koltunski
    }
593 f2fe7e28 Leszek Koltunski
  }