Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueMatrix.java @ 30548891

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
package org.distorted.library;
21

    
22
import android.opengl.GLES30;
23
import android.opengl.Matrix;
24

    
25
import org.distorted.library.message.EffectMessage;
26
import org.distorted.library.type.Data1D;
27
import org.distorted.library.type.Data3D;
28
import org.distorted.library.type.Data4D;
29
import org.distorted.library.type.Dynamic1D;
30
import org.distorted.library.type.Dynamic3D;
31
import org.distorted.library.type.Dynamic4D;
32
import org.distorted.library.type.DynamicQuat;
33
import org.distorted.library.type.Static1D;
34
import org.distorted.library.type.Static3D;
35
import org.distorted.library.type.Static4D;
36

    
37
///////////////////////////////////////////////////////////////////////////////////////////////////
38

    
39
class EffectQueueMatrix extends EffectQueue
40
  {   
41
  private static final int NUM_UNIFORMS = 7;
42
  private static final int NUM_CACHE    = 0;
43
  private static final int INDEX = EffectTypes.MATRIX.ordinal();
44

    
45
  private static float[] mMVPMatrix = new float[16];
46
  private static float[] mTmpMatrix = new float[16];
47
  private static float[] mViewMatrix= new float[16];
48

    
49
  private static int mObjDH;      // This is a handle to half a Object dimensions
50
  private static int mDepthH;     // Handle to the max Depth, i.e (farplane-nearplane)/2
51
  private static int mMVPMatrixH; // pass in the transformation matrix
52
  private static int mMVMatrixH;  // pass in the modelview matrix.
53
  
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55
   
56
  EffectQueueMatrix(long id)
57
    { 
58
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
59
    }
60

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

    
63
  private static void multiplyByQuat(float[] matrix, float X, float Y, float Z, float W)
64
    {
65
    float xx= X * X;
66
    float xy= X * Y;
67
    float xz= X * Z;
68
    float xw= X * W;
69
    float yy= Y * Y;
70
    float yz= Y * Z;
71
    float yw= Y * W;
72
    float zz= Z * Z;
73
    float zw= Z * W;
74

    
75
    mTmpMatrix[0]  = 1 - 2 * ( yy + zz );
76
    mTmpMatrix[1]  =     2 * ( xy - zw );
77
    mTmpMatrix[2]  =     2 * ( xz + yw );
78
    mTmpMatrix[4]  =     2 * ( xy + zw );
79
    mTmpMatrix[5]  = 1 - 2 * ( xx + zz );
80
    mTmpMatrix[6]  =     2 * ( yz - xw );
81
    mTmpMatrix[8]  =     2 * ( xz - yw );
82
    mTmpMatrix[9]  =     2 * ( yz + xw );
83
    mTmpMatrix[10] = 1 - 2 * ( xx + yy );
84
    mTmpMatrix[3]  = mTmpMatrix[7] = mTmpMatrix[11] = mTmpMatrix[12] = mTmpMatrix[13] = mTmpMatrix[14] = 0;
85
    mTmpMatrix[15] = 1;
86
    
87
    Matrix.multiplyMM(mMVPMatrix, 0, matrix, 0, mTmpMatrix, 0);  
88

    
89
    matrix[ 0] = mMVPMatrix[ 0];
90
    matrix[ 1] = mMVPMatrix[ 1];
91
    matrix[ 2] = mMVPMatrix[ 2];
92
    matrix[ 3] = mMVPMatrix[ 3];
93
    matrix[ 4] = mMVPMatrix[ 4];
94
    matrix[ 5] = mMVPMatrix[ 5];
95
    matrix[ 6] = mMVPMatrix[ 6];
96
    matrix[ 7] = mMVPMatrix[ 7];
97
    matrix[ 8] = mMVPMatrix[ 8];
98
    matrix[ 9] = mMVPMatrix[ 9];
99
    matrix[10] = mMVPMatrix[10];
100
    matrix[11] = mMVPMatrix[11];
101
    matrix[12] = mMVPMatrix[12];
102
    matrix[13] = mMVPMatrix[13];
103
    matrix[14] = mMVPMatrix[14];
104
    matrix[15] = mMVPMatrix[15];
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
// here construct the ModelView and the ModelViewProjection Matrices
109

    
110
  void constructMatrices(DistortedOutputSurface projection, float halfX, float halfY)
111
    {
112
    Matrix.setIdentityM(mViewMatrix, 0);
113
    Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
114

    
115
    float x,y,z, sx,sy,sz;
116

    
117
    for(int i=0; i<mNumEffects; i++)
118
      {
119
      if (mName[i] == EffectNames.ROTATE.ordinal() )
120
        {
121
        x = mUniforms[NUM_UNIFORMS*i+4];
122
        y = mUniforms[NUM_UNIFORMS*i+5];
123
        z = mUniforms[NUM_UNIFORMS*i+6];
124

    
125
        Matrix.translateM(mViewMatrix, 0, x,-y, z);
126
        Matrix.rotateM( mViewMatrix, 0, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
127
        Matrix.translateM(mViewMatrix, 0,-x, y,-z);
128
        }
129
      else if(mName[i] == EffectNames.QUATERNION.ordinal() )
130
        {
131
        x = mUniforms[NUM_UNIFORMS*i+4];
132
        y = mUniforms[NUM_UNIFORMS*i+5];
133
        z = mUniforms[NUM_UNIFORMS*i+6];
134

    
135
        Matrix.translateM(mViewMatrix, 0, x,-y, z);
136
        multiplyByQuat(mViewMatrix, mUniforms[NUM_UNIFORMS*i], mUniforms[NUM_UNIFORMS*i+1], mUniforms[NUM_UNIFORMS*i+2], mUniforms[NUM_UNIFORMS*i+3]);
137
        Matrix.translateM(mViewMatrix, 0,-x, y,-z);
138
        }
139
      else if(mName[i] == EffectNames.MOVE.ordinal() )
140
        {
141
        sx = mUniforms[NUM_UNIFORMS*i  ];
142
        sy = mUniforms[NUM_UNIFORMS*i+1];
143
        sz = mUniforms[NUM_UNIFORMS*i+2];
144

    
145
        Matrix.translateM(mViewMatrix, 0, sx,-sy, sz);
146
        }
147
      else if(mName[i] == EffectNames.SCALE.ordinal() )
148
        {
149
        sx = mUniforms[NUM_UNIFORMS*i  ];
150
        sy = mUniforms[NUM_UNIFORMS*i+1];
151
        sz = mUniforms[NUM_UNIFORMS*i+2];
152

    
153
        Matrix.scaleM(mViewMatrix, 0, sx, sy, sz);
154
        }
155
      else if(mName[i] == EffectNames.SHEAR.ordinal() )
156
        {
157
        sx = mUniforms[NUM_UNIFORMS*i  ];
158
        sy = mUniforms[NUM_UNIFORMS*i+1];
159
        sz = mUniforms[NUM_UNIFORMS*i+2];
160

    
161
        x  = mUniforms[NUM_UNIFORMS*i+4];
162
        y  = mUniforms[NUM_UNIFORMS*i+5];
163
        z  = mUniforms[NUM_UNIFORMS*i+6];
164

    
165
        Matrix.translateM(mViewMatrix, 0, x,-y, z);
166

    
167
        mViewMatrix[4] += sx*mViewMatrix[0]; // Multiply viewMatrix by 1 x 0 0 , i.e. X-shear.
168
        mViewMatrix[5] += sx*mViewMatrix[1]; //                        0 1 0 0
169
        mViewMatrix[6] += sx*mViewMatrix[2]; //                        0 0 1 0
170
        mViewMatrix[7] += sx*mViewMatrix[3]; //                        0 0 0 1
171

    
172
        mViewMatrix[0] += sy*mViewMatrix[4]; // Multiply viewMatrix by 1 0 0 0 , i.e. Y-shear.
173
        mViewMatrix[1] += sy*mViewMatrix[5]; //                        y 1 0 0
174
        mViewMatrix[2] += sy*mViewMatrix[6]; //                        0 0 1 0
175
        mViewMatrix[3] += sy*mViewMatrix[7]; //                        0 0 0 1
176

    
177
        mViewMatrix[4] += sz*mViewMatrix[8]; // Multiply viewMatrix by 1 0 0 0 , i.e. Z-shear.
178
        mViewMatrix[5] += sz*mViewMatrix[9]; //                        0 1 0 0
179
        mViewMatrix[6] += sz*mViewMatrix[10];//                        0 z 1 0
180
        mViewMatrix[7] += sz*mViewMatrix[11];//                        0 0 0 1
181

    
182
        Matrix.translateM(mViewMatrix, 0,-x, y, -z);
183
        }
184
      }
185

    
186
    Matrix.translateM(mViewMatrix, 0, halfX,-halfY, 0);
187
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  static void getUniforms(int mProgramH)
193
    {
194
    mObjDH     = GLES30.glGetUniformLocation(mProgramH, "u_objD");
195
    mDepthH    = GLES30.glGetUniformLocation(mProgramH, "u_Depth");
196
    mMVPMatrixH= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
197
    mMVMatrixH = GLES30.glGetUniformLocation(mProgramH, "u_MVMatrix"); 
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  float[] getMVP()
203
    {
204
    return mMVPMatrix;
205
    }
206

    
207
///////////////////////////////////////////////////////////////////////////////////////////////////
208
// return true if in the queue there are only translations, scales, x- and y-shears, and rotations along
209
// a vector which is perpendicular to the screen, i.e. if the resulting matrix keeps the front wall of the
210
// Mesh parallel to the screen.
211

    
212
  boolean canUseShortcut()
213
    {
214
    for(int i=0; i<mNumEffects; i++)
215
      {
216
      if (mName[i] == EffectNames.ROTATE.ordinal() )
217
        {
218
        if( mUniforms[NUM_UNIFORMS*i] != 0.0f || mUniforms[NUM_UNIFORMS*i+1] != 0.0f ) return false;  // rotation along vector with x or y non-zero
219
        }
220
      else if(mName[i] == EffectNames.QUATERNION.ordinal() )
221
        {
222
        if( mUniforms[NUM_UNIFORMS*i] != 0.0f || mUniforms[NUM_UNIFORMS*i+1] != 0.0f ) return false;  // quaternion rotation along vector with x or y non-zero
223
        }
224
      else if(mName[i] == EffectNames.SHEAR.ordinal() )
225
        {
226
        if( mUniforms[NUM_UNIFORMS*i+2] != 0.0f ) return false; // shear with non-zero Z
227
        }
228
      }
229

    
230
    return true;
231
    }
232

    
233
///////////////////////////////////////////////////////////////////////////////////////////////////
234

    
235
  synchronized void compute(long currTime) 
236
    {
237
    if( currTime==mTime ) return;
238
    if( mTime==0 ) mTime = currTime;
239
    long step = (currTime-mTime);
240
   
241
    for(int i=0; i<mNumEffects; i++)
242
      {
243
      mCurrentDuration[i] += step;
244

    
245
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
246
        {
247
        for(int j=0; j<mNumListeners; j++)
248
          EffectMessageSender.newMessage( mListeners.elementAt(j),
249
                                          EffectMessage.EFFECT_FINISHED,
250
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.MATRIX.type,
251
                                          mName[i],
252
                                          mObjectID);
253

    
254
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
255
          {
256
          remove(i);
257
          i--;
258
          continue;
259
          }
260
        else mInter[0][i] = null;
261
        }
262

    
263
      if( mInter[1][i]!=null )
264
        {
265
        mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+4, mCurrentDuration[i], step);
266
        }
267
      }
268
     
269
    mTime = currTime;  
270
    }  
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  protected void moveEffect(int index)
275
    {
276
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
277
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
278
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
279
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
280
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
281
    mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
282
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
283
    }
284

    
285
///////////////////////////////////////////////////////////////////////////////////////////////////
286

    
287
  synchronized void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
288
    {
289
    constructMatrices(projection,halfX,halfY);
290

    
291
    GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
292
    GLES30.glUniform1f( mDepthH, projection.mDepth);
293
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
294
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
// here construct the ModelView Matrix, but without any effects
299

    
300
  synchronized static void sendZero(DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
301
    {
302
    Matrix.setIdentityM(mTmpMatrix, 0);
303
    Matrix.translateM(mTmpMatrix, 0, halfX-projection.mWidth/2, projection.mHeight/2-halfY, -projection.mDistance);
304
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mTmpMatrix, 0);
305
    
306
    GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
307
    GLES30.glUniform1f( mDepthH, projection.mDepth);
308
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mTmpMatrix, 0);
309
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
310
    }
311

    
312
///////////////////////////////////////////////////////////////////////////////////////////////////
313
// move, scale
314

    
315
  synchronized long add(EffectNames eln, Data3D vector)
316
    {
317
    if( mMax[INDEX]>mNumEffects )
318
      {
319
      mInter[1][mNumEffects] = null;
320

    
321
      if( vector instanceof Dynamic3D)
322
        {
323
        mInter[0][mNumEffects] = (Dynamic3D)vector;
324
        }
325
      else if( vector instanceof Static3D )
326
        {
327
        mInter[0][mNumEffects] = null;
328
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)vector).getX();
329
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)vector).getY();
330
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)vector).getZ();
331
        }
332
      else return -1;
333

    
334
      return addBase(eln);
335
      }
336

    
337
    return -1;
338
    }
339

    
340
///////////////////////////////////////////////////////////////////////////////////////////////////
341
// rotate - static axis
342

    
343
  synchronized long add(EffectNames eln, Data1D angle, Static3D axis, Data3D center)
344
    {
345
    if( mMax[INDEX]>mNumEffects )
346
      {
347
      if( angle instanceof Dynamic1D)
348
        {
349
        mInter[0][mNumEffects] = (Dynamic1D)angle;
350
        }
351
      else if( angle instanceof Static1D)
352
        {
353
        mInter[0][mNumEffects] = null;
354
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)angle).getX();
355
        }
356
      else return -1;
357

    
358
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = axis.getX();
359
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = axis.getY();
360
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = axis.getZ();
361

    
362
      if( center instanceof Dynamic3D)
363
        {
364
        mInter[1][mNumEffects] = (Dynamic3D)center;
365
        }
366
      else if( center instanceof Static3D )
367
        {
368
        mInter[1][mNumEffects] = null;
369
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
370
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
371
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
372
        }
373
      else return -1;
374

    
375
      return addBase(eln);
376
      }
377
      
378
    return -1;
379
    }
380

    
381
///////////////////////////////////////////////////////////////////////////////////////////////////
382
// quaternion or rotate - dynamic axis
383

    
384
  synchronized long add(EffectNames eln, Data4D data, Data3D center)
385
    {
386
    if( mMax[INDEX]>mNumEffects )
387
      {
388
      if( data instanceof Dynamic4D  )
389
        {
390
        mInter[0][mNumEffects] = (Dynamic4D)data;
391
        }
392
      else if( data instanceof DynamicQuat)
393
        {
394
        mInter[0][mNumEffects] = (DynamicQuat)data;
395
        }
396
      else if( data instanceof Static4D   )
397
        {
398
        mInter[0][mNumEffects] = null;
399
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static4D)data).getX();
400
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static4D)data).getY();
401
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static4D)data).getZ();
402
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static4D)data).getW();
403
        }
404
      else return -1;
405

    
406
      if( center instanceof Dynamic3D)
407
        {
408
        mInter[1][mNumEffects] = (Dynamic3D)center;
409
        }
410
      else if( center instanceof Static3D )
411
        {
412
        mInter[1][mNumEffects] = null;
413
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
414
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
415
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
416
        }
417
      else return -1;
418

    
419
      return addBase(eln);
420
      }
421

    
422
    return -1;
423
    }
424

    
425
///////////////////////////////////////////////////////////////////////////////////////////////////
426
// shear
427

    
428
  synchronized long add(EffectNames eln, Data3D shear, Data3D center)
429
    {
430
    if( mMax[INDEX]>mNumEffects )
431
      {
432
      if( shear instanceof Dynamic3D)
433
        {
434
        mInter[0][mNumEffects] = (Dynamic3D)shear;
435
        }
436
      else if( shear instanceof Static3D )
437
        {
438
        mInter[0][mNumEffects] = null;
439
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)shear).getX();
440
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)shear).getY();
441
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)shear).getZ();
442
        }
443
      else return -1;
444

    
445
      if( center instanceof Dynamic3D)
446
        {
447
        mInter[1][mNumEffects] = (Dynamic3D)center;
448
        }
449
      else if( center instanceof Static3D )
450
        {
451
        mInter[1][mNumEffects] = null;
452
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
453
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
454
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
455
        }
456
      else return -1;
457

    
458
      return addBase(eln);
459
      }
460
      
461
    return -1;
462
    }
463
  }
(16-16/22)