Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueueMatrix.java @ de77a6c5

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 46b572b5 Leszek Koltunski
// Distorted is free software: you can redistribute it and/or modify                             //
7 d333eb6b Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// Distorted is distributed in the hope that it will be useful,                                  //
12 d333eb6b Leszek Koltunski
// 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 46b572b5 Leszek Koltunski
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 809dcae3 Leszek Koltunski
package org.distorted.library.effectqueue;
21 6a06a912 Leszek Koltunski
22 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
23 6a06a912 Leszek Koltunski
import android.opengl.Matrix;
24
25 da9b3f07 Leszek Koltunski
import org.distorted.library.effect.EffectType;
26 fe82a979 Leszek Koltunski
import org.distorted.library.effect.MatrixEffect;
27 ed06301f Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
28 a4835695 Leszek Koltunski
29 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
30
31 d07f2950 Leszek Koltunski
class EffectQueueMatrix extends EffectQueue
32 96e3b88a Leszek Koltunski
  {
33
  private static final int NUM_FLOAT_UNIFORMS = MatrixEffect.NUM_FLOAT_UNIFORMS;
34
  private static final int NUM_INT_UNIFORMS   = MatrixEffect.NUM_INT_UNIFORMS;
35
36 da9b3f07 Leszek Koltunski
  private static final int INDEX = EffectType.MATRIX.ordinal();
37 1a940548 Leszek Koltunski
38 de77a6c5 Leszek Koltunski
  private static final float[] mMVPMatrix       = new float[16];
39
  private static final float[] mModelViewMatrixP= new float[16];
40
  private static final float[] mModelViewMatrixV= new float[16];
41 1a940548 Leszek Koltunski
42 de77a6c5 Leszek Koltunski
  private static final int[] mMVPMatrixH = new int[MAIN_VARIANTS];
43
  private static final int[] mMVMatrixPH = new int[MAIN_VARIANTS];
44
  private static final int[] mMVMatrixVH = new int[MAIN_VARIANTS];
45 bed13bea leszek
46 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
47
   
48 20dbec0e Leszek Koltunski
  EffectQueueMatrix()
49 6a06a912 Leszek Koltunski
    { 
50 96e3b88a Leszek Koltunski
    super(NUM_FLOAT_UNIFORMS, NUM_INT_UNIFORMS, INDEX);
51 6a06a912 Leszek Koltunski
    }
52
53 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
55
  EffectQueueMatrix(EffectQueueMatrix source)
56
    {
57
    super(source);
58
    }
59
60 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 809dcae3 Leszek Koltunski
  static void uniforms(int mProgramH, int variant)
63 c1a38ba3 Leszek Koltunski
    {
64 b7074bc6 Leszek Koltunski
    mMVPMatrixH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
65 62c869ad Leszek Koltunski
    mMVMatrixPH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixP");
66
    mMVMatrixVH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixV");
67 c1a38ba3 Leszek Koltunski
    }
68
69 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
70
71 735a8757 Leszek Koltunski
  void compute(long currTime)
72 02de77c9 Leszek Koltunski
    {
73
    if( currTime==mTime ) return;
74
    if( mTime==0 ) mTime = currTime;
75
    long step = (currTime-mTime);
76 de77a6c5 Leszek Koltunski
    float[] array = mUBF.getBackingArray();
77 15aa7d94 Leszek Koltunski
78 02de77c9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
79
      {
80 de77a6c5 Leszek Koltunski
      if( mEffects[i].compute(array, NUM_FLOAT_UNIFORMS*i, currTime, step) )
81 02de77c9 Leszek Koltunski
        {
82 20dbec0e Leszek Koltunski
        EffectMessageSender.newMessage(mEffects[i]);
83 02de77c9 Leszek Koltunski
        }
84
      }
85
     
86
    mTime = currTime;  
87
    }  
88
89 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
91 62c869ad Leszek Koltunski
  void send(float distance, float mipmap, float[] projection, int variant)
92 3fc9327a Leszek Koltunski
    {
93 62c869ad Leszek Koltunski
    Matrix.setIdentityM(mModelViewMatrixP, 0);
94
    Matrix.translateM(mModelViewMatrixP, 0, 0,0, -distance);
95 02de77c9 Leszek Koltunski
96 62c869ad Leszek Koltunski
    if( mipmap!=1 )
97
      {
98
      Matrix.scaleM(mModelViewMatrixP, 0, mipmap, mipmap, mipmap);
99
      }
100 3eb6f354 Leszek Koltunski
101 62c869ad Leszek Koltunski
    mModelViewMatrixV[ 0] = mModelViewMatrixP[ 0];
102
    mModelViewMatrixV[ 1] = mModelViewMatrixP[ 1];
103
    mModelViewMatrixV[ 2] = mModelViewMatrixP[ 2];
104
    mModelViewMatrixV[ 3] = mModelViewMatrixP[ 3];
105
    mModelViewMatrixV[ 4] = mModelViewMatrixP[ 4];
106
    mModelViewMatrixV[ 5] = mModelViewMatrixP[ 5];
107
    mModelViewMatrixV[ 6] = mModelViewMatrixP[ 6];
108
    mModelViewMatrixV[ 7] = mModelViewMatrixP[ 7];
109
    mModelViewMatrixV[ 8] = mModelViewMatrixP[ 8];
110
    mModelViewMatrixV[ 9] = mModelViewMatrixP[ 9];
111
    mModelViewMatrixV[10] = mModelViewMatrixP[10];
112
    mModelViewMatrixV[11] = mModelViewMatrixP[11];
113
    mModelViewMatrixV[12] = mModelViewMatrixP[12];
114
    mModelViewMatrixV[13] = mModelViewMatrixP[13];
115
    mModelViewMatrixV[14] = mModelViewMatrixP[14];
116
    mModelViewMatrixV[15] = mModelViewMatrixP[15];
117 c1a38ba3 Leszek Koltunski
118 de77a6c5 Leszek Koltunski
    float[] array = mUBF.getBackingArray();
119
120 7490d738 Leszek Koltunski
    // the 'Model' part of the MV matrix
121 62c869ad Leszek Koltunski
    for(int i=mNumEffects-1; i>=0; i--)
122
      {
123 de77a6c5 Leszek Koltunski
      ((MatrixEffect)mEffects[i]).apply(mModelViewMatrixP,mModelViewMatrixV,array,i);
124 62c869ad Leszek Koltunski
      }
125 7490d738 Leszek Koltunski
126
    // combined Model-View-Projection matrix
127 62c869ad Leszek Koltunski
    Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mModelViewMatrixP, 0);
128 c1a38ba3 Leszek Koltunski
129 62c869ad Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVMatrixVH[variant], 1, false, mModelViewMatrixV, 0);
130
    GLES30.glUniformMatrix4fv(mMVMatrixPH[variant], 1, false, mModelViewMatrixP, 0);
131
    GLES30.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix       , 0);
132 c1a38ba3 Leszek Koltunski
    }
133 6a06a912 Leszek Koltunski
  }