Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ b9798977

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 194ab46f Leszek Koltunski
import android.opengl.GLES30;
23 f6fb3c6d Leszek Koltunski
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25 dedacd82 Leszek Koltunski
/**
26
 * Class which represents a OpenGL Framebuffer object.
27 71c3fecc Leszek Koltunski
 * <p>
28 c5369f1b leszek
 * User is able to create offscreen FBOs and both a) render to them b) use their COLOR0 attachment as
29 2ed1c692 leszek
 * an input texture. Attaching Depths and/or Stencils is also possible.
30 dedacd82 Leszek Koltunski
 */
31 af4cc5db Leszek Koltunski
public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface
32 f6fb3c6d Leszek Koltunski
  {
33 bd3da5b2 Leszek Koltunski
34 b9798977 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
35
36
  DistortedFramebuffer getBuffer()
37
    {
38
    return this;
39
    }
40
41 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
42 6537ba91 Leszek Koltunski
// Must be called from a thread holding OpenGL Context
43 227ac49a Leszek Koltunski
// Watch out - this has the side-effect of binding a Texture and a Framebuffer!
44 f6fb3c6d Leszek Koltunski
45 f8377ef8 leszek
  void create()
46 f6fb3c6d Leszek Koltunski
    {
47 c7da4e65 leszek
    if( mColorCreated==NOT_CREATED_YET )
48 7cf783cb Leszek Koltunski
      {
49 133cbb2b Leszek Koltunski
      GLES30.glGenTextures(1, mColorH, 0);
50
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
51 194ab46f Leszek Koltunski
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
52
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
53
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
54
      GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
55 133cbb2b Leszek Koltunski
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
56 194ab46f Leszek Koltunski
57 133cbb2b Leszek Koltunski
      GLES30.glGenFramebuffers(1, mFBOH, 0);
58
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
59
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
60 dedacd82 Leszek Koltunski
61 c7da4e65 leszek
      mColorCreated = checkStatus("color");
62 7cf783cb Leszek Koltunski
      }
63 c7da4e65 leszek
    if( mDepthCreated==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
64 8c327653 Leszek Koltunski
      {
65 133cbb2b Leszek Koltunski
      GLES30.glGenTextures(1, mDepthH, 0);
66
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
67 194ab46f Leszek Koltunski
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
68
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
69
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
70
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
71 133cbb2b Leszek Koltunski
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
72 8c327653 Leszek Koltunski
73 133cbb2b Leszek Koltunski
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
74
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
75 8c327653 Leszek Koltunski
76 c7da4e65 leszek
      mDepthCreated = checkStatus("depth");
77 8c327653 Leszek Koltunski
      }
78 c7da4e65 leszek
    if( mDepthCreated==DONT_CREATE && mDepthH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
79 8c327653 Leszek Koltunski
      {
80 133cbb2b Leszek Koltunski
      GLES30.glDeleteTextures(1, mDepthH, 0);
81 c7da4e65 leszek
      mDepthH[0]=0;
82 8c327653 Leszek Koltunski
      }
83 8337fa41 Leszek Koltunski
    }
84
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86
87 c7da4e65 leszek
  private int checkStatus(String message)
88 8337fa41 Leszek Koltunski
    {
89 194ab46f Leszek Koltunski
    int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
90 8337fa41 Leszek Koltunski
91 194ab46f Leszek Koltunski
    if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
92 8337fa41 Leszek Koltunski
      {
93
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
94
95 133cbb2b Leszek Koltunski
      GLES30.glDeleteTextures(1, mColorH, 0);
96
      GLES30.glDeleteTextures(1, mDepthH, 0);
97
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
98 c7da4e65 leszek
      mFBOH[0]= 0;
99 8337fa41 Leszek Koltunski
100 c7da4e65 leszek
      return FAILED_TO_CREATE;
101 8337fa41 Leszek Koltunski
      }
102 8c327653 Leszek Koltunski
103 c7da4e65 leszek
    return CREATED;
104 f6fb3c6d Leszek Koltunski
    }
105
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107 6537ba91 Leszek Koltunski
// Must be called from a thread holding OpenGL Context
108 f6fb3c6d Leszek Koltunski
109 227ac49a Leszek Koltunski
  void delete()
110 f6fb3c6d Leszek Koltunski
    {
111 c7da4e65 leszek
    if( mColorH[0]>0 )
112 e6cf7d50 Leszek Koltunski
      {
113 c7da4e65 leszek
      if( mDepthH[0]>0 )
114 8c327653 Leszek Koltunski
        {
115 133cbb2b Leszek Koltunski
        GLES30.glDeleteTextures(1, mDepthH, 0);
116 c7da4e65 leszek
        mDepthH[0]=0;
117
        mDepthCreated = NOT_CREATED_YET;
118 8c327653 Leszek Koltunski
        }
119
120 133cbb2b Leszek Koltunski
      GLES30.glDeleteTextures(1, mColorH, 0);
121 c7da4e65 leszek
      mColorH[0] = 0;
122
      mColorCreated = NOT_CREATED_YET;
123 bd3da5b2 Leszek Koltunski
124 133cbb2b Leszek Koltunski
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
125
      mFBOH[0] = 0;
126 e6cf7d50 Leszek Koltunski
      }
127 bd3da5b2 Leszek Koltunski
    }
128
129 4ebbb17a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// called from onDestroy(); mark OpenGL assets as 'not created'
131
132 f8377ef8 leszek
  void recreate()
133 4ebbb17a Leszek Koltunski
    {
134 c7da4e65 leszek
    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
135
    if( mDepthCreated!=DONT_CREATE ) mDepthCreated = NOT_CREATED_YET;
136 4ebbb17a Leszek Koltunski
    }
137
138 86eb00a9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
139
// if new size fits into the size of the underlying Texture, just change the projection without
140
// reallocating the Texture. Otherwise, we need to reallocate.
141
//
142 af4cc5db Leszek Koltunski
// Must be called from a thread holding the OpenGL context.
143 86eb00a9 Leszek Koltunski
144
  void resizeFast(int width, int height)
145
    {
146 af4cc5db Leszek Koltunski
    if( mWidth!=width || mHeight!=height )
147 86eb00a9 Leszek Koltunski
      {
148 af4cc5db Leszek Koltunski
      mWidth = width;
149
      mHeight= height;
150
      createProjection();
151
152 c5369f1b leszek
      if( width> mSizeX || height> mSizeY)
153 86eb00a9 Leszek Koltunski
        {
154 c5369f1b leszek
        mSizeX = width;
155
        mSizeY = height;
156 133cbb2b Leszek Koltunski
        delete();
157 86eb00a9 Leszek Koltunski
        }
158
      }
159 cc0734e7 Leszek Koltunski
160 133cbb2b Leszek Koltunski
    create();
161 86eb00a9 Leszek Koltunski
    }
162
163 af27df87 leszek
///////////////////////////////////////////////////////////////////////////////////////////////////
164 09ab7524 Leszek Koltunski
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
165
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
166
// inside a Tree of DistortedNodes (TREE)
167
// SYSTEM surfaces do not get removed in onDestroy().
168 af27df87 leszek
169 09ab7524 Leszek Koltunski
  DistortedFramebuffer(boolean depthEnabled, int type, int width, int height)
170 af27df87 leszek
    {
171 09ab7524 Leszek Koltunski
    super(width,height,NOT_CREATED_YET, (depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET, type);
172 af27df87 leszek
    }
173
174 f6fb3c6d Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// PUBLIC API
176 dedacd82 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
177 cdd5e827 Leszek Koltunski
/**
178
 * Create a new offscreen Framebuffer.
179
 *
180
 * @param width Width of the COLOR attachment.
181
 * @param height Height of the COLOR attachment.
182
 * @param depthEnabled Add DEPTH attachment?
183
 */
184
  @SuppressWarnings("unused")
185
  public DistortedFramebuffer(int width, int height, boolean depthEnabled)
186
    {
187 09ab7524 Leszek Koltunski
    super(width,height,NOT_CREATED_YET,(depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET,TYPE_USER);
188 cdd5e827 Leszek Koltunski
    }
189
190 133cbb2b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
191
192 dedacd82 Leszek Koltunski
/**
193 57578636 Leszek Koltunski
 * Create a new offscreen Framebuffer.
194 dedacd82 Leszek Koltunski
 *
195
 * @param width Width of the COLOR attachment.
196
 * @param height Height of the COLOR attachment.
197
 */
198 1942537e Leszek Koltunski
  @SuppressWarnings("unused")
199 ed13a5de Leszek Koltunski
  public DistortedFramebuffer(int width, int height)
200 f6fb3c6d Leszek Koltunski
    {
201 09ab7524 Leszek Koltunski
    super(width,height,NOT_CREATED_YET,DONT_CREATE,NOT_CREATED_YET,TYPE_USER);
202 f6fb3c6d Leszek Koltunski
    }
203
204 b448e6b9 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
205 dedacd82 Leszek Koltunski
/**
206 c5369f1b leszek
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
207 af4cc5db Leszek Koltunski
 *
208 a436ccc5 leszek
 * @return <code>true</code> if successful.
209 dedacd82 Leszek Koltunski
 */
210 c5369f1b leszek
  public boolean setAsInput()
211 b448e6b9 Leszek Koltunski
    {
212 c5369f1b leszek
    if( mColorH[0]>0 )
213
      {
214
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
215
      return true;
216
      }
217
218
    return false;
219
    }
220 2e49718d Leszek Koltunski
221 d1e740c5 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
222
/**
223 09d4f4b1 Leszek Koltunski
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
224
 * <p>
225
 * Catch: this will only work if the library has had time to actually create the texture. Remember
226
 * that the texture gets created only on first render, thus creating a Texture object and immediately
227
 * calling this method will return an invalid (negative) result.
228
 *
229 ab12f06b Leszek Koltunski
 * @return If there was not a single render between creation of the Object and calling this method on
230
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
231 d1e740c5 Leszek Koltunski
 */
232 09d4f4b1 Leszek Koltunski
  public int getTextureID()
233 d1e740c5 Leszek Koltunski
    {
234 133cbb2b Leszek Koltunski
    return mColorH[0];
235 8c327653 Leszek Koltunski
    }
236 f6fb3c6d Leszek Koltunski
  }