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.GLES30;
|
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
|
|
39
|
private static int mObjDH; // This is a handle to half a Object dimensions
|
40
|
private static int mMVPMatrixH; // the transformation matrix
|
41
|
private static int mMVMatrixH; // the modelview matrix.
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
EffectQueueMatrix(long id)
|
46
|
{
|
47
|
super(id,NUM_UNIFORMS,INDEX );
|
48
|
}
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
private void magnify(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
|
53
|
{
|
54
|
float scale, nx, ny;
|
55
|
float[] result= new float[4];
|
56
|
float[] point = new float[4];
|
57
|
float[] matrix= new float[16];
|
58
|
float minx = Integer.MAX_VALUE;
|
59
|
float maxx = Integer.MIN_VALUE;
|
60
|
float miny = Integer.MAX_VALUE;
|
61
|
float maxy = Integer.MIN_VALUE;
|
62
|
|
63
|
point[3] = 1.0f;
|
64
|
|
65
|
Matrix.multiplyMM(matrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
|
66
|
|
67
|
point[0] = +halfX; point[1] = +halfY; point[2] = +halfZ;
|
68
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
69
|
nx = result[0]/result[3];
|
70
|
ny = result[1]/result[3];
|
71
|
if( nx<minx ) minx = nx;
|
72
|
if( nx>maxx ) maxx = nx;
|
73
|
if( ny<miny ) miny = ny;
|
74
|
if( ny>maxy ) maxy = ny;
|
75
|
|
76
|
point[0] = +halfX; point[1] = +halfY; point[2] = -halfZ;
|
77
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
78
|
nx = result[0]/result[3];
|
79
|
ny = result[1]/result[3];
|
80
|
if( nx<minx ) minx = nx;
|
81
|
if( nx>maxx ) maxx = nx;
|
82
|
if( ny<miny ) miny = ny;
|
83
|
if( ny>maxy ) maxy = ny;
|
84
|
|
85
|
point[0] = +halfX; point[1] = -halfY; point[2] = +halfZ;
|
86
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
87
|
nx = result[0]/result[3];
|
88
|
ny = result[1]/result[3];
|
89
|
if( nx<minx ) minx = nx;
|
90
|
if( nx>maxx ) maxx = nx;
|
91
|
if( ny<miny ) miny = ny;
|
92
|
if( ny>maxy ) maxy = ny;
|
93
|
|
94
|
point[0] = +halfX; point[1] = -halfY; point[2] = -halfZ;
|
95
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
96
|
nx = result[0]/result[3];
|
97
|
ny = result[1]/result[3];
|
98
|
if( nx<minx ) minx = nx;
|
99
|
if( nx>maxx ) maxx = nx;
|
100
|
if( ny<miny ) miny = ny;
|
101
|
if( ny>maxy ) maxy = ny;
|
102
|
|
103
|
point[0] = -halfX; point[1] = +halfY; point[2] = +halfZ;
|
104
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
105
|
nx = result[0]/result[3];
|
106
|
ny = result[1]/result[3];
|
107
|
if( nx<minx ) minx = nx;
|
108
|
if( nx>maxx ) maxx = nx;
|
109
|
if( ny<miny ) miny = ny;
|
110
|
if( ny>maxy ) maxy = ny;
|
111
|
|
112
|
point[0] = -halfX; point[1] = +halfY; point[2] = -halfZ;
|
113
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
114
|
nx = result[0]/result[3];
|
115
|
ny = result[1]/result[3];
|
116
|
if( nx<minx ) minx = nx;
|
117
|
if( nx>maxx ) maxx = nx;
|
118
|
if( ny<miny ) miny = ny;
|
119
|
if( ny>maxy ) maxy = ny;
|
120
|
|
121
|
point[0] = -halfX; point[1] = -halfY; point[2] = +halfZ;
|
122
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
123
|
nx = result[0]/result[3];
|
124
|
ny = result[1]/result[3];
|
125
|
if( nx<minx ) minx = nx;
|
126
|
if( nx>maxx ) maxx = nx;
|
127
|
if( ny<miny ) miny = ny;
|
128
|
if( ny>maxy ) maxy = ny;
|
129
|
|
130
|
point[0] = -halfX; point[1] = -halfY; point[2] = -halfZ;
|
131
|
Matrix.multiplyMV(result,0,matrix,0,point,0);
|
132
|
nx = result[0]/result[3];
|
133
|
ny = result[1]/result[3];
|
134
|
if( nx<minx ) minx = nx;
|
135
|
if( nx>maxx ) maxx = nx;
|
136
|
if( ny<miny ) miny = ny;
|
137
|
if( ny>maxy ) maxy = ny;
|
138
|
|
139
|
float xlen = projection.mWidth *(maxx-minx)/2;
|
140
|
float ylen = projection.mHeight*(maxy-miny)/2;
|
141
|
|
142
|
scale = 1.0f + marginInPixels/( xlen>ylen ? ylen:xlen );
|
143
|
|
144
|
//android.util.Log.d("scale", ""+marginInPixels+" scale= "+scale+" xlen="+xlen+" ylen="+ylen);
|
145
|
|
146
|
Matrix.scaleM(mViewMatrix, 0, scale, scale, scale);
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
|
151
|
static void getUniforms(int mProgramH)
|
152
|
{
|
153
|
mObjDH = GLES30.glGetUniformLocation(mProgramH, "u_objD");
|
154
|
mMVPMatrixH= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
|
155
|
mMVMatrixH = GLES30.glGetUniformLocation(mProgramH, "u_MVMatrix");
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
void compute(long currTime)
|
161
|
{
|
162
|
if( currTime==mTime ) return;
|
163
|
if( mTime==0 ) mTime = currTime;
|
164
|
long step = (currTime-mTime);
|
165
|
|
166
|
for(int i=0; i<mNumEffects; i++)
|
167
|
{
|
168
|
mCurrentDuration[i] += step;
|
169
|
|
170
|
if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
171
|
{
|
172
|
for(int j=0; j<mNumListeners; j++)
|
173
|
EffectMessageSender.newMessage( mListeners.get(j), EffectMessage.EFFECT_FINISHED, mEffects[i].getID(), mDistortedEffectsID);
|
174
|
|
175
|
if( mEffects[i].isUnity( mUniforms, NUM_UNIFORMS*i) )
|
176
|
{
|
177
|
remove(i--);
|
178
|
mNumEffectsToBe--;
|
179
|
}
|
180
|
}
|
181
|
}
|
182
|
|
183
|
mTime = currTime;
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
float[] getMVP()
|
189
|
{
|
190
|
return mMVPMatrix;
|
191
|
}
|
192
|
|
193
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
194
|
|
195
|
void send(DistortedOutputSurface projection, float halfX, float halfY, float halfZ, float marginInPixels)
|
196
|
{
|
197
|
Matrix.setIdentityM(mViewMatrix, 0);
|
198
|
Matrix.translateM(mViewMatrix, 0, -projection.mWidth/2, projection.mHeight/2, -projection.mDistance);
|
199
|
float mipmap = projection.mMipmap;
|
200
|
if( mipmap!=1 ) Matrix.scaleM(mViewMatrix, 0, mipmap, mipmap, mipmap);
|
201
|
|
202
|
for(int i=0; i<mNumEffects; i++) ((MatrixEffect)mEffects[i]).apply(mViewMatrix,mUniforms,i);
|
203
|
|
204
|
Matrix.translateM(mViewMatrix, 0, halfX,-halfY,-halfZ);
|
205
|
if( marginInPixels!=0 ) magnify(projection,halfX,halfY,halfZ, marginInPixels);
|
206
|
Matrix.multiplyMM(mMVPMatrix, 0, projection.mProjectionMatrix, 0, mViewMatrix, 0);
|
207
|
|
208
|
GLES30.glUniform3f( mObjDH , halfX, halfY, halfZ);
|
209
|
GLES30.glUniformMatrix4fv(mMVMatrixH , 1, false, mViewMatrix, 0);
|
210
|
GLES30.glUniformMatrix4fv(mMVPMatrixH, 1, false, mMVPMatrix , 0);
|
211
|
}
|
212
|
}
|