Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueMatrix.java @ 86782a25

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
// move, scale
299

    
300
  synchronized long add(EffectNames eln, Data3D vector)
301
    {
302
    if( mMax[INDEX]>mNumEffects )
303
      {
304
      mInter[1][mNumEffects] = null;
305

    
306
      if( vector instanceof Dynamic3D)
307
        {
308
        mInter[0][mNumEffects] = (Dynamic3D)vector;
309
        }
310
      else if( vector instanceof Static3D )
311
        {
312
        mInter[0][mNumEffects] = null;
313
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)vector).getX();
314
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)vector).getY();
315
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)vector).getZ();
316
        }
317
      else return -1;
318

    
319
      return addBase(eln);
320
      }
321

    
322
    return -1;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326
// rotate - static axis
327

    
328
  synchronized long add(EffectNames eln, Data1D angle, Static3D axis, Data3D center)
329
    {
330
    if( mMax[INDEX]>mNumEffects )
331
      {
332
      if( angle instanceof Dynamic1D)
333
        {
334
        mInter[0][mNumEffects] = (Dynamic1D)angle;
335
        }
336
      else if( angle instanceof Static1D)
337
        {
338
        mInter[0][mNumEffects] = null;
339
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)angle).getX();
340
        }
341
      else return -1;
342

    
343
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = axis.getX();
344
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = axis.getY();
345
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = axis.getZ();
346

    
347
      if( center instanceof Dynamic3D)
348
        {
349
        mInter[1][mNumEffects] = (Dynamic3D)center;
350
        }
351
      else if( center instanceof Static3D )
352
        {
353
        mInter[1][mNumEffects] = null;
354
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
355
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
356
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
357
        }
358
      else return -1;
359

    
360
      return addBase(eln);
361
      }
362
      
363
    return -1;
364
    }
365

    
366
///////////////////////////////////////////////////////////////////////////////////////////////////
367
// quaternion or rotate - dynamic axis
368

    
369
  synchronized long add(EffectNames eln, Data4D data, Data3D center)
370
    {
371
    if( mMax[INDEX]>mNumEffects )
372
      {
373
      if( data instanceof Dynamic4D  )
374
        {
375
        mInter[0][mNumEffects] = (Dynamic4D)data;
376
        }
377
      else if( data instanceof DynamicQuat)
378
        {
379
        mInter[0][mNumEffects] = (DynamicQuat)data;
380
        }
381
      else if( data instanceof Static4D   )
382
        {
383
        mInter[0][mNumEffects] = null;
384
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static4D)data).getX();
385
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static4D)data).getY();
386
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static4D)data).getZ();
387
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static4D)data).getW();
388
        }
389
      else return -1;
390

    
391
      if( center instanceof Dynamic3D)
392
        {
393
        mInter[1][mNumEffects] = (Dynamic3D)center;
394
        }
395
      else if( center instanceof Static3D )
396
        {
397
        mInter[1][mNumEffects] = null;
398
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
399
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
400
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
401
        }
402
      else return -1;
403

    
404
      return addBase(eln);
405
      }
406

    
407
    return -1;
408
    }
409

    
410
///////////////////////////////////////////////////////////////////////////////////////////////////
411
// shear
412

    
413
  synchronized long add(EffectNames eln, Data3D shear, Data3D center)
414
    {
415
    if( mMax[INDEX]>mNumEffects )
416
      {
417
      if( shear instanceof Dynamic3D)
418
        {
419
        mInter[0][mNumEffects] = (Dynamic3D)shear;
420
        }
421
      else if( shear instanceof Static3D )
422
        {
423
        mInter[0][mNumEffects] = null;
424
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)shear).getX();
425
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)shear).getY();
426
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)shear).getZ();
427
        }
428
      else return -1;
429

    
430
      if( center instanceof Dynamic3D)
431
        {
432
        mInter[1][mNumEffects] = (Dynamic3D)center;
433
        }
434
      else if( center instanceof Static3D )
435
        {
436
        mInter[1][mNumEffects] = null;
437
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
438
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
439
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
440
        }
441
      else return -1;
442

    
443
      return addBase(eln);
444
      }
445
      
446
    return -1;
447
    }
448
  }
(17-17/23)