Project

General

Profile

« Previous | Next » 

Revision 133cbb2b

Added by Leszek Koltunski about 7 years ago

Introduce DistortedRenderable, a base class for Texture and Framebuffer

View differences:

src/main/java/org/distorted/library/DistortedFramebuffer.java
39 39
 * framework where one is able to mark for deletion at any time and actual deletion takes place
40 40
 * on the next render).
41 41
 */
42
public class DistortedFramebuffer
42
public class DistortedFramebuffer extends DistortedRenderable
43 43
  {
44
  private static final int FAILED_TO_CREATE = -1;
45
  private static final int NOT_CREATED_YET  = -2;
46
  private static final int DONT_CREATE      = -3;
47

  
48 44
  private static boolean mListMarked = false;
49 45
  private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>();
50 46

  
51
  private int[] colorIds = new int[1];
52
  private int[] depthIds = new int[1];
53
  private int[] fboIds   = new int[1];
47
  private int[] mDepthH = new int[1];
48
  private int[] mFBOH   = new int[1];
54 49

  
55 50
  private boolean mMarked;
56 51
  private boolean mDepthEnabled;
57 52

  
58
  private int mTexWidth, mTexHeight;
59

  
60 53
  // Projection stuff
61 54
  private float mX, mY, mFOV;
62 55
  int mWidth,mHeight,mDepth;
......
66 59
///////////////////////////////////////////////////////////////////////////////////////////////////
67 60
// Must be called from a thread holding OpenGL Context
68 61

  
69
  void createFBO()
62
  void create()
70 63
    {
71
    if( colorIds[0]==NOT_CREATED_YET )
64
    if( mColorH[0]==NOT_CREATED_YET )
72 65
      {
73
      GLES30.glGenTextures(1, colorIds, 0);
74
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, colorIds[0]);
66
      GLES30.glGenTextures(1, mColorH, 0);
67
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
75 68
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
76 69
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
77 70
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
78 71
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
79
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mTexWidth, mTexHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
72
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
80 73

  
81
      GLES30.glGenFramebuffers(1, fboIds, 0);
82
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, fboIds[0]);
83
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, colorIds[0], 0);
74
      GLES30.glGenFramebuffers(1, mFBOH, 0);
75
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
76
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
84 77

  
85 78
      checkStatus("color");
86 79
      }
87
    if( mDepthEnabled && depthIds[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
80
    if( mDepthEnabled && mDepthH[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
88 81
      {
89
      GLES30.glGenTextures(1, depthIds, 0);
90
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, depthIds[0]);
82
      GLES30.glGenTextures(1, mDepthH, 0);
83
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
91 84
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
92 85
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
93 86
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
94 87
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
95
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mTexWidth, mTexHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
88
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
96 89

  
97
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, fboIds[0]);
98
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, depthIds[0], 0);
90
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
91
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
99 92

  
100 93
      checkStatus("depth");
101 94
      }
102
    if( !mDepthEnabled && depthIds[0]!=NOT_CREATED_YET ) // we need to detach and destroy the DEPTH attachment.
95
    if( !mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET ) // we need to detach and destroy the DEPTH attachment.
103 96
      {
104
      GLES30.glDeleteTextures(1, depthIds, 0);
105
      depthIds[0]=NOT_CREATED_YET;
97
      GLES30.glDeleteTextures(1, mDepthH, 0);
98
      mDepthH[0]=NOT_CREATED_YET;
106 99
      }
107 100
    }
108 101

  
......
116 109
      {
117 110
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
118 111

  
119
      GLES30.glDeleteTextures(1, colorIds, 0);
120
      GLES30.glDeleteTextures(1, depthIds, 0);
121
      GLES30.glDeleteFramebuffers(1, fboIds, 0);
122
      fboIds[0]   = 0;
123
      colorIds[0] = FAILED_TO_CREATE;
124
      depthIds[0] = FAILED_TO_CREATE;
112
      GLES30.glDeleteTextures(1, mColorH, 0);
113
      GLES30.glDeleteTextures(1, mDepthH, 0);
114
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
115
      mFBOH[0]   = 0;
116
      mColorH[0] = FAILED_TO_CREATE;
117
      mDepthH[0] = FAILED_TO_CREATE;
125 118

  
126 119
      return false;
127 120
      }
......
132 125
///////////////////////////////////////////////////////////////////////////////////////////////////
133 126
// Must be called from a thread holding OpenGL Context
134 127

  
135
  private void deleteFBO()
128
  private void delete()
136 129
    {
137
    if( colorIds[0]>=0 )
130
    if( mColorH[0]>=0 )
138 131
      {
139
      //android.util.Log.e("FBO", "deleting ("+mWidth+","+mHeight+") "+fboIds[0]);
140

  
141
      if( depthIds[0]>=0 )
132
      if( mDepthH[0]>=0 )
142 133
        {
143
        GLES30.glDeleteTextures(1, depthIds, 0);
144
        depthIds[0]=NOT_CREATED_YET;
134
        GLES30.glDeleteTextures(1, mDepthH, 0);
135
        mDepthH[0]=NOT_CREATED_YET;
145 136
        }
146 137

  
147
      GLES30.glDeleteTextures(1, colorIds, 0);
148
      colorIds[0] = NOT_CREATED_YET;
138
      GLES30.glDeleteTextures(1, mColorH, 0);
139
      mColorH[0] = NOT_CREATED_YET;
149 140

  
150
      GLES30.glDeleteFramebuffers(1, fboIds, 0);
151
      fboIds[0] = 0;
141
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
142
      mFBOH[0] = 0;
152 143
      }
153 144

  
154 145
    mMarked = false;
......
158 149

  
159 150
  void setAsOutput()
160 151
    {
161
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, fboIds[0]);
152
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
162 153

  
163
    if( depthIds[0]!=NOT_CREATED_YET )
154
    if( mDepthH[0]!=NOT_CREATED_YET )
164 155
      {
165 156
      GLES30.glEnable(GLES30.GL_DEPTH_TEST);
166 157
      GLES30.glDepthMask(true);
......
172 163
      }
173 164
    }
174 165

  
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

  
177
  void setAsInput()
178
    {
179
    GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, colorIds[0]);
180
    }
181

  
182 166
///////////////////////////////////////////////////////////////////////////////////////////////////
183 167

  
184 168
  private void createProjection()
......
220 204
    {
221 205
    for( DistortedFramebuffer fbo : mList)
222 206
      {
223
      if( fbo.colorIds[0]!=DONT_CREATE ) fbo.colorIds[0] = NOT_CREATED_YET;
224
      if( fbo.mDepthEnabled) fbo.depthIds[0] = NOT_CREATED_YET;
207
      if( fbo.mColorH[0]!=DONT_CREATE ) fbo.mColorH[0] = NOT_CREATED_YET;
208
      if( fbo.mDepthEnabled           ) fbo.mDepthH[0] = NOT_CREATED_YET;
225 209
      }
226 210
    }
227 211

  
......
241 225

  
242 226
        if( tmp.mMarked )
243 227
          {
244
          tmp.deleteFBO();
228
          tmp.delete();
245 229
          iterator.remove();
246 230
          }
247 231
        }
......
265 249

  
266 250
      createProjection();
267 251

  
268
      if( mWidth>mTexWidth || mHeight>mTexHeight )
252
      if( mWidth> mSizeX || mHeight> mSizeY)
269 253
        {
270
        mTexWidth = mWidth;
271
        mTexHeight= mHeight;
272
        deleteFBO();
254
        mSizeX = mWidth;
255
        mSizeY = mHeight;
256
        delete();
273 257
        }
274 258
      }
275 259

  
276
    createFBO();
260
    create();
277 261
    }
278 262

  
279 263
///////////////////////////////////////////////////////////////////////////////////////////////////
......
293 277

  
294 278
    mHeight      = height;
295 279
    mWidth       = width;
296
    mTexHeight   = height;
297
    mTexWidth    = width;
280
    mSizeY       = height;
281
    mSizeX       = width;
298 282
    mFOV         = 60.0f;
299 283
    mX           = 0.0f;
300 284
    mY           = 0.0f;
301 285
    mMarked      = false;
302 286
    mDepthEnabled= depthEnabled;
303 287

  
304
    fboIds[0]  =-1;
305
    colorIds[0]= NOT_CREATED_YET;
306
    depthIds[0]= NOT_CREATED_YET;
288
    mFBOH[0]  =-1;
289
    mColorH[0]= NOT_CREATED_YET;
290
    mDepthH[0]= NOT_CREATED_YET;
307 291

  
308 292
    createProjection();
309 293

  
310 294
    mList.add(this);
311 295
    }
312 296

  
297
///////////////////////////////////////////////////////////////////////////////////////////////////
298

  
313 299
/**
314 300
 * Create a new offscreen Framebuffer.
315 301
 *
......
323 309

  
324 310
    mHeight      = height;
325 311
    mWidth       = width;
326
    mTexHeight   = height;
327
    mTexWidth    = width;
312
    mSizeY       = height;
313
    mSizeX       = width;
328 314
    mFOV         = 60.0f;
329 315
    mX           = 0.0f;
330 316
    mY           = 0.0f;
331 317
    mMarked      = false;
332 318
    mDepthEnabled= false;
333 319

  
334
    fboIds[0]  =-1;
335
    colorIds[0]= NOT_CREATED_YET;
336
    depthIds[0]= NOT_CREATED_YET;
320
    mFBOH[0]  =-1;
321
    mColorH[0]= NOT_CREATED_YET;
322
    mDepthH[0]= NOT_CREATED_YET;
337 323

  
338 324
    createProjection();
339 325

  
......
358 344
    mMarked      = false;
359 345
    mDepthEnabled= true;
360 346

  
361
    fboIds[0]  = fbo;
362
    colorIds[0]= DONT_CREATE;
363
    depthIds[0]= DONT_CREATE;
347
    mFBOH[0]  = fbo;
348
    mColorH[0]= DONT_CREATE;
349
    mDepthH[0]= DONT_CREATE;
364 350
    }
365 351

  
366 352
///////////////////////////////////////////////////////////////////////////////////////////////////
......
389 375
 */
390 376
  public void renderTo(DistortedTexture tex, MeshObject mesh, DistortedEffects effects, long time)
391 377
    {
392
    tex.createTexture();
393
    DistortedFramebuffer.deleteAllMarked();
394
    DistortedTexture.deleteAllMarked();
395
    createFBO();
396
    tex.setAsInput();
397
    effects.drawPriv(tex.mHalfX, tex.mHalfY, mesh, this, time);
378
    tex.create();
379

  
380
    if( tex.setAsInput() )
381
      {
382
      DistortedFramebuffer.deleteAllMarked();
383
      DistortedTexture.deleteAllMarked();
384
      create();
385
      effects.drawPriv(tex.getWidth()/2.0f, tex.getHeight()/2.0f, mesh, this, time);
386
      }
398 387
    }
399 388

  
400 389
///////////////////////////////////////////////////////////////////////////////////////////////////
......
412 401
 */
413 402
  public void renderTo(DistortedFramebuffer fbo, MeshObject mesh, DistortedEffects effects, long time)
414 403
    {
415
    fbo.createFBO();
404
    fbo.create();
416 405

  
417
    if( fbo.colorIds[0]>0 )    // fbo created with the first constructor
406
    if( fbo.setAsInput() )
418 407
      {
419 408
      DistortedFramebuffer.deleteAllMarked();
420 409
      DistortedTexture.deleteAllMarked();
421
      createFBO();
422
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, fbo.colorIds[0]);
423
      effects.drawPriv(fbo.mWidth/2, fbo.mHeight/2, mesh, this, time);
410
      create();
411
      effects.drawPriv(fbo.getWidth()/2.0f, fbo.getHeight()/2.0f, mesh, this, time);
424 412
      }
425 413
    }
426 414

  
......
437 425
    {
438 426
    DistortedFramebuffer.deleteAllMarked();
439 427
    DistortedTexture.deleteAllMarked();
440
    createFBO();
428
    create();
441 429
    dt.drawRecursive(time,this);
442 430
    }
443 431

  
......
447 435
 */
448 436
  public void markForDeletion()
449 437
    {
450
    //android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+fboIds[0]);
438
    //android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+mFBOH[0]);
451 439

  
452 440
    mListMarked = true;
453 441
    mMarked     = true;
......
490 478
      {
491 479
      mWidth    = width;
492 480
      mHeight   = height;
493
      mTexWidth = width;
494
      mTexHeight= height;
481
      mSizeX = width;
482
      mSizeY = height;
495 483

  
496 484
      createProjection();
497 485

  
498
      if( colorIds[0]>0 ) markForDeletion();
486
      if( mColorH[0]>0 ) markForDeletion();
499 487
      }
500 488
    }
501 489

  
......
512 500
 */
513 501
  public int getTextureID()
514 502
    {
515
    return colorIds[0];
503
    return mColorH[0];
516 504
    }
517 505

  
518 506
///////////////////////////////////////////////////////////////////////////////////////////////////

Also available in: Unified diff