Project

General

Profile

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

library / src / main / java / org / distorted / library / effectqueue / EffectQueueMatrix.java @ 7602a827

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4 7602a827 Leszek Koltunski
// This file is part of DistortedLibrary.                                                               //
5 d333eb6b Leszek Koltunski
//                                                                                               //
6 7602a827 Leszek Koltunski
// DistortedLibrary 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 7602a827 Leszek Koltunski
// DistortedLibrary 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 7602a827 Leszek Koltunski
// along with DistortedLibrary.  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 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 809dcae3 Leszek Koltunski
  private static int[] mObjDH      = new int[MAIN_VARIANTS];
41
  private static int[] mMVPMatrixH = new int[MAIN_VARIANTS];
42
  private static int[] mMVMatrixH  = new int[MAIN_VARIANTS];
43 bed13bea leszek
44 8e88389e Leszek Koltunski
  private static float[] mTmpMatrix = new float[16];
45
  private static float[] mTmpResult = new float[4];
46
  private static float[] mTmpPoint  = new float[4];
47
  private static float mMinX,mMaxX,mMinY,mMaxY;
48
49 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
50
   
51 0a046359 Leszek Koltunski
  EffectQueueMatrix(long id)
52 6a06a912 Leszek Koltunski
    { 
53 15aa7d94 Leszek Koltunski
    super(id,NUM_UNIFORMS,INDEX );
54 6a06a912 Leszek Koltunski
    }
55
56 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
57
58 809dcae3 Leszek Koltunski
  static void uniforms(int mProgramH, int variant)
59 c1a38ba3 Leszek Koltunski
    {
60 86c352e0 Leszek Koltunski
    mObjDH[variant]     = GLES31.glGetUniformLocation(mProgramH, "u_objD");
61
    mMVPMatrixH[variant]= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
62
    mMVMatrixH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
63 c1a38ba3 Leszek Koltunski
    }
64
65 8e88389e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
66
67
  private void magnifyDir()
68
    {
69
    Matrix.multiplyMV(mTmpResult,0,mTmpMatrix,0,mTmpPoint,0);
70
    float nx = mTmpResult[0]/mTmpResult[3];
71
    float ny = mTmpResult[1]/mTmpResult[3];
72
73
    if( nx<mMinX ) mMinX = nx;
74
    if( nx>mMaxX ) mMaxX = nx;
75
    if( ny<mMinY ) mMinY = ny;
76
    if( ny>mMaxY ) mMaxY = ny;
77
    }
78
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// return a float which describes how much larger an object must be so that it appears to be (about)
81
// 'marginInPixels' pixels larger in each direction. Used in Postprocessing.
82
83 809dcae3 Leszek Koltunski
  float magnify(float[] projection, int width, int height, float mipmap, float halfX, float halfY, float halfZ, float marginInPixels)
84 8e88389e Leszek Koltunski
    {
85
    mMinX = Integer.MAX_VALUE;
86
    mMaxX = Integer.MIN_VALUE;
87
    mMinY = Integer.MAX_VALUE;
88
    mMaxY = Integer.MIN_VALUE;
89
90
    mTmpPoint[3] = 1.0f;
91
92 809dcae3 Leszek Koltunski
    Matrix.multiplyMM(mTmpMatrix, 0, projection, 0, mViewMatrix, 0);
93 8e88389e Leszek Koltunski
94
    mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
95
    mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = -halfZ; magnifyDir();
96
    mTmpPoint[0] = +halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = +halfZ; magnifyDir();
97
    mTmpPoint[0] = +halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = -halfZ; magnifyDir();
98
    mTmpPoint[0] = -halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
99
    mTmpPoint[0] = -halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = -halfZ; magnifyDir();
100
    mTmpPoint[0] = -halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = +halfZ; magnifyDir();
101
    mTmpPoint[0] = -halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = -halfZ; magnifyDir();
102
103 809dcae3 Leszek Koltunski
    float xLenInPixels = width *(mMaxX-mMinX)/2;
104
    float yLenInPixels = height*(mMaxY-mMinY)/2;
105 8e88389e Leszek Koltunski
106 2f1f7570 Leszek Koltunski
    // already margin / avg(xLen,yLen) is the size of the halo.
107 8e88389e Leszek Koltunski
    // Here we need a bit more because we are marking not only the halo, but a little bit around
108
    // it as well so that the (blur for example) will be smooth on the edges. Thus the 2.0f.
109 2f1f7570 Leszek Koltunski
    // ( 4.0 because there is an extra 2.0 from the avg(xLen,yLen) )
110 8e88389e Leszek Koltunski
    //
111 20156af7 Leszek Koltunski
    // mMipmap ( 1.0, 0.5, 0.25, 0.125 ) - we need to make the size of the halo independent
112 8e88389e Leszek Koltunski
    // of postprocessing effect quality.
113
114 809dcae3 Leszek Koltunski
    return mipmap*4.0f*marginInPixels/( xLenInPixels+yLenInPixels );
115 8e88389e Leszek Koltunski
    }
116
117 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
118
119 735a8757 Leszek Koltunski
  void compute(long currTime)
120 02de77c9 Leszek Koltunski
    {
121
    if( currTime==mTime ) return;
122
    if( mTime==0 ) mTime = currTime;
123
    long step = (currTime-mTime);
124 15aa7d94 Leszek Koltunski
125 02de77c9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
126
      {
127 3a70bd6d leszek
      mCurrentDuration[i] += step;
128
129 15aa7d94 Leszek Koltunski
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
130 02de77c9 Leszek Koltunski
        {
131
        for(int j=0; j<mNumListeners; j++)
132 26a4e5f6 leszek
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
133 02de77c9 Leszek Koltunski
        }
134
      }
135
     
136
    mTime = currTime;  
137
    }  
138
139 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
140
141
  float[] getMVP()
142
    {
143
    return mMVPMatrix;
144
    }
145
146 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148 809dcae3 Leszek Koltunski
  void send(int width, int height, float distance, float mipmap, float[] projection, float halfX, float halfY, float halfZ, int variant)
149 c1a38ba3 Leszek Koltunski
    {
150
    Matrix.setIdentityM(mViewMatrix, 0);
151 809dcae3 Leszek Koltunski
    Matrix.translateM(mViewMatrix, 0, -width*0.5f, -height*0.5f, -distance);
152 c1a38ba3 Leszek Koltunski
    if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
153
154
    for(int i=0; i<mNumEffects; i++) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
155
156 7bebb196 Leszek Koltunski
    Matrix.translateM(mViewMatrix, 0, halfX,halfY,halfZ);
157 809dcae3 Leszek Koltunski
    Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mViewMatrix, 0);
158 c1a38ba3 Leszek Koltunski
159 86c352e0 Leszek Koltunski
    GLES31.glUniform3f( mObjDH[variant] , halfX, halfY, halfZ);
160
    GLES31.glUniformMatrix4fv(mMVMatrixH[variant] , 1, false, mViewMatrix, 0);
161
    GLES31.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix , 0);
162 c1a38ba3 Leszek Koltunski
    }
163 6a06a912 Leszek Koltunski
  }