Project

General

Profile

« Previous | Next » 

Revision a31dbc5c

Added by Leszek Koltunski over 7 years ago

Beginnings of support for the GLOW effect.

View differences:

src/main/java/org/distorted/library/DistortedEffectsPostprocess.java
21 21

  
22 22
import org.distorted.library.message.EffectListener;
23 23
import org.distorted.library.type.Data1D;
24
import org.distorted.library.type.Data4D;
24 25

  
25 26
import java.util.ArrayList;
26 27

  
......
371 372
    {
372 373
    return mP.add(EffectNames.BLUR, radius);
373 374
    }
375

  
376
///////////////////////////////////////////////////////////////////////////////////////////////////
377
/**
378
 * Make the object glow with a specific color and a halo of specific radius.
379
 *
380
 * @param radius The 'strength' if the effect, in pixels. 0 = no halo, 10 = halo of roughly 10 pixels
381
 *               around the whole object.
382
 * @param color  RGBA of the color with which to draw the glow.
383
 * @return ID of the effect added, or -1 if we failed to add one.
384
 */
385
  public long glow(Data1D radius, Data4D color)
386
    {
387
    return mP.add(EffectNames.GLOW, radius, color);
388
    }
374 389
  }
src/main/java/org/distorted/library/EffectNames.java
241 241

  
242 242
  /////////////////////////////////////////////////////////////////////////////////
243 243
  // POSTPROCESSING EFFECTS.
244
  // Always 4 Uniforms: 4 per-effect interpolated values.
244
  // Always 5 Uniforms: 5 per-effect interpolated values.
245 245
 /**
246 246
   * Blur the area around the center.
247 247
   * <p>
248
   * Uniforms: (radius,UNUSED,UNUSED,UNUSED)
248
   * Uniforms: (radius,UNUSED,UNUSED,UNUSED,UNUSED)
249 249
   * <p>
250 250
   * Unity: radius = 0
251 251
   */
252
  BLUR             ( EffectTypes.POSTPROCESS,new float[] {0.0f}          , 1, false, false );
252
  BLUR             ( EffectTypes.POSTPROCESS,new float[] {0.0f}          , 1, false, false ),
253
/**
254
   * Make the object Glow with a certain color and configurable radius of the halo around it.
255
   * <p>
256
   * Uniforms: (A,R,G,B,radius)
257
   * <p>
258
   * Unity: radius = 0
259
   */
260
  GLOW             ( EffectTypes.POSTPROCESS,new float[] {0.0f}          , 5, false, false );
253 261

  
254 262
///////////////////////////////////////////////////////////////////////////////////////////////////
255 263
  
256
  private static final int MAXDIM = 4;  // maximum supported dimension of an effect  
257
  
264
  private static final int MAXDIM = 4;  // maximum supported dimension of the Unity of an Effect
265
                                        // (not to be confused with dimension of the Effect itself!)
258 266
  private final EffectTypes type;
259 267
  private final float[] unity;
260 268
  private final int dimension;
src/main/java/org/distorted/library/EffectQueuePostprocess.java
30 30
import org.distorted.library.program.VertexCompilationException;
31 31
import org.distorted.library.program.VertexUniformsException;
32 32
import org.distorted.library.type.Data1D;
33
import org.distorted.library.type.Data4D;
33 34
import org.distorted.library.type.Dynamic1D;
35
import org.distorted.library.type.Dynamic4D;
34 36
import org.distorted.library.type.Static1D;
37
import org.distorted.library.type.Static4D;
35 38

  
36 39
import java.io.InputStream;
37 40
import java.nio.ByteBuffer;
......
42 45

  
43 46
class EffectQueuePostprocess extends EffectQueue
44 47
  {
48
  private static final int MAX_HALO = 50;    // Support effects creating up to MAX_HALO pixels wide 'halo' around the object.
49

  
45 50
  private static final int POS_DATA_SIZE= 2; // Post Program: size of the position data in elements
46 51
  private static final int TEX_DATA_SIZE= 2; // Post Program: size of the texture coordinate data in elements.
47 52

  
48
  private static final int NUM_UNIFORMS = 4;
53
  private static final int NUM_UNIFORMS = 5;
49 54
  private static final int NUM_CACHE    = 0;
50 55
  private static final int INDEX = EffectTypes.POSTPROCESS.ordinal();
51 56

  
......
68 73
    mQuadTextureInv.put(textureInv).position(0);
69 74
    }
70 75

  
76
  int mQualityLevel;
77
  float mQualityScale;
78

  
79
  /////////////////////////////////////////////////////////////////////////////////
71 80
  // BLUR effect
72 81
  private static final float GAUSSIAN[] =   // G(0.00), G(0.03), G(0.06), ..., G(3.00), 0
73 82
    {                                       // where G(x)= (1/(sqrt(2*PI))) * e^(-(x^2)/2). The last 0 terminates.
......
85 94
    };
86 95
  private static final int NUM_GAUSSIAN = GAUSSIAN.length-2;
87 96

  
88
  // Support blurs consisting of the present pixel and up to MAX_BLUR pixels in each direction
89
  static final int MAX_BLUR = 50;
90

  
91 97
  // 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,...
92 98
  // i.e. k(i)=floor((i+3)/2).  (the 'i' in k(i) means 'blur taking into account the present pixel and 'i' pixels
93 99
  // in all 4 directions)
94 100
  // We need room for MAX_BLUR of them, and sum(i=0...N, floor((i+3)/2)) = N + floor(N*N/4)
95
  private static float[] weightsCache = new float[MAX_BLUR + MAX_BLUR*MAX_BLUR/4];
96
  private static float[] offsetsCache = new float[MAX_BLUR + MAX_BLUR*MAX_BLUR/4];
101
  private static float[] weightsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
102
  private static float[] offsetsCache = new float[MAX_HALO + MAX_HALO*MAX_HALO/4];
97 103

  
98 104
  private static DistortedProgram mBlur1Program, mBlur2Program;
99 105
  private static int mRadius1H,mOffsets1H,mWeights1H,mDepth1H, mColorTexture1H;
100 106
  private static int mRadius2H,mOffsets2H,mWeights2H,mDepth2H, mColorTexture2H;
101
  private static float[] mWeights = new float[MAX_BLUR];
102
  private static float[] mOffsets = new float[MAX_BLUR];
103
  // another effect ....
107
  private static float[] mWeights = new float[MAX_HALO];
108
  private static float[] mOffsets = new float[MAX_HALO];
109
  /////////////////////////////////////////////////////////////////////////////////
110
  // GLOW effect
104 111

  
105
  int mQualityLevel;
106
  float mQualityScale;
107 112

  
108 113
///////////////////////////////////////////////////////////////////////////////////////////////////
109 114

  
......
127 132
      {
128 133
      mBlur1Program = new DistortedProgram(blur1VertexStream,blur1FragmentStream,
129 134
                                          Distorted.GLSL_VERSION,
130
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
135
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, Distorted.GLSL);
131 136
      }
132 137
    catch(Exception e)
133 138
      {
......
149 154
      {
150 155
      mBlur2Program = new DistortedProgram(blur2VertexStream,blur2FragmentStream,
151 156
                                          Distorted.GLSL_VERSION,
152
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_BLUR, Distorted.GLSL);
157
                                          Distorted.GLSL_VERSION + "#define MAX_BLUR "+MAX_HALO, Distorted.GLSL);
153 158
      }
154 159
    catch(Exception e)
155 160
      {
......
168 173

  
169 174
///////////////////////////////////////////////////////////////////////////////////////////////////
170 175

  
171
  synchronized boolean compute(long currTime)
176
  private boolean compute(long currTime)
172 177
    {
173 178
    if( currTime==mTime ) return false;
174 179
    if( mTime==0 ) mTime = currTime;
......
195 200
          }
196 201
        else mInter[0][i] = null;
197 202
        }
203

  
204
      if( mInter[1][i]!=null ) mInter[1][i].interpolateMain( mUniforms, NUM_UNIFORMS*i+1, mCurrentDuration[i], step);
198 205
      }
199 206

  
200 207
    mTime = currTime;
......
210 217
    mUniforms[NUM_UNIFORMS*index+1] = mUniforms[NUM_UNIFORMS*(index+1)+1];
211 218
    mUniforms[NUM_UNIFORMS*index+2] = mUniforms[NUM_UNIFORMS*(index+1)+2];
212 219
    mUniforms[NUM_UNIFORMS*index+3] = mUniforms[NUM_UNIFORMS*(index+1)+3];
220
    mUniforms[NUM_UNIFORMS*index+4] = mUniforms[NUM_UNIFORMS*(index+1)+4];
213 221
    }
214 222

  
215 223
///////////////////////////////////////////////////////////////////////////////////////////////////
......
280 288
        {
281 289
        if (mName[i] == EffectNames.BLUR.ordinal() )
282 290
          {
283
          blur(mUniforms[i],surface);
291
          blur(NUM_UNIFORMS*i,surface);
292
          numRenders += 2;
293
          }
294
        else if (mName[i] == EffectNames.GLOW.ordinal() )
295
          {
296
          glow(NUM_UNIFORMS*i,surface);
284 297
          numRenders += 2;
285 298
          }
286 299
        }
......
291 304

  
292 305
///////////////////////////////////////////////////////////////////////////////////////////////////
293 306

  
294
  private void blur(float degree, DistortedOutputSurface surface)
307
  private void blur(int index, DistortedOutputSurface surface)
295 308
    {
296 309
    DistortedFramebuffer buffer = surface.mBuffer[mQualityLevel];
297 310
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, buffer.mFBOH[0]);
......
299 312
    float w1 = buffer.mWidth;
300 313
    float h1 = buffer.mHeight;
301 314

  
302
    int radius = (int)(degree*mQualityScale);
303
    if( radius>=MAX_BLUR ) radius = MAX_BLUR-1;
315
    int radius = (int)(mUniforms[index]*mQualityScale);
316
    if( radius>=MAX_HALO ) radius = MAX_HALO-1;
304 317
    computeGaussianKernel(radius);
305 318

  
306 319
    int offset = radius + radius*radius/4;
......
354 367
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
355 368
    }
356 369

  
370
///////////////////////////////////////////////////////////////////////////////////////////////////
371

  
372
  private void glow(int index, DistortedOutputSurface surface)
373
    {
374

  
375
    }
376

  
357 377
///////////////////////////////////////////////////////////////////////////////////////////////////
358 378
// blur
359 379

  
......
380 400
      
381 401
    return -1;
382 402
    }
403

  
404
///////////////////////////////////////////////////////////////////////////////////////////////////
405
// glow
406

  
407
  synchronized long add(EffectNames eln, Data1D degree, Data4D color)
408
    {
409
    if( mMax[INDEX]>mNumEffects )
410
      {
411
      if( degree instanceof Dynamic1D)
412
        {
413
        mInter[0][mNumEffects] = (Dynamic1D)degree;
414
        }
415
      else if( degree instanceof Static1D)
416
        {
417
        mInter[0][mNumEffects] = null;
418
        mUniforms[NUM_UNIFORMS*mNumEffects] = ((Static1D)degree).getX();
419
        }
420
      else return -1;
421

  
422
      if( color instanceof Dynamic4D)
423
        {
424
        mInter[1][mNumEffects] = (Dynamic4D)color;
425
        }
426
      else if( color instanceof Static4D)
427
        {
428
        mInter[1][mNumEffects] = null;
429
        Static4D tmp = (Static4D)color;
430
        mUniforms[NUM_UNIFORMS*mNumEffects+1] = tmp.getW();  //
431
        mUniforms[NUM_UNIFORMS*mNumEffects+2] = tmp.getX();  // Invert: RGBA sent
432
        mUniforms[NUM_UNIFORMS*mNumEffects+3] = tmp.getY();  // in, ARGB inside
433
        mUniforms[NUM_UNIFORMS*mNumEffects+4] = tmp.getZ();  //
434
        }
435
      else return -1;
436

  
437
      mInter[2][mNumEffects] = null;
438

  
439
      return addBase(eln);
440
      }
441

  
442
    return -1;
443
    }
383 444
  }

Also available in: Unified diff