Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueueMatrix.java @ 9a3607b3

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
  private static float[] mTmpMatrix = new float[16];
40
  private static float[] mTmpResult = new float[4];
41
  private static float[] mTmpPoint  = new float[4];
42
  private static float mMinx;
43
  private static float mMaxx;
44
  private static float mMiny;
45
  private static float mMaxy;
46

    
47
  private static int[] mObjDH      = new int[Distorted.MAIN_VARIANTS];
48
  private static int[] mMVPMatrixH = new int[Distorted.MAIN_VARIANTS];
49
  private static int[] mMVMatrixH  = new int[Distorted.MAIN_VARIANTS];
50

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52
   
53
  EffectQueueMatrix(long id)
54
    { 
55
    super(id,NUM_UNIFORMS,INDEX );
56
    }
57

    
58
///////////////////////////////////////////////////////////////////////////////////////////////////
59

    
60
  private void magnifyDir()
61
    {
62
    Matrix.multiplyMV(mTmpResult,0,mTmpMatrix,0,mTmpPoint,0);
63
    float nx = mTmpResult[0]/mTmpResult[3];
64
    float ny = mTmpResult[1]/mTmpResult[3];
65

    
66
    if( nx<mMinx ) mMinx = nx;
67
    if( nx>mMaxx ) mMaxx = nx;
68
    if( ny<mMiny ) mMiny = ny;
69
    if( ny>mMaxy ) mMaxy = ny;
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73
// modify the ModelView matrix so that the object drawn appears to be (about) 'marginInPixels' pixels
74
// larger in each direction when rendered. Used in Postprocessing.
75

    
76
  private void magnify(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
77
    {
78
    mMinx = Integer.MAX_VALUE;
79
    mMaxx = Integer.MIN_VALUE;
80
    mMiny = Integer.MAX_VALUE;
81
    mMaxy = Integer.MIN_VALUE;
82

    
83
    mTmpPoint[3] = 1.0f;
84

    
85
    Matrix.multiplyMM(mTmpMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
86

    
87
    mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
88
    mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = -halfZ; magnifyDir();
89
    mTmpPoint[0] = +halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = +halfZ; magnifyDir();
90
    mTmpPoint[0] = +halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = -halfZ; magnifyDir();
91
    mTmpPoint[0] = -halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
92
    mTmpPoint[0] = -halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = -halfZ; magnifyDir();
93
    mTmpPoint[0] = -halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = +halfZ; magnifyDir();
94
    mTmpPoint[0] = -halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = -halfZ; magnifyDir();
95

    
96
    float xlen = projection.mWidth *(mMaxx-mMinx)/2;
97
    float ylen = projection.mHeight*(mMaxy-mMiny)/2;
98
    float scale = 1.0f + 2*marginInPixels/( xlen>ylen ? ylen:xlen );
99

    
100
    Matrix.scaleM(mViewMatrix, 0, scale, scale, scale);
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  static void getUniforms(int mProgramH, int variant)
106
    {
107
    mObjDH[variant]     = GLES31.glGetUniformLocation(mProgramH, "u_objD");
108
    mMVPMatrixH[variant]= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
109
    mMVMatrixH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
110
    }
111

    
112
///////////////////////////////////////////////////////////////////////////////////////////////////
113

    
114
  void compute(long currTime)
115
    {
116
    if( currTime==mTime ) return;
117
    if( mTime==0 ) mTime = currTime;
118
    long step = (currTime-mTime);
119

    
120
    for(int i=0; i<mNumEffects; i++)
121
      {
122
      mCurrentDuration[i] += step;
123

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

    
129
        if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
130
          {
131
          remove(i--);
132
          mNumEffectsToBe--;
133
          }
134
        }
135
      }
136
     
137
    mTime = currTime;  
138
    }  
139

    
140
///////////////////////////////////////////////////////////////////////////////////////////////////
141

    
142
  float[] getMVP()
143
    {
144
    return mMVPMatrix;
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

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

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

    
158
    Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
159
    if( marginInPixels!=0 ) magnify(projection,halfX,halfY,halfZ, marginInPixels);
160
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
161

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