Project

General

Profile

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

library / src / main / java / org / distorted / library / main / EffectQueueMatrix.java @ c1a38ba3

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 fe82a979 Leszek Koltunski
package org.distorted.library.main;
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 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 03aa6c3b Leszek Koltunski
  private static float[] mTmpMatrix = new float[16];
39
  private static float[] mTmpResult = new float[4];
40
  private static float[] mTmpPoint  = new float[4];
41
  private static float mMinx;
42
  private static float mMaxx;
43
  private static float mMiny;
44
  private static float mMaxy;
45 1a940548 Leszek Koltunski
46 c1a38ba3 Leszek Koltunski
  private static int mObjDH;         // This is a handle to half a Object dimensions
47
  private static int mMVPMatrixH;    // the transformation matrix
48
  private static int mMVMatrixH;     // the modelview matrix.
49
50
  private static int mObjDOITH;      //
51
  private static int mMVPMatrixOITH; // Same like above, but in main OIT program.
52
  private static int mMVMatrixOITH;  //
53 bed13bea leszek
54 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
55
   
56 0a046359 Leszek Koltunski
  EffectQueueMatrix(long id)
57 6a06a912 Leszek Koltunski
    { 
58 15aa7d94 Leszek Koltunski
    super(id,NUM_UNIFORMS,INDEX );
59 6a06a912 Leszek Koltunski
    }
60
61 a8162df9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
62
63 03aa6c3b Leszek Koltunski
  private void magnifyDir()
64
    {
65
    Matrix.multiplyMV(mTmpResult,0,mTmpMatrix,0,mTmpPoint,0);
66
    float nx = mTmpResult[0]/mTmpResult[3];
67
    float ny = mTmpResult[1]/mTmpResult[3];
68
69
    if( nx<mMinx ) mMinx = nx;
70
    if( nx>mMaxx ) mMaxx = nx;
71
    if( ny<mMiny ) mMiny = ny;
72
    if( ny>mMaxy ) mMaxy = ny;
73
    }
74
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
// modify the ModelView matrix so that the object drawn appears to be (about) 'marginInPixels' pixels
77 bed13bea leszek
// larger in each direction when rendered. Used in Postprocessing.
78 03aa6c3b Leszek Koltunski
79 270c27bc Leszek Koltunski
  private void magnify(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
80 a8162df9 Leszek Koltunski
    {
81 03aa6c3b Leszek Koltunski
    mMinx = Integer.MAX_VALUE;
82
    mMaxx = Integer.MIN_VALUE;
83
    mMiny = Integer.MAX_VALUE;
84
    mMaxy = Integer.MIN_VALUE;
85
86
    mTmpPoint[3] = 1.0f;
87
88
    Matrix.multiplyMM(mTmpMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
89
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
    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
99
    float xlen = projection.mWidth *(mMaxx-mMinx)/2;
100
    float ylen = projection.mHeight*(mMaxy-mMiny)/2;
101 bed13bea leszek
    float scale = 1.0f + 2*marginInPixels/( xlen>ylen ? ylen:xlen );
102 a15631bc leszek
103 a8162df9 Leszek Koltunski
    Matrix.scaleM(mViewMatrix, 0, scale, scale, scale);
104
    }
105
106 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
107
108 1aedf874 leszek
  static void getUniforms(int mProgramH)
109 02de77c9 Leszek Koltunski
    {
110 e6519ac8 Leszek Koltunski
    mObjDH     = GLES31.glGetUniformLocation(mProgramH, "u_objD");
111
    mMVPMatrixH= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
112
    mMVMatrixH = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix"); 
113 02de77c9 Leszek Koltunski
    }
114
115 c1a38ba3 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
116
117
  static void getUniformsOIT(int mProgramH)
118
    {
119
    mObjDOITH     = GLES31.glGetUniformLocation(mProgramH, "u_objD");
120
    mMVPMatrixOITH= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
121
    mMVMatrixOITH = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
122
    }
123
124 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
125
126 735a8757 Leszek Koltunski
  void compute(long currTime)
127 02de77c9 Leszek Koltunski
    {
128
    if( currTime==mTime ) return;
129
    if( mTime==0 ) mTime = currTime;
130
    long step = (currTime-mTime);
131 15aa7d94 Leszek Koltunski
132 02de77c9 Leszek Koltunski
    for(int i=0; i<mNumEffects; i++)
133
      {
134 3a70bd6d leszek
      mCurrentDuration[i] += step;
135
136 15aa7d94 Leszek Koltunski
      if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
137 02de77c9 Leszek Koltunski
        {
138
        for(int j=0; j<mNumListeners; j++)
139 26a4e5f6 leszek
          EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
140 02de77c9 Leszek Koltunski
141 da9b3f07 Leszek Koltunski
        if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
142 6b0b4f60 leszek
          {
143 15aa7d94 Leszek Koltunski
          remove(i--);
144 6b0b4f60 leszek
          mNumEffectsToBe--;
145
          }
146 02de77c9 Leszek Koltunski
        }
147
      }
148
     
149
    mTime = currTime;  
150
    }  
151
152 3fc9327a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154
  float[] getMVP()
155
    {
156
    return mMVPMatrix;
157
    }
158
159 02de77c9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161 735a8757 Leszek Koltunski
  void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
162 02de77c9 Leszek Koltunski
    {
163 735a8757 Leszek Koltunski
    Matrix.setIdentityM(mViewMatrix, 0);
164
    Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
165
    float mipmap = projection.mMipmap;
166
    if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
167
168
    for(int i=0; i<mNumEffects; i++) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
169
170
    Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
171
    if( marginInPixels!=0 ) magnify(projection,halfX,halfY,halfZ, marginInPixels);
172
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
173 02de77c9 Leszek Koltunski
174 e6519ac8 Leszek Koltunski
    GLES31.glUniform3f( mObjDH , halfX, halfY, halfZ);
175
    GLES31.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
176
    GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
177 6a06a912 Leszek Koltunski
    }
178 c1a38ba3 Leszek Koltunski
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180
181
  void sendOIT(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
182
    {
183
    Matrix.setIdentityM(mViewMatrix, 0);
184
    Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
185
    float mipmap = projection.mMipmap;
186
    if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
187
188
    for(int i=0; i<mNumEffects; i++) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
189
190
    Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
191
    if( marginInPixels!=0 ) magnify(projection,halfX,halfY,halfZ, marginInPixels);
192
    Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
193
194
    GLES31.glUniform3f( mObjDOITH , halfX, halfY, halfZ);
195
    GLES31.glUniformMatrix4fv(mMVMatrixOITH , 1, false, mViewMatrix, 0);
196
    GLES31.glUniformMatrix4fv(mMVPMatrixOITH, 1, false, mMVPMatrix , 0);
197
    }
198 6a06a912 Leszek Koltunski
  }