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.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.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[] mMVPMatrixH = new int[MAIN_VARIANTS];
|
41
|
private static int[] mMVMatrixH = new int[MAIN_VARIANTS];
|
42
|
|
43
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
44
|
|
45
|
EffectQueueMatrix()
|
46
|
{
|
47
|
super(NUM_UNIFORMS,INDEX );
|
48
|
}
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
EffectQueueMatrix(EffectQueueMatrix source)
|
53
|
{
|
54
|
super(source);
|
55
|
}
|
56
|
|
57
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
58
|
|
59
|
static void uniforms(int mProgramH, int variant)
|
60
|
{
|
61
|
mMVPMatrixH[variant]= GLES30.glGetUniformLocation(mProgramH, "u_MVPMatrix");
|
62
|
mMVMatrixH[variant] = GLES30.glGetUniformLocation(mProgramH, "u_MVMatrix");
|
63
|
}
|
64
|
|
65
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
66
|
|
67
|
void compute(long currTime)
|
68
|
{
|
69
|
if( currTime==mTime ) return;
|
70
|
if( mTime==0 ) mTime = currTime;
|
71
|
long step = (currTime-mTime);
|
72
|
|
73
|
for(int i=0; i<mNumEffects; i++)
|
74
|
{
|
75
|
mCurrentDuration[i] += step;
|
76
|
|
77
|
if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
78
|
{
|
79
|
EffectMessageSender.newMessage(mEffects[i]);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
mTime = currTime;
|
84
|
}
|
85
|
|
86
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
87
|
|
88
|
float[] getMVP()
|
89
|
{
|
90
|
return mMVPMatrix;
|
91
|
}
|
92
|
|
93
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
94
|
|
95
|
void send(float distance, float mipmap, float[] projection, MeshBase mesh, int variant)
|
96
|
{
|
97
|
Matrix.setIdentityM(mModelViewMatrix, 0);
|
98
|
|
99
|
// The 'View' part of the MV matrix
|
100
|
Matrix.translateM(mModelViewMatrix, 0, 0,0, -distance);
|
101
|
if( mipmap!=1 ) Matrix.scaleM(mModelViewMatrix, 0, mipmap, mipmap, mipmap);
|
102
|
|
103
|
// the 'Model' part of the MV matrix
|
104
|
for(int i=mNumEffects-1; i>=0; i--) ((MatrixEffect)mEffects[i]).apply(mModelViewMatrix,mUniforms,i);
|
105
|
|
106
|
// combined Model-View-Projection matrix
|
107
|
Matrix.multiplyMM(mMVPMatrix, 0, projection, 0, mModelViewMatrix, 0);
|
108
|
|
109
|
GLES30.glUniformMatrix4fv(mMVMatrixH[variant] , 1, false, mModelViewMatrix, 0);
|
110
|
GLES30.glUniformMatrix4fv(mMVPMatrixH[variant], 1, false, mMVPMatrix , 0);
|
111
|
}
|
112
|
}
|