Project

General

Profile

« Previous | Next » 

Revision 9e771d06

Added by Leszek Koltunski almost 6 years ago

Progress with thr Glow effect - moving glow app looks ok now.

View differences:

src/main/java/org/distorted/library/effect/PostprocessEffectGlow.java
22 22
import android.opengl.GLES31;
23 23

  
24 24
import org.distorted.library.main.Distorted;
25
import org.distorted.library.main.DistortedEffects;
26 25
import org.distorted.library.main.DistortedFramebuffer;
27
import org.distorted.library.main.DistortedOutputSurface;
28 26
import org.distorted.library.main.DistortedRenderState;
29 27
import org.distorted.library.program.DistortedProgram;
30 28
import org.distorted.library.type.Data1D;
......
129 127
    }
130 128

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

  
142 140
///////////////////////////////////////////////////////////////////////////////////////////////////
143 141
/**
......
145 143
 *
146 144
 * @y.exclude
147 145
 */
148
  public int apply(float[] uniforms, int index, DistortedOutputSurface[] buffers)
146
  public int apply(float[] uniforms, int index, DistortedFramebuffer buffer)
149 147
    {
150 148
    if( mProgram1 ==null)
151 149
      {
......
162 160

  
163 161
    DistortedRenderState.useStencilMark();
164 162

  
165
    DistortedFramebuffer  inBuffer = (DistortedFramebuffer)buffers[0];
166
    DistortedFramebuffer outBuffer = (DistortedFramebuffer)buffers[mQualityLevel];
163
    buffer.setAsOutput();
167 164

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

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

  
......
180 177

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

  
184 180
    GLES31.glViewport(0, 0, (int)w, (int)h);
185 181

  
186
    // horizontal glow
182
    // horizontal blur
187 183
    for(int i=0; i<=radius; i++) mOffsets[i] = offsetsCache[offset+i]*offsetCorrW;
188 184

  
189 185
    mProgram1.useProgram();
190
    outBuffer.setAsOutput();
191
    outBuffer.bindForOutput(1);
192
    inBuffer.setAsInput(0);
186
    buffer.bindForOutput(1);
187
    buffer.setAsInput(0);
193 188

  
194 189
    GLES31.glColorMask(true,true,true,true);
195 190
    GLES31.glClearColor(1.0f,1.0f,1.0f,0.0f);
......
201 196
    GLES31.glUniform1fv( mProgram1.mUniform[3] , radius+1, mOffsets,0);
202 197
    GLES31.glUniform1fv( mProgram1.mUniform[4] , radius+1, weightsCache,offset);
203 198
    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 199
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[0], POS_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadPositions);
206 200
    GLES31.glVertexAttribPointer(mProgram1.mAttribute[1], TEX_DATA_SIZE, GLES31.GL_FLOAT, false, 0, mQuadTexture);
207 201
    GLES31.glDrawArrays(GLES31.GL_TRIANGLE_STRIP, 0, 4);
208 202

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

  
212 206
    mProgram2.useProgram();
213
    outBuffer.bindForOutput(0);
214
    outBuffer.setAsInput(1);
207
    buffer.bindForOutput(0);
208
    buffer.setAsInput(1);
215 209

  
216
    if( mQualityLevel==0 )
217
      {
218
      GLES31.glEnable(GLES31.GL_BLEND);
219
      }
210
    GLES31.glClear(GLES31.GL_COLOR_BUFFER_BIT);
220 211

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

  
232
    if( mQualityLevel==0 )
233
      {
234
      GLES31.glDisable(GLES31.GL_BLEND);
235
      }
236

  
237 222
    DistortedRenderState.unuseStencilMark();
238 223

  
239
    // blit back to inBuffer if we have to
240
    if( mQualityLevel>0 )
241
      {
242
      inBuffer.setAsOutput();
243
      inBuffer.bindForOutput(0);
244
      outBuffer.setAsInput(0);
245
      GLES31.glEnable(GLES31.GL_BLEND);
246
      DistortedEffects.blitPriv(inBuffer);
247
      GLES31.glDisable(GLES31.GL_BLEND);
248
      return 3;
249
      }
250

  
251 224
    return 2;
252 225
    }
253 226

  
......
262 235
    final String glowVertex =
263 236

  
264 237
        Distorted.GLSL_VERSION   +
265
      "precision lowp float;  \n"+
266
      "in vec2 a_Position;    \n"+
267
      "in vec2 a_TexCoord;    \n"+
268
      "out vec2 v_TexCoord;   \n"+
269
      "uniform float u_Depth; \n"+
270
      "uniform vec2 u_TexCorr;\n"+
271

  
272
      "void main()                                      \n"+
273
      "  {                                              \n"+
274
      "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
275
      "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
276
      "  }";
238
            "precision lowp float;  \n"+
239
            "in vec2 a_Position;    \n"+
240
            "in vec2 a_TexCoord;    \n"+
241
            "out vec2 v_TexCoord;   \n"+
242
            "uniform float u_Depth; \n"+
243
            "uniform vec2 u_TexCorr;\n"+
244

  
245
            "void main()                                      \n"+
246
            "  {                                              \n"+
247
            "  v_TexCoord = a_TexCoord * u_TexCorr;           \n"+
248
            "  gl_Position= vec4(2.0*a_Position,u_Depth,1.0); \n"+
249
            "  }";
277 250

  
278 251
    final String glowFragment1 =
279 252

  
280 253
        Distorted.GLSL_VERSION               +
281
      "#define MAX_BLUR "+MAX_HALO+      "\n"+
282
      "precision lowp float;              \n"+
283
      "in vec2 v_TexCoord;                \n"+
284
      "out vec4 fragColor;                \n"+
285
      "uniform sampler2D u_ColorTexture;  \n"+
286
      "uniform float u_Offsets[MAX_BLUR]; \n"+
287
      "uniform float u_Weights[MAX_BLUR]; \n"+
288
      "uniform int u_Radius;              \n"+
289
      "uniform vec4 u_Color;              \n"+
290

  
291
      "void main()                                                                                           \n"+
292
      "  {                                                                                                   \n"+
293
      "  vec4 pixel = texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                     \n"+
294
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
295
      "    {                                                                                                 \n"+
296
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
297
      "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
298
      "    }                                                                                                 \n"+
299
      "  fragColor = vec4( 0.5*(pixel.rgb+u_Color.rgb), sqrt(pixel.a*u_Color.a));                            \n"+
300
      "  }";
254
            "#define MAX_BLUR "+MAX_HALO+      "\n"+
255
            "precision lowp float;              \n"+
256
            "in vec2 v_TexCoord;                \n"+
257
            "out vec4 fragColor;                \n"+
258
            "uniform sampler2D u_ColorTexture;  \n"+
259
            "uniform float u_Offsets[MAX_BLUR]; \n"+
260
            "uniform float u_Weights[MAX_BLUR]; \n"+
261
            "uniform int u_Radius;              \n"+
262

  
263
            "void main()                                                                                           \n"+
264
            "  {                                                                                                   \n"+
265
            "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
266
            "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
267
            "    {                                                                                                 \n"+
268
            "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x+u_Offsets[i],v_TexCoord.y)) +                 \n"+
269
            "               texture(u_ColorTexture,vec2(v_TexCoord.x-u_Offsets[i],v_TexCoord.y)) ) * u_Weights[i]; \n"+
270
            "    }                                                                                                 \n"+
271
            "  fragColor = pixel;                                                                                  \n"+
272
            "  }";
301 273

  
302 274
    final String glowFragment2 =
303 275

  
304 276
        Distorted.GLSL_VERSION               +
305
      "#define MAX_BLUR "+MAX_HALO+      "\n"+
306
      "precision lowp float;              \n"+
307
      "in vec2 v_TexCoord;                \n"+
308
      "out vec4 fragColor;                \n"+
309
      "uniform sampler2D u_ColorTexture;  \n"+
310
      "uniform float u_Offsets[MAX_BLUR]; \n"+
311
      "uniform float u_Weights[MAX_BLUR]; \n"+
312
      "uniform int u_Radius;              \n"+
313
      "uniform vec4 u_Color;              \n"+
314

  
315
      "void main()                                                                                           \n"+
316
      "  {                                                                                                   \n"+
317
      "  vec4 pixel = texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                     \n"+
318
      "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
319
      "    {                                                                                                 \n"+
320
      "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
321
      "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
322
      "    }                                                                                                 \n"+
323
      "  fragColor = vec4( 0.5*(pixel.rgb+u_Color.rgb), sqrt(pixel.a*u_Color.a));                            \n"+
324
      "  }";
277
            "#define MAX_BLUR "+MAX_HALO+      "\n"+
278
            "precision lowp float;              \n"+
279
            "in vec2 v_TexCoord;                \n"+
280
            "out vec4 fragColor;                \n"+
281
            "uniform sampler2D u_ColorTexture;  \n"+
282
            "uniform float u_Offsets[MAX_BLUR]; \n"+
283
            "uniform float u_Weights[MAX_BLUR]; \n"+
284
            "uniform int u_Radius;              \n"+
285

  
286
            "void main()                                                                                           \n"+
287
            "  {                                                                                                   \n"+
288
            "  vec4 pixel= texture(u_ColorTexture,v_TexCoord) * u_Weights[0];                                      \n"+
289
            "  for (int i=1; i<=u_Radius; i+=1)                                                                    \n"+
290
            "    {                                                                                                 \n"+
291
            "    pixel += ( texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y+u_Offsets[i])) +                 \n"+
292
            "               texture(u_ColorTexture,vec2(v_TexCoord.x,v_TexCoord.y-u_Offsets[i])) ) * u_Weights[i]; \n"+
293
            "    }                                                                                                 \n"+
294
            "  fragColor = pixel;                                                                                  \n"+
295
            "  }";
325 296

  
326 297
    mIndex1 = PostprocessEffect.register("GLOW1", glowVertex,glowFragment1);
327 298
    mIndex2 = PostprocessEffect.register("GLOW2", glowVertex,glowFragment2);

Also available in: Unified diff