Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueueMatrix.java @ 8e88389e

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

    
22
import android.opengl.GLES31;
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.EffectMessage;
28
import org.distorted.library.message.EffectMessageSender;
29

    
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31

    
32
class EffectQueueMatrix extends EffectQueue
33
  {   
34
  private static final int NUM_UNIFORMS = MatrixEffect.NUM_UNIFORMS;
35
  private static final int INDEX = EffectType.MATRIX.ordinal();
36

    
37
  private static float[] mMVPMatrix = new float[16];
38
  private static float[] mViewMatrix= new float[16];
39

    
40
  private static int[] mObjDH      = new int[Distorted.MAIN_VARIANTS];
41
  private static int[] mMVPMatrixH = new int[Distorted.MAIN_VARIANTS];
42
  private static int[] mMVMatrixH  = new int[Distorted.MAIN_VARIANTS];
43

    
44
  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
///////////////////////////////////////////////////////////////////////////////////////////////////
50
   
51
  EffectQueueMatrix(long id)
52
    { 
53
    super(id,NUM_UNIFORMS,INDEX );
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  static void getUniforms(int mProgramH, int variant)
59
    {
60
    mObjDH[variant]     = GLES31.glGetUniformLocation(mProgramH, "u_objD");
61
    mMVPMatrixH[variant]= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
62
    mMVMatrixH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
63
    }
64

    
65
///////////////////////////////////////////////////////////////////////////////////////////////////
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
  float magnify(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
84
    {
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
    Matrix.multiplyMM(mTmpMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
93

    
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
    float xLenInPixels = projection.mWidth *(mMaxX-mMinX)/2;
104
    float yLenInPixels = projection.mHeight*(mMaxY-mMinY)/2;
105

    
106
    // already margin / min(xLen,yLen) is the size of the halo.
107
    // 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
    //
110
    // mMipmap ( 1.0 , 0.5, 0.25, 0.125 ) - we need to make the size of the halo independent
111
    // of postprocessing effect quality.
112

    
113
    return projection.mMipmap*2.0f*marginInPixels/( xLenInPixels>yLenInPixels ? yLenInPixels:xLenInPixels );
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117

    
118
  void compute(long currTime)
119
    {
120
    if( currTime==mTime ) return;
121
    if( mTime==0 ) mTime = currTime;
122
    long step = (currTime-mTime);
123

    
124
    for(int i=0; i<mNumEffects; i++)
125
      {
126
      mCurrentDuration[i] += step;
127

    
128
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
129
        {
130
        for(int j=0; j<mNumListeners; j++)
131
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
132

    
133
        if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
134
          {
135
          remove(i--);
136
          mNumEffectsToBe--;
137
          }
138
        }
139
      }
140
     
141
    mTime = currTime;  
142
    }  
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  float[] getMVP()
147
    {
148
    return mMVPMatrix;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, int variant)
154
    {
155
    Matrix.setIdentityM(mViewMatrix, 0);
156
    Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
157
    float mipmap = projection.mMipmap;
158
    if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
159

    
160
    for(int i=0; i<mNumEffects; i++) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
161

    
162
    Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
163
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
164

    
165
    GLES31.glUniform3f( mObjDH[variant] , halfX, halfY, halfZ);
166
    GLES31.glUniformMatrix4fv(mMVMatrixH[variant] , 1, false, mViewMatrix, 0);
167
    GLES31.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix , 0);
168
    }
169
  }
(15-15/17)