Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 46e25345

1 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 8e34674e Leszek Koltunski
import java.util.Iterator;
26
import java.util.LinkedList;
27
28 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
29 dedacd82 Leszek Koltunski
/**
30
 * Class which represents a OpenGL Framebuffer object.
31 71c3fecc Leszek Koltunski
 * <p>
32 65362dd4 Leszek Koltunski
 * 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 b455eb48 Leszek Koltunski
 * FBOs (used by the DistortedObjectTree, but also can be used by external users of the library)
35 71c3fecc Leszek Koltunski
 * <p>
36 8e34674e Leszek Koltunski
 * 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 65362dd4 Leszek Koltunski
 * framework where one is able to mark for deletion at any time and actual deletion takes place
40 8e34674e Leszek Koltunski
 * on the next render).
41 dedacd82 Leszek Koltunski
 */
42 ed13a5de Leszek Koltunski
public class DistortedFramebuffer
43 f6fb3c6d Leszek Koltunski
  {
44
  private static final int TEXTURE_FAILED_TO_CREATE = -1;
45
  private static final int TEXTURE_NOT_CREATED_YET  = -2;
46 b448e6b9 Leszek Koltunski
  private static final int TEXTURE_DONT_CREATE      = -3;
47 f6fb3c6d Leszek Koltunski
48 8e34674e Leszek Koltunski
  private static boolean mListMarked = false;
49
  private static LinkedList<DistortedFramebuffer> mList = new LinkedList<>();
50
51 f6fb3c6d Leszek Koltunski
  private float mX, mY;
52
  private float mFOV;
53
54 7304d89f Leszek Koltunski
  private int[] texIds = new int[1];
55
  private int[] fboIds = new int[1];
56
57 8e34674e Leszek Koltunski
  private boolean mMarked;
58
59 e6cf7d50 Leszek Koltunski
  int mWidth,mHeight,mDepth;
60
  float mDistance;
61 f6fb3c6d Leszek Koltunski
  float[] mProjectionMatrix;
62 bd3da5b2 Leszek Koltunski
63 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  private void createProjection()
66
    {
67 e6cf7d50 Leszek Koltunski
    if( mWidth>0 && mHeight>1 )
68 f6fb3c6d Leszek Koltunski
      {
69 98455aa2 Leszek Koltunski
      if( mFOV>0.0f )  // perspective projection
70 16d8b8f3 Leszek Koltunski
        {
71 e6cf7d50 Leszek Koltunski
        float left   = (-mX-mWidth /2.0f)/mHeight;
72
        float right  = (-mX+mWidth /2.0f)/mHeight;
73
        float bottom = (-mY-mHeight/2.0f)/mHeight;
74
        float top    = (-mY+mHeight/2.0f)/mHeight;
75
        float near   = (top-bottom) / (2.0f*(float)Math.tan(mFOV*Math.PI/360));
76
        mDistance    = mHeight*near/(top-bottom);
77 57578636 Leszek Koltunski
        float far    = 2*mDistance-near;
78
        mDepth       = (int)((far-near)/2);
79 98455aa2 Leszek Koltunski
80 16d8b8f3 Leszek Koltunski
        Matrix.frustumM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
81 98455aa2 Leszek Koltunski
        }
82
      else             // parallel projection
83
        {
84 e6cf7d50 Leszek Koltunski
        float left   = -mX-mWidth /2.0f;
85
        float right  = -mX+mWidth /2.0f;
86
        float bottom = -mY-mHeight/2.0f;
87
        float top    = -mY+mHeight/2.0f;
88 a5bbbdea Leszek Koltunski
        float near   = (mWidth+mHeight)/2;
89
        mDistance    = 2*near;
90
        float far    = 3*near;
91
        mDepth       = (int)near;
92 f6fb3c6d Leszek Koltunski
93 98455aa2 Leszek Koltunski
        Matrix.orthoM(mProjectionMatrix, 0, left, right, bottom, top, near, far);
94
        }
95 f6fb3c6d Leszek Koltunski
      }
96
    }
97
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99
// must be called form a thread holding OpenGL Context
100
101
  private boolean createFBO()
102
    {
103 64a642c1 Leszek Koltunski
    GLES20.glGenTextures(1, texIds, 0);
104 7304d89f Leszek Koltunski
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
105 f6fb3c6d Leszek Koltunski
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
106
    GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
107
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_REPEAT);
108
    GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_REPEAT);
109
    GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, mWidth, mHeight, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
110
111 64a642c1 Leszek Koltunski
    GLES20.glGenFramebuffers(1, fboIds, 0);
112
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
113 7304d89f Leszek Koltunski
    GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texIds[0], 0);
114 dedacd82 Leszek Koltunski
115 f6fb3c6d Leszek Koltunski
    int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
116
117
    if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
118
      {
119 64a642c1 Leszek Koltunski
      android.util.Log.e("DistortedFramebuffer", "failed to create framebuffer, error="+status);
120
      GLES20.glDeleteTextures(1, texIds, 0);
121
      GLES20.glDeleteFramebuffers(1, fboIds, 0);
122 7304d89f Leszek Koltunski
      fboIds[0] = 0;
123
      texIds[0] = TEXTURE_FAILED_TO_CREATE;
124 f6fb3c6d Leszek Koltunski
      return false;
125
      }
126
127 8e34674e Leszek Koltunski
    mList.add(this);
128 46e25345 Leszek Koltunski
    //android.util.Log.e("FBO", "created ("+mWidth+","+mHeight+") "+fboIds[0]);
129 16d8b8f3 Leszek Koltunski
130 f6fb3c6d Leszek Koltunski
    return true;
131
    }
132
133
///////////////////////////////////////////////////////////////////////////////////////////////////
134 e6cf7d50 Leszek Koltunski
// must be called from a thread holding OpenGL Context
135 f6fb3c6d Leszek Koltunski
136
  private void deleteFBO()
137
    {
138 e6cf7d50 Leszek Koltunski
    if( texIds[0]>=0 )
139
      {
140 46e25345 Leszek Koltunski
      //android.util.Log.e("FBO", "deleting ("+mWidth+","+mHeight+") "+fboIds[0]);
141 f6fb3c6d Leszek Koltunski
142 e6cf7d50 Leszek Koltunski
      GLES20.glDeleteTextures(1, texIds, 0);
143
      GLES20.glDeleteFramebuffers(1, fboIds, 0);
144 bd3da5b2 Leszek Koltunski
145 e6cf7d50 Leszek Koltunski
      fboIds[0] = 0;
146
      texIds[0] = TEXTURE_NOT_CREATED_YET;
147
      }
148 bd3da5b2 Leszek Koltunski
149
    mMarked = false;
150
    }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154
  void reset()
155
    {
156 7304d89f Leszek Koltunski
    texIds[0] = TEXTURE_NOT_CREATED_YET;
157 bd3da5b2 Leszek Koltunski
    }
158
159 8e34674e Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 e6cf7d50 Leszek Koltunski
          tmp.deleteFBO();
184 8e34674e Leszek Koltunski
          iterator.remove();
185
          }
186
        }
187
188
      mListMarked = false;
189
      }
190
    }
191 57578636 Leszek Koltunski
192 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
193
// PUBLIC API
194 dedacd82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
195
/**
196 57578636 Leszek Koltunski
 * Create a new offscreen Framebuffer.
197 dedacd82 Leszek Koltunski
 *
198
 * @param width Width of the COLOR attachment.
199
 * @param height Height of the COLOR attachment.
200
 */
201 ed13a5de Leszek Koltunski
  public DistortedFramebuffer(int width, int height)
202 f6fb3c6d Leszek Koltunski
    {
203
    mProjectionMatrix = new float[16];
204
205 57578636 Leszek Koltunski
    mHeight  = height;
206
    mWidth   = width;
207
    fboIds[0]= -1;
208
    texIds[0]= TEXTURE_NOT_CREATED_YET;
209
    mFOV     = 60.0f;
210
    mX       = 0.0f;
211
    mY       = 0.0f;
212
    mMarked  = false;
213 bd3da5b2 Leszek Koltunski
214 f6fb3c6d Leszek Koltunski
    createProjection();
215
    }
216
217 b448e6b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
218 dedacd82 Leszek Koltunski
/**
219 57578636 Leszek Koltunski
 * Create a new Framebuffer from an already created OpenGL Framebuffer.
220
 * <p>
221
 * Has to be followed by a 'resize()' to set the size.
222 dedacd82 Leszek Koltunski
 *
223
 * @param fbo the ID of a OpenGL Framebuffer object. Typically 0 (the screen)
224
 */
225 ed13a5de Leszek Koltunski
  public DistortedFramebuffer(int fbo)
226 b448e6b9 Leszek Koltunski
    {
227
    mProjectionMatrix = new float[16];
228
229 57578636 Leszek Koltunski
    fboIds[0]= fbo;
230
    texIds[0]= TEXTURE_DONT_CREATE;
231
    mFOV     = 60.0f;
232
    mX       = 0.0f;
233
    mY       = 0.0f;
234
    mMarked  = false;
235 b448e6b9 Leszek Koltunski
    }
236
237 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
238 dedacd82 Leszek Koltunski
/**
239
 * Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
240
 */
241 bd3da5b2 Leszek Koltunski
  public void markForDeletion()
242 f6fb3c6d Leszek Koltunski
    {
243 46e25345 Leszek Koltunski
    //android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+fboIds[0]);
244 16d8b8f3 Leszek Koltunski
245 8e34674e Leszek Koltunski
    mListMarked = true;
246
    mMarked     = true;
247 f6fb3c6d Leszek Koltunski
    }
248
249
///////////////////////////////////////////////////////////////////////////////////////////////////
250 dedacd82 Leszek Koltunski
/**
251
 * Create new Projection matrix.
252
 *
253
 * @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
254 46a52b78 Leszek Koltunski
 * @param x X-coordinate of the point at which our camera looks at. 0 is the center.
255
 * @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
256 dedacd82 Leszek Koltunski
 */
257
  public void setProjection(float fov, float x, float y)
258 f6fb3c6d Leszek Koltunski
    {
259 dedacd82 Leszek Koltunski
    mFOV = fov;
260 f6fb3c6d Leszek Koltunski
    mX   = x;
261
    mY   = y;
262
263
    createProjection();
264
    }
265
266
///////////////////////////////////////////////////////////////////////////////////////////////////
267 dedacd82 Leszek Koltunski
/**
268
 * Resize the underlying Framebuffer.
269
 *
270
 * As the Framebuffer is not created until the first render, typical usage of this API is actually
271
 * to set the size of an not-yet-created Framebuffer of an object that has been created with the
272
 * second constructor.
273 71c3fecc Leszek Koltunski
 * <p>
274 65362dd4 Leszek Koltunski
 * Fully creating an object, rendering to it, then resizing mid-render is also possible. Actual
275
 * resize takes place on the next render.
276 dedacd82 Leszek Koltunski
 *
277
 * @param width The new width.
278
 * @param height The new height.
279
 */
280 f6fb3c6d Leszek Koltunski
  public void resize(int width, int height)
281
    {
282 bd3da5b2 Leszek Koltunski
    if( mWidth!=width || mHeight!=height )
283
      {
284
      mWidth = width;
285
      mHeight= height;
286 f6fb3c6d Leszek Koltunski
287 bd3da5b2 Leszek Koltunski
      createProjection();
288 f6fb3c6d Leszek Koltunski
289 7304d89f Leszek Koltunski
      if( texIds[0]>0 ) markForDeletion();
290 bd3da5b2 Leszek Koltunski
      }
291 f6fb3c6d Leszek Koltunski
    }
292 d1e740c5 Leszek Koltunski
293
///////////////////////////////////////////////////////////////////////////////////////////////////
294
/**
295
 *  Set this as the Framebuffer to write to.
296
 */
297
  public void setAsOutput()
298
    {
299
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
300
301
    GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
302
    }
303
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305
/**
306
 *  Set this as the Framebuffer to read from.
307
 */
308
  public void setAsInput()
309
    {
310
    if( texIds[0]==TEXTURE_NOT_CREATED_YET ) createFBO();
311
312
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
313
    }
314
315 f6fb3c6d Leszek Koltunski
  }