Project

General

Profile

« Previous | Next » 

Revision 86eb00a9

Added by Leszek Koltunski over 7 years ago

Initial version of the first post-processing effect - BLUR - ready for the first app!

View differences:

src/main/java/org/distorted/library/Distorted.java
90 90
    
91 91
    }
92 92

  
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

  
95
  static DistortedFramebuffer getFBO(int w, int h)
96
    {
97
    // TODO: a static factory of Framebuffers.
98

  
99
    return null;
100
    }
101

  
102 93
///////////////////////////////////////////////////////////////////////////////////////////////////
103 94

  
104 95
  static boolean isInitialized()
......
106 97
    return (mMainProgramAttributes!=null);
107 98
    }
108 99

  
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
/**
111
 * Release all internal memory caches (in particular the FBOs used as buffers for postprocessing)
112
 */
113
  public static void clean()
114
    {
115
    // TODO
116
    }
117

  
118 100
///////////////////////////////////////////////////////////////////////////////////////////////////
119 101
/**
120 102
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
......
209 191
    EffectMessageSender.stopSending();
210 192

  
211 193
    mMainProgramAttributes = null;
194
    mPostProgramAttributes = null;
212 195
    }
213 196
  }
src/main/java/org/distorted/library/DistortedEffects.java
49 49
  private static long mNextID =0;
50 50
  private long mID;
51 51

  
52
  private static DistortedFramebuffer mBufferFBO;
53

  
52 54
  private EffectQueueMatrix      mM;
53 55
  private EffectQueueFragment    mF;
54 56
  private EffectQueueVertex      mV;
......
67 69
    mQuadPositions.put(positionData).position(0);
68 70
    mQuadTexture   = ByteBuffer.allocateDirect(TEX_DATA_SIZE     *dataLength*BYTES_PER_FLOAT).order(ByteOrder.nativeOrder()).asFloatBuffer();
69 71
    mQuadTexture.put(textureData).position(0);
72

  
73
    mBufferFBO = new DistortedFramebuffer(1,1);
70 74
    }
71 75

  
72 76
///////////////////////////////////////////////////////////////////////////////////////////////////
......
130 134
    float halfZ = halfInputW*mesh.zFactor;
131 135

  
132 136
    GLES20.glUseProgram(Distorted.mainProgramH);
137
    GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
133 138

  
134 139
    if( mP.mNumEffects==0 )
135 140
      {
136
      GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
137

  
138 141
      mM.send(df,halfInputW,halfInputH,halfZ);
139 142
      mV.send(halfInputW,halfInputH,halfZ);
140 143
      mF.send(halfInputW,halfInputH);
......
143 146
      }
144 147
    else
145 148
      {
146
      DistortedFramebuffer buffer = Distorted.getFBO(df.mWidth,df.mHeight);
147

  
148
      GLES20.glViewport(0, 0, buffer.mWidth, buffer.mHeight);
149
      buffer.setAsOutput();
149
      mBufferFBO.resizeFast(df.mWidth, df.mHeight);
150
      mBufferFBO.setAsOutput();
150 151

  
151
      mM.send(buffer,halfInputW,halfInputH,halfZ);
152
      mM.send(mBufferFBO,halfInputW,halfInputH,halfZ);
152 153
      mV.send(halfInputW,halfInputH,halfZ);
153 154
      mF.send(halfInputW,halfInputH);
154 155

  
......
156 157

  
157 158
      GLES20.glUseProgram(Distorted.postProgramH);
158 159
      GLES20.glViewport(0, 0, df.mWidth, df.mHeight);
159
      buffer.setAsInput();
160
      mBufferFBO.setAsInput();
160 161
      df.setAsOutput();
161 162
      mP.send(halfInputW,halfInputH);
162 163

  
src/main/java/org/distorted/library/DistortedFramebuffer.java
55 55
  private boolean mMarked;
56 56
  private boolean mDepthWanted;
57 57

  
58
  private int mTexWidth, mTexHeight;
59

  
58 60
  // Projection stuff
59 61
  private float mX, mY, mFOV;
60 62
  int mWidth,mHeight,mDepth;
......
74 76
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
75 77
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
76 78
      GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
77
      GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
79
      GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mTexWidth, mTexHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
78 80

  
79 81
      GLES20.glGenFramebuffers(1, fboIds, 0);
80 82
      GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
81 83
      GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, colorIds[0], 0);
82 84

  
83 85
      checkStatus("color");
84

  
85
      mList.add(this);
86 86
      }
87

  
88
    if(  mDepthWanted && depthIds[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
87
    if( mDepthWanted && depthIds[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
89 88
      {
90 89
      GLES20.glGenTextures(1, depthIds, 0);
91 90
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, depthIds[0]);
......
93 92
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
94 93
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
95 94
      GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
96
      GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES20.GL_DEPTH_COMPONENT, GLES20.GL_UNSIGNED_SHORT, null);
95
      GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_DEPTH_COMPONENT, mTexWidth, mTexHeight, 0, GLES20.GL_DEPTH_COMPONENT, GLES20.GL_UNSIGNED_SHORT, null);
97 96

  
98 97
      GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
99 98
      GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT, GLES20.GL_TEXTURE_2D, depthIds[0], 0);
......
265 264
      }
266 265
    }
267 266

  
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
// if new size fits into the size of the underlying Texture, just change the projection without
269
// reallocating the Texture. Otherwise, we need to reallocate.
270
//
271
// Must be called form a thread holding the OpenGL context.
272

  
273
  void resizeFast(int width, int height)
274
    {
275
    if( mWidth!=width || mHeight!=height )
276
      {
277
      mWidth = width;
278
      mHeight= height;
279

  
280
      createProjection();
281

  
282
      if( mWidth>mTexWidth || mHeight>mTexHeight )
283
        {
284
        mTexWidth = mWidth;
285
        mTexHeight= mHeight;
286
        deleteFBO();
287
        createFBO();
288
        }
289
      }
290
    }
291

  
268 292
///////////////////////////////////////////////////////////////////////////////////////////////////
269 293
// PUBLIC API
270 294
///////////////////////////////////////////////////////////////////////////////////////////////////
......
281 305

  
282 306
    mHeight     = height;
283 307
    mWidth      = width;
308
    mTexHeight  = height;
309
    mTexWidth   = width;
284 310
    mFOV        = 60.0f;
285 311
    mX          = 0.0f;
286 312
    mY          = 0.0f;
......
292 318
    depthIds[0]= NOT_CREATED_YET;
293 319

  
294 320
    createProjection();
321

  
322
    mList.add(this);
295 323
    }
296 324

  
297 325
///////////////////////////////////////////////////////////////////////////////////////////////////
......
345 373
    {
346 374
    tex.createTexture();
347 375
    tex.setAsInput();
376
    DistortedFramebuffer.deleteAllMarked();
377
    DistortedTexture.deleteAllMarked();
348 378
    createFBO();
349 379
    setAsOutput();
350 380
    effects.drawPriv(tex.mHalfX, tex.mHalfY, mesh, this, time);
351
    DistortedFramebuffer.deleteAllMarked();
352
    DistortedTexture.deleteAllMarked();
353 381
    }
354 382

  
355 383
///////////////////////////////////////////////////////////////////////////////////////////////////
......
371 399

  
372 400
    if( fbo.colorIds[0]>0 )    // fbo created with the first constructor
373 401
      {
402
      DistortedFramebuffer.deleteAllMarked();
403
      DistortedTexture.deleteAllMarked();
374 404
      createFBO();
375 405
      setAsOutput();
376 406
      GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, fbo.colorIds[0]);
377 407
      effects.drawPriv(fbo.mWidth/2, fbo.mHeight/2, mesh, this, time);
378
      DistortedFramebuffer.deleteAllMarked();
379
      DistortedTexture.deleteAllMarked();
380 408
      }
381 409
    }
382 410

  
......
391 419
 */
392 420
  public void renderTo(DistortedTree dt, long time)
393 421
    {
422
    DistortedFramebuffer.deleteAllMarked();
423
    DistortedTexture.deleteAllMarked();
394 424
    createFBO();
395 425
    setAsOutput();
396 426
    dt.drawRecursive(time,this);
397
    DistortedFramebuffer.deleteAllMarked();
398
    DistortedTexture.deleteAllMarked();
399 427
    }
400 428

  
401 429
///////////////////////////////////////////////////////////////////////////////////////////////////
......
445 473
    {
446 474
    if( mWidth!=width || mHeight!=height )
447 475
      {
448
      mWidth = width;
449
      mHeight= height;
476
      mWidth    = width;
477
      mHeight   = height;
478
      mTexWidth = width;
479
      mTexHeight= height;
450 480

  
451 481
      createProjection();
452 482

  

Also available in: Unified diff