Project

General

Profile

Download (14.9 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / effect / PostprocessEffectGlow.java @ c25273e0

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.GLES31;
23

    
24
import org.distorted.library.main.Distorted;
25
import org.distorted.library.main.DistortedEffects;
26
import org.distorted.library.main.DistortedFramebuffer;
27
import org.distorted.library.main.DistortedOutputSurface;
28
import org.distorted.library.main.DistortedRenderState;
29
import org.distorted.library.program.DistortedProgram;
30
import org.distorted.library.type.Data1D;
31
import org.distorted.library.type.Data4D;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
/**
35
 * Add a (colored) glow about each object in the Framebuffer.
36
 */
37
public class PostprocessEffectGlow extends PostprocessEffect
38
  {
39
  private static final int MAX_HALO = 50;
40

    
41
  private Data1D mGlowRadius;
42
  private Data4D mColor;
43

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

    
60
  // 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,...
61
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
62
  // in all 4 directions)
63
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
64
  private static float[] weightsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
65
  private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
66
  private static float[] mWeights = new float[MAX_HALO];
67
  private static float[] mOffsets = new float[MAX_HALO];
68

    
69
  private static DistortedProgram mProgram1, mProgram2;
70
  private static int mIndex1, mIndex2;
71

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

    
76
  private void computeGaussianKernel(int radius)
77
    {
78
    int offset = radius + radius*radius/4;
79

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

    
87
      for(int i=1; i<=radius; i++)
88
        {
89
        x += P;
90
        j = (int)x;
91
        z = x-j;
92

    
93
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
94
        sum += 2*mWeights[i];
95
        }
96

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

    
99
      int numloops = radius/2;
100
      weightsCache[offset] = mWeights[0];
101
      offsetsCache[offset] = 0.0f;
102

    
103
      for(int i=0; i<numloops; i++)
104
        {
105
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
106
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
107
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
108
        }
109

    
110
      if( radius%2 == 1 )
111
        {
112
        int index = offset + radius/2 +1;
113
        offsetsCache[index]=radius;
114
        weightsCache[index]=mWeights[radius];
115
        }
116
      }
117
    }
118

    
119
///////////////////////////////////////////////////////////////////////////////////////////////////
120
/**
121
 * Only for use by the library itself.
122
 *
123
 * @y.exclude
124
 */
125
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
126
    {
127
    mColor.get(uniforms,index+1,currentDuration,step);
128
    return mGlowRadius.get(uniforms,index,currentDuration,step);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132
/**
133
 * Only for use by the library itself.
134
 *
135
 * @y.exclude
136
 */
137
 public int getQuality()
138
   {
139
   return 0;
140
   }
141

    
142
///////////////////////////////////////////////////////////////////////////////////////////////////
143
/**
144
 * Only for use by the library itself.
145
 *
146
 * @y.exclude
147
 */
148
  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers, int fbo)
149
    {
150
    if( mProgram1 ==null)
151
      {
152
      try
153
        {
154
        mProgram1 = mPrograms.get(mIndex1);
155
        mProgram2 = mPrograms.get(mIndex2);
156
        }
157
      catch(Exception ex)
158
        {
159
        return 0;
160
        }
161
      }
162

    
163
    DistortedRenderState.useStencilMark();
164

    
165
    DistortedFramebuffer  inBuffer = (DistortedFramebuffer)buffers[0];
166
    DistortedFramebuffer outBuffer = (DistortedFramebuffer)buffers[mQualityLevel];
167

    
168
    float w= outBuffer.getWidth();
169
    float h= outBuffer.getHeight();
170
    float n= 1.0f - outBuffer.getNear();
171

    
172
    float corrW = inBuffer.getWidthCorrection();
173
    float corrH = inBuffer.getHeightCorrection();
174
    float offsetCorrW = corrW/w;
175
    float offsetCorrH = corrH/h;
176

    
177
    int radius = (int)(uniforms[index]*mQualityScale);
178
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
179
    computeGaussianKernel(radius);
180

    
181
    int offset = radius + radius*radius/4;
182
    radius = (radius+1)/2;
183

    
184
    outBuffer.setAsOutputFBO(fbo);
185
    GLES31.glViewport(0, 0, (int)w, (int)h);
186

    
187
    // horizontal glow
188
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
189

    
190
    mProgram1.useProgram();
191
    outBuffer.bindForOutput(2*fbo+1);
192
    inBuffer.setAsInput(2*fbo);
193

    
194
    GLES31.glColorMask(true,true,true,true);
195
    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
196
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
197

    
198
    GLES31.glUniform1f ( mProgram1.mUniform[0] , n );
199
    GLES31.glUniform2f ( mProgram1.mUniform[1] , corrW, corrH );
200
    GLES31.glUniform1i ( mProgram1.mUniform[2] , 0 );
201
    GLES31.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
202
    GLES31.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
203
    GLES31.glUniform1i ( mProgram1.mUniform[5] , radius);
204
    GLES31.glUniform4f ( mProgram1.mUniform[6] , uniforms[index+1], uniforms[index+2], uniforms[index+3], uniforms[index+4]);
205
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
206
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
207
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
208

    
209
    // vertical glow
210
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrH;
211

    
212
    mProgram2.useProgram();
213
    outBuffer.bindForOutput(2*fbo);
214
    outBuffer.setAsInput(2*fbo+1);
215

    
216
    GLES31.glUniform1f ( mProgram2.mUniform[0] , n );
217
    GLES31.glUniform2f ( mProgram2.mUniform[1] , corrW, corrH );
218
    GLES31.glUniform1i ( mProgram2.mUniform[2] , 0 );
219
    GLES31.glUniform1fv( mProgram2.mUniform[3] , radius+1, mOffsets,0);
220
    GLES31.glUniform1fv( mProgram2.mUniform[4] , radius+1, weightsCache,offset);
221
    GLES31.glUniform1i ( mProgram2.mUniform[5] , radius);
222
    GLES31.glUniform4f ( mProgram1.mUniform[6] , uniforms[index+1], uniforms[index+2], uniforms[index+3], uniforms[index+4]);
223
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
224
    GLES31.glVertexAttribPointer(mProgram2.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
225
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
226

    
227
    DistortedRenderState.unuseStencilMark();
228

    
229
    // blit back to inBuffer if we have to
230
    if( mQualityLevel>0 )
231
      {
232
      inBuffer.setAsOutput();
233
      outBuffer.setAsInput(0);
234
      GLES31.glEnable(GLES31.GL_BLEND);
235
      DistortedEffects.blitPriv(inBuffer);
236
      GLES31.glDisable(GLES31.GL_BLEND);
237
      return 3;
238
      }
239

    
240
    return 2;
241
    }
242

    
243
///////////////////////////////////////////////////////////////////////////////////////////////////
244
// PUBLIC API
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246
/**
247
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
248
 */
249
  public static void enable()
250
    {
251
    final String glowVertex =
252

    
253
        Distorted.GLSL_VERSION   +
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 glowFragment1 =
268

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

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

    
291
    final String glowFragment2 =
292

    
293
        Distorted.GLSL_VERSION               +
294
      "#define MAX_BLUR "+MAX_HALO+      "\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"+
302
      "uniform vec4 u_Color;              \n"+
303

    
304
      "void main()                                                                                           \n"+
305
      "  {                                                                                                   \n"+
306
      "  vec4 pixel = texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                     \n"+
307
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
308
      "    {                                                                                                 \n"+
309
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
310
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
311
      "    }                                                                                                 \n"+
312
      "  fragColor = vec4( 0.5*(pixel.rgb+u_Color.rgb), sqrt(pixel.a*u_Color.a));                            \n"+
313
      "  }";
314

    
315
    mIndex1 = PostprocessEffect.register("GLOW1", glowVertex,glowFragment1);
316
    mIndex2 = PostprocessEffect.register("GLOW2", glowVertex,glowFragment2);
317
    }
318

    
319
///////////////////////////////////////////////////////////////////////////////////////////////////
320

    
321
/**
322
 * Make the object glow with a specific color and a halo of specific radius.
323
 *
324
 * @param glowRadius The 'strength' if the effect, in pixels. 0 = no halo, 10 = halo of roughly 10 pixels
325
 *                   around the whole object.
326
 * @param color      RGBA of the color with which to draw the glow.
327
 */
328
  public PostprocessEffectGlow(Data1D glowRadius, Data4D color)
329
    {
330
    super(EffectName.GLOW);
331
    mGlowRadius = glowRadius;
332
    mColor      = color;
333
    }
334
  }
(19-19/26)