Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueueMatrix.java @ 1dec66e0

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2 dbdf8a73 Leszek Koltunski
// Copyright 2016 Leszek Koltunski  leszek@koltunski.pl                                          //
3 d333eb6b Leszek Koltunski
//                                                                                               //
4 46b572b5 Leszek Koltunski
// This file is part of Distorted.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 dbdf8a73 Leszek Koltunski
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10 d333eb6b Leszek Koltunski
//                                                                                               //
11 dbdf8a73 Leszek Koltunski
// This library 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 dbdf8a73 Leszek Koltunski
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU                             //
14
// Lesser General Public License for more details.                                               //
15 d333eb6b Leszek Koltunski
//                                                                                               //
16 dbdf8a73 Leszek Koltunski
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21 809dcae3 Leszek Koltunski
package org.distorted.library.effectqueue;
22 6a06a912 Leszek Koltunski
23 b7074bc6 Leszek Koltunski
import android.opengl.GLES30;
24 6a06a912 Leszek Koltunski
25 1dec66e0 Leszek Koltunski
import org.distorted.library.helpers.MatrixHelper;
26 da9b3f07 Leszek Koltunski
import org.distorted.library.effect.EffectType;
27 fe82a979 Leszek Koltunski
import org.distorted.library.effect.MatrixEffect;
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 96e3b88a Leszek Koltunski
  {
34
  private static final int NUM_FLOAT_UNIFORMS = MatrixEffect.NUM_FLOAT_UNIFORMS;
35
  private static final int NUM_INT_UNIFORMS   = MatrixEffect.NUM_INT_UNIFORMS;
36 2b7d2abb Leszek Koltunski
  private static final boolean USE_UBO        = false;
37 da9b3f07 Leszek Koltunski
  private static final int INDEX = EffectType.MATRIX.ordinal();
38 1a940548 Leszek Koltunski
39 de77a6c5 Leszek Koltunski
  private static final float[] mMVPMatrix       = new float[16];
40
  private static final float[] mModelViewMatrixP= new float[16];
41
  private static final float[] mModelViewMatrixV= new float[16];
42 1a940548 Leszek Koltunski
43 de77a6c5 Leszek Koltunski
  private static final int[] mMVPMatrixH = new int[MAIN_VARIANTS];
44
  private static final int[] mMVMatrixPH = new int[MAIN_VARIANTS];
45
  private static final int[] mMVMatrixVH = new int[MAIN_VARIANTS];
46 bed13bea leszek
47 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
48
   
49 20dbec0e Leszek Koltunski
  EffectQueueMatrix()
50 6a06a912 Leszek Koltunski
    { 
51 2b7d2abb Leszek Koltunski
    super(NUM_FLOAT_UNIFORMS, NUM_INT_UNIFORMS, USE_UBO, INDEX);
52 6a06a912 Leszek Koltunski
    }
53
54 f046b159 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
56
  EffectQueueMatrix(EffectQueueMatrix source)
57
    {
58
    super(source);
59
    }
60
61 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 809dcae3 Leszek Koltunski
  static void uniforms(int mProgramH, int variant)
64 c1a38ba3 Leszek Koltunski
    {
65 b7074bc6 Leszek Koltunski
    mMVPMatrixH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
66 62c869ad Leszek Koltunski
    mMVMatrixPH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixP");
67
    mMVMatrixVH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVMatrixV");
68 c1a38ba3 Leszek Koltunski
    }
69
70 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 835b197e Leszek Koltunski
  void compute(long currTime, long step)
73 02de77c9 Leszek Koltunski
    {
74 835b197e Leszek Koltunski
    if( mNumEffects>0 )
75 02de77c9 Leszek Koltunski
      {
76 835b197e Leszek Koltunski
      float[] array = mUBF.getBackingArray();
77
78
      for(int i=0; i<mNumEffects; i++)
79 02de77c9 Leszek Koltunski
        {
80 835b197e Leszek Koltunski
        if( mEffects[i].compute(array, NUM_FLOAT_UNIFORMS*i, currTime, step) )
81
          {
82
          EffectMessageSender.newMessage(mEffects[i]);
83
          }
84 02de77c9 Leszek Koltunski
        }
85
      }
86
    }  
87
88 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
89
90 62c869ad Leszek Koltunski
  void send(float distance, float mipmap, float[] projection, int variant)
91 3fc9327a Leszek Koltunski
    {
92 1dec66e0 Leszek Koltunski
    // i.e. setIdentity(); translate(0,0,-distance); scale(mipmap,mipmap,mipmap)
93
    mModelViewMatrixV[ 0] = mModelViewMatrixP[ 0] = mipmap;
94
    mModelViewMatrixV[ 1] = mModelViewMatrixP[ 1] = 0;
95
    mModelViewMatrixV[ 2] = mModelViewMatrixP[ 2] = 0;
96
    mModelViewMatrixV[ 3] = mModelViewMatrixP[ 3] = 0;
97
    mModelViewMatrixV[ 4] = mModelViewMatrixP[ 4] = 0;
98
    mModelViewMatrixV[ 5] = mModelViewMatrixP[ 5] = mipmap;
99
    mModelViewMatrixV[ 6] = mModelViewMatrixP[ 6] = 0;
100
    mModelViewMatrixV[ 7] = mModelViewMatrixP[ 7] = 0;
101
    mModelViewMatrixV[ 8] = mModelViewMatrixP[ 8] = 0;
102
    mModelViewMatrixV[ 9] = mModelViewMatrixP[ 9] = 0;
103
    mModelViewMatrixV[10] = mModelViewMatrixP[10] = mipmap;
104
    mModelViewMatrixV[11] = mModelViewMatrixP[11] = 0;
105
    mModelViewMatrixV[12] = mModelViewMatrixP[12] = 0;
106
    mModelViewMatrixV[13] = mModelViewMatrixP[13] = 0;
107
    mModelViewMatrixV[14] = mModelViewMatrixP[14] = -distance;
108
    mModelViewMatrixV[15] = mModelViewMatrixP[15] = 1;
109 c1a38ba3 Leszek Koltunski
110 de77a6c5 Leszek Koltunski
    float[] array = mUBF.getBackingArray();
111
112 7490d738 Leszek Koltunski
    // the 'Model' part of the MV matrix
113 62c869ad Leszek Koltunski
    for(int i=mNumEffects-1; i>=0; i--)
114
      {
115 de77a6c5 Leszek Koltunski
      ((MatrixEffect)mEffects[i]).apply(mModelViewMatrixP,mModelViewMatrixV,array,i);
116 62c869ad Leszek Koltunski
      }
117 7490d738 Leszek Koltunski
118
    // combined Model-View-Projection matrix
119 1dec66e0 Leszek Koltunski
    MatrixHelper.multiply(mMVPMatrix, projection, mModelViewMatrixP);
120 c1a38ba3 Leszek Koltunski
121 62c869ad Leszek Koltunski
    GLES30.glUniformMatrix4fv(mMVMatrixVH[variant], 1, false, mModelViewMatrixV, 0);
122
    GLES30.glUniformMatrix4fv(mMVMatrixPH[variant], 1, false, mModelViewMatrixP, 0);
123
    GLES30.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix       , 0);
124 c1a38ba3 Leszek Koltunski
    }
125 6a06a912 Leszek Koltunski
  }