Project

General

Profile

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

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

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