Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueueMatrix.java @ 62c869ad

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 6a06a912 Leszek Koltunski
  {   
33 15aa7d94 Leszek Koltunski
  private static final int NUM_UNIFORMS = MatrixEffect.NUM_UNIFORMS;
34 da9b3f07 Leszek Koltunski
  private static final int INDEX = EffectType.MATRIX.ordinal();
35 1a940548 Leszek Koltunski
36 7490d738 Leszek Koltunski
  private static float[] mMVPMatrix       = new float[16];
37 62c869ad Leszek Koltunski
  private static float[] mModelViewMatrixP= new float[16];
38
  private static float[] mModelViewMatrixV= new float[16];
39 1a940548 Leszek Koltunski
40 809dcae3 Leszek Koltunski
  private static int[] mMVPMatrixH = new int[MAIN_VARIANTS];
41 62c869ad Leszek Koltunski
  private static int[] mMVMatrixPH = new int[MAIN_VARIANTS];
42
  private static int[] mMVMatrixVH = new int[MAIN_VARIANTS];
43 bed13bea leszek
44 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45
   
46 20dbec0e Leszek Koltunski
  EffectQueueMatrix()
47 6a06a912 Leszek Koltunski
    { 
48 20dbec0e Leszek Koltunski
    super(NUM_UNIFORMS,INDEX );
49 6a06a912 Leszek Koltunski
    }
50
51 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53
  EffectQueueMatrix(EffectQueueMatrix source)
54
    {
55
    super(source);
56
    }
57
58 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
59
60 809dcae3 Leszek Koltunski
  static void uniforms(int mProgramH, int variant)
61 c1a38ba3 Leszek Koltunski
    {
62 b7074bc6 Leszek Koltunski
    mMVPMatrixH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
63 62c869ad Leszek Koltunski
    mMVMatrixPH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixP");
64
    mMVMatrixVH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixV");
65 c1a38ba3 Leszek Koltunski
    }
66
67 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
68
69 735a8757 Leszek Koltunski
  void compute(long currTime)
70 02de77c9 Leszek Koltunski
    {
71
    if( currTime==mTime ) return;
72
    if( mTime==0 ) mTime = currTime;
73
    long step = (currTime-mTime);
74 15aa7d94 Leszek Koltunski
75 02de77c9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
76
      {
77 0273ef2a Leszek Koltunski
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, currTime, step) )
78 02de77c9 Leszek Koltunski
        {
79 20dbec0e Leszek Koltunski
        EffectMessageSender.newMessage(mEffects[i]);
80 02de77c9 Leszek Koltunski
        }
81
      }
82
     
83
    mTime = currTime;  
84
    }  
85
86 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
87
88 62c869ad Leszek Koltunski
  void send(float distance, float mipmap, float[] projection, int variant)
89 3fc9327a Leszek Koltunski
    {
90 62c869ad Leszek Koltunski
    Matrix.setIdentityM(mModelViewMatrixP, 0);
91
    Matrix.translateM(mModelViewMatrixP, 0, 0,0, -distance);
92 02de77c9 Leszek Koltunski
93 62c869ad Leszek Koltunski
    if( mipmap!=1 )
94
      {
95
      Matrix.scaleM(mModelViewMatrixP, 0, mipmap, mipmap, mipmap);
96
      }
97 3eb6f354 Leszek Koltunski
98 62c869ad Leszek Koltunski
    mModelViewMatrixV[ 0] = mModelViewMatrixP[ 0];
99
    mModelViewMatrixV[ 1] = mModelViewMatrixP[ 1];
100
    mModelViewMatrixV[ 2] = mModelViewMatrixP[ 2];
101
    mModelViewMatrixV[ 3] = mModelViewMatrixP[ 3];
102
    mModelViewMatrixV[ 4] = mModelViewMatrixP[ 4];
103
    mModelViewMatrixV[ 5] = mModelViewMatrixP[ 5];
104
    mModelViewMatrixV[ 6] = mModelViewMatrixP[ 6];
105
    mModelViewMatrixV[ 7] = mModelViewMatrixP[ 7];
106
    mModelViewMatrixV[ 8] = mModelViewMatrixP[ 8];
107
    mModelViewMatrixV[ 9] = mModelViewMatrixP[ 9];
108
    mModelViewMatrixV[10] = mModelViewMatrixP[10];
109
    mModelViewMatrixV[11] = mModelViewMatrixP[11];
110
    mModelViewMatrixV[12] = mModelViewMatrixP[12];
111
    mModelViewMatrixV[13] = mModelViewMatrixP[13];
112
    mModelViewMatrixV[14] = mModelViewMatrixP[14];
113
    mModelViewMatrixV[15] = mModelViewMatrixP[15];
114 c1a38ba3 Leszek Koltunski
115 7490d738 Leszek Koltunski
    // the 'Model' part of the MV matrix
116 62c869ad Leszek Koltunski
    for(int i=mNumEffects-1; i>=0; i--)
117
      {
118
      ((MatrixEffect)mEffects[i]).apply(mModelViewMatrixP,mModelViewMatrixV,mUniforms,i);
119
      }
120 7490d738 Leszek Koltunski
121
    // combined Model-View-Projection matrix
122 62c869ad Leszek Koltunski
    Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mModelViewMatrixP, 0);
123 c1a38ba3 Leszek Koltunski
124 62c869ad Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVMatrixVH[variant], 1, false, mModelViewMatrixV, 0);
125
    GLES30.glUniformMatrix4fv(mMVMatrixPH[variant], 1, false, mModelViewMatrixP, 0);
126
    GLES30.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix       , 0);
127 c1a38ba3 Leszek Koltunski
    }
128 6a06a912 Leszek Koltunski
  }