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