Project

General

Profile

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

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

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.effectqueue;
21

    
22
import android.opengl.GLES30;
23
import android.opengl.Matrix;
24

    
25
import org.distorted.library.effect.EffectType;
26
import org.distorted.library.effect.MatrixEffect;
27
import org.distorted.library.message.EffectMessageSender;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
class EffectQueueMatrix extends EffectQueue
32
  {
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
  private static final int INDEX = EffectType.MATRIX.ordinal();
37

    
38
  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

    
42
  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

    
46
///////////////////////////////////////////////////////////////////////////////////////////////////
47
   
48
  EffectQueueMatrix()
49
    { 
50
    super(NUM_FLOAT_UNIFORMS, NUM_INT_UNIFORMS, INDEX);
51
    }
52

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

    
55
  EffectQueueMatrix(EffectQueueMatrix source)
56
    {
57
    super(source);
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  static void uniforms(int mProgramH, int variant)
63
    {
64
    mMVPMatrixH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
65
    mMVMatrixPH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixP");
66
    mMVMatrixVH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixV");
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  void compute(long currTime)
72
    {
73
    if( currTime==mTime ) return;
74
    if( mTime==0 ) mTime = currTime;
75
    long step = (currTime-mTime);
76
    float[] array = mUBF.getBackingArray();
77

    
78
    for(int i=0; i<mNumEffects; i++)
79
      {
80
      if( mEffects[i].compute(array, NUM_FLOAT_UNIFORMS*i, currTime, step) )
81
        {
82
        EffectMessageSender.newMessage(mEffects[i]);
83
        }
84
      }
85
     
86
    mTime = currTime;  
87
    }  
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  void send(float distance, float mipmap, float[] projection, int variant)
92
    {
93
    Matrix.setIdentityM(mModelViewMatrixP, 0);
94
    Matrix.translateM(mModelViewMatrixP, 0, 0,0, -distance);
95

    
96
    if( mipmap!=1 )
97
      {
98
      Matrix.scaleM(mModelViewMatrixP, 0, mipmap, mipmap, mipmap);
99
      }
100

    
101
    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

    
118
    float[] array = mUBF.getBackingArray();
119

    
120
    // the 'Model' part of the MV matrix
121
    for(int i=mNumEffects-1; i>=0; i--)
122
      {
123
      ((MatrixEffect)mEffects[i]).apply(mModelViewMatrixP,mModelViewMatrixV,array,i);
124
      }
125

    
126
    // combined Model-View-Projection matrix
127
    Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mModelViewMatrixP, 0);
128

    
129
    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
    }
133
  }
(3-3/5)