Project

General

Profile

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

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

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.program.DistortedProgram;
26
import org.distorted.library.type.Data1D;
27
import org.distorted.library.type.Data4D;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class PostprocessEffectGlow extends PostprocessEffect
32
  {
33
  private static final int MAX_HALO = 50;
34

    
35
  private Data1D mGlowRadius;
36
  private Data4D mColor;
37

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

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

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

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

    
70
  private void computeGaussianKernel(int radius)
71
    {
72
    int offset = radius + radius*radius/4;
73

    
74
    if( weightsCache[offset]==0.0f )
75
      {
76
      float z, x= 0.0f, P= (float)NUM_GAUSSIAN / (radius>3 ? radius:3);
77
      mWeights[0] = GAUSSIAN[0];
78
      float sum   = GAUSSIAN[0];
79
      int j;
80

    
81
      for(int i=1; i<=radius; i++)
82
        {
83
        x += P;
84
        j = (int)x;
85
        z = x-j;
86

    
87
        mWeights[i] = (1-z)*GAUSSIAN[j] + z*GAUSSIAN[j+1];
88
        sum += 2*mWeights[i];
89
        }
90

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

    
93
      int numloops = radius/2;
94
      weightsCache[offset] = mWeights[0];
95
      offsetsCache[offset] = 0.0f;
96

    
97
      for(int i=0; i<numloops; i++)
98
        {
99
        offsetsCache[offset+i+1] = mWeights[2*i+1]*(2*i+1) + mWeights[2*i+2]*(2*i+2);
100
        weightsCache[offset+i+1] = mWeights[2*i+1] + mWeights[2*i+2];
101
        offsetsCache[offset+i+1]/= weightsCache[offset+i+1];
102
        }
103

    
104
      if( radius%2 == 1 )
105
        {
106
        int index = offset + radius/2 +1;
107
        offsetsCache[index]=radius;
108
        weightsCache[index]=mWeights[radius];
109
        }
110
      }
111
    }
112

    
113
///////////////////////////////////////////////////////////////////////////////////////////////////
114
/**
115
 * Only for use by the library itself.
116
 *
117
 * @y.exclude
118
 */
119
  public boolean compute(float[] uniforms, int index, long currentDuration, long step )
120
    {
121
    mColor.get(uniforms,index+1,currentDuration,step);
122
    return mGlowRadius.get(uniforms,index,currentDuration,step);
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
/**
127
 * Only for use by the library itself.
128
 *
129
 * @y.exclude
130
 */
131
  public int apply(float[] uniforms, int index, float qualityScale, DistortedFramebuffer buffer)
132
    {
133
    if( mProgram1 ==null)
134
      {
135
      mProgram1 = mPrograms.get(mIndex1);
136
      mProgram2 = mPrograms.get(mIndex2);
137
      }
138

    
139
    buffer.setAsOutput();
140

    
141
    float w1  = buffer.getWidth();
142
    float h1  = buffer.getHeight();
143
    float near= 1.0f - buffer.getNear();
144

    
145
    int radius = (int)(uniforms[index]*qualityScale);
146
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
147
    computeGaussianKernel(radius);
148

    
149
    int offset = radius + radius*radius/4;
150
    radius = (radius+1)/2;
151
    GLES30.glViewport(0, 0, (int)w1, (int)h1);
152

    
153
    // horizontal blur
154
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/h1;
155

    
156
    mProgram1.useProgram();
157
    buffer.bindForOutput(1);
158
    buffer.setAsInput(0);
159

    
160
    GLES30.glUniform1f ( mProgram1.mUniform[0] , near);
161
    GLES30.glUniform1i ( mProgram1.mUniform[1] , 0 );
162
    GLES30.glUniform1fv( mProgram1.mUniform[2] , radius+1, mOffsets,0);
163
    GLES30.glUniform1fv( mProgram1.mUniform[3] , radius+1, weightsCache,offset);
164
    GLES30.glUniform1i ( mProgram1.mUniform[4] , radius);
165
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadPositions);
166
    GLES30.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES30.GL_FLOAT, false, 0, mQuadTexture);
167
    GLES30.glDrawArrays(GLES30.GL_TRIANGLE_STRIP, 0, 4);
168

    
169
    // vertical blur
170
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]/w1;
171

    
172
    mProgram2.useProgram();
173
    buffer.bindForOutput(0);
174
    buffer.setAsInput(1);
175

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

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

    
189
    return 2;
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// PUBLIC API
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
/**
196
 * Have to call this before the shaders get compiled (i.e before Distorted.onCreate()) for the Effect to work.
197
 */
198
  public static void enable()
199
    {
200
    final String glowVertex =
201

    
202
      "#version 300 es        \n"+
203
      "precision lowp float;  \n"+
204
      "in vec2 a_Position;    \n"+
205
      "in vec2 a_TexCoord;    \n"+
206
      "out vec2 v_TexCoord;   \n"+
207
      "uniform float u_Depth; \n"+
208

    
209
      "void main()                                      \n"+
210
      "  {                                              \n"+
211
      "  v_TexCoord = a_TexCoord;                       \n"+
212
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
213
      "  }";
214

    
215
    final String glowFragment1 =
216

    
217
      "#version 300 es                    \n"+
218
      "#define MAX_BLUR "+MAX_HALO+      "\n"+
219
      "precision lowp float;              \n"+
220
      "in vec2 v_TexCoord;                \n"+
221
      "out vec4 fragColor;                \n"+
222
      "uniform sampler2D u_ColorTexture;  \n"+
223
      "uniform float u_Offsets[MAX_BLUR]; \n"+
224
      "uniform float u_Weights[MAX_BLUR]; \n"+
225
      "uniform int u_Radius;              \n"+
226

    
227
      "void main()                                                                                           \n"+
228
      "  {                                                                                                   \n"+
229
      "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
230
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
231
      "    {                                                                                                 \n"+
232
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
233
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
234
      "    }                                                                                                 \n"+
235
      "  fragColor = pixel;                                                                                  \n"+
236
      "  }";
237

    
238
    final String glowFragment2 =
239

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

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

    
261
    mIndex1 = PostprocessEffect.register("BLUR1", glowVertex,glowFragment1);
262
    mIndex2 = PostprocessEffect.register("BLUR2", glowVertex,glowFragment2);
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
/**
268
 * Make the object glow with a specific color and a halo of specific radius.
269
 *
270
 * @param glowRadius The 'strength' if the effect, in pixels. 0 = no halo, 10 = halo of roughly 10 pixels
271
 *                   around the whole object.
272
 * @param color      RGBA of the color with which to draw the glow.
273
 */
274
  public PostprocessEffectGlow(Data1D glowRadius, Data4D color)
275
    {
276
    super(EffectName.GLOW);
277
    mGlowRadius = glowRadius;
278
    mColor      = color;
279
    }
280
  }
(19-19/26)