Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 133cbb2b

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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;
21

    
22
import android.opengl.GLES30;
23
import android.opengl.Matrix;
24

    
25
import java.util.Iterator;
26
import java.util.LinkedList;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Class which represents a OpenGL Framebuffer object.
31
 * <p>
32
 * User is able to create either Framebuffers from objects already constructed outside
33
 * of the library (the first constructor; primary use case: the screen) or an offscreen
34
 * FBOs.
35
 * <p>
36
 * Keep all objects created in a static LinkedList. The point: we need to be able to mark
37
 * Framebuffers for deletion, and delete all marked objects later at a convenient time (that's
38
 * because we can only delete from a thread that holds the OpenGL context so here we provide a
39
 * framework where one is able to mark for deletion at any time and actual deletion takes place
40
 * on the next render).
41
 */
42
public class DistortedFramebuffer extends DistortedRenderable
43
  {
44
  private static boolean mListMarked = false;
45
  private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>();
46

    
47
  private int[] mDepthH = new int[1];
48
  private int[] mFBOH   = new int[1];
49

    
50
  private boolean mMarked;
51
  private boolean mDepthEnabled;
52

    
53
  // Projection stuff
54
  private float mX, mY, mFOV;
55
  int mWidth,mHeight,mDepth;
56
  float mDistance;
57
  float[] mProjectionMatrix;
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60
// Must be called from a thread holding OpenGL Context
61

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

    
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);
77

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

    
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);
92

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

    
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103

    
104
  private boolean checkStatus(String message)
105
    {
106
    int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
107

    
108
    if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
109
      {
110
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
111

    
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;
118

    
119
      return false;
120
      }
121

    
122
    return true;
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126
// Must be called from a thread holding OpenGL Context
127

    
128
  private void delete()
129
    {
130
    if( mColorH[0]>=0 )
131
      {
132
      if( mDepthH[0]>=0 )
133
        {
134
        GLES30.glDeleteTextures(1, mDepthH, 0);
135
        mDepthH[0]=NOT_CREATED_YET;
136
        }
137

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

    
141
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
142
      mFBOH[0] = 0;
143
      }
144

    
145
    mMarked = false;
146
    }
147

    
148
///////////////////////////////////////////////////////////////////////////////////////////////////
149

    
150
  void setAsOutput()
151
    {
152
    GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
153

    
154
    if( mDepthH[0]!=NOT_CREATED_YET )
155
      {
156
      GLES30.glEnable(GLES30.GL_DEPTH_TEST);
157
      GLES30.glDepthMask(true);
158
      }
159
    else
160
      {
161
      GLES30.glDisable(GLES30.GL_DEPTH_TEST);
162
      GLES30.glDepthMask(false);
163
      }
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  private void createProjection()
169
    {
170
    if( mWidth>0 && mHeight>1 )
171
      {
172
      if( mFOV>0.0f )  // perspective projection
173
        {
174
        float left   = (-mX-mWidth /2.0f)/mHeight;
175
        float right  = (-mX+mWidth /2.0f)/mHeight;
176
        float bottom = (-mY-mHeight/2.0f)/mHeight;
177
        float top    = (-mY+mHeight/2.0f)/mHeight;
178
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
179
        mDistance    = mHeight*near/(top-bottom);
180
        float far    = 2*mDistance-near;
181
        mDepth       = (int)((far-near)/2);
182

    
183
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
184
        }
185
      else             // parallel projection
186
        {
187
        float left   = -mX-mWidth /2.0f;
188
        float right  = -mX+mWidth /2.0f;
189
        float bottom = -mY-mHeight/2.0f;
190
        float top    = -mY+mHeight/2.0f;
191
        float near   = (mWidth+mHeight)/2;
192
        mDistance    = 2*near;
193
        float far    = 3*near;
194
        mDepth       = (int)near;
195

    
196
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
197
        }
198
      }
199
    }
200

    
201
///////////////////////////////////////////////////////////////////////////////////////////////////
202

    
203
  static synchronized void onDestroy()
204
    {
205
    for( DistortedFramebuffer fbo : mList)
206
      {
207
      if( fbo.mColorH[0]!=DONT_CREATE ) fbo.mColorH[0] = NOT_CREATED_YET;
208
      if( fbo.mDepthEnabled           ) fbo.mDepthH[0] = NOT_CREATED_YET;
209
      }
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
// must be called form a thread holding OpenGL Context
214

    
215
  private static synchronized void deleteAllMarked()
216
    {
217
    if( mListMarked )
218
      {
219
      DistortedFramebuffer tmp;
220
      Iterator<DistortedFramebuffer> iterator = mList.iterator();
221

    
222
      while(iterator.hasNext())
223
        {
224
        tmp = iterator.next();
225

    
226
        if( tmp.mMarked )
227
          {
228
          tmp.delete();
229
          iterator.remove();
230
          }
231
        }
232

    
233
      mListMarked = false;
234
      }
235
    }
236

    
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
// if new size fits into the size of the underlying Texture, just change the projection without
239
// reallocating the Texture. Otherwise, we need to reallocate.
240
//
241
// Must be called form a thread holding the OpenGL context.
242

    
243
  void resizeFast(int width, int height)
244
    {
245
    if( mWidth!=width || mHeight!=height )
246
      {
247
      mWidth = width;
248
      mHeight= height;
249

    
250
      createProjection();
251

    
252
      if( mWidth> mSizeX || mHeight> mSizeY)
253
        {
254
        mSizeX = mWidth;
255
        mSizeY = mHeight;
256
        delete();
257
        }
258
      }
259

    
260
    create();
261
    }
262

    
263
///////////////////////////////////////////////////////////////////////////////////////////////////
264
// PUBLIC API
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266
/**
267
 * Create a new offscreen Framebuffer.
268
 *
269
 * @param width Width of the COLOR attachment.
270
 * @param height Height of the COLOR attachment.
271
 * @param depthEnabled Add DEPTH attachment?
272
 */
273
  @SuppressWarnings("unused")
274
  public DistortedFramebuffer(int width, int height, boolean depthEnabled)
275
    {
276
    mProjectionMatrix = new float[16];
277

    
278
    mHeight      = height;
279
    mWidth       = width;
280
    mSizeY       = height;
281
    mSizeX       = width;
282
    mFOV         = 60.0f;
283
    mX           = 0.0f;
284
    mY           = 0.0f;
285
    mMarked      = false;
286
    mDepthEnabled= depthEnabled;
287

    
288
    mFBOH[0]  =-1;
289
    mColorH[0]= NOT_CREATED_YET;
290
    mDepthH[0]= NOT_CREATED_YET;
291

    
292
    createProjection();
293

    
294
    mList.add(this);
295
    }
296

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

    
299
/**
300
 * Create a new offscreen Framebuffer.
301
 *
302
 * @param width Width of the COLOR attachment.
303
 * @param height Height of the COLOR attachment.
304
 */
305
  @SuppressWarnings("unused")
306
  public DistortedFramebuffer(int width, int height)
307
    {
308
    mProjectionMatrix = new float[16];
309

    
310
    mHeight      = height;
311
    mWidth       = width;
312
    mSizeY       = height;
313
    mSizeX       = width;
314
    mFOV         = 60.0f;
315
    mX           = 0.0f;
316
    mY           = 0.0f;
317
    mMarked      = false;
318
    mDepthEnabled= false;
319

    
320
    mFBOH[0]  =-1;
321
    mColorH[0]= NOT_CREATED_YET;
322
    mDepthH[0]= NOT_CREATED_YET;
323

    
324
    createProjection();
325

    
326
    mList.add(this);
327
    }
328

    
329
///////////////////////////////////////////////////////////////////////////////////////////////////
330
/**
331
 * Create a new Framebuffer from an already created OpenGL Framebuffer.
332
 * <p>
333
 * Has to be followed by a 'resize()' to set the size.
334
 *
335
 * @param fbo the ID of a OpenGL Framebuffer object. Typically 0 (the screen)
336
 */
337
  public DistortedFramebuffer(int fbo)
338
    {
339
    mProjectionMatrix = new float[16];
340

    
341
    mFOV         = 60.0f;
342
    mX           = 0.0f;
343
    mY           = 0.0f;
344
    mMarked      = false;
345
    mDepthEnabled= true;
346

    
347
    mFBOH[0]  = fbo;
348
    mColorH[0]= DONT_CREATE;
349
    mDepthH[0]= DONT_CREATE;
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353
/**
354
 * Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTh attachment and destroy it.
355
 *
356
 * @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
357
 *               <bold>false</bold> if we want to detach the DEPTH attachment.
358
 */
359
  public void enableDepthAttachment(boolean enable)
360
    {
361
    mDepthEnabled = enable;
362
    }
363

    
364
///////////////////////////////////////////////////////////////////////////////////////////////////
365

    
366
/**
367
 * Draw the (texture,mesh,effects) object to the Framebuffer.
368
 * <p>
369
 * Must be called from a thread holding OpenGL Context.
370
 *
371
 * @param tex input Texture to use.
372
 * @param mesh Class descendant from MeshObject
373
 * @param effects The DistortedEffects to use when rendering
374
 * @param time Current time, in milliseconds.
375
 */
376
  public void renderTo(DistortedTexture tex, MeshObject mesh, DistortedEffects effects, long time)
377
    {
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
      }
387
    }
388

    
389
///////////////////////////////////////////////////////////////////////////////////////////////////
390
/**
391
 * Draw the (framebuffer,mesh,effects) object to the Framebuffer.
392
 * <p>
393
 * Must be called from a thread holding OpenGL Context.
394
 *
395
 * @param fbo The Framebuffer (previously created with the first constructor, drawing FROM the screen
396
 *            is unsupported!) whose COLOR attachment 0 will be used as input texture.
397
 *            Please note that rendering from an FBO to itself is unsupported by OpenGL!
398
 * @param mesh Class descendant from MeshObject
399
 * @param effects The DistortedEffects to use when rendering
400
 * @param time Current time, in milliseconds.
401
 */
402
  public void renderTo(DistortedFramebuffer fbo, MeshObject mesh, DistortedEffects effects, long time)
403
    {
404
    fbo.create();
405

    
406
    if( fbo.setAsInput() )
407
      {
408
      DistortedFramebuffer.deleteAllMarked();
409
      DistortedTexture.deleteAllMarked();
410
      create();
411
      effects.drawPriv(fbo.getWidth()/2.0f, fbo.getHeight()/2.0f, mesh, this, time);
412
      }
413
    }
414

    
415
///////////////////////////////////////////////////////////////////////////////////////////////////
416
/**
417
 * Draws the Tree, and all its children, to the Framebuffer.
418
 * <p>
419
 * Must be called from a thread holding OpenGL Context.
420
 *
421
 * @param dt DistortedTree to render.
422
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the Tree.
423
 */
424
  public void renderTo(DistortedTree dt, long time)
425
    {
426
    DistortedFramebuffer.deleteAllMarked();
427
    DistortedTexture.deleteAllMarked();
428
    create();
429
    dt.drawRecursive(time,this);
430
    }
431

    
432
///////////////////////////////////////////////////////////////////////////////////////////////////
433
/**
434
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
435
 */
436
  public void markForDeletion()
437
    {
438
    //android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+mFBOH[0]);
439

    
440
    mListMarked = true;
441
    mMarked     = true;
442
    }
443

    
444
///////////////////////////////////////////////////////////////////////////////////////////////////
445
/**
446
 * Create new Projection matrix.
447
 *
448
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
449
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
450
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
451
 */
452
  public void setProjection(float fov, float x, float y)
453
    {
454
    mFOV = fov;
455
    mX   = x;
456
    mY   = y;
457

    
458
    createProjection();
459
    }
460

    
461
///////////////////////////////////////////////////////////////////////////////////////////////////
462
/**
463
 * Resize the underlying Framebuffer.
464
 *
465
 * As the Framebuffer is not created until the first render, typical usage of this API is actually
466
 * to set the size of an not-yet-created Framebuffer of an object that has been created with the
467
 * second constructor.
468
 * <p>
469
 * Fully creating an object, rendering to it, then resizing mid-render is also possible. Actual
470
 * resize takes place on the next render.
471
 *
472
 * @param width The new width.
473
 * @param height The new height.
474
 */
475
  public void resize(int width, int height)
476
    {
477
    if( mWidth!=width || mHeight!=height )
478
      {
479
      mWidth    = width;
480
      mHeight   = height;
481
      mSizeX = width;
482
      mSizeY = height;
483

    
484
      createProjection();
485

    
486
      if( mColorH[0]>0 ) markForDeletion();
487
      }
488
    }
489

    
490
///////////////////////////////////////////////////////////////////////////////////////////////////
491
/**
492
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
493
 * <p>
494
 * Catch: this will only work if the library has had time to actually create the texture. Remember
495
 * that the texture gets created only on first render, thus creating a Texture object and immediately
496
 * calling this method will return an invalid (negative) result.
497
 *
498
 * @return If there was not a single render between creation of the Object and calling this method on
499
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
500
 */
501
  public int getTextureID()
502
    {
503
    return mColorH[0];
504
    }
505

    
506
///////////////////////////////////////////////////////////////////////////////////////////////////
507
/**
508
 * Return true if the FBO contains a DEPTH attachment.
509
 *
510
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
511
 */
512
  public boolean hasDepth()
513
    {
514
    return mDepthEnabled;
515
    }
516
  }
(3-3/17)