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.effectqueue;
|
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.mesh.MeshBase;
|
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[] mModelViewMatrix = new float[16];
|
39
|
|
40
|
private static int[] mBoundingH = new int[MAIN_VARIANTS];
|
41
|
private static int[] mStretchH = new int[MAIN_VARIANTS];
|
42
|
private static int[] mMVPMatrixH = new int[MAIN_VARIANTS];
|
43
|
private static int[] mMVMatrixH = new int[MAIN_VARIANTS];
|
44
|
|
45
|
private static float[] mTmpMatrix = new float[16];
|
46
|
private static float[] mTmpResult = new float[4];
|
47
|
private static float[] mTmpPoint = new float[4];
|
48
|
private static float mMinX,mMaxX,mMinY,mMaxY;
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
EffectQueueMatrix()
|
53
|
{
|
54
|
super(NUM_UNIFORMS,INDEX );
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
static void uniforms(int mProgramH, int variant)
|
60
|
{
|
61
|
mBoundingH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_Bounding");
|
62
|
mStretchH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_Stretch");
|
63
|
mMVPMatrixH[variant]= GLES31.glGetUniformLocation(mProgramH, "u_MVPMatrix");
|
64
|
mMVMatrixH[variant] = GLES31.glGetUniformLocation(mProgramH, "u_MVMatrix");
|
65
|
}
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
private void magnifyDir()
|
70
|
{
|
71
|
Matrix.multiplyMV(mTmpResult,0,mTmpMatrix,0,mTmpPoint,0);
|
72
|
float nx = mTmpResult[0]/mTmpResult[3];
|
73
|
float ny = mTmpResult[1]/mTmpResult[3];
|
74
|
|
75
|
if( nx<mMinX ) mMinX = nx;
|
76
|
if( nx>mMaxX ) mMaxX = nx;
|
77
|
if( ny<mMinY ) mMinY = ny;
|
78
|
if( ny>mMaxY ) mMaxY = ny;
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
// return a float which describes how much larger an object must be so that it appears to be (about)
|
83
|
// 'marginInPixels' pixels larger in each direction. Used in Postprocessing.
|
84
|
|
85
|
float magnify(float[] projection, int width, int height, float mipmap, MeshBase mesh, float marginInPixels)
|
86
|
{
|
87
|
mMinX = Integer.MAX_VALUE;
|
88
|
mMaxX = Integer.MIN_VALUE;
|
89
|
mMinY = Integer.MAX_VALUE;
|
90
|
mMaxY = Integer.MIN_VALUE;
|
91
|
|
92
|
mTmpPoint[3] = 1.0f;
|
93
|
|
94
|
Matrix.multiplyMM(mTmpMatrix, 0, projection, 0, mModelViewMatrix, 0);
|
95
|
|
96
|
float halfX = mesh.getBoundingX();
|
97
|
float halfY = mesh.getBoundingY();
|
98
|
float halfZ = mesh.getBoundingZ();
|
99
|
|
100
|
mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
|
101
|
mTmpPoint[0] = +halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = -halfZ; magnifyDir();
|
102
|
mTmpPoint[0] = +halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = +halfZ; magnifyDir();
|
103
|
mTmpPoint[0] = +halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = -halfZ; magnifyDir();
|
104
|
mTmpPoint[0] = -halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = +halfZ; magnifyDir();
|
105
|
mTmpPoint[0] = -halfX; mTmpPoint[1] = +halfY; mTmpPoint[2] = -halfZ; magnifyDir();
|
106
|
mTmpPoint[0] = -halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = +halfZ; magnifyDir();
|
107
|
mTmpPoint[0] = -halfX; mTmpPoint[1] = -halfY; mTmpPoint[2] = -halfZ; magnifyDir();
|
108
|
|
109
|
float xLenInPixels = width *(mMaxX-mMinX)/2;
|
110
|
float yLenInPixels = height*(mMaxY-mMinY)/2;
|
111
|
|
112
|
// already margin / avg(xLen,yLen) is the size of the halo.
|
113
|
// Here we need a bit more because we are marking not only the halo, but a little bit around
|
114
|
// it as well so that the (blur for example) will be smooth on the edges. Thus the 2.0f.
|
115
|
// ( 4.0 because there is an extra 2.0 from the avg(xLen,yLen) )
|
116
|
//
|
117
|
// mMipmap ( 1.0, 0.5, 0.25, 0.125 ) - we need to make the size of the halo independent
|
118
|
// of postprocessing effect quality.
|
119
|
|
120
|
return mipmap*4.0f*marginInPixels/( xLenInPixels+yLenInPixels );
|
121
|
}
|
122
|
|
123
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
124
|
|
125
|
void compute(long currTime)
|
126
|
{
|
127
|
if( currTime==mTime ) return;
|
128
|
if( mTime==0 ) mTime = currTime;
|
129
|
long step = (currTime-mTime);
|
130
|
|
131
|
for(int i=0; i<mNumEffects; i++)
|
132
|
{
|
133
|
mCurrentDuration[i] += step;
|
134
|
|
135
|
if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
136
|
{
|
137
|
EffectMessageSender.newMessage(mEffects[i]);
|
138
|
}
|
139
|
}
|
140
|
|
141
|
mTime = currTime;
|
142
|
}
|
143
|
|
144
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
145
|
|
146
|
float[] getMVP()
|
147
|
{
|
148
|
return mMVPMatrix;
|
149
|
}
|
150
|
|
151
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
152
|
|
153
|
void send(float distance, float mipmap, float[] projection, MeshBase mesh, int variant)
|
154
|
{
|
155
|
Matrix.setIdentityM(mModelViewMatrix, 0);
|
156
|
|
157
|
// The 'View' part of the MV matrix
|
158
|
Matrix.translateM(mModelViewMatrix, 0, 0,0, -distance);
|
159
|
if( mipmap!=1 ) Matrix.scaleM(mModelViewMatrix, 0, mipmap, mipmap, mipmap);
|
160
|
|
161
|
// the 'Model' part of the MV matrix
|
162
|
for(int i=mNumEffects-1; i>=0; i--) ((MatrixEffect)mEffects[i]).apply(mModelViewMatrix,mUniforms,i);
|
163
|
|
164
|
// combined Model-View-Projection matrix
|
165
|
Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mModelViewMatrix, 0);
|
166
|
|
167
|
GLES31.glUniform3f( mBoundingH[variant] , mesh.getBoundingX(), mesh.getBoundingY(), mesh.getBoundingZ());
|
168
|
GLES31.glUniform3f( mStretchH[variant] , mesh.getStretchX() , mesh.getStretchY() , mesh.getStretchZ() );
|
169
|
GLES31.glUniformMatrix4fv(mMVMatrixH[variant] , 1, false, mModelViewMatrix, 0);
|
170
|
GLES31.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix , 0);
|
171
|
}
|
172
|
}
|