Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 227ac49a

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
// Watch out - this has the side-effect of binding a Texture and a Framebuffer!
62

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

    
75
      GLES30.glGenFramebuffers(1, mFBOH, 0);
76
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
77
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
78

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

    
91
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
92
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
93

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

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

    
113
      GLES30.glDeleteTextures(1, mColorH, 0);
114
      GLES30.glDeleteTextures(1, mDepthH, 0);
115
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
116
      mFBOH[0]   = 0;
117
      mColorH[0] = FAILED_TO_CREATE;
118
      mDepthH[0] = FAILED_TO_CREATE;
119

    
120
      return false;
121
      }
122

    
123
    return true;
124
    }
125

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

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

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

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

    
146
    mMarked = false;
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150

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

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

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

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

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

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

    
202
///////////////////////////////////////////////////////////////////////////////////////////////////
203

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

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

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

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

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

    
234
      mListMarked = false;
235
      }
236
    }
237

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

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

    
251
      createProjection();
252

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

    
261
    create();
262
    }
263

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

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

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

    
293
    createProjection();
294

    
295
    mList.add(this);
296
    }
297

    
298
///////////////////////////////////////////////////////////////////////////////////////////////////
299

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

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

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

    
325
    createProjection();
326

    
327
    mList.add(this);
328
    }
329

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

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

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

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

    
365
///////////////////////////////////////////////////////////////////////////////////////////////////
366

    
367
/**
368
 * Draw the (texture,mesh,effects) object to the Framebuffer.
369
 * <p>
370
 * Must be called from a thread holding OpenGL Context.
371
 *
372
 * @param ren input Renderable to use.
373
 * @param mesh Class descendant from MeshObject
374
 * @param effects The DistortedEffects to use when rendering
375
 * @param time Current time, in milliseconds.
376
 */
377
  public void renderTo(DistortedRenderable ren, MeshObject mesh, DistortedEffects effects, long time)
378
    {
379
    ren.create();  // Watch out  - this needs to be before
380
    create();      // the 'setAsInput' because this has side-effects!
381

    
382
    if( ren.setAsInput() )
383
      {
384
      DistortedFramebuffer.deleteAllMarked();
385
      DistortedTexture.deleteAllMarked();
386
      effects.drawPriv(ren.getWidth()/2.0f, ren.getHeight()/2.0f, mesh, this, time);
387
      }
388
    }
389

    
390
///////////////////////////////////////////////////////////////////////////////////////////////////
391
/**
392
 * Draws the Tree, and all its children, to the Framebuffer.
393
 * <p>
394
 * Must be called from a thread holding OpenGL Context.
395
 *
396
 * @param dt DistortedTree to render.
397
 * @param time Current time, in milliseconds. This will be passed to all the Effects stored in the Tree.
398
 */
399
  public void renderTo(DistortedTree dt, long time)
400
    {
401
    DistortedFramebuffer.deleteAllMarked();
402
    DistortedTexture.deleteAllMarked();
403
    create();
404
    dt.drawRecursive(time,this);
405
    }
406

    
407
///////////////////////////////////////////////////////////////////////////////////////////////////
408
/**
409
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
410
 */
411
  public void markForDeletion()
412
    {
413
    mListMarked = true;
414
    mMarked     = true;
415
    }
416

    
417
///////////////////////////////////////////////////////////////////////////////////////////////////
418
/**
419
 * Create new Projection matrix.
420
 *
421
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
422
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
423
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
424
 */
425
  public void setProjection(float fov, float x, float y)
426
    {
427
    mFOV = fov;
428
    mX   = x;
429
    mY   = y;
430

    
431
    createProjection();
432
    }
433

    
434
///////////////////////////////////////////////////////////////////////////////////////////////////
435
/**
436
 * Resize the underlying Framebuffer.
437
 *
438
 * As the Framebuffer is not created until the first render, typical usage of this API is actually
439
 * to set the size of an not-yet-created Framebuffer of an object that has been created with the
440
 * second constructor.
441
 * <p>
442
 * Fully creating an object, rendering to it, then resizing mid-render is also possible. Actual
443
 * resize takes place on the next render.
444
 *
445
 * @param width The new width.
446
 * @param height The new height.
447
 */
448
  public void resize(int width, int height)
449
    {
450
    if( mWidth!=width || mHeight!=height )
451
      {
452
      mWidth    = width;
453
      mHeight   = height;
454
      mSizeX = width;
455
      mSizeY = height;
456

    
457
      createProjection();
458

    
459
      if( mColorH[0]>0 ) markForDeletion();
460
      }
461
    }
462

    
463
///////////////////////////////////////////////////////////////////////////////////////////////////
464
/**
465
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
466
 * <p>
467
 * Catch: this will only work if the library has had time to actually create the texture. Remember
468
 * that the texture gets created only on first render, thus creating a Texture object and immediately
469
 * calling this method will return an invalid (negative) result.
470
 *
471
 * @return If there was not a single render between creation of the Object and calling this method on
472
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
473
 */
474
  public int getTextureID()
475
    {
476
    return mColorH[0];
477
    }
478

    
479
///////////////////////////////////////////////////////////////////////////////////////////////////
480
/**
481
 * Return true if the FBO contains a DEPTH attachment.
482
 *
483
 * @return <bold>true</bold> if the FBO contains a DEPTH attachment.
484
 */
485
  public boolean hasDepth()
486
    {
487
    return mDepthEnabled;
488
    }
489
  }
(3-3/17)