Project

General

Profile

« Previous | Next » 

Revision d58407a8

Added by Leszek Koltunski over 2 years ago

New postprocess effect 'Border' (not working yet) and adding it to the testing app 'Triblur'.

View differences:

src/main/java/org/distorted/library/effect/EffectName.java
75 75
  SMOOTH_CONTRAST  ( EffectType.FRAGMENT,   new float[] {1.0f}           , 1, 3,     3    , FragmentEffectContrast.class   ),
76 76

  
77 77
  BLUR             ( EffectType.POSTPROCESS,new float[] {0.0f}           , 2, 0,     0    , PostprocessEffectBlur.class    ),
78
  GLOW             ( EffectType.POSTPROCESS,new float[] {0.0f}           , 6, 0,     0    , PostprocessEffectGlow.class    );
78
  GLOW             ( EffectType.POSTPROCESS,new float[] {0.0f}           , 6, 0,     0    , PostprocessEffectGlow.class    ),
79
  BORDER           ( EffectType.POSTPROCESS,new float[] {0.0f}           , 5, 0,     0    , PostprocessEffectBorder.class  ),
80
  ;
79 81

  
80 82
///////////////////////////////////////////////////////////////////////////////////////////////////
81 83

  
src/main/java/org/distorted/library/effect/PostprocessEffect.java
70 70

  
71 71
  private static class Source
72 72
    {
73
    private String mName, mVertexShader, mFragmentShader;
73
    private final String mName, mVertexShader, mFragmentShader;
74 74

  
75 75
    Source(String name, String vertex, String fragment)
76 76
      {
......
81 81
    }
82 82

  
83 83
  static ArrayList<DistortedProgram> mPrograms = new ArrayList<>();
84
  private static ArrayList<Source> mSources = new ArrayList<>();
84
  private final static ArrayList<Source> mSources = new ArrayList<>();
85 85
  private static int mNumSources = 0;
86 86

  
87 87
  private static class Job
......
96 96
      }
97 97
    }
98 98

  
99
  private ArrayList<Job> mJobs = new ArrayList<>();
99
  private final ArrayList<Job> mJobs = new ArrayList<>();
100 100
  private int mQualityLevel;
101 101

  
102 102
  float mQualityScale;
......
141 141

  
142 142
///////////////////////////////////////////////////////////////////////////////////////////////////
143 143
/**
144
 * At this moment the 'buffer' contains a) preprocessed object b) real object rendered 'on top' of
145
 * the preprocessed one.
146
 * Postprocess buffer. What this means exactly depends on the effect -
147
 *
144 148
 * Only for use by the library itself.
145 149
 *
146 150
 * @y.exclude
147 151
 */
148
  public abstract int apply(float[] uniforms, int index, DistortedFramebuffer buffer);
152
  public abstract int postprocess(float[] uniforms, int index, DistortedFramebuffer buffer);
149 153

  
150 154
///////////////////////////////////////////////////////////////////////////////////////////////////
151 155
  /**
......
153 157
   *
154 158
   * @y.exclude
155 159
   */
156
  public abstract boolean getRender();
160
  public abstract boolean getRenderDirectly();
157 161

  
158 162
///////////////////////////////////////////////////////////////////////////////////////////////////
159 163
  /**
src/main/java/org/distorted/library/effect/PostprocessEffectBlur.java
19 19

  
20 20
package org.distorted.library.effect;
21 21

  
22
import android.opengl.GLES30;
23

  
24
import org.distorted.library.main.DistortedFramebuffer;
25
import org.distorted.library.main.InternalRenderState;
26
import org.distorted.library.program.DistortedProgram;
27 22
import org.distorted.library.type.Data2D;
28 23

  
29 24
///////////////////////////////////////////////////////////////////////////////////////////////////
30 25
/**
31 26
 * Blur the Framebuffer.
32 27
 */
33
public class PostprocessEffectBlur extends PostprocessEffect
28
public class PostprocessEffectBlur extends PostprocessEffectBlurred
34 29
  {
35
  private static final int MAX_RADIUS = 50;
36

  
37
  private final Data2D mBlurHaloAndRadius;
38

  
39
  private static final float[] GAUSSIAN =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
40
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
41
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
42
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
43
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
44
    0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
45
    0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
46
    0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
47
    0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
48
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
49
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
50
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
51
    0.004432f, 0.000000f
52
    };
53
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
54

  
55
  // The (fixed-function-sampled) Gaussian Blur kernels are of the size k0=1, k1=2, k2=2, k3=3, k4=3, k5=4, k6=4,...
56
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
57
  // in all 4 directions)
58
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
59
  private static final float[] weightsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
60
  private static final float[] offsetsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
61
  private static final float[] mWeights = new float[MAX_RADIUS];
62
  private static final float[] mOffsets = new float[MAX_RADIUS];
63

  
64
  private static DistortedProgram mProgram1, mProgram2;
65
  private static int mIndex1, mIndex2;
66

  
67
///////////////////////////////////////////////////////////////////////////////////////////////////
68
// Clean up of static variables on exit.
69
// called by reflection from super class.
70

  
71
  @SuppressWarnings("unused")
72
  static void destroyStatics()
73
    {
74
    mProgram1 = null;
75
    mProgram2 = null;
76

  
77
    mIndex1 = 0;
78
    mIndex2 = 0;
79
    }
80

  
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
// This implements the 'Better separable implementation using GPU fixed function sampling' from
83
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
84

  
85
  private void computeGaussianKernel(int radius)
86
    {
87
    int offset = radius + radius*radius/4;
88

  
89
    if( weightsCache[offset]==0.0f )
90
      {
91
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
92
      mWeights[0] = GAUSSIAN[0];
93
      float sum   = GAUSSIAN[0];
94
      int j;
95

  
96
      for(int i=1; i<=radius; i++)
97
        {
98
        x += P;
99
        j = (int)x;
100
        z = x-j;
101

  
102
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
103
        sum += 2*mWeights[i];
104
        }
105

  
106
      for(int i=0; i<=radius; i++) mWeights[i] /= sum;
107

  
108
      int numloops = radius/2;
109
      weightsCache[offset] = mWeights[0];
110
      offsetsCache[offset] = 0.0f;
111

  
112
      for(int i=0; i<numloops; i++)
113
        {
114
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
115
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
116
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
117
        }
118

  
119
      if( radius%2 == 1 )
120
        {
121
        int index = offset + radius/2 +1;
122
        offsetsCache[index]=radius;
123
        weightsCache[index]=mWeights[radius];
124
        }
125
      }
126
    }
30
  private final Data2D mHaloAndRadius;
127 31

  
128 32
///////////////////////////////////////////////////////////////////////////////////////////////////
129 33
/**
......
133 37
 */
134 38
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
135 39
    {
136
    return mBlurHaloAndRadius.get(uniforms,index,currentDuration,step);
40
    return mHaloAndRadius.get(uniforms,index,currentDuration,step);
137 41
    }
138 42

  
139 43
///////////////////////////////////////////////////////////////////////////////////////////////////
......
142 46
 *
143 47
 * @y.exclude
144 48
 */
145
  public boolean getRender()
49
  public boolean getRenderDirectly()
146 50
    {
147 51
    return false;
148 52
    }
149 53

  
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
/**
152
 * Only for use by the library itself.
153
 *
154
 * @y.exclude
155
 */
156
  public int apply(float[] uniforms, int index, DistortedFramebuffer buffer)
157
    {
158
    if( mProgram1 ==null)
159
      {
160
      try
161
        {
162
        mProgram1 = mPrograms.get(mIndex1);
163
        mProgram2 = mPrograms.get(mIndex2);
164
        }
165
      catch(Exception ex)
166
        {
167
        return 0;
168
        }
169
      }
170

  
171
    InternalRenderState.useStencilMark();
172

  
173
    buffer.setAsOutput();
174

  
175
    float w= buffer.getWidth();
176
    float h= buffer.getHeight();
177
    float n= 1.0f - buffer.getNear();
178

  
179
    float corrW = buffer.getWidthCorrection();
180
    float corrH = buffer.getHeightCorrection();
181
    float offsetCorrW = corrW/w;
182
    float offsetCorrH = corrH/h;
183

  
184
    int radius = (int)(uniforms[index+1]*mQualityScale);
185
    if( radius>=MAX_RADIUS ) radius = MAX_RADIUS-1;
186
    if( radius<=0          ) radius = 1;
187
    computeGaussianKernel(radius);
188

  
189
    int offset = radius + radius*radius/4;
190
    radius = (radius+1)/2;
191
    GLES30.glViewport(0, 0, (int)w, (int)h);
192

  
193
    // horizontal blur
194
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
195

  
196
    mProgram1.useProgram();
197
    buffer.bindForOutput(1);
198
    buffer.setAsInput(0);
199

  
200
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
201
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
202

  
203
    GLES30.glColorMask(true,true,true,true);
204
    GLES30.glClearColor(1.0f,1.0f,1.0f,0.0f);
205
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
206

  
207
    GLES30.glUniform1f ( mProgram1.mUniform[0] , n );
208
    GLES30.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
209
    GLES30.glUniform1i ( mProgram1.mUniform[2] , 0 );
210
    GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
211
    GLES30.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
212
    GLES30.glUniform1i ( mProgram1.mUniform[5] , radius);
213
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
214
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
215
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
216

  
217
    // vertical blur
218
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
219

  
220
    mProgram2.useProgram();
221
    buffer.bindForOutput(0);
222
    buffer.setAsInput(1);
223

  
224
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
225
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
226

  
227
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
228

  
229
    GLES30.glUniform1f ( mProgram2.mUniform[0] , n );
230
    GLES30.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
231
    GLES30.glUniform1i ( mProgram2.mUniform[2] , 0 );
232
    GLES30.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
233
    GLES30.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
234
    GLES30.glUniform1i ( mProgram2.mUniform[5] , radius);
235
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
236
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
237
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
238

  
239
    InternalRenderState.unuseStencilMark();
240

  
241
    return 2;
242
    }
243

  
244 54
///////////////////////////////////////////////////////////////////////////////////////////////////
245 55
// PUBLIC API
246 56
///////////////////////////////////////////////////////////////////////////////////////////////////
......
249 59
 */
250 60
  public static void enable()
251 61
    {
252
    final String blurVertex =
253

  
254
      "precision lowp float;  \n"+
255
      "in vec2 a_Position;    \n"+
256
      "in vec2 a_TexCoord;    \n"+
257
      "out vec2 v_TexCoord;   \n"+
258
      "uniform float u_Depth; \n"+
259
      "uniform vec2 u_TexCorr;\n"+
260

  
261
      "void main()                                      \n"+
262
      "  {                                              \n"+
263
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
264
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
265
      "  }";
266

  
267
    final String blurFragment1 =
268

  
269
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
270
      "precision lowp float;              \n"+
271
      "in vec2 v_TexCoord;                \n"+
272
      "out vec4 fragColor;                \n"+
273
      "uniform sampler2D u_ColorTexture;  \n"+
274
      "uniform float u_Offsets[MAX_BLUR]; \n"+
275
      "uniform float u_Weights[MAX_BLUR]; \n"+
276
      "uniform int u_Radius;              \n"+
277

  
278
      "void main()                                                                                           \n"+
279
      "  {                                                                                                   \n"+
280
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
281
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
282
      "    {                                                                                                 \n"+
283
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
284
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
285
      "    }                                                                                                 \n"+
286
      "  fragColor = pixel;                                                                                  \n"+
287
      "  }";
288

  
289
    final String blurFragment2 =
290

  
291
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
292
      "precision lowp float;              \n"+
293
      "in vec2 v_TexCoord;                \n"+
294
      "out vec4 fragColor;                \n"+
295
      "uniform sampler2D u_ColorTexture;  \n"+
296
      "uniform float u_Offsets[MAX_BLUR]; \n"+
297
      "uniform float u_Weights[MAX_BLUR]; \n"+
298
      "uniform int u_Radius;              \n"+
62
    PostprocessEffectBlurred.enable("BLUR1","BLUR2");
63
    }
299 64

  
300
      "void main()                                                                                           \n"+
301
      "  {                                                                                                   \n"+
302
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
303
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
304
      "    {                                                                                                 \n"+
305
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
306
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
307
      "    }                                                                                                 \n"+
308
      "  fragColor = pixel;                                                                                  \n"+
309
      "  }";
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66
// Clean up of static variables on exit. Called by reflection from super class.
310 67

  
311
    mIndex1 = PostprocessEffect.register("BLUR1", blurVertex,blurFragment1);
312
    mIndex2 = PostprocessEffect.register("BLUR2", blurVertex,blurFragment2);
68
  @SuppressWarnings("unused")
69
  static void destroyStatics()
70
    {
71
    PostprocessEffectBlurred.destroyStatics();
313 72
    }
314 73

  
315 74
///////////////////////////////////////////////////////////////////////////////////////////////////
316 75
/**
317 76
 * Blur the Framebuffer.
318 77
 *
319
 * @param blurHaloAndRadius First float: the halo.
320
 *                          How far beyond the object does the effect stretch to? Unit: Percentage
321
 *                          of the size of the original object, i.e. Halo=0 --> no halo around, this
322
 *                          would mean sharp edges around the object; Halo=100 --> halo of the size
323
 *                          of the object itself around (in case of blur, this would be - in vast
324
 *                          majority of cases except an object rendered very closely to the near plane-
325
 *                          an overkill).
326
 *                          Second float: the radius.
327
 *                          The 'strength' if the blur of the edges, in pixels. 0 = no blur, 10 =
328
 *                          blur of roughly 10 pixels around the whole halo.
78
 * @param haloAndRadius First float: the halo.
79
 *                      How far beyond the object does the effect stretch to? Unit: Percentage
80
 *                      of the size of the original object, i.e. Halo=0 --> no halo around, this
81
 *                      would mean sharp edges around the object; Halo=100 --> halo of the size
82
 *                      of the object itself around (in case of blur, this would be - in vast
83
 *                      majority of cases except an object rendered very closely to the near plane-
84
 *                      an overkill).
85
 *                      Second float: the radius.
86
 *                      The 'strength' if the blur of the edges, in pixels. 0 = no blur, 10 =
87
 *                      blur of roughly 10 pixels around the whole halo.
329 88

  
330 89
 */
331
  public PostprocessEffectBlur(Data2D blurHaloAndRadius)
90
  public PostprocessEffectBlur(Data2D haloAndRadius)
332 91
    {
333 92
    super(EffectName.BLUR);
334
    mBlurHaloAndRadius = blurHaloAndRadius;
93
    mHaloAndRadius = haloAndRadius;
335 94
    }
336 95
  }
src/main/java/org/distorted/library/effect/PostprocessEffectBlurred.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2017 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.effect;
21

  
22
import android.opengl.GLES30;
23

  
24
import org.distorted.library.main.DistortedFramebuffer;
25
import org.distorted.library.main.InternalRenderState;
26
import org.distorted.library.program.DistortedProgram;
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29

  
30
/**
31
 * Postprocessed effect in which postprocessing is a Gaussian blur - i.e. Blur or Glow.
32
 */
33
abstract public class PostprocessEffectBlurred extends PostprocessEffect
34
  {
35
  private static final int MAX_RADIUS = 50;
36

  
37
  private static final float[] GAUSSIAN =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
38
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
39
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
40
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
41
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
42
    0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
43
    0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
44
    0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
45
    0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
46
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
47
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
48
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
49
    0.004432f, 0.000000f
50
    };
51
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
52

  
53
  // The (fixed-function-sampled) Gaussian Blur kernels are of the size k0=1, k1=2, k2=2, k3=3, k4=3, k5=4, k6=4,...
54
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
55
  // in all 4 directions)
56
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
57
  private static final float[] weightsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
58
  private static final float[] offsetsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
59
  private static final float[] mWeights = new float[MAX_RADIUS];
60
  private static final float[] mOffsets = new float[MAX_RADIUS];
61

  
62
  private static DistortedProgram mProgram1, mProgram2;
63
  private static int mIndex1, mIndex2;
64

  
65
///////////////////////////////////////////////////////////////////////////////////////////////////
66

  
67
  static void destroyStatics()
68
    {
69
    mProgram1 = null;
70
    mProgram2 = null;
71

  
72
    mIndex1 = 0;
73
    mIndex2 = 0;
74
    }
75

  
76
///////////////////////////////////////////////////////////////////////////////////////////////////
77
// This implements the 'Better separable implementation using GPU fixed function sampling' from
78
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
79

  
80
  private void computeGaussianKernel(int radius)
81
    {
82
    int offset = radius + radius*radius/4;
83

  
84
    if( weightsCache[offset]==0.0f )
85
      {
86
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
87
      mWeights[0] = GAUSSIAN[0];
88
      float sum   = GAUSSIAN[0];
89
      int j;
90

  
91
      for(int i=1; i<=radius; i++)
92
        {
93
        x += P;
94
        j = (int)x;
95
        z = x-j;
96

  
97
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
98
        sum += 2*mWeights[i];
99
        }
100

  
101
      for(int i=0; i<=radius; i++) mWeights[i] /= sum;
102

  
103
      int numloops = radius/2;
104
      weightsCache[offset] = mWeights[0];
105
      offsetsCache[offset] = 0.0f;
106

  
107
      for(int i=0; i<numloops; i++)
108
        {
109
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
110
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
111
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
112
        }
113

  
114
      if( radius%2 == 1 )
115
        {
116
        int index = offset + radius/2 +1;
117
        offsetsCache[index]=radius;
118
        weightsCache[index]=mWeights[radius];
119
        }
120
      }
121
    }
122

  
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
/**
125
 * Only for use by the library itself.
126
 *
127
 * @y.exclude
128
 */
129
  public int postprocess(float[] uniforms, int index, DistortedFramebuffer buffer)
130
    {
131
    if( mProgram1 ==null)
132
      {
133
      try
134
        {
135
        mProgram1 = mPrograms.get(mIndex1);
136
        mProgram2 = mPrograms.get(mIndex2);
137
        }
138
      catch(Exception ex)
139
        {
140
        return 0;
141
        }
142
      }
143

  
144
    InternalRenderState.useStencilMark();
145

  
146
    buffer.setAsOutput();
147

  
148
    float w= buffer.getWidth();
149
    float h= buffer.getHeight();
150
    float n= 1.0f - buffer.getNear();
151

  
152
    float corrW = buffer.getWidthCorrection();
153
    float corrH = buffer.getHeightCorrection();
154
    float offsetCorrW = corrW/w;
155
    float offsetCorrH = corrH/h;
156

  
157
    int radius = (int)(uniforms[index+1]*mQualityScale);
158
    if( radius>=MAX_RADIUS ) radius = MAX_RADIUS-1;
159
    if( radius<=0          ) radius = 1;
160
    computeGaussianKernel(radius);
161

  
162
    int offset = radius + radius*radius/4;
163
    radius = (radius+1)/2;
164
    GLES30.glViewport(0, 0, (int)w, (int)h);
165

  
166
    // horizontal blur
167
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
168

  
169
    mProgram1.useProgram();
170
    buffer.bindForOutput(1);
171
    buffer.setAsInput(0);
172

  
173
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
174
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
175

  
176
    GLES30.glColorMask(true,true,true,true);
177
    GLES30.glClearColor(1.0f,1.0f,1.0f,0.0f);
178
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
179

  
180
    GLES30.glUniform1f ( mProgram1.mUniform[0] , n );
181
    GLES30.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
182
    GLES30.glUniform1i ( mProgram1.mUniform[2] , 0 );
183
    GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
184
    GLES30.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
185
    GLES30.glUniform1i ( mProgram1.mUniform[5] , radius);
186
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
187
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
188
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
189

  
190
    // vertical blur
191
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
192

  
193
    mProgram2.useProgram();
194
    buffer.bindForOutput(0);
195
    buffer.setAsInput(1);
196

  
197
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
198
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
199

  
200
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
201

  
202
    GLES30.glUniform1f ( mProgram2.mUniform[0] , n );
203
    GLES30.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
204
    GLES30.glUniform1i ( mProgram2.mUniform[2] , 0 );
205
    GLES30.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
206
    GLES30.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
207
    GLES30.glUniform1i ( mProgram2.mUniform[5] , radius);
208
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
209
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
210
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
211

  
212
    InternalRenderState.unuseStencilMark();
213

  
214
    return 2;
215
    }
216

  
217
///////////////////////////////////////////////////////////////////////////////////////////////////
218
// PUBLIC API
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220
/**
221
 * Have to call this before the shaders get compiled (i.e before DistortedLibrary.onCreate()) for the Effect to work.
222
 */
223
  public static void enable(String prog1, String prog2)
224
    {
225
    final String vertex =
226

  
227
      "precision lowp float;  \n"+
228
      "in vec2 a_Position;    \n"+
229
      "in vec2 a_TexCoord;    \n"+
230
      "out vec2 v_TexCoord;   \n"+
231
      "uniform float u_Depth; \n"+
232
      "uniform vec2 u_TexCorr;\n"+
233

  
234
      "void main()                                      \n"+
235
      "  {                                              \n"+
236
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
237
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
238
      "  }";
239

  
240
    final String fragment1 =
241

  
242
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
243
      "precision lowp float;              \n"+
244
      "in vec2 v_TexCoord;                \n"+
245
      "out vec4 fragColor;                \n"+
246
      "uniform sampler2D u_ColorTexture;  \n"+
247
      "uniform float u_Offsets[MAX_BLUR]; \n"+
248
      "uniform float u_Weights[MAX_BLUR]; \n"+
249
      "uniform int u_Radius;              \n"+
250

  
251
      "void main()                                                                                           \n"+
252
      "  {                                                                                                   \n"+
253
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
254
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
255
      "    {                                                                                                 \n"+
256
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
257
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
258
      "    }                                                                                                 \n"+
259
      "  fragColor = pixel;                                                                                  \n"+
260
      "  }";
261

  
262
    final String fragment2 =
263

  
264
      "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
265
      "precision lowp float;              \n"+
266
      "in vec2 v_TexCoord;                \n"+
267
      "out vec4 fragColor;                \n"+
268
      "uniform sampler2D u_ColorTexture;  \n"+
269
      "uniform float u_Offsets[MAX_BLUR]; \n"+
270
      "uniform float u_Weights[MAX_BLUR]; \n"+
271
      "uniform int u_Radius;              \n"+
272

  
273
      "void main()                                                                                           \n"+
274
      "  {                                                                                                   \n"+
275
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
276
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
277
      "    {                                                                                                 \n"+
278
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
279
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
280
      "    }                                                                                                 \n"+
281
      "  fragColor = pixel;                                                                                  \n"+
282
      "  }";
283

  
284
    mIndex1 = PostprocessEffect.register(prog1, vertex,fragment1);
285
    mIndex2 = PostprocessEffect.register(prog2, vertex,fragment2);
286
    }
287

  
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
/**
290
 * Default constructor.
291
 */
292
  public PostprocessEffectBlurred(EffectName name)
293
    {
294
    super(name);
295
    }
296
  }
src/main/java/org/distorted/library/effect/PostprocessEffectBorder.java
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2021 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.effect;
21

  
22
import org.distorted.library.main.DistortedFramebuffer;
23
import org.distorted.library.type.Data1D;
24
import org.distorted.library.type.Data4D;
25

  
26
///////////////////////////////////////////////////////////////////////////////////////////////////
27

  
28
/**
29
 * Similar effect to Glow, but this one is not blurred and it's entirely behind the object.
30
 */
31
public class PostprocessEffectBorder extends PostprocessEffect
32
  {
33
  private final Data1D mHalo;
34
  private final Data4D mColor;
35

  
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37
// Clean up of static variables on exit. Called by reflection from super class.
38

  
39
  @SuppressWarnings("unused")
40
  static void destroyStatics()
41
    {
42

  
43
    }
44

  
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46
/**
47
 * Only for use by the library itself.
48
 *
49
 * @y.exclude
50
 */
51
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
52
    {
53
    mColor.get(uniforms,index+1,currentDuration,step);
54
    return mHalo.get(uniforms,index,currentDuration,step);
55
    }
56

  
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58
/**
59
 * Only for use by the library itself.
60
 *
61
 * @y.exclude
62
 */
63
  public boolean getRenderDirectly()
64
    {
65
    return true;
66
    }
67

  
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
/**
70
 * Only for use by the library itself.
71
 *
72
 * @y.exclude
73
 */
74
  public int postprocess(float[] uniforms, int index, DistortedFramebuffer buffer)
75
    {
76
    return 0;
77
    }
78

  
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80
// PUBLIC API
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
/**
83
 * No local programs; we do not postprocess anything here. No need to do anything
84
 */
85
  public static void enable()
86
    {
87

  
88
    }
89

  
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91
/**
92
 * Draw a sharp 'border' around the object. Unlike 'GLOW', the border is entirely around the object
93
 * and not in front of it.
94
 *
95
 * @param halo   How far beyond the object does the effect stretch to? Unit: Percentage of the size
96
 *               of the original object, i.e. halo=0 --> no effect at all, halo=100 --> border of
97
 *               size of the object itself.
98
 *
99
 * @param color  RGBA of the color with which to draw the border; example: (1.0f,0.0f,0.0f,0.5f) -
100
 *               half transparent red.
101
 */
102
  public PostprocessEffectBorder(Data1D halo, Data4D color)
103
    {
104
    super(EffectName.BORDER);
105

  
106
    mHalo = halo;
107
    mColor= color;
108
    }
109
  }
src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
19 19

  
20 20
package org.distorted.library.effect;
21 21

  
22
import android.opengl.GLES30;
23

  
24
import org.distorted.library.main.DistortedFramebuffer;
25
import org.distorted.library.main.InternalRenderState;
26
import org.distorted.library.program.DistortedProgram;
27 22
import org.distorted.library.type.Data2D;
28 23
import org.distorted.library.type.Data4D;
29 24

  
......
31 26
/**
32 27
 * Add a (colored) glow around an object.
33 28
 */
34
public class PostprocessEffectGlow extends PostprocessEffect
29
public class PostprocessEffectGlow extends PostprocessEffectBlurred
35 30
  {
36
  private static final int MAX_RADIUS = 50;
37

  
38
  private final Data2D mGlowHaloAndRadius;
31
  private final Data2D mHaloAndRadius;
39 32
  private final Data4D mColor;
40 33

  
41
  private static final float[] GAUSSIAN =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
42
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
43
    0.398948f, 0.398769f, 0.398231f, 0.397336f, 0.396086f, 0.394485f, 0.392537f, 0.390247f, 0.387622f, 0.384668f,
44
    0.381393f, 0.377806f, 0.373916f, 0.369733f, 0.365268f, 0.360532f, 0.355538f, 0.350297f, 0.344823f, 0.339129f,
45
    0.333229f, 0.327138f, 0.320868f, 0.314436f, 0.307856f, 0.301142f, 0.294309f, 0.287373f, 0.280348f, 0.273248f,
46
    0.266089f, 0.258884f, 0.251648f, 0.244394f, 0.237135f, 0.229886f, 0.222657f, 0.215461f, 0.208311f, 0.201217f,
47
    0.194189f, 0.187238f, 0.180374f, 0.173605f, 0.166940f, 0.160386f, 0.153951f, 0.147641f, 0.141462f, 0.135420f,
48
    0.129520f, 0.123765f, 0.118159f, 0.112706f, 0.107408f, 0.102266f, 0.097284f, 0.092461f, 0.087797f, 0.083294f,
49
    0.078951f, 0.074767f, 0.070741f, 0.066872f, 0.063158f, 0.059596f, 0.056184f, 0.052920f, 0.049801f, 0.046823f,
50
    0.043984f, 0.041280f, 0.038707f, 0.036262f, 0.033941f, 0.031740f, 0.029655f, 0.027682f, 0.025817f, 0.024056f,
51
    0.022395f, 0.020830f, 0.019357f, 0.017971f, 0.016670f, 0.015450f, 0.014305f, 0.013234f, 0.012232f, 0.011295f,
52
    0.010421f, 0.009606f, 0.008847f, 0.008140f, 0.007483f, 0.006873f, 0.006307f, 0.005782f, 0.005296f, 0.004847f,
53
    0.004432f, 0.000000f
54
    };
55
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
56

  
57
  // The (fixed-function-sampled) Gaussian Blur kernels are of the size k0=1, k1=2, k2=2, k3=3, k4=3, k5=4, k6=4,...
58
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
59
  // in all 4 directions)
60
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
61
  private static final float[] weightsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
62
  private static final float[] offsetsCache = new float[MAX_RADIUS + MAX_RADIUS*MAX_RADIUS/4];
63
  private static final float[] mWeights = new float[MAX_RADIUS];
64
  private static final float[] mOffsets = new float[MAX_RADIUS];
65

  
66
  private static DistortedProgram mProgram1, mProgram2;
67
  private static int mIndex1, mIndex2;
68

  
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70
// Clean up of static variables on exit.
71
// called by reflection from super class.
72

  
73
  @SuppressWarnings("unused")
74
  static void destroyStatics()
75
    {
76
    mProgram1 = null;
77
    mProgram2 = null;
78

  
79
    mIndex1 = 0;
80
    mIndex2 = 0;
81
    }
82

  
83
///////////////////////////////////////////////////////////////////////////////////////////////////
84
// This implements the 'Better separable implementation using GPU fixed function sampling' from
85
// https://software.intel.com/en-us/blogs/2014/07/15/an-investigation-of-fast-real-time-gpu-based-image-blur-algorithms
86

  
87
  private void computeGaussianKernel(int radius)
88
    {
89
    int offset = radius + radius*radius/4;
90

  
91
    if( weightsCache[offset]==0.0f )
92
      {
93
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
94
      mWeights[0] = GAUSSIAN[0];
95
      float sum   = GAUSSIAN[0];
96
      int j;
97

  
98
      for(int i=1; i<=radius; i++)
99
        {
100
        x += P;
101
        j = (int)x;
102
        z = x-j;
103

  
104
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
105
        sum += 2*mWeights[i];
106
        }
107

  
108
      for(int i=0; i<=radius; i++) mWeights[i] /= sum;
109

  
110
      int numloops = radius/2;
111
      weightsCache[offset] = mWeights[0];
112
      offsetsCache[offset] = 0.0f;
113

  
114
      for(int i=0; i<numloops; i++)
115
        {
116
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
117
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
118
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
119
        }
120

  
121
      if( radius%2 == 1 )
122
        {
123
        int index = offset + radius/2 +1;
124
        offsetsCache[index]=radius;
125
        weightsCache[index]=mWeights[radius];
126
        }
127
      }
128
    }
129

  
130 34
///////////////////////////////////////////////////////////////////////////////////////////////////
131 35
/**
132 36
 * Only for use by the library itself.
......
136 40
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
137 41
    {
138 42
    mColor.get(uniforms,index+2,currentDuration,step);
139
    return mGlowHaloAndRadius.get(uniforms,index,currentDuration,step);
43
    return mHaloAndRadius.get(uniforms,index,currentDuration,step);
140 44
    }
141 45

  
142 46
///////////////////////////////////////////////////////////////////////////////////////////////////
......
145 49
 *
146 50
 * @y.exclude
147 51
 */
148
  public boolean getRender()
52
  public boolean getRenderDirectly()
149 53
    {
150 54
    return true;
151 55
    }
152 56

  
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154
/**
155
 * Only for use by the library itself.
156
 *
157
 * @y.exclude
158
 */
159
  public int apply(float[] uniforms, int index, DistortedFramebuffer buffer)
160
    {
161
    if( mProgram1 ==null)
162
      {
163
      try
164
        {
165
        mProgram1 = mPrograms.get(mIndex1);
166
        mProgram2 = mPrograms.get(mIndex2);
167
        }
168
      catch(Exception ex)
169
        {
170
        return 0;
171
        }
172
      }
173

  
174
    InternalRenderState.useStencilMark();
175

  
176
    buffer.setAsOutput();
177

  
178
    float w= buffer.getWidth();
179
    float h= buffer.getHeight();
180
    float n= 1.0f - buffer.getNear();
181

  
182
    float corrW = buffer.getWidthCorrection();
183
    float corrH = buffer.getHeightCorrection();
184
    float offsetCorrW = corrW/w;
185
    float offsetCorrH = corrH/h;
186

  
187
    int radius = (int)(uniforms[index+1]*mQualityScale);
188
    if( radius>=MAX_RADIUS ) radius = MAX_RADIUS-1;
189
    if( radius<=0          ) radius = 1;
190
    computeGaussianKernel(radius);
191

  
192
    int offset = radius + radius*radius/4;
193
    radius = (radius+1)/2;
194
    GLES30.glViewport(0, 0, (int)w, (int)h);
195

  
196
    // horizontal blur
197
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
198

  
199
    mProgram1.useProgram();
200
    buffer.bindForOutput(1);
201
    buffer.setAsInput(0);
202

  
203
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
204
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
205

  
206
    GLES30.glColorMask(true,true,true,true);
207
    GLES30.glClearColor(1.0f,1.0f,1.0f,0.0f);
208
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
209

  
210
    GLES30.glUniform1f ( mProgram1.mUniform[0] , n );
211
    GLES30.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
212
    GLES30.glUniform1i ( mProgram1.mUniform[2] , 0 );
213
    GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
214
    GLES30.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
215
    GLES30.glUniform1i ( mProgram1.mUniform[5] , radius);
216
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
217
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
218
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
219

  
220
    // vertical blur
221
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
222

  
223
    mProgram2.useProgram();
224
    buffer.bindForOutput(0);
225
    buffer.setAsInput(1);
226

  
227
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE);
228
    GLES30.glTexParameteri( GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE);
229

  
230
    GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT);
231

  
232
    GLES30.glUniform1f ( mProgram2.mUniform[0] , n );
233
    GLES30.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
234
    GLES30.glUniform1i ( mProgram2.mUniform[2] , 0 );
235
    GLES30.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
236
    GLES30.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
237
    GLES30.glUniform1i ( mProgram2.mUniform[5] , radius);
238
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
239
    GLES30.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
240
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
241

  
242
    InternalRenderState.unuseStencilMark();
243

  
244
    return 2;
245
    }
246

  
247 57
///////////////////////////////////////////////////////////////////////////////////////////////////
248 58
// PUBLIC API
249 59
///////////////////////////////////////////////////////////////////////////////////////////////////
......
252 62
 */
253 63
  public static void enable()
254 64
    {
255
    final String glowVertex =
256

  
257
            "precision lowp float;  \n"+
258
            "in vec2 a_Position;    \n"+
259
            "in vec2 a_TexCoord;    \n"+
260
            "out vec2 v_TexCoord;   \n"+
261
            "uniform float u_Depth; \n"+
262
            "uniform vec2 u_TexCorr;\n"+
263

  
264
            "void main()                                      \n"+
265
            "  {                                              \n"+
266
            "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
267
            "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
268
            "  }";
269

  
270
    final String glowFragment1 =
271

  
272
            "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
273
            "precision lowp float;              \n"+
274
            "in vec2 v_TexCoord;                \n"+
275
            "out vec4 fragColor;                \n"+
276
            "uniform sampler2D u_ColorTexture;  \n"+
277
            "uniform float u_Offsets[MAX_BLUR]; \n"+
278
            "uniform float u_Weights[MAX_BLUR]; \n"+
279
            "uniform int u_Radius;              \n"+
280

  
281
            "void main()                                                                                           \n"+
282
            "  {                                                                                                   \n"+
283
            "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
284
            "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
285
            "    {                                                                                                 \n"+
286
            "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
287
            "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
288
            "    }                                                                                                 \n"+
289
            "  fragColor = pixel;                                                                                  \n"+
290
            "  }";
291

  
292
    final String glowFragment2 =
293

  
294
            "#define MAX_BLUR "+MAX_RADIUS+    "\n"+
295
            "precision lowp float;              \n"+
296
            "in vec2 v_TexCoord;                \n"+
297
            "out vec4 fragColor;                \n"+
298
            "uniform sampler2D u_ColorTexture;  \n"+
299
            "uniform float u_Offsets[MAX_BLUR]; \n"+
300
            "uniform float u_Weights[MAX_BLUR]; \n"+
301
            "uniform int u_Radius;              \n"+
65
    PostprocessEffectBlurred.enable("GLOW1","GLOW2");
66
    }
302 67

  
303
            "void main()                                                                                           \n"+
304
            "  {                                                                                                   \n"+
305
            "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
306
            "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
307
            "    {                                                                                                 \n"+
308
            "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
309
            "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
310
            "    }                                                                                                 \n"+
311
            "  fragColor = pixel;                                                                                  \n"+
312
            "  }";
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
// Clean up of static variables on exit. Called by reflection from super class.
313 70

  
314
    mIndex1 = PostprocessEffect.register("GLOW1", glowVertex,glowFragment1);
315
    mIndex2 = PostprocessEffect.register("GLOW2", glowVertex,glowFragment2);
71
  @SuppressWarnings("unused")
72
  static void destroyStatics()
73
    {
74
    PostprocessEffectBlurred.destroyStatics();
316 75
    }
317 76

  
318 77
///////////////////////////////////////////////////////////////////////////////////////////////////
319 78
/**
320 79
 * Make the object glow with a specific color and a halo of specific radius.
321 80
 *
322
 * @param glowHaloAndRadius First float: the halo.
323
 *                          How far beyond the object does the effect stretch to? Unit: Percentage
324
 *                          of the size of the original object, i.e. Halo=0 --> no halo around, this
325
 *                          would mean sharp edges around the object; Halo=100 --> halo of the size
326
 *                          of the object itself around.
327
 *                          Second float: the radius.
328
 *                          The 'strength' if the blur of the edges, in pixels. 0 = no blur, 10 =
329
 *                          blur of roughly 10 pixels around the whole halo.
330
 * @param color             RGBA of the color with which to draw the glow; example: (1.0f,0.0f,0.0f,0.5f) -
331
 *                          half transparent red.
81
 * @param haloAndRadius First float: the halo.
82
 *                      How far beyond the object does the effect stretch to? Unit: Percentage
83
 *                      of the size of the original object, i.e. Halo=0 --> no halo around, this
84
 *                      would mean sharp edges around the object; Halo=100 --> halo of the size
85
 *                      of the object itself around.
86
 *                      Second float: the radius.
87
 *                      The 'strength' of the blur of the edges, in pixels. 0 = no blur, 10 =
88
 *                      blur of roughly 10 pixels around the whole halo.
89
 * @param color         RGBA of the color with which to draw the glow; example: (1.0f,0.0f,0.0f,0.5f) -
90
 *                      half transparent red.
332 91
 */
333
  public PostprocessEffectGlow(Data2D glowHaloAndRadius, Data4D color)
92
  public PostprocessEffectGlow(Data2D haloAndRadius, Data4D color)
334 93
    {
335 94
    super(EffectName.GLOW);
336 95

  
337
    mGlowHaloAndRadius = glowHaloAndRadius;
338
    mColor             = color;
96
    mHaloAndRadius = haloAndRadius;
97
    mColor         = color;
339 98
    }
340 99
  }
src/main/java/org/distorted/library/effectqueue/EffectQueuePostprocess.java
156 156
///////////////////////////////////////////////////////////////////////////////////////////////////
157 157
// TODO  (now only really works in case of 1 effect!)
158 158

  
159
  public boolean getRender()
159
  public boolean getRenderDirectly()
160 160
    {
161
    return mNumEffects > 0 && ((PostprocessEffect) mEffects[0]).getRender();
161
    return mNumEffects > 0 && ((PostprocessEffect) mEffects[0]).getRenderDirectly();
162 162
    }
163 163

  
164 164
///////////////////////////////////////////////////////////////////////////////////////////////////
......
213 213

  
214 214
    for(int i=0; i<mNumEffects; i++)
215 215
      {
216
      numRenders += ((PostprocessEffect)mEffects[i]).apply(array,NUM_FLOAT_UNIFORMS*i, buffer);
216
      numRenders += ((PostprocessEffect)mEffects[i]).postprocess(array,NUM_FLOAT_UNIFORMS*i, buffer);
217 217
      }
218 218

  
219 219
    GLES30.glEnable(GLES30.GL_BLEND);
src/main/java/org/distorted/library/main/InternalOutputSurface.java
43 43
  static final float DEFAULT_NEAR=  0.1f;
44 44

  
45 45
  private float mFOV;
46
  private int mTmpFBO;
46
  private final int mTmpFBO;
47 47

  
48 48
  private long[] mTime;
49 49
  private float mClearR, mClearG, mClearB, mClearA, mClearDepth;
50 50
  private int mClear, mClearStencil;
51 51
  private boolean mRenderWayOIT;
52
  private InternalChildrenList mChildren;
52
  private final InternalChildrenList mChildren;
53 53

  
54 54
  // Global buffers used for postprocessing
55
  private static DistortedFramebuffer[] mBuffer= new DistortedFramebuffer[EffectQuality.LENGTH];
55
  private final static DistortedFramebuffer[] mBuffer= new DistortedFramebuffer[EffectQuality.LENGTH];
56 56

  
57 57
  float mDistance, mNear, mMipmap;
58 58
  float[] mProjectionMatrix;
......
456 456

  
457 457
          buffer= mBuffer[currQuality];
458 458
          bucketChange= i;
459
          renderDirectly = currQueue.getRender();
459
          renderDirectly = currQueue.getRenderDirectly();
460 460
          }
461 461

  
462 462
        if( renderDirectly )

Also available in: Unified diff