Project

General

Profile

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

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

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