Project

General

Profile

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

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

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 mMVPMatrixH; // pass in the transformation matrix
51
  private static int mMVMatrixH;  // pass in the modelview matrix.
52
  
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54
   
55
  EffectQueueMatrix(long id)
56
    { 
57
    super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

    
200
  synchronized void compute(long currTime) 
201
    {
202
    if( currTime==mTime ) return;
203
    if( mTime==0 ) mTime = currTime;
204
    long step = (currTime-mTime);
205
   
206
    for(int i=0; i<mNumEffects; i++)
207
      {
208
      mCurrentDuration[i] += step;
209

    
210
      if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
211
        {
212
        for(int j=0; j<mNumListeners; j++)
213
          EffectMessageSender.newMessage( mListeners.elementAt(j),
214
                                          EffectMessage.EFFECT_FINISHED,
215
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.MATRIX.type,
216
                                          mName[i],
217
                                          mObjectID);
218

    
219
        if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
220
          {
221
          remove(i);
222
          i--;
223
          continue;
224
          }
225
        else mInter[0][i] = null;
226
        }
227

    
228
      if( mInter[1][i]!=null )
229
        {
230
        mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+4, mCurrentDuration[i], step);
231
        }
232
      }
233
     
234
    mTime = currTime;  
235
    }  
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238

    
239
  protected void moveEffect(int index)
240
    {
241
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
242
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
243
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
244
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
245
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
246
    mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
247
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251

    
252
  synchronized void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ)
253
    {
254
    constructMatrices(projection,halfX,halfY,halfZ);
255

    
256
    GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
257
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
258
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262
// move, scale
263

    
264
  synchronized long add(EffectNames eln, Data3D vector)
265
    {
266
    if( mMax[INDEX]>mNumEffects )
267
      {
268
      mInter[1][mNumEffects] = null;
269

    
270
      if( vector instanceof Dynamic3D)
271
        {
272
        mInter[0][mNumEffects] = (Dynamic3D)vector;
273
        }
274
      else if( vector instanceof Static3D )
275
        {
276
        mInter[0][mNumEffects] = null;
277
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)vector).getX();
278
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)vector).getY();
279
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)vector).getZ();
280
        }
281
      else return -1;
282

    
283
      return addBase(eln);
284
      }
285

    
286
    return -1;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290
// rotate - static axis
291

    
292
  synchronized long add(EffectNames eln, Data1D angle, Static3D axis, Data3D center)
293
    {
294
    if( mMax[INDEX]>mNumEffects )
295
      {
296
      if( angle instanceof Dynamic1D)
297
        {
298
        mInter[0][mNumEffects] = (Dynamic1D)angle;
299
        }
300
      else if( angle instanceof Static1D)
301
        {
302
        mInter[0][mNumEffects] = null;
303
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)angle).getX();
304
        }
305
      else return -1;
306

    
307
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = axis.getX();
308
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = axis.getY();
309
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = axis.getZ();
310

    
311
      if( center instanceof Dynamic3D)
312
        {
313
        mInter[1][mNumEffects] = (Dynamic3D)center;
314
        }
315
      else if( center instanceof Static3D )
316
        {
317
        mInter[1][mNumEffects] = null;
318
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
319
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
320
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
321
        }
322
      else return -1;
323

    
324
      return addBase(eln);
325
      }
326
      
327
    return -1;
328
    }
329

    
330
///////////////////////////////////////////////////////////////////////////////////////////////////
331
// quaternion or rotate - dynamic axis
332

    
333
  synchronized long add(EffectNames eln, Data4D data, Data3D center)
334
    {
335
    if( mMax[INDEX]>mNumEffects )
336
      {
337
      if( data instanceof Dynamic4D  )
338
        {
339
        mInter[0][mNumEffects] = (Dynamic4D)data;
340
        }
341
      else if( data instanceof DynamicQuat)
342
        {
343
        mInter[0][mNumEffects] = (DynamicQuat)data;
344
        }
345
      else if( data instanceof Static4D   )
346
        {
347
        mInter[0][mNumEffects] = null;
348
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static4D)data).getX();
349
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static4D)data).getY();
350
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static4D)data).getZ();
351
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = ((Static4D)data).getW();
352
        }
353
      else return -1;
354

    
355
      if( center instanceof Dynamic3D)
356
        {
357
        mInter[1][mNumEffects] = (Dynamic3D)center;
358
        }
359
      else if( center instanceof Static3D )
360
        {
361
        mInter[1][mNumEffects] = null;
362
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = ((Static3D)center).getX();
363
        mUniforms[NUM_UNIFORMS*mNumEffects+5] = ((Static3D)center).getY();
364
        mUniforms[NUM_UNIFORMS*mNumEffects+6] = ((Static3D)center).getZ();
365
        }
366
      else return -1;
367

    
368
      return addBase(eln);
369
      }
370

    
371
    return -1;
372
    }
373

    
374
///////////////////////////////////////////////////////////////////////////////////////////////////
375
// shear
376

    
377
  synchronized long add(EffectNames eln, Data3D shear, Data3D center)
378
    {
379
    if( mMax[INDEX]>mNumEffects )
380
      {
381
      if( shear instanceof Dynamic3D)
382
        {
383
        mInter[0][mNumEffects] = (Dynamic3D)shear;
384
        }
385
      else if( shear instanceof Static3D )
386
        {
387
        mInter[0][mNumEffects] = null;
388
        mUniforms[NUM_UNIFORMS*mNumEffects  ] = ((Static3D)shear).getX();
389
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static3D)shear).getY();
390
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static3D)shear).getZ();
391
        }
392
      else return -1;
393

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

    
407
      return addBase(eln);
408
      }
409
      
410
    return -1;
411
    }
412
  }
(18-18/24)