Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 1942537e

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

    
64
  private void createProjection()
65
    {
66
    if( mWidth>0 && mHeight>1 )
67
      {
68
      if( mFOV>0.0f )  // perspective projection
69
        {
70
        float left   = (-mX-mWidth /2.0f)/mHeight;
71
        float right  = (-mX+mWidth /2.0f)/mHeight;
72
        float bottom = (-mY-mHeight/2.0f)/mHeight;
73
        float top    = (-mY+mHeight/2.0f)/mHeight;
74
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
75
        mDistance    = mHeight*near/(top-bottom);
76
        float far    = 2*mDistance-near;
77
        mDepth       = (int)((far-near)/2);
78

    
79
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
80
        }
81
      else             // parallel projection
82
        {
83
        float left   = -mX-mWidth /2.0f;
84
        float right  = -mX+mWidth /2.0f;
85
        float bottom = -mY-mHeight/2.0f;
86
        float top    = -mY+mHeight/2.0f;
87
        float near   = (mWidth+mHeight)/2;
88
        mDistance    = 2*near;
89
        float far    = 3*near;
90
        mDepth       = (int)near;
91

    
92
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
93
        }
94
      }
95
    }
96

    
97
///////////////////////////////////////////////////////////////////////////////////////////////////
98
// must be called form a thread holding OpenGL Context
99

    
100
  private boolean createFBO()
101
    {
102
    GLES20.glGenTextures(1, texIds, 0);
103
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
104
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
105
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
106
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
107
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
108
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
109

    
110
    GLES20.glGenFramebuffers(1, fboIds, 0);
111
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
112
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texIds[0], 0);
113

    
114
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
115

    
116
    if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
117
      {
118
      android.util.Log.e("DistortedFramebuffer", "failed to create framebuffer, error="+status);
119
      GLES20.glDeleteTextures(1, texIds, 0);
120
      GLES20.glDeleteFramebuffers(1, fboIds, 0);
121
      fboIds[0] = 0;
122
      texIds[0] = TEXTURE_FAILED_TO_CREATE;
123
      return false;
124
      }
125

    
126
    mList.add(this);
127
    //android.util.Log.e("FBO", "created ("+mWidth+","+mHeight+") "+fboIds[0]);
128

    
129
    return true;
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
// must be called from a thread holding OpenGL Context
134

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

    
141
      GLES20.glDeleteTextures(1, texIds, 0);
142
      GLES20.glDeleteFramebuffers(1, fboIds, 0);
143

    
144
      fboIds[0] = 0;
145
      texIds[0] = TEXTURE_NOT_CREATED_YET;
146
      }
147

    
148
    mMarked = false;
149
    }
150

    
151
///////////////////////////////////////////////////////////////////////////////////////////////////
152

    
153
  void reset()
154
    {
155
    if( texIds[0]!=TEXTURE_DONT_CREATE)
156
      texIds[0] = TEXTURE_NOT_CREATED_YET;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  static synchronized void release()
162
    {
163
    mListMarked = false;
164
    mList.clear();
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// must be called form a thread holding OpenGL Context
169

    
170
  static synchronized void deleteAllMarked()
171
    {
172
    if( mListMarked )
173
      {
174
      DistortedFramebuffer tmp;
175
      Iterator<DistortedFramebuffer> iterator = mList.iterator();
176

    
177
      while(iterator.hasNext())
178
        {
179
        tmp = iterator.next();
180

    
181
        if( tmp.mMarked )
182
          {
183
          tmp.deleteFBO();
184
          iterator.remove();
185
          }
186
        }
187

    
188
      mListMarked = false;
189
      }
190
    }
191

    
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// PUBLIC API
194
///////////////////////////////////////////////////////////////////////////////////////////////////
195
/**
196
 * Create a new offscreen Framebuffer.
197
 *
198
 * @param width Width of the COLOR attachment.
199
 * @param height Height of the COLOR attachment.
200
 */
201
  @SuppressWarnings("unused")
202
  public DistortedFramebuffer(int width, int height)
203
    {
204
    mProjectionMatrix = new float[16];
205

    
206
    mHeight  = height;
207
    mWidth   = width;
208
    fboIds[0]= -1;
209
    texIds[0]= TEXTURE_NOT_CREATED_YET;
210
    mFOV     = 60.0f;
211
    mX       = 0.0f;
212
    mY       = 0.0f;
213
    mMarked  = false;
214

    
215
    createProjection();
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219
/**
220
 * Create a new Framebuffer from an already created OpenGL Framebuffer.
221
 * <p>
222
 * Has to be followed by a 'resize()' to set the size.
223
 *
224
 * @param fbo the ID of a OpenGL Framebuffer object. Typically 0 (the screen)
225
 */
226
  public DistortedFramebuffer(int fbo)
227
    {
228
    mProjectionMatrix = new float[16];
229

    
230
    fboIds[0]= fbo;
231
    texIds[0]= TEXTURE_DONT_CREATE;
232
    mFOV     = 60.0f;
233
    mX       = 0.0f;
234
    mY       = 0.0f;
235
    mMarked  = false;
236
    }
237

    
238
///////////////////////////////////////////////////////////////////////////////////////////////////
239
/**
240
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
241
 */
242
  public void markForDeletion()
243
    {
244
    //android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+fboIds[0]);
245

    
246
    mListMarked = true;
247
    mMarked     = true;
248
    }
249

    
250
///////////////////////////////////////////////////////////////////////////////////////////////////
251
/**
252
 * Create new Projection matrix.
253
 *
254
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
255
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
256
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
257
 */
258
  public void setProjection(float fov, float x, float y)
259
    {
260
    mFOV = fov;
261
    mX   = x;
262
    mY   = y;
263

    
264
    createProjection();
265
    }
266

    
267
///////////////////////////////////////////////////////////////////////////////////////////////////
268
/**
269
 * Resize the underlying Framebuffer.
270
 *
271
 * As the Framebuffer is not created until the first render, typical usage of this API is actually
272
 * to set the size of an not-yet-created Framebuffer of an object that has been created with the
273
 * second constructor.
274
 * <p>
275
 * Fully creating an object, rendering to it, then resizing mid-render is also possible. Actual
276
 * resize takes place on the next render.
277
 *
278
 * @param width The new width.
279
 * @param height The new height.
280
 */
281
  public void resize(int width, int height)
282
    {
283
    if( mWidth!=width || mHeight!=height )
284
      {
285
      mWidth = width;
286
      mHeight= height;
287

    
288
      createProjection();
289

    
290
      if( texIds[0]>0 ) markForDeletion();
291
      }
292
    }
293

    
294
///////////////////////////////////////////////////////////////////////////////////////////////////
295
/**
296
 *  Set this as the Framebuffer to write to.
297
 */
298
  public void setAsOutput()
299
    {
300
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
301

    
302
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
303
    }
304

    
305
///////////////////////////////////////////////////////////////////////////////////////////////////
306
/**
307
 *  Set this as the Framebuffer to read from.
308
 */
309
  public void setAsInput()
310
    {
311
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
312

    
313
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
314
    }
315

    
316
  }
(3-3/15)