Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 09d4f4b1

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.GLES20;
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 (used by the DistortedObjectTree, but also can be used by external users of the library)
35
 * <p>
36
 * Also, 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
43
  {
44
  private static final int TEXTURE_FAILED_TO_CREATE = -1;
45
  private static final int TEXTURE_NOT_CREATED_YET  = -2;
46
  private static final int TEXTURE_DONT_CREATE      = -3;
47

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

    
51
  private float mX, mY, mFOV;
52

    
53
  private int[] texIds = new int[1];
54
  private int[] fboIds = new int[1];
55

    
56
  private boolean mMarked;
57

    
58
  int mWidth,mHeight,mDepth;
59
  float mDistance;
60
  float[] mProjectionMatrix;
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
// Must be called from a thread holding OpenGL Context
64

    
65
  private boolean createFBO()
66
    {
67
    GLES20.glGenTextures(1, texIds, 0);
68
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
69
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
70
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
71
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
72
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
73
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
74

    
75
    GLES20.glGenFramebuffers(1, fboIds, 0);
76
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
77
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texIds[0], 0);
78

    
79
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
80

    
81
    if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
82
      {
83
      android.util.Log.e("DistortedFramebuffer", "failed to create framebuffer, error="+status);
84
      GLES20.glDeleteTextures(1, texIds, 0);
85
      GLES20.glDeleteFramebuffers(1, fboIds, 0);
86
      fboIds[0] = 0;
87
      texIds[0] = TEXTURE_FAILED_TO_CREATE;
88
      return false;
89
      }
90

    
91
    mList.add(this);
92
    //android.util.Log.e("FBO", "created ("+mWidth+","+mHeight+") "+fboIds[0]);
93

    
94
    return true;
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// Must be called from a thread holding OpenGL Context
99

    
100
  private void deleteFBO()
101
    {
102
    if( texIds[0]>=0 )
103
      {
104
      //android.util.Log.e("FBO", "deleting ("+mWidth+","+mHeight+") "+fboIds[0]);
105

    
106
      GLES20.glDeleteTextures(1, texIds, 0);
107
      GLES20.glDeleteFramebuffers(1, fboIds, 0);
108

    
109
      fboIds[0] = 0;
110
      texIds[0] = TEXTURE_NOT_CREATED_YET;
111
      }
112

    
113
    mMarked = false;
114
    }
115

    
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
// Must be called from a thread holding OpenGL Context
118

    
119
  void setAsOutput()
120
    {
121
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
122

    
123
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
124
    }
125

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

    
129
  void setAsInput()
130
    {
131
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
132

    
133
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
134
    }
135

    
136
///////////////////////////////////////////////////////////////////////////////////////////////////
137

    
138
  void reset()
139
    {
140
    if( texIds[0]!=TEXTURE_DONT_CREATE)
141
      texIds[0] = TEXTURE_NOT_CREATED_YET;
142
    }
143

    
144
///////////////////////////////////////////////////////////////////////////////////////////////////
145

    
146
  private void createProjection()
147
    {
148
    if( mWidth>0 && mHeight>1 )
149
      {
150
      if( mFOV>0.0f )  // perspective projection
151
        {
152
        float left   = (-mX-mWidth /2.0f)/mHeight;
153
        float right  = (-mX+mWidth /2.0f)/mHeight;
154
        float bottom = (-mY-mHeight/2.0f)/mHeight;
155
        float top    = (-mY+mHeight/2.0f)/mHeight;
156
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
157
        mDistance    = mHeight*near/(top-bottom);
158
        float far    = 2*mDistance-near;
159
        mDepth       = (int)((far-near)/2);
160

    
161
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
162
        }
163
      else             // parallel projection
164
        {
165
        float left   = -mX-mWidth /2.0f;
166
        float right  = -mX+mWidth /2.0f;
167
        float bottom = -mY-mHeight/2.0f;
168
        float top    = -mY+mHeight/2.0f;
169
        float near   = (mWidth+mHeight)/2;
170
        mDistance    = 2*near;
171
        float far    = 3*near;
172
        mDepth       = (int)near;
173

    
174
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
175
        }
176
      }
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  static synchronized void release()
182
    {
183
    mListMarked = false;
184
    mList.clear();
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188
// must be called form a thread holding OpenGL Context
189

    
190
  static synchronized void deleteAllMarked()
191
    {
192
    if( mListMarked )
193
      {
194
      DistortedFramebuffer tmp;
195
      Iterator<DistortedFramebuffer> iterator = mList.iterator();
196

    
197
      while(iterator.hasNext())
198
        {
199
        tmp = iterator.next();
200

    
201
        if( tmp.mMarked )
202
          {
203
          tmp.deleteFBO();
204
          iterator.remove();
205
          }
206
        }
207

    
208
      mListMarked = false;
209
      }
210
    }
211

    
212
///////////////////////////////////////////////////////////////////////////////////////////////////
213
// PUBLIC API
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215
/**
216
 * Create a new offscreen Framebuffer.
217
 *
218
 * @param width Width of the COLOR attachment.
219
 * @param height Height of the COLOR attachment.
220
 */
221
  @SuppressWarnings("unused")
222
  public DistortedFramebuffer(int width, int height)
223
    {
224
    mProjectionMatrix = new float[16];
225

    
226
    mHeight  = height;
227
    mWidth   = width;
228
    fboIds[0]= -1;
229
    texIds[0]= TEXTURE_NOT_CREATED_YET;
230
    mFOV     = 60.0f;
231
    mX       = 0.0f;
232
    mY       = 0.0f;
233
    mMarked  = false;
234

    
235
    createProjection();
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Create a new Framebuffer from an already created OpenGL Framebuffer.
241
 * <p>
242
 * Has to be followed by a 'resize()' to set the size.
243
 *
244
 * @param fbo the ID of a OpenGL Framebuffer object. Typically 0 (the screen)
245
 */
246
  public DistortedFramebuffer(int fbo)
247
    {
248
    mProjectionMatrix = new float[16];
249

    
250
    fboIds[0]= fbo;
251
    texIds[0]= TEXTURE_DONT_CREATE;
252
    mFOV     = 60.0f;
253
    mX       = 0.0f;
254
    mY       = 0.0f;
255
    mMarked  = false;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259
/**
260
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
261
 */
262
  public void markForDeletion()
263
    {
264
    //android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+fboIds[0]);
265

    
266
    mListMarked = true;
267
    mMarked     = true;
268
    }
269

    
270
///////////////////////////////////////////////////////////////////////////////////////////////////
271
/**
272
 * Create new Projection matrix.
273
 *
274
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
275
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
276
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
277
 */
278
  public void setProjection(float fov, float x, float y)
279
    {
280
    mFOV = fov;
281
    mX   = x;
282
    mY   = y;
283

    
284
    createProjection();
285
    }
286

    
287
///////////////////////////////////////////////////////////////////////////////////////////////////
288
/**
289
 * Resize the underlying Framebuffer.
290
 *
291
 * As the Framebuffer is not created until the first render, typical usage of this API is actually
292
 * to set the size of an not-yet-created Framebuffer of an object that has been created with the
293
 * second constructor.
294
 * <p>
295
 * Fully creating an object, rendering to it, then resizing mid-render is also possible. Actual
296
 * resize takes place on the next render.
297
 *
298
 * @param width The new width.
299
 * @param height The new height.
300
 */
301
  public void resize(int width, int height)
302
    {
303
    if( mWidth!=width || mHeight!=height )
304
      {
305
      mWidth = width;
306
      mHeight= height;
307

    
308
      createProjection();
309

    
310
      if( texIds[0]>0 ) markForDeletion();
311
      }
312
    }
313

    
314
///////////////////////////////////////////////////////////////////////////////////////////////////
315
/**
316
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
317
 * <p>
318
 * Catch: this will only work if the library has had time to actually create the texture. Remember
319
 * that the texture gets created only on first render, thus creating a Texture object and immediately
320
 * calling this method will return an invalid (negative) result.
321
 *
322
 * @returns If there was not a single render between creation of the Object and calling this method on
323
 *          it, return a negative value. Otherwise, return ID of COLOR attachment 0.
324
 */
325
  public int getTextureID()
326
    {
327
    return texIds[0];
328
    }
329
  }
(3-3/15)