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.content.res.Resources;
|
23
|
import android.opengl.GLES31;
|
24
|
import android.util.Log;
|
25
|
|
26
|
import org.distorted.library.R;
|
27
|
import org.distorted.library.effect.EffectType;
|
28
|
import org.distorted.library.effect.PostprocessEffect;
|
29
|
import org.distorted.library.effect.VertexEffect;
|
30
|
import org.distorted.library.main.DistortedLibrary;
|
31
|
import org.distorted.library.main.DistortedFramebuffer;
|
32
|
import org.distorted.library.main.DistortedNode;
|
33
|
import org.distorted.library.main.InternalOutputSurface;
|
34
|
import org.distorted.library.main.InternalRenderState;
|
35
|
import org.distorted.library.main.InternalSurface;
|
36
|
import org.distorted.library.mesh.MeshBase;
|
37
|
import org.distorted.library.message.EffectMessageSender;
|
38
|
import org.distorted.library.program.DistortedProgram;
|
39
|
|
40
|
import java.io.InputStream;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
/**
|
44
|
* Not part of public API, do not document
|
45
|
*
|
46
|
* @y.exclude
|
47
|
*/
|
48
|
public class EffectQueuePostprocess extends EffectQueue
|
49
|
{
|
50
|
private static final int NUM_UNIFORMS = PostprocessEffect.NUM_UNIFORMS;
|
51
|
private static final int INDEX = EffectType.POSTPROCESS.ordinal();
|
52
|
|
53
|
private int mHalo;
|
54
|
private float mR, mG, mB, mA;
|
55
|
|
56
|
private static DistortedProgram mPreProgram;
|
57
|
private static int mPreColorH;
|
58
|
private static int mPreTextureH;
|
59
|
|
60
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
61
|
|
62
|
EffectQueuePostprocess()
|
63
|
{
|
64
|
super(NUM_UNIFORMS,INDEX );
|
65
|
}
|
66
|
|
67
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
68
|
|
69
|
void compute(long currTime)
|
70
|
{
|
71
|
if( currTime==mTime ) return;
|
72
|
if( mTime==0 ) mTime = currTime;
|
73
|
long step = (currTime-mTime);
|
74
|
|
75
|
mR = mG = mB = mA = 0.0f;
|
76
|
mHalo = 0;
|
77
|
int halo;
|
78
|
|
79
|
for(int i=0; i<mNumEffects; i++)
|
80
|
{
|
81
|
mCurrentDuration[i] += step;
|
82
|
|
83
|
// first zero out the 'alpha' because BLUR effect will not overwrite this (it is a 1D effect)
|
84
|
// and if previously there was a GLOW effect here then mA would be non-zero and we don't want
|
85
|
// that (see preprocess())
|
86
|
mUniforms[NUM_UNIFORMS*i+4]=0.0f;
|
87
|
|
88
|
if( mEffects[i].compute(mUniforms, NUM_UNIFORMS*i, mCurrentDuration[i], step) )
|
89
|
{
|
90
|
EffectMessageSender.newMessage(mEffects[i]);
|
91
|
}
|
92
|
|
93
|
halo = (int)mUniforms[NUM_UNIFORMS*i];
|
94
|
if( halo>mHalo ) mHalo = halo;
|
95
|
}
|
96
|
|
97
|
// TODO (now only really works in case of 1 effect!)
|
98
|
if( mNumEffects>0 )
|
99
|
{
|
100
|
mR = mUniforms[1];
|
101
|
mG = mUniforms[2];
|
102
|
mB = mUniforms[3];
|
103
|
mA = mUniforms[4];
|
104
|
}
|
105
|
|
106
|
mTime = currTime;
|
107
|
}
|
108
|
|
109
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
110
|
|
111
|
public static void createPrograms(Resources resources)
|
112
|
{
|
113
|
final InputStream mainVertStream = resources.openRawResource(R.raw.main_vertex_shader);
|
114
|
final InputStream mainFragStream = resources.openRawResource(R.raw.preprocess_fragment_shader);
|
115
|
|
116
|
int numV = VertexEffect.getNumEnabled();
|
117
|
|
118
|
String mainVertHeader= DistortedLibrary.GLSL_VERSION + ("#define NUM_VERTEX " + ( numV>0 ? DistortedLibrary.getMax(EffectType.VERTEX ) : 0 ) + "\n");
|
119
|
String mainFragHeader= DistortedLibrary.GLSL_VERSION + "\n";
|
120
|
|
121
|
String enabledEffectV= VertexEffect.getGLSL();
|
122
|
|
123
|
try
|
124
|
{
|
125
|
mPreProgram = new DistortedProgram(mainVertStream, mainFragStream, mainVertHeader, mainFragHeader,
|
126
|
enabledEffectV, null, DistortedLibrary.GLSL, null);
|
127
|
}
|
128
|
catch(Exception e)
|
129
|
{
|
130
|
Log.e("POSTPROCESS", e.getClass().getSimpleName()+" trying to compile PRE program: "+e.getMessage());
|
131
|
throw new RuntimeException(e.getMessage());
|
132
|
}
|
133
|
|
134
|
int preProgramH = mPreProgram.getProgramHandle();
|
135
|
EffectQueueVertex.getUniforms( preProgramH,2 );
|
136
|
EffectQueueMatrix.getUniforms( preProgramH,2 );
|
137
|
mPreColorH = GLES31.glGetUniformLocation( preProgramH, "u_Color" );
|
138
|
mPreTextureH= GLES31.glGetUniformLocation( preProgramH, "u_Texture");
|
139
|
}
|
140
|
|
141
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
142
|
// TODO (now only really works in case of 1 effect!)
|
143
|
|
144
|
public int getQuality()
|
145
|
{
|
146
|
return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getQuality() : 0;
|
147
|
}
|
148
|
|
149
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
150
|
// TODO (now only really works in case of 1 effect!)
|
151
|
|
152
|
public boolean getRender()
|
153
|
{
|
154
|
return mNumEffects>0 ? ((PostprocessEffect)mEffects[0]).getRender() : false;
|
155
|
}
|
156
|
|
157
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
158
|
|
159
|
public int preprocess(InternalOutputSurface buffer, DistortedNode node, float distance, float mipmap, float[] projection)
|
160
|
{
|
161
|
buffer.setAsOutput();
|
162
|
InternalSurface input = node.getInternalSurface();
|
163
|
|
164
|
if( input.setAsInput() )
|
165
|
{
|
166
|
MeshBase mesh = node.getMesh();
|
167
|
|
168
|
float halfW = input.getWidth() / 2.0f;
|
169
|
float halfH = input.getHeight()/ 2.0f;
|
170
|
float halfZ = halfW*mesh.getZFactor();
|
171
|
|
172
|
int width = buffer.getWidth();
|
173
|
int height = buffer.getHeight();
|
174
|
|
175
|
InternalRenderState.setUpStencilMark(mA!=0.0f);
|
176
|
InternalRenderState.disableBlending();
|
177
|
|
178
|
GLES31.glViewport(0, 0, width, height );
|
179
|
|
180
|
mPreProgram.useProgram();
|
181
|
|
182
|
mesh.bindVertexAttribs(mPreProgram);
|
183
|
|
184
|
EffectQueue[] queues = node.getEffects().getQueues();
|
185
|
EffectQueueMatrix matrix = (EffectQueueMatrix)queues[0];
|
186
|
EffectQueueVertex vertex = (EffectQueueVertex)queues[1];
|
187
|
|
188
|
float inflate=0.0f;
|
189
|
|
190
|
matrix.send(width, height, distance, mipmap, projection, halfW, halfH, halfZ, 2);
|
191
|
|
192
|
if( mHalo!=0.0f )
|
193
|
{
|
194
|
inflate = matrix.magnify(projection, width, height, mipmap, halfW, halfH, halfZ, mHalo);
|
195
|
}
|
196
|
|
197
|
vertex.send(inflate,2);
|
198
|
|
199
|
if( mA!=0.0f )
|
200
|
{
|
201
|
GLES31.glUniform4f(mPreColorH, mR, mG, mB, mA);
|
202
|
GLES31.glUniform1i(mPreTextureH, 0);
|
203
|
}
|
204
|
|
205
|
GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, mesh.getNumVertices() );
|
206
|
|
207
|
InternalRenderState.restoreBlending();
|
208
|
InternalRenderState.unsetUpStencilMark();
|
209
|
|
210
|
return 1;
|
211
|
}
|
212
|
return 0;
|
213
|
}
|
214
|
|
215
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
216
|
|
217
|
public int postprocess(DistortedFramebuffer buffer)
|
218
|
{
|
219
|
int numRenders = 0;
|
220
|
|
221
|
GLES31.glDisable(GLES31.GL_BLEND);
|
222
|
|
223
|
for(int i=0; i<mNumEffects; i++)
|
224
|
{
|
225
|
numRenders += ((PostprocessEffect)mEffects[i]).apply(mUniforms,NUM_UNIFORMS*i, buffer);
|
226
|
}
|
227
|
|
228
|
GLES31.glEnable(GLES31.GL_BLEND);
|
229
|
|
230
|
return numRenders;
|
231
|
}
|
232
|
}
|