Project

General

Profile

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

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

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