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
|
|
29
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
30
|
|
31
|
class EffectQueueMatrix extends EffectQueue
|
32
|
{
|
33
|
private static final int NUM_UNIFORMS = MatrixEffect.NUM_UNIFORMS;
|
34
|
private static final int INDEX = EffectType.MATRIX.ordinal();
|
35
|
|
36
|
private static float[] mMVPMatrix = new float[16];
|
37
|
private static float[] mViewMatrix= new float[16];
|
38
|
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
|
|
46
|
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
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
EffectQueueMatrix(long id)
|
53
|
{
|
54
|
super(id,NUM_UNIFORMS,INDEX );
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
private void magnifyDir()
|
60
|
{
|
61
|
Matrix.multiplyMV(mTmpResult,0,mTmpMatrix,0,mTmpPoint,0);
|
62
|
float nx = mTmpResult[0]/mTmpResult[3];
|
63
|
float ny = mTmpResult[1]/mTmpResult[3];
|
64
|
|
65
|
if( nx<mMinx ) mMinx = nx;
|
66
|
if( nx>mMaxx ) mMaxx = nx;
|
67
|
if( ny<mMiny ) mMiny = ny;
|
68
|
if( ny>mMaxy ) mMaxy = ny;
|
69
|
}
|
70
|
|
71
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72
|
// modify the ModelView matrix so that the object drawn appears to be (about) 'marginInPixels' pixels
|
73
|
// larger in each direction when rendered. Used in Postprocessing.
|
74
|
|
75
|
private void magnify(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
|
76
|
{
|
77
|
mMinx = Integer.MAX_VALUE;
|
78
|
mMaxx = Integer.MIN_VALUE;
|
79
|
mMiny = Integer.MAX_VALUE;
|
80
|
mMaxy = Integer.MIN_VALUE;
|
81
|
|
82
|
mTmpPoint[3] = 1.0f;
|
83
|
|
84
|
Matrix.multiplyMM(mTmpMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
|
85
|
|
86
|
mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
|
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
|
|
95
|
float xlen = projection.mWidth *(mMaxx-mMinx)/2;
|
96
|
float ylen = projection.mHeight*(mMaxy-mMiny)/2;
|
97
|
float scale = 1.0f + 2*marginInPixels/( xlen>ylen ? ylen:xlen );
|
98
|
|
99
|
Matrix.scaleM(mViewMatrix, 0, scale, scale, scale);
|
100
|
}
|
101
|
|
102
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
103
|
|
104
|
static void getUniforms(int mProgramH)
|
105
|
{
|
106
|
mObjDH = GLES31.glGetUniformLocation(mProgramH, "u_objD");
|
107
|
mMVPMatrixH= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
|
108
|
mMVMatrixH = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
|
109
|
}
|
110
|
|
111
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
112
|
|
113
|
void compute(long currTime)
|
114
|
{
|
115
|
if( currTime==mTime ) return;
|
116
|
if( mTime==0 ) mTime = currTime;
|
117
|
long step = (currTime-mTime);
|
118
|
|
119
|
for(int i=0; i<mNumEffects; i++)
|
120
|
{
|
121
|
mCurrentDuration[i] += step;
|
122
|
|
123
|
if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
124
|
{
|
125
|
for(int j=0; j<mNumListeners; j++)
|
126
|
EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
|
127
|
|
128
|
if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
|
129
|
{
|
130
|
remove(i--);
|
131
|
mNumEffectsToBe--;
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
|
136
|
mTime = currTime;
|
137
|
}
|
138
|
|
139
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
140
|
|
141
|
float[] getMVP()
|
142
|
{
|
143
|
return mMVPMatrix;
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
|
148
|
void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
|
149
|
{
|
150
|
Matrix.setIdentityM(mViewMatrix, 0);
|
151
|
Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
|
152
|
float mipmap = projection.mMipmap;
|
153
|
if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
|
154
|
|
155
|
for(int i=0; i<mNumEffects; i++) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
|
156
|
|
157
|
Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
|
158
|
if( marginInPixels!=0 ) magnify(projection,halfX,halfY,halfZ, marginInPixels);
|
159
|
Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
|
160
|
|
161
|
GLES31.glUniform3f( mObjDH , halfX, halfY, halfZ);
|
162
|
GLES31.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
|
163
|
GLES31.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
|
164
|
}
|
165
|
}
|