Project

General

Profile

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

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

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.GLES20;
23
import android.opengl.Matrix;
24

    
25
///////////////////////////////////////////////////////////////////////////////////////////////////
26

    
27
class EffectQueueMatrix extends EffectQueue
28
  {   
29
  private static final int NUM_UNIFORMS = 7;
30
  private static final int INDEX = EffectTypes.MATRIX.ordinal();
31
  private static float[] mMVPMatrix= new float[16];
32
  private static float[] mTmpMatrix= new float[16];
33
  
34
  private static int mBmpDH;      // This is a handle to half a bitmap dimensions
35
  private static int mDepthH;     // Handle to the max Depth, i.e (farplane-nearplane)/2
36
  private static int mMVPMatrixH; // pass in the transformation matrix
37
  private static int mMVMatrixH;  // pass in the modelview matrix.
38
  
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
   
41
  public EffectQueueMatrix(DistortedObject obj)
42
    { 
43
    super(obj,NUM_UNIFORMS, INDEX );
44
    }
45

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47

    
48
  private static void multiplyByQuat(float[] matrix, float X, float Y, float Z, float W)
49
    {
50
    float xx= X * X;
51
    float xy= X * Y;
52
    float xz= X * Z;
53
    float xw= X * W;
54
    float yy= Y * Y;
55
    float yz= Y * Z;
56
    float yw= Y * W;
57
    float zz= Z * Z;
58
    float zw= Z * W;
59

    
60
    mTmpMatrix[0]  = 1 - 2 * ( yy + zz );
61
    mTmpMatrix[1]  =     2 * ( xy - zw );
62
    mTmpMatrix[2]  =     2 * ( xz + yw );
63
    mTmpMatrix[4]  =     2 * ( xy + zw );
64
    mTmpMatrix[5]  = 1 - 2 * ( xx + zz );
65
    mTmpMatrix[6]  =     2 * ( yz - xw );
66
    mTmpMatrix[8]  =     2 * ( xz - yw );
67
    mTmpMatrix[9]  =     2 * ( yz + xw );
68
    mTmpMatrix[10] = 1 - 2 * ( xx + yy );
69
    mTmpMatrix[3]  = mTmpMatrix[7] = mTmpMatrix[11] = mTmpMatrix[12] = mTmpMatrix[13] = mTmpMatrix[14] = 0;
70
    mTmpMatrix[15] = 1;
71
    
72
    Matrix.multiplyMM(mMVPMatrix, 0, matrix, 0, mTmpMatrix, 0);  
73
    for(int j=0; j<16; j++) matrix[j] = mMVPMatrix[j];   
74
    }
75

    
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77

    
78
  static void getUniforms(int mProgramH)
79
    {
80
    mBmpDH     = GLES20.glGetUniformLocation(mProgramH, "u_bmpD");
81
    mDepthH    = GLES20.glGetUniformLocation(mProgramH, "u_Depth");
82
    mMVPMatrixH= GLES20.glGetUniformLocation(mProgramH, "u_MVPMatrix");
83
    mMVMatrixH = GLES20.glGetUniformLocation(mProgramH, "u_MVMatrix"); 
84
    }
85

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87
  
88
  synchronized void compute(long currTime) 
89
    {
90
    if( currTime==mTime ) return;
91
    if( mTime==0 ) mTime = currTime;
92
    long step = (currTime-mTime);
93
   
94
    for(int i=0; i<mNumEffects; i++)
95
      {
96
      if( mInterI[i]==null ) continue;    
97
           
98
      if( mInterP[i]!=null ) 
99
        {
100
        mInterP[i].interpolateMain(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i]);
101
        }
102
        
103
      if( mInterI[i].interpolateMain(mUniforms ,NUM_UNIFORMS*i+3, mCurrentDuration[i], step) )      
104
        {   
105
        for(int j=0; j<mNumListeners; j++)   
106
          EffectMessageSender.newMessage( mListeners.elementAt(j),
107
                                          EffectMessage.EFFECT_FINISHED, 
108
                                         (mID[i]<<EffectTypes.LENGTH)+EffectTypes.MATRIX.type,
109
                                          mType[i], 
110
                                          mBitmapID,
111
                                          null);
112
       
113
        if( EffectNames.isUnity(mType[i], mUniforms, NUM_UNIFORMS*i+3) )
114
          {  
115
          remove(i);
116
          i--;
117
          continue;
118
          }
119
        }
120
    
121
      mCurrentDuration[i] += step;
122
      }
123
     
124
    mTime = currTime;  
125
    }  
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128

    
129
  protected void moveEffect(int index)
130
    {
131
    mUniforms[NUM_UNIFORMS*index  ] = mUniforms[NUM_UNIFORMS*(index+1)  ];
132
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
133
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
134
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
135
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
136
    mUniforms[NUM_UNIFORMS*index+5] = mUniforms[NUM_UNIFORMS*(index+1)+5];
137
    mUniforms[NUM_UNIFORMS*index+6] = mUniforms[NUM_UNIFORMS*(index+1)+6];
138
    }
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141
// here construct the ModelView Matrix
142

    
143
  synchronized void send(float[] viewMatrix, DistortedProjection dp) 
144
    {
145
    Matrix.setIdentityM(viewMatrix, 0);
146
    Matrix.translateM(viewMatrix, 0, -dp.width/2, dp.height/2, -dp.distance);
147
    
148
    float x,y,z, sx,sy,sz=1.0f;
149
   
150
    for(int i=0; i<mNumEffects; i++)
151
      {
152
      if (mType[i] == EffectNames.ROTATE.ordinal() )
153
        {
154
        x = mUniforms[NUM_UNIFORMS*i  ];
155
        y = mUniforms[NUM_UNIFORMS*i+1];
156
        z = mUniforms[NUM_UNIFORMS*i+2];
157
     
158
        Matrix.translateM(viewMatrix, 0, x,-y, z); 
159
        Matrix.rotateM( viewMatrix, 0, mUniforms[NUM_UNIFORMS*i+3], mUniforms[NUM_UNIFORMS*i+4], mUniforms[NUM_UNIFORMS*i+5], mUniforms[NUM_UNIFORMS*i+6]);  
160
        Matrix.translateM(viewMatrix, 0,-x, y,-z);  
161
        }
162
      else if(mType[i] == EffectNames.QUATERNION.ordinal() )
163
        {
164
        x = mUniforms[NUM_UNIFORMS*i  ];
165
        y = mUniforms[NUM_UNIFORMS*i+1];
166
        z = mUniforms[NUM_UNIFORMS*i+2];
167
     	
168
        Matrix.translateM(viewMatrix, 0, x,-y, z); 
169
        multiplyByQuat(viewMatrix, mUniforms[NUM_UNIFORMS*i+3], mUniforms[NUM_UNIFORMS*i+4], mUniforms[NUM_UNIFORMS*i+5], mUniforms[NUM_UNIFORMS*i+6]);
170
        Matrix.translateM(viewMatrix, 0,-x, y,-z);  
171
        }
172
      else if(mType[i] == EffectNames.MOVE.ordinal() )
173
        {
174
        sx = mUniforms[NUM_UNIFORMS*i+3];   
175
        sy = mUniforms[NUM_UNIFORMS*i+4];   
176
        sz = mUniforms[NUM_UNIFORMS*i+5];   
177
        
178
        Matrix.translateM(viewMatrix, 0, sx,-sy, sz);   
179
        }
180
      else if(mType[i] == EffectNames.SCALE.ordinal() )
181
        {
182
        sx = mUniforms[NUM_UNIFORMS*i+3];   
183
        sy = mUniforms[NUM_UNIFORMS*i+4];   
184
        sz = mUniforms[NUM_UNIFORMS*i+5];   
185

    
186
        Matrix.scaleM(viewMatrix, 0, sx, sy, sz);  
187
        }
188
      else if(mType[i] == EffectNames.SHEAR.ordinal() )
189
        {
190
        x  = mUniforms[NUM_UNIFORMS*i  ];
191
        y  = mUniforms[NUM_UNIFORMS*i+1];
192
        z  = mUniforms[NUM_UNIFORMS*i+2];
193
        
194
        sx = mUniforms[NUM_UNIFORMS*i+3];   
195
        sy = mUniforms[NUM_UNIFORMS*i+4];   
196
        sz = mUniforms[NUM_UNIFORMS*i+5];   
197
        
198
        Matrix.translateM(viewMatrix, 0, x,-y, z); 
199
      
200
        viewMatrix[4] += sx*viewMatrix[0]; // Multiply viewMatrix by 1 x 0 0 , i.e. X-shear. TODO: change this so it is symmetric w respect to all the axis.
201
        viewMatrix[5] += sx*viewMatrix[1]; //                        0 1 0 0 
202
        viewMatrix[6] += sx*viewMatrix[2]; //                        0 0 1 0
203
        viewMatrix[7] += sx*viewMatrix[3]; //                        0 0 0 1
204
      
205
        viewMatrix[0] += sy*viewMatrix[4]; // Multiply viewMatrix by 1 0 0 0 , i.e. Y-shear. TODO: change this so it is symmetric w respect to all the axis.
206
        viewMatrix[1] += sy*viewMatrix[5]; //                        y 1 0 0
207
        viewMatrix[2] += sy*viewMatrix[6]; //                        0 0 1 0
208
        viewMatrix[3] += sy*viewMatrix[7]; //                        0 0 0 1      
209
      
210
        // TODO: implement Z-shear.
211
        
212
        Matrix.translateM(viewMatrix, 0,-x, y, -z);
213
        }
214
      }
215
   
216
    Matrix.translateM(viewMatrix, 0, mObjHalfX,-mObjHalfY, -mObjHalfZ);
217
    Matrix.multiplyMM(mMVPMatrix, 0, dp.projectionMatrix, 0, viewMatrix, 0);
218
    
219
    GLES20.glUniform3f( mBmpDH , mObjHalfX, mObjHalfY, mObjHalfZ);
220
    GLES20.glUniform1f( mDepthH, dp.depth);   
221
    GLES20.glUniformMatrix4fv(mMVMatrixH , 1, false, viewMatrix, 0);
222
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
223
    }
224

    
225
///////////////////////////////////////////////////////////////////////////////////////////////////
226
// here construct the ModelView Matrix, but without any effects
227

    
228
  synchronized void sendNoEffects(DistortedProjection dp) 
229
    {
230
    Matrix.setIdentityM(mTmpMatrix, 0);
231
    Matrix.translateM(mTmpMatrix, 0, mObjHalfX-dp.width/2, dp.height/2-mObjHalfY, mObjHalfZ-dp.distance);
232
    Matrix.multiplyMM(mMVPMatrix, 0, dp.projectionMatrix, 0, mTmpMatrix, 0);
233
    
234
    GLES20.glUniform3f( mBmpDH , mObjHalfX, mObjHalfY, mObjHalfZ);
235
    GLES20.glUniform1f( mDepthH, dp.depth);  
236
    GLES20.glUniformMatrix4fv(mMVMatrixH , 1, false, mTmpMatrix, 0);
237
    GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix, 0);
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241
  
242
  synchronized long add(EffectNames eln, Interpolator3D p, Interpolator i)
243
    {
244
    if( mMax[INDEX]>mNumEffects )
245
      {
246
      mInterP[mNumEffects] = p;
247
      mInterI[mNumEffects] = i;
248
      
249
      return addBase(eln);
250
      }
251
      
252
    return -1;
253
    }
254

    
255
///////////////////////////////////////////////////////////////////////////////////////////////////
256
  
257
  synchronized long add(EffectNames eln, float x, float y, float z, Interpolator i)
258
    {
259
    if( mMax[INDEX]>mNumEffects )
260
      {
261
      mInterP[mNumEffects] = null;
262
      mInterI[mNumEffects] = i;
263
      
264
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = x;
265
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = y;
266
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = z;
267
            
268
      return addBase(eln);
269
      }
270
      
271
    return -1;
272
    }
273

    
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
  
276
  synchronized long add(EffectNames eln, float x, float y, float z, Interpolator1D i, float aX, float aY, float aZ)
277
    {
278
    if( mMax[INDEX]>mNumEffects )
279
      {
280
      mInterP[mNumEffects] = null;
281
      mInterI[mNumEffects] = i;
282
      
283
      mUniforms[NUM_UNIFORMS*mNumEffects  ] = x;
284
      mUniforms[NUM_UNIFORMS*mNumEffects+1] = y;
285
      mUniforms[NUM_UNIFORMS*mNumEffects+2] = z;
286
      
287
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = aX;
288
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = aY;  
289
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = aZ;  
290
      
291
      return addBase(eln);
292
      }
293
      
294
    return -1;
295
    }
296

    
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298
  
299
  synchronized long add(EffectNames eln, Interpolator3D p, Interpolator1D i, float aX, float aY, float aZ)
300
    {
301
    if( mMax[INDEX]>mNumEffects )
302
      {
303
      mInterP[mNumEffects] = p;
304
      mInterI[mNumEffects] = i;
305
      
306
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = aX;
307
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = aY;  
308
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = aZ;  
309
      
310
      return addBase(eln);
311
      }
312
      
313
    return -1;
314
    }
315
  
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317
  
318
  synchronized long add(EffectNames eln, float x, float y, float z, float aA, float aX, float aY, float aZ)
319
    {
320
    if( mMax[INDEX]>mNumEffects )
321
      {
322
      mInterP[mNumEffects] = null; 
323
      mInterI[mNumEffects] = null;
324
      
325
      mUniforms[NUM_UNIFORMS*mNumEffects  ] =  x;
326
      mUniforms[NUM_UNIFORMS*mNumEffects+1] =  y;  
327
      mUniforms[NUM_UNIFORMS*mNumEffects+2] =  z;  
328
      mUniforms[NUM_UNIFORMS*mNumEffects+3] = aA;  
329
      mUniforms[NUM_UNIFORMS*mNumEffects+4] = aX;
330
      mUniforms[NUM_UNIFORMS*mNumEffects+5] = aY;  
331
      mUniforms[NUM_UNIFORMS*mNumEffects+6] = aZ;  
332
      
333
      return addBase(eln);   
334
      }
335
      
336
    return -1;
337
    }
338

    
339
///////////////////////////////////////////////////////////////////////////////////////////////////
340
  }
(14-14/30)