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;
|
21
|
|
22
|
import android.content.res.Resources;
|
23
|
import android.opengl.GLES20;
|
24
|
|
25
|
import org.distorted.library.message.EffectMessage;
|
26
|
import org.distorted.library.program.DistortedProgram;
|
27
|
import org.distorted.library.program.FragmentCompilationException;
|
28
|
import org.distorted.library.program.FragmentUniformsException;
|
29
|
import org.distorted.library.program.LinkingException;
|
30
|
import org.distorted.library.program.VertexCompilationException;
|
31
|
import org.distorted.library.program.VertexUniformsException;
|
32
|
import org.distorted.library.type.Data1D;
|
33
|
import org.distorted.library.type.Data2D;
|
34
|
import org.distorted.library.type.Dynamic1D;
|
35
|
import org.distorted.library.type.Dynamic2D;
|
36
|
import org.distorted.library.type.Static1D;
|
37
|
import org.distorted.library.type.Static2D;
|
38
|
|
39
|
import java.io.InputStream;
|
40
|
import java.nio.ByteBuffer;
|
41
|
import java.nio.ByteOrder;
|
42
|
import java.nio.FloatBuffer;
|
43
|
|
44
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
45
|
|
46
|
class EffectQueuePostprocess extends EffectQueue
|
47
|
{
|
48
|
private static final int BYTES_PER_FLOAT = 4;
|
49
|
private static final int POSITION_DATA_SIZE= 2; // Post Program: size of the position data in elements
|
50
|
private static final int TEX_DATA_SIZE = 2; // Post Program: size of the texture coordinate data in elements.
|
51
|
|
52
|
private static final int NUM_UNIFORMS = 3;
|
53
|
private static final int NUM_CACHE = 0;
|
54
|
private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
|
55
|
|
56
|
private static int mNumEffectsH;
|
57
|
private static int mTypeH;
|
58
|
private static int mUniformsH;
|
59
|
private static int mObjDH;
|
60
|
private static int mMVPMatrixH;
|
61
|
|
62
|
private static final FloatBuffer mQuadPositions, mQuadTexture;
|
63
|
private static DistortedProgram mProgram;
|
64
|
|
65
|
static
|
66
|
{
|
67
|
int dataLength = 4;
|
68
|
|
69
|
float[] positionData= { -0.5f, -0.5f, -0.5f, 0.5f, 0.5f,-0.5f, 0.5f, 0.5f };
|
70
|
float[] textureData = { 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f };
|
71
|
|
72
|
mQuadPositions = ByteBuffer.allocateDirect(POSITION_DATA_SIZE*dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
73
|
mQuadPositions.put(positionData).position(0);
|
74
|
mQuadTexture = ByteBuffer.allocateDirect(TEX_DATA_SIZE *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
75
|
mQuadTexture.put(textureData).position(0);
|
76
|
}
|
77
|
|
78
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
79
|
|
80
|
EffectQueuePostprocess(long id)
|
81
|
{
|
82
|
super(id,NUM_UNIFORMS,NUM_CACHE,INDEX );
|
83
|
}
|
84
|
|
85
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
86
|
|
87
|
static void createProgram(Resources resources)
|
88
|
throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
|
89
|
{
|
90
|
final InputStream postVertexStream = resources.openRawResource(R.raw.post_vertex_shader);
|
91
|
final InputStream postFragmentStream = resources.openRawResource(R.raw.post_fragment_shader);
|
92
|
|
93
|
String postFragmentHeader= ("#version 100\n#define NUM_POSTPROCESS " + DistortedEffects.getMaxPostprocess()+"\n");
|
94
|
|
95
|
for(EffectNames name: EffectNames.values() )
|
96
|
{
|
97
|
if( name.getType()== EffectTypes.POSTPROCESS)
|
98
|
postFragmentHeader += ("#define "+name.name()+" "+name.ordinal()+"\n");
|
99
|
}
|
100
|
|
101
|
mProgram = new DistortedProgram(postVertexStream,postFragmentStream, "", postFragmentHeader);
|
102
|
|
103
|
int postProgramH = mProgram.getProgramHandle();
|
104
|
getUniforms(postProgramH);
|
105
|
}
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108
|
|
109
|
static void getUniforms(int mProgramH)
|
110
|
{
|
111
|
mNumEffectsH= GLES20.glGetUniformLocation( mProgramH, "pNumEffects");
|
112
|
mTypeH = GLES20.glGetUniformLocation( mProgramH, "pType");
|
113
|
mUniformsH = GLES20.glGetUniformLocation( mProgramH, "pUniforms");
|
114
|
mObjDH = GLES20.glGetUniformLocation( mProgramH, "u_objD");
|
115
|
mMVPMatrixH = GLES20.glGetUniformLocation(mProgramH, "u_MVPMatrix");
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
synchronized void compute(long currTime)
|
121
|
{
|
122
|
if( currTime==mTime ) return;
|
123
|
if( mTime==0 ) mTime = currTime;
|
124
|
long step = (currTime-mTime);
|
125
|
|
126
|
for(int i=0; i<mNumEffects; i++)
|
127
|
{
|
128
|
if( mInter[0][i]!=null && mInter[0][i].interpolateMain(mUniforms ,NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
129
|
{
|
130
|
for(int j=0; j<mNumListeners; j++)
|
131
|
EffectMessageSender.newMessage( mListeners.elementAt(j),
|
132
|
EffectMessage.EFFECT_FINISHED,
|
133
|
(mID[i]<<EffectTypes.LENGTH)+EffectTypes.POSTPROCESS.type,
|
134
|
mName[i],
|
135
|
mObjectID);
|
136
|
|
137
|
if( EffectNames.isUnity(mName[i], mUniforms, NUM_UNIFORMS*i) )
|
138
|
{
|
139
|
remove(i);
|
140
|
i--;
|
141
|
continue;
|
142
|
}
|
143
|
else mInter[0][i] = null;
|
144
|
}
|
145
|
|
146
|
if( mInter[1][i]!=null )
|
147
|
{
|
148
|
mInter[1][i].interpolateMain(mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i], step);
|
149
|
}
|
150
|
|
151
|
mCurrentDuration[i] += step;
|
152
|
}
|
153
|
|
154
|
mTime = currTime;
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
|
159
|
protected void moveEffect(int index)
|
160
|
{
|
161
|
mUniforms[NUM_UNIFORMS*index ] = mUniforms[NUM_UNIFORMS*(index+1) ];
|
162
|
mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
|
163
|
mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
|
164
|
}
|
165
|
|
166
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
167
|
|
168
|
synchronized static void sendZero(float w, float h, float[] mvp)
|
169
|
{
|
170
|
GLES20.glUniform1i( mNumEffectsH, 0);
|
171
|
GLES20.glUniform2f( mObjDH , w, h);
|
172
|
GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
|
173
|
}
|
174
|
|
175
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
176
|
// w,h - width and height of hte input texture. MVP - Model-View-Projection matrix to apply to the
|
177
|
// texture; df - output FBO.
|
178
|
|
179
|
synchronized void render(float w, float h, float[] mvp, DistortedFramebuffer df)
|
180
|
{
|
181
|
mProgram.useProgram();
|
182
|
df.setAsOutput();
|
183
|
|
184
|
GLES20.glUniform1i( mNumEffectsH, mNumEffects);
|
185
|
GLES20.glUniform2f( mObjDH , w, h );
|
186
|
GLES20.glUniformMatrix4fv(mMVPMatrixH, 1, false, mvp , 0);
|
187
|
|
188
|
if( mNumEffects>0 )
|
189
|
{
|
190
|
GLES20.glUniform1iv( mTypeH , mNumEffects, mName ,0);
|
191
|
GLES20.glUniform4fv( mUniformsH,2*mNumEffects, mUniforms,0);
|
192
|
}
|
193
|
|
194
|
GLES20.glVertexAttribPointer(mProgram.mAttribute[0], POSITION_DATA_SIZE, GLES20.GL_FLOAT, false, 0, mQuadPositions);
|
195
|
GLES20.glVertexAttribPointer(mProgram.mAttribute[1], TEX_DATA_SIZE , GLES20.GL_FLOAT, false, 0, mQuadTexture);
|
196
|
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, 4);
|
197
|
}
|
198
|
|
199
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
200
|
// blur
|
201
|
|
202
|
synchronized long add(EffectNames eln, Data1D degree, Data2D center)
|
203
|
{
|
204
|
if( mMax[INDEX]>mNumEffects )
|
205
|
{
|
206
|
if( degree instanceof Dynamic1D)
|
207
|
{
|
208
|
mInter[0][mNumEffects] = (Dynamic1D)degree;
|
209
|
}
|
210
|
else if( degree instanceof Static1D)
|
211
|
{
|
212
|
mInter[0][mNumEffects] = null;
|
213
|
mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
|
214
|
}
|
215
|
else return -1;
|
216
|
|
217
|
if( center instanceof Dynamic2D)
|
218
|
{
|
219
|
mInter[1][mNumEffects] = (Dynamic2D)center;
|
220
|
}
|
221
|
else if( center instanceof Static2D)
|
222
|
{
|
223
|
mInter[1][mNumEffects] = null;
|
224
|
mUniforms[NUM_UNIFORMS*mNumEffects+1] = ((Static2D)center).getX();
|
225
|
mUniforms[NUM_UNIFORMS*mNumEffects+2] = ((Static2D)center).getY();
|
226
|
}
|
227
|
else return -1;
|
228
|
|
229
|
return addBase(eln);
|
230
|
}
|
231
|
|
232
|
return -1;
|
233
|
}
|
234
|
}
|