Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedFramebuffer.java @ 05ecc6fe

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.GLES30;
23

    
24
///////////////////////////////////////////////////////////////////////////////////////////////////
25
/**
26
 * Class which represents a OpenGL Framebuffer object.
27
 * <p>
28
 * User is able to create offscreen FBOs and both a) render to them b) use their COLOR0 attachment as
29
 * an input texture. Attaching Depths and/or Stencils is also possible.
30
 */
31
public class DistortedFramebuffer extends DistortedOutputSurface implements DistortedInputSurface
32
  {
33

    
34
///////////////////////////////////////////////////////////////////////////////////////////////////
35
// Must be called from a thread holding OpenGL Context
36
// Watch out - this has the side-effect of binding a Texture and a Framebuffer!
37

    
38
  void create()
39
    {
40
    if( mColorCreated==NOT_CREATED_YET )
41
      {
42
      GLES30.glGenTextures(1, mColorH, 0);
43
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
44
      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
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
49

    
50
      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

    
54
      mColorCreated = checkStatus("color");
55
      }
56
    if( mDepthCreated==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
57
      {
58
      GLES30.glGenTextures(1, mDepthH, 0);
59
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
60
      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
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
65

    
66
      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

    
69
      mDepthCreated = checkStatus("depth");
70
      }
71
    if( mDepthCreated==DONT_CREATE && mDepthH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
72
      {
73
      GLES30.glDeleteTextures(1, mDepthH, 0);
74
      mDepthH[0]=0;
75
      }
76
    }
77

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

    
80
  private int checkStatus(String message)
81
    {
82
    int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
83

    
84
    if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
85
      {
86
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
87

    
88
      GLES30.glDeleteTextures(1, mColorH, 0);
89
      GLES30.glDeleteTextures(1, mDepthH, 0);
90
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
91
      mFBOH[0]= 0;
92

    
93
      return FAILED_TO_CREATE;
94
      }
95

    
96
    return CREATED;
97
    }
98

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100
// Must be called from a thread holding OpenGL Context
101

    
102
  void delete()
103
    {
104
    if( mColorH[0]>0 )
105
      {
106
      if( mDepthH[0]>0 )
107
        {
108
        GLES30.glDeleteTextures(1, mDepthH, 0);
109
        mDepthH[0]=0;
110
        mDepthCreated = NOT_CREATED_YET;
111
        }
112

    
113
      GLES30.glDeleteTextures(1, mColorH, 0);
114
      mColorH[0] = 0;
115
      mColorCreated = NOT_CREATED_YET;
116

    
117
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
118
      mFBOH[0] = 0;
119
      }
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123
// called from onDestroy(); mark OpenGL assets as 'not created'
124

    
125
  void recreate()
126
    {
127
    if( mColorCreated!=DONT_CREATE )
128
      {
129
      mColorCreated = NOT_CREATED_YET;
130
      mColorH[0] = 0;
131
      }
132
    if( mDepthCreated!=DONT_CREATE )
133
      {
134
      mDepthCreated = NOT_CREATED_YET;
135
      mDepthH[0] = 0;
136
      }
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
141
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
142
// inside a Tree of DistortedNodes (TREE)
143
// SYSTEM surfaces do not get removed in onDestroy().
144

    
145
  DistortedFramebuffer(boolean depthEnabled, int type, int width, int height)
146
    {
147
    super(width,height,NOT_CREATED_YET, (depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET, type);
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// For setting the Depth texture as input to fragment shaders that merge many planes. (currently: blur2)
152

    
153
  boolean setAsDepth()
154
    {
155
    if( mDepthH[0]>0 )
156
      {
157
      GLES30.glActiveTexture(GLES30.GL_TEXTURE1);
158
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
159
      return true;
160
      }
161

    
162
    return false;
163
    }
164

    
165
///////////////////////////////////////////////////////////////////////////////////////////////////
166
// PUBLIC API
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
/**
169
 * Create a new offscreen Framebuffer.
170
 *
171
 * @param width Width of the COLOR attachment.
172
 * @param height Height of the COLOR attachment.
173
 * @param depthEnabled Add DEPTH attachment?
174
 */
175
  @SuppressWarnings("unused")
176
  public DistortedFramebuffer(int width, int height, boolean depthEnabled)
177
    {
178
    super(width,height,NOT_CREATED_YET,(depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET,TYPE_USER);
179
    }
180

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182

    
183
/**
184
 * Create a new offscreen Framebuffer.
185
 *
186
 * @param width Width of the COLOR attachment.
187
 * @param height Height of the COLOR attachment.
188
 */
189
  @SuppressWarnings("unused")
190
  public DistortedFramebuffer(int width, int height)
191
    {
192
    super(width,height,NOT_CREATED_YET,DONT_CREATE,NOT_CREATED_YET,TYPE_USER);
193
    }
194

    
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
/**
197
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
198
 *
199
 * @return <code>true</code> if successful.
200
 */
201
  public boolean setAsInput()
202
    {
203
    if( mColorH[0]>0 )
204
      {
205
      GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
206
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
207
      return true;
208
      }
209

    
210
    return false;
211
    }
212

    
213
///////////////////////////////////////////////////////////////////////////////////////////////////
214
/**
215
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
216
 * <p>
217
 * Catch: this will only work if the library has had time to actually create the texture. Remember
218
 * that the texture gets created only on first render, thus creating a Texture object and immediately
219
 * calling this method will return an invalid (negative) result.
220
 *
221
 * @return If there was not a single render between creation of the Object and calling this method on
222
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
223
 */
224
  public int getTextureID()
225
    {
226
    return mColorH[0];
227
    }
228
  }
(4-4/24)