Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueueMatrix.java @ 7a5e538a

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
21 6a06a912 Leszek Koltunski
22 e6519ac8 Leszek Koltunski
import android.opengl.GLES31;
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 e458a4ba Leszek Koltunski
import org.distorted.library.message.EffectMessage;
28 ed06301f Leszek Koltunski
import org.distorted.library.message.EffectMessageSender;
29 a4835695 Leszek Koltunski
30 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 d07f2950 Leszek Koltunski
class EffectQueueMatrix extends EffectQueue
33 6a06a912 Leszek Koltunski
  {   
34 15aa7d94 Leszek Koltunski
  private static final int NUM_UNIFORMS = MatrixEffect.NUM_UNIFORMS;
35 da9b3f07 Leszek Koltunski
  private static final int INDEX = EffectType.MATRIX.ordinal();
36 1a940548 Leszek Koltunski
37
  private static float[] mMVPMatrix = new float[16];
38
  private static float[] mViewMatrix= new float[16];
39
40 86c352e0 Leszek Koltunski
  private static int[] mObjDH      = new int[Distorted.MAIN_VARIANTS];
41
  private static int[] mMVPMatrixH = new int[Distorted.MAIN_VARIANTS];
42
  private static int[] mMVMatrixH  = new int[Distorted.MAIN_VARIANTS];
43 bed13bea leszek
44 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
45
   
46 0a046359 Leszek Koltunski
  EffectQueueMatrix(long id)
47 6a06a912 Leszek Koltunski
    { 
48 15aa7d94 Leszek Koltunski
    super(id,NUM_UNIFORMS,INDEX );
49 6a06a912 Leszek Koltunski
    }
50
51 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
52
53 86c352e0 Leszek Koltunski
  static void getUniforms(int mProgramH, int variant)
54 c1a38ba3 Leszek Koltunski
    {
55 86c352e0 Leszek Koltunski
    mObjDH[variant]     = GLES31.glGetUniformLocation(mProgramH, "u_objD");
56
    mMVPMatrixH[variant]= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
57
    mMVMatrixH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
58 c1a38ba3 Leszek Koltunski
    }
59
60 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
61
62 735a8757 Leszek Koltunski
  void compute(long currTime)
63 02de77c9 Leszek Koltunski
    {
64
    if( currTime==mTime ) return;
65
    if( mTime==0 ) mTime = currTime;
66
    long step = (currTime-mTime);
67 15aa7d94 Leszek Koltunski
68 02de77c9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
69
      {
70 3a70bd6d leszek
      mCurrentDuration[i] += step;
71
72 15aa7d94 Leszek Koltunski
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
73 02de77c9 Leszek Koltunski
        {
74
        for(int j=0; j<mNumListeners; j++)
75 26a4e5f6 leszek
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
76 02de77c9 Leszek Koltunski
77 da9b3f07 Leszek Koltunski
        if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
78 6b0b4f60 leszek
          {
79 15aa7d94 Leszek Koltunski
          remove(i--);
80 6b0b4f60 leszek
          mNumEffectsToBe--;
81
          }
82 02de77c9 Leszek Koltunski
        }
83
      }
84
     
85
    mTime = currTime;  
86
    }  
87
88 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90
  float[] getMVP()
91
    {
92
    return mMVPMatrix;
93
    }
94
95 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
96
97 7a5e538a Leszek Koltunski
  void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, int variant)
98 c1a38ba3 Leszek Koltunski
    {
99
    Matrix.setIdentityM(mViewMatrix, 0);
100
    Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
101
    float mipmap = projection.mMipmap;
102
    if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
103
104
    for(int i=0; i<mNumEffects; i++) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
105
106
    Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
107
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
108
109 86c352e0 Leszek Koltunski
    GLES31.glUniform3f( mObjDH[variant] , halfX, halfY, halfZ);
110
    GLES31.glUniformMatrix4fv(mMVMatrixH[variant] , 1, false, mViewMatrix, 0);
111
    GLES31.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix , 0);
112 c1a38ba3 Leszek Koltunski
    }
113 6a06a912 Leszek Koltunski
  }