Project

General

Profile

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

library / src / main / java / org / distorted / library / main / DistortedEffects.java @ 9b94626c

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 a4835695 Leszek Koltunski
35 c90b9e01 Leszek Koltunski
import java.io.InputStream;
36 c638c1b0 Leszek Koltunski
import java.nio.ByteBuffer;
37
import java.nio.ByteOrder;
38
import java.nio.FloatBuffer;
39
40 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
41 b329f352 Leszek Koltunski
/**
42 faa3ff56 Leszek Koltunski
 * Class containing Matrix, Vertex, Fragment and Postprocessing effect queues.
43 b73dcaa7 Leszek Koltunski
 * <p>
44 faa3ff56 Leszek Koltunski
 * The queues hold actual effects to be applied to a given (InputSurface,MeshObject) combo.
45 b329f352 Leszek Koltunski
 */
46 86d322b5 Leszek Koltunski
public class DistortedEffects
47 d425545a Leszek Koltunski
  {
48 1aedf874 leszek
  /// MAIN PROGRAM ///
49
  private static DistortedProgram mMainProgram;
50
  private static int mMainTextureH;
51 8fa96e69 Leszek Koltunski
52 c1e24646 leszek
  /// BLIT PROGRAM ///
53
  private static DistortedProgram mBlitProgram;
54
  private static int mBlitTextureH;
55 c2c08950 leszek
  private static int mBlitDepthH;
56 c90b9e01 Leszek Koltunski
  private static final FloatBuffer mQuadPositions;
57
58
  static
59
    {
60
    float[] positionData= { -0.5f, -0.5f,  -0.5f, 0.5f,  0.5f,-0.5f,  0.5f, 0.5f };
61
    mQuadPositions = ByteBuffer.allocateDirect(32).order(ByteOrder.nativeOrder()).asFloatBuffer();
62
    mQuadPositions.put(positionData).position(0);
63
    }
64
65 7170e4eb leszek
  /// BLIT DEPTH PROGRAM ///
66
  private static DistortedProgram mBlitDepthProgram;
67
  private static int mBlitDepthTextureH;
68
  private static int mBlitDepthDepthTextureH;
69
  private static int mBlitDepthDepthH;
70 ae2802b1 Leszek Koltunski
  private static int mBlitDepthTexCorrH;
71 7170e4eb leszek
72 3fc9327a Leszek Koltunski
  /// NORMAL PROGRAM /////
73
  private static DistortedProgram mNormalProgram;
74
  private static int mNormalMVPMatrixH;
75
  /// END PROGRAMS //////
76 c638c1b0 Leszek Koltunski
77 8e34674e Leszek Koltunski
  private static long mNextID =0;
78 4e2382f3 Leszek Koltunski
  private long mID;
79 8e34674e Leszek Koltunski
80 310e14fb leszek
  private EffectQueueMatrix mM;
81 3fc9327a Leszek Koltunski
  private EffectQueueFragment mF;
82 310e14fb leszek
  private EffectQueueVertex mV;
83 80b3acf6 Leszek Koltunski
  private EffectQueuePostprocess mP;
84
85
  private boolean matrixCloned, vertexCloned, fragmentCloned, postprocessCloned;
86 3d590d8d Leszek Koltunski
87 9361b337 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
88 55c14a19 Leszek Koltunski
89 c90b9e01 Leszek Koltunski
  static void createProgram(Resources resources)
90 55c14a19 Leszek Koltunski
    {
91 cab7c165 Leszek Koltunski
    // MAIN PROGRAM ////////////////////////////////////
92 1aedf874 leszek
    final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
93
    final InputStream mainFragStream = resources.openRawResource(R.raw.main_fragment_shader);
94
95 7cd24173 leszek
    int numF = FragmentEffect.getNumEnabled();
96
    int numV = VertexEffect.getNumEnabled();
97 c90b9e01 Leszek Koltunski
98 7cd24173 leszek
    String mainVertHeader= Distorted.GLSL_VERSION + ("#define NUM_VERTEX "   + ( numV>0 ? getMax(EffectType.VERTEX  ) : 0 ) + "\n");
99
    String mainFragHeader= Distorted.GLSL_VERSION + ("#define NUM_FRAGMENT " + ( numF>0 ? getMax(EffectType.FRAGMENT) : 0 ) + "\n");
100
    String enabledEffectV= VertexEffect.getGLSL();
101
    String enabledEffectF= FragmentEffect.getGLSL();
102 c90b9e01 Leszek Koltunski
103 03cb451d Leszek Koltunski
    //android.util.Log.e("Effects", "vertHeader= "+mainVertHeader);
104
    //android.util.Log.e("Effects", "fragHeader= "+mainFragHeader);
105 7cd24173 leszek
    //android.util.Log.e("Effects", "enabledV= "+enabledEffectV);
106
    //android.util.Log.e("Effects", "enabledF= "+enabledEffectF);
107 c90b9e01 Leszek Koltunski
108 3fc9327a Leszek Koltunski
    String[] feedback = { "v_Position", "v_endPosition" };
109 cab7c165 Leszek Koltunski
110 040cd18c Leszek Koltunski
    try
111
      {
112
      mMainProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
113
                                          enabledEffectV, enabledEffectF, Distorted.GLSL, feedback);
114
      }
115
    catch(Exception e)
116
      {
117
      Log.e("EFFECTS", e.getClass().getSimpleName()+" trying to compile MAIN program: "+e.getMessage());
118
      throw new RuntimeException(e.getMessage());
119
      }
120 c90b9e01 Leszek Koltunski
121 c1e24646 leszek
    int mainProgramH = mMainProgram.getProgramHandle();
122 c90b9e01 Leszek Koltunski
    EffectQueueFragment.getUniforms(mainProgramH);
123 1aedf874 leszek
    EffectQueueVertex.getUniforms(mainProgramH);
124
    EffectQueueMatrix.getUniforms(mainProgramH);
125 e6519ac8 Leszek Koltunski
    mMainTextureH= GLES31.glGetUniformLocation( mainProgramH, "u_Texture");
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 040cd18c Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" 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 040cd18c Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" 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 040cd18c Leszek Koltunski
      Log.e("EFFECTS", e.getClass().getSimpleName()+" 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 12f45260 Leszek Koltunski
289 040cd18c Leszek Koltunski
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, mesh.mAttVBO[0]);
290
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[0], MeshObject.POS_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET0);
291
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[1], MeshObject.NOR_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET1);
292
    GLES31.glVertexAttribPointer(mMainProgram.mAttribute[2], MeshObject.TEX_DATA_SIZE, GLES31.GL_FLOAT, false, MeshObject.VERTSIZE, MeshObject.OFFSET2);
293
    GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, 0);
294 cab7c165 Leszek Koltunski
295 270c27bc Leszek Koltunski
    mM.send(surface,halfW,halfH,halfZ,marginInPixels);
296 8fbd0237 Leszek Koltunski
    mV.send();
297
    mF.send();
298 42571056 Leszek Koltunski
299 e6519ac8 Leszek Koltunski
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.numVertices);
300 c90b9e01 Leszek Koltunski
301 3fc9327a Leszek Koltunski
    if( mesh.mShowNormals ) displayNormals(mesh);
302 d425545a Leszek Koltunski
    }
303 6a06a912 Leszek Koltunski
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305 7266d8ef Leszek Koltunski
/**
306
 * Only for use by the library itself.
307
 *
308
 * @y.exclude
309
 */
310
  public static void blitPriv(DistortedOutputSurface surface)
311 d425545a Leszek Koltunski
    {
312 c1e24646 leszek
    mBlitProgram.useProgram();
313
314 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
315
    GLES31.glUniform1i(mBlitTextureH, 0);
316
    GLES31.glUniform1f( mBlitDepthH , 1.0f-surface.mNear);
317
    GLES31.glVertexAttribPointer(mBlitProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
318
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
319 d425545a Leszek Koltunski
    }
320 7170e4eb leszek
321
///////////////////////////////////////////////////////////////////////////////////////////////////
322
323 ae2802b1 Leszek Koltunski
  static void blitDepthPriv(DistortedOutputSurface surface, float corrW, float corrH)
324 7170e4eb leszek
    {
325
    mBlitDepthProgram.useProgram();
326
327 e6519ac8 Leszek Koltunski
    GLES31.glViewport(0, 0, surface.mWidth, surface.mHeight );
328
    GLES31.glUniform1i(mBlitDepthTextureH, 0);
329
    GLES31.glUniform1i(mBlitDepthDepthTextureH, 1);
330 ae2802b1 Leszek Koltunski
    GLES31.glUniform2f(mBlitDepthTexCorrH, corrW, corrH );
331 e6519ac8 Leszek Koltunski
    GLES31.glUniform1f( mBlitDepthDepthH , 1.0f-surface.mNear);
332
    GLES31.glVertexAttribPointer(mBlitDepthProgram.mAttribute[0], 2, GLES31.GL_FLOAT, false, 0, mQuadPositions);
333
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
334 7170e4eb leszek
    }
335
336 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
337 9b94626c Leszek Koltunski
338 8e34674e Leszek Koltunski
  private void releasePriv()
339 d425545a Leszek Koltunski
    {
340 9b94626c Leszek Koltunski
    if( !matrixCloned      ) mM.abortAll(false);
341
    if( !vertexCloned      ) mV.abortAll(false);
342
    if( !fragmentCloned    ) mF.abortAll(false);
343
    if( !postprocessCloned ) mP.abortAll(false);
344 d425545a Leszek Koltunski
345 4e2382f3 Leszek Koltunski
    mM = null;
346
    mV = null;
347
    mF = null;
348 80b3acf6 Leszek Koltunski
    mP = null;
349 8e34674e Leszek Koltunski
    }
350
351 1942537e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
352
353 7b8086eb Leszek Koltunski
  static void onDestroy()
354 1942537e Leszek Koltunski
    {
355 9b94626c Leszek Koltunski
    mNextID =  0;
356 1942537e Leszek Koltunski
    }
357
358 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
359
// PUBLIC API
360 ada90d33 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
361 d425545a Leszek Koltunski
/**
362 4e2382f3 Leszek Koltunski
 * Create empty effect queue.
363 d425545a Leszek Koltunski
 */
364 421c2728 Leszek Koltunski
  public DistortedEffects()
365 d425545a Leszek Koltunski
    {
366 c7da4e65 leszek
    mID = ++mNextID;
367 4e2382f3 Leszek Koltunski
    initializeEffectLists(this,0);
368 d425545a Leszek Koltunski
    }
369 ada90d33 Leszek Koltunski
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371 d425545a Leszek Koltunski
/**
372 4e2382f3 Leszek Koltunski
 * Copy constructor.
373 d425545a Leszek Koltunski
 * <p>
374
 * Whatever we do not clone gets created just like in the default constructor.
375
 *
376
 * @param dc    Source object to create our object from
377
 * @param flags A bitmask of values specifying what to copy.
378 e6ab30eb Leszek Koltunski
 *              For example, CLONE_VERTEX | CLONE_MATRIX.
379 d425545a Leszek Koltunski
 */
380 421c2728 Leszek Koltunski
  public DistortedEffects(DistortedEffects dc, int flags)
381 d425545a Leszek Koltunski
    {
382 c7da4e65 leszek
    mID = ++mNextID;
383 4e2382f3 Leszek Koltunski
    initializeEffectLists(dc,flags);
384 d425545a Leszek Koltunski
    }
385 ada90d33 Leszek Koltunski
386 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
387
/**
388 07305c87 Leszek Koltunski
 * Releases all resources. After this call, the queue should not be used anymore.
389 6a06a912 Leszek Koltunski
 */
390 13687207 leszek
  @SuppressWarnings("unused")
391 8e34674e Leszek Koltunski
  public synchronized void delete()
392 d425545a Leszek Koltunski
    {
393
    releasePriv();
394
    }
395 6a06a912 Leszek Koltunski
396
///////////////////////////////////////////////////////////////////////////////////////////////////
397
/**
398 4e2382f3 Leszek Koltunski
 * Returns unique ID of this instance.
399
 *
400
 * @return ID of the object.
401 6a06a912 Leszek Koltunski
 */
402 4e2382f3 Leszek Koltunski
  public long getID()
403 d425545a Leszek Koltunski
      {
404 4e2382f3 Leszek Koltunski
      return mID;
405 d425545a Leszek Koltunski
      }
406 4e2382f3 Leszek Koltunski
407 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
408
/**
409
 * Adds the calling class to the list of Listeners that get notified each time some event happens 
410 faa3ff56 Leszek Koltunski
 * to one of the Effects in our queues. Nothing will happen if 'el' is already in the list.
411 6a06a912 Leszek Koltunski
 * 
412
 * @param el A class implementing the EffectListener interface that wants to get notifications.
413
 */
414 13687207 leszek
  @SuppressWarnings("unused")
415 3fc994b2 Leszek Koltunski
  public void registerForMessages(EffectListener el)
416 d425545a Leszek Koltunski
    {
417 26a4e5f6 leszek
    mM.registerForMessages(el);
418 3fc994b2 Leszek Koltunski
    mV.registerForMessages(el);
419
    mF.registerForMessages(el);
420 80b3acf6 Leszek Koltunski
    mP.registerForMessages(el);
421 d425545a Leszek Koltunski
    }
422 6a06a912 Leszek Koltunski
423
///////////////////////////////////////////////////////////////////////////////////////////////////
424
/**
425 faa3ff56 Leszek Koltunski
 * Removes the calling class from the list of Listeners that get notified if something happens to Effects in our queue.
426 6a06a912 Leszek Koltunski
 * 
427
 * @param el A class implementing the EffectListener interface that no longer wants to get notifications.
428
 */
429 13687207 leszek
  @SuppressWarnings("unused")
430 3fc994b2 Leszek Koltunski
  public void deregisterForMessages(EffectListener el)
431 d425545a Leszek Koltunski
    {
432 26a4e5f6 leszek
    mM.deregisterForMessages(el);
433 3fc994b2 Leszek Koltunski
    mV.deregisterForMessages(el);
434
    mF.deregisterForMessages(el);
435 80b3acf6 Leszek Koltunski
    mP.deregisterForMessages(el);
436 d425545a Leszek Koltunski
    }
437 6a06a912 Leszek Koltunski
438
///////////////////////////////////////////////////////////////////////////////////////////////////
439
/**
440 d07f2950 Leszek Koltunski
 * Aborts all Effects.
441
 * @return Number of effects aborted.
442 6a06a912 Leszek Koltunski
 */
443 d425545a Leszek Koltunski
  public int abortAllEffects()
444 13687207 leszek
    {
445
    return mM.abortAll(true) + mV.abortAll(true) + mF.abortAll(true);
446
    }
447 6a06a912 Leszek Koltunski
448
///////////////////////////////////////////////////////////////////////////////////////////////////
449
/**
450 d07f2950 Leszek Koltunski
 * Aborts all Effects of a given type, for example all MATRIX Effects.
451 6a06a912 Leszek Koltunski
 * 
452 da9b3f07 Leszek Koltunski
 * @param type one of the constants defined in {@link EffectType}
453 d07f2950 Leszek Koltunski
 * @return Number of effects aborted.
454 6a06a912 Leszek Koltunski
 */
455 da9b3f07 Leszek Koltunski
  public int abortByType(EffectType type)
456 d425545a Leszek Koltunski
    {
457
    switch(type)
458 6a06a912 Leszek Koltunski
      {
459 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.abortAll(true);
460
      case VERTEX     : return mV.abortAll(true);
461
      case FRAGMENT   : return mF.abortAll(true);
462 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.abortAll(true);
463 da9b3f07 Leszek Koltunski
      default         : return 0;
464 6a06a912 Leszek Koltunski
      }
465 d425545a Leszek Koltunski
    }
466 47316d20 leszek
467
///////////////////////////////////////////////////////////////////////////////////////////////////
468
/**
469 faa3ff56 Leszek Koltunski
 * Aborts an Effect by its ID.
470 47316d20 leszek
 *
471
 * @param id the Id of the Effect to be removed, as returned by getID().
472
 * @return Number of effects aborted.
473
 */
474
  public int abortById(long id)
475
    {
476
    long type = id&EffectType.MASK;
477
478 2ef5dd9e leszek
    if( type == EffectType.MATRIX.ordinal()      ) return mM.removeById(id);
479
    if( type == EffectType.VERTEX.ordinal()      ) return mV.removeById(id);
480
    if( type == EffectType.FRAGMENT.ordinal()    ) return mF.removeById(id);
481 80b3acf6 Leszek Koltunski
    if( type == EffectType.POSTPROCESS.ordinal() ) return mP.removeById(id);
482 47316d20 leszek
483
    return 0;
484
    }
485
486 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
487
/**
488
 * Aborts a single Effect.
489
 * 
490 6bb59aad Leszek Koltunski
 * @param effect the Effect we want to abort.
491 476bbc81 Leszek Koltunski
 * @return number of Effects aborted. Always either 0 or 1.
492 6a06a912 Leszek Koltunski
 */
493 6bb59aad Leszek Koltunski
  public int abortEffect(Effect effect)
494 d425545a Leszek Koltunski
    {
495 6bb59aad Leszek Koltunski
    switch(effect.getType())
496
      {
497 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeEffect(effect);
498
      case VERTEX     : return mV.removeEffect(effect);
499
      case FRAGMENT   : return mF.removeEffect(effect);
500 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeEffect(effect);
501 da9b3f07 Leszek Koltunski
      default         : return 0;
502 6bb59aad Leszek Koltunski
      }
503 d425545a Leszek Koltunski
    }
504 6a06a912 Leszek Koltunski
505
///////////////////////////////////////////////////////////////////////////////////////////////////
506
/**
507 e8c81a8e Leszek Koltunski
 * Abort all Effects of a given name, for example all rotations.
508 6a06a912 Leszek Koltunski
 * 
509 da9b3f07 Leszek Koltunski
 * @param name one of the constants defined in {@link EffectName}
510 476bbc81 Leszek Koltunski
 * @return number of Effects aborted.
511 6a06a912 Leszek Koltunski
 */
512 da9b3f07 Leszek Koltunski
  public int abortByName(EffectName name)
513 d425545a Leszek Koltunski
    {
514 da9b3f07 Leszek Koltunski
    switch(name.getType())
515 6a06a912 Leszek Koltunski
      {
516 da9b3f07 Leszek Koltunski
      case MATRIX     : return mM.removeByName(name);
517
      case VERTEX     : return mV.removeByName(name);
518
      case FRAGMENT   : return mF.removeByName(name);
519 80b3acf6 Leszek Koltunski
      case POSTPROCESS: return mP.removeByName(name);
520 6bb59aad Leszek Koltunski
      default                : return 0;
521 6a06a912 Leszek Koltunski
      }
522 d425545a Leszek Koltunski
    }
523 432442f9 Leszek Koltunski
524
///////////////////////////////////////////////////////////////////////////////////////////////////
525
/**
526 faa3ff56 Leszek Koltunski
 * Returns the maximum number of effects of a given type that can be simultaneously applied to a
527
 * single (InputSurface,MeshObject) combo.
528 432442f9 Leszek Koltunski
 *
529 fe6fe99a leszek
 * @param type {@link EffectType}
530
 * @return The maximum number of effects of a given type.
531 432442f9 Leszek Koltunski
 */
532 13687207 leszek
  @SuppressWarnings("unused")
533 fe6fe99a leszek
  public static int getMax(EffectType type)
534 432442f9 Leszek Koltunski
    {
535 fe6fe99a leszek
    return EffectQueue.getMax(type.ordinal());
536 432442f9 Leszek Koltunski
    }
537
538
///////////////////////////////////////////////////////////////////////////////////////////////////
539
/**
540 fe6fe99a leszek
 * Sets the maximum number of effects that can be stored in a single EffectQueue at one time.
541 432442f9 Leszek Koltunski
 * This can fail if:
542
 * <ul>
543
 * <li>the value of 'max' is outside permitted range (0 &le; max &le; Byte.MAX_VALUE)
544
 * <li>We try to increase the value of 'max' when it is too late to do so already. It needs to be called
545
 *     before the Vertex Shader gets compiled, i.e. before the call to {@link Distorted#onCreate}. After this
546
 *     time only decreasing the value of 'max' is permitted.
547
 * <li>Furthermore, this needs to be called before any instances of the DistortedEffects class get created.
548
 * </ul>
549
 *
550 fe6fe99a leszek
 * @param type {@link EffectType}
551
 * @param max new maximum number of simultaneous effects. Has to be a non-negative number not greater
552 80b3acf6 Leszek Koltunski
 *            than Byte.MAX_VALUE
553
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
554
 */
555
  @SuppressWarnings("unused")
556 fe6fe99a leszek
  public static boolean setMax(EffectType type, int max)
557 80b3acf6 Leszek Koltunski
    {
558 fe6fe99a leszek
    return EffectQueue.setMax(type.ordinal(),max);
559 80b3acf6 Leszek Koltunski
    }
560
561 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
562
/**
563 6bb59aad Leszek Koltunski
 * Add a new Effect to our queue.
564 f2fe7e28 Leszek Koltunski
 *
565 6bb59aad Leszek Koltunski
 * @param effect The Effect to add.
566 ae77d55e Leszek Koltunski
 * @return <code>true</code> if operation was successful, <code>false</code> otherwise.
567 6a06a912 Leszek Koltunski
 */
568 ae77d55e Leszek Koltunski
  public boolean apply(Effect effect)
569 d425545a Leszek Koltunski
    {
570 6bb59aad Leszek Koltunski
    switch(effect.getType())
571
      {
572 26a4e5f6 leszek
      case MATRIX      : return mM.add(effect);
573 ae77d55e Leszek Koltunski
      case VERTEX      : return mV.add(effect);
574
      case FRAGMENT    : return mF.add(effect);
575 80b3acf6 Leszek Koltunski
      case POSTPROCESS : return mP.add(effect);
576 6bb59aad Leszek Koltunski
      }
577 ae77d55e Leszek Koltunski
578
    return false;
579 4fde55a0 Leszek Koltunski
    }
580 f2fe7e28 Leszek Koltunski
  }