Project

General

Profile

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

library / src / main / java / org / distorted / library / EffectQueueMatrix.java @ 34f52b8a

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
  private void constructMatrices(DistortedOutputSurface projection, float halfX, float halfY)
111
    {
112
    Matrix.setIdentityM(mViewMatrix, 0);
113
    Matrix.translateM(mViewMatrix, 0, 0, 0, -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]*projection.mWidth;
122
        y = mUniforms[NUM_UNIFORMS*i+5]*projection.mHeight;
123
        z = mUniforms[NUM_UNIFORMS*i+6]*projection.mDistance;
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]*projection.mWidth;
132
        y = mUniforms[NUM_UNIFORMS*i+5]*projection.mHeight;
133
        z = mUniforms[NUM_UNIFORMS*i+6]*projection.mDistance;
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  ]*projection.mWidth;
142
        sy = mUniforms[NUM_UNIFORMS*i+1]*projection.mHeight;
143
        sz = mUniforms[NUM_UNIFORMS*i+2]*projection.mDistance;
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]*projection.mWidth;
162
        y  = mUniforms[NUM_UNIFORMS*i+5]*projection.mHeight;
163
        z  = mUniforms[NUM_UNIFORMS*i+6]*projection.mDistance;
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.scaleM(mViewMatrix, 0, projection.mWidth/(2*halfX), projection.mHeight/(2*halfY), 1);
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
  synchronized void compute(long currTime) 
203
    {
204
    if( currTime==mTime ) return;
205
    if( mTime==0 ) mTime = currTime;
206
    long step = (currTime-mTime);
207
   
208
    for(int i=0; i<mNumEffects; i++)
209
      {
210
      mCurrentDuration[i] += step;
211

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

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

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

    
239
///////////////////////////////////////////////////////////////////////////////////////////////////
240

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

    
252
///////////////////////////////////////////////////////////////////////////////////////////////////
253

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

    
258
    GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
259
    GLES30.glUniform1f( mDepthH, projection.mDepth);
260
    GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
261
    GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
262
    }
263

    
264
///////////////////////////////////////////////////////////////////////////////////////////////////
265
// move, scale
266

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

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

    
286
      return addBase(eln);
287
      }
288

    
289
    return -1;
290
    }
291

    
292
///////////////////////////////////////////////////////////////////////////////////////////////////
293
// rotate - static axis
294

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

    
310
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = axis.getX();
311
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = axis.getY();
312
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = axis.getZ();
313

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

    
327
      return addBase(eln);
328
      }
329
      
330
    return -1;
331
    }
332

    
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
// quaternion or rotate - dynamic axis
335

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

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

    
371
      return addBase(eln);
372
      }
373

    
374
    return -1;
375
    }
376

    
377
///////////////////////////////////////////////////////////////////////////////////////////////////
378
// shear
379

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

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

    
410
      return addBase(eln);
411
      }
412
      
413
    return -1;
414
    }
415
  }
(18-18/24)