Project

General

Profile

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

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

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.main;
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

    
36
  void prepareDebug(long time) {}
37
  void renderDebug(long time)  {}
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40
// Must be called from a thread holding OpenGL Context
41

    
42
  void create()
43
    {
44
    if( mColorCreated==NOT_CREATED_YET )
45
      {
46
      GLES30.glGenTextures( mNumColors, mColorH, 0);
47
      GLES30.glGenFramebuffers(1, mFBOH, 0);
48
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
49

    
50
      for(int i=0; i<mNumColors; i++)
51
        {
52
        GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[i]);
53
        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
54
        GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
55
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
56
        GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
57
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mWidth, mHeight, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
58
        }
59
      GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
60
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
61
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
62

    
63
      mColorCreated = checkStatus("color");
64
      }
65
    if( mDepthStencilCreated==NOT_CREATED_YET ) // we need to create a new DEPTH or STENCIL attachment
66
      {
67
      GLES30.glGenTextures(1, mDepthStencilH, 0);
68
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthStencilH[0]);
69
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
70
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
71
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
72
      GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
73

    
74
      if( mDepthStencil==DEPTH_NO_STENCIL )
75
        {
76
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mWidth, mHeight, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_INT, null);
77
        }
78
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
79
        {
80
        GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH24_STENCIL8, mWidth, mHeight, 0, GLES30.GL_DEPTH_STENCIL, GLES30.GL_UNSIGNED_INT_24_8, null);
81
        }
82

    
83
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0);
84
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
85

    
86
      if( mDepthStencil==DEPTH_NO_STENCIL )
87
        {
88
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
89
        }
90
      else if( mDepthStencil==BOTH_DEPTH_STENCIL )
91
        {
92
        GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_STENCIL_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthStencilH[0], 0);
93
        }
94

    
95
      GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0);
96

    
97
      mDepthStencilCreated = checkStatus("depth");
98
      }
99
    if( mDepthStencilCreated==DONT_CREATE && mDepthStencilH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
100
      {
101
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
102
      mDepthStencilH[0]=0;
103
      }
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private int checkStatus(String message)
109
    {
110
    int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
111

    
112
    if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
113
      {
114
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
115

    
116
      GLES30.glDeleteTextures(1, mColorH, 0);
117
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
118
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
119
      mFBOH[0]= 0;
120

    
121
      return FAILED_TO_CREATE;
122
      }
123

    
124
    return CREATED;
125
    }
126

    
127
///////////////////////////////////////////////////////////////////////////////////////////////////
128
// Must be called from a thread holding OpenGL Context
129

    
130
  void delete()
131
    {
132
    if( mColorH[0]>0 )
133
      {
134
      GLES30.glDeleteTextures(1, mColorH, 0);
135
      mColorH[0] = 0;
136
      mColorCreated = NOT_CREATED_YET;
137
      }
138

    
139
    if( mDepthStencilH[0]>0 )
140
      {
141
      GLES30.glDeleteTextures(1, mDepthStencilH, 0);
142
      mDepthStencilH[0]=0;
143
      mDepthStencilCreated = NOT_CREATED_YET;
144
      }
145

    
146
    GLES30.glDeleteFramebuffers(1, mFBOH, 0);
147
    mFBOH[0] = 0;
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151
// called from onDestroy(); mark OpenGL assets as 'not created'
152

    
153
  void recreate()
154
    {
155
    if( mColorCreated!=DONT_CREATE )
156
      {
157
      mColorCreated = NOT_CREATED_YET;
158
      mColorH[0] = 0;
159
      }
160
    if( mDepthStencilCreated!=DONT_CREATE )
161
      {
162
      mDepthStencilCreated = NOT_CREATED_YET;
163
      mDepthStencilH[0] = 0;
164
      }
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168
// create SYSTEM or TREE framebuffers (those are just like normal FBOs, just hold information
169
// that they were autocreated only for the Library's internal purposes (SYSTEM) or for using
170
// inside a Tree of DistortedNodes (TREE)
171
// SYSTEM surfaces do not get removed in onDestroy().
172

    
173
  DistortedFramebuffer(int numcolors, int depthStencil, int type, int width, int height)
174
    {
175
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET, type);
176
    }
177

    
178
///////////////////////////////////////////////////////////////////////////////////////////////////
179
// PUBLIC API
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
/**
182
 * Create new offscreen Framebuffer with configurable number of COLOR, DEPTH and STENCIL attachments.
183
 *
184
 * @param width        Width of all the COLOR attachments.
185
 * @param height       Height of all the COLOR attachments.
186
 * @param numcolors    How many COLOR attachments to create?
187
 * @param depthStencil Add DEPTH or STENCIL attachment?
188
 *                     Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
189
 */
190
  @SuppressWarnings("unused")
191
  public DistortedFramebuffer(int width, int height, int numcolors, int depthStencil)
192
    {
193
    super(width,height,NOT_CREATED_YET,numcolors,depthStencil,NOT_CREATED_YET,TYPE_USER);
194
    }
195

    
196
///////////////////////////////////////////////////////////////////////////////////////////////////
197

    
198
/**
199
 * Create new offscreen Framebuffer with COLOR0 attachment only.
200
 *
201
 * @param width Width of the COLOR0 attachment.
202
 * @param height Height of the COLOR0 attachment.
203
 */
204
  @SuppressWarnings("unused")
205
  public DistortedFramebuffer(int width, int height)
206
    {
207
    super(width,height,NOT_CREATED_YET, 1, NO_DEPTH_NO_STENCIL,NOT_CREATED_YET,TYPE_USER);
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
/**
212
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
213
 *
214
 * @return <code>true</code> if successful.
215
 */
216
  public boolean setAsInput()
217
    {
218
    if( mColorH[0]>0 )
219
      {
220
      GLES30.glActiveTexture(GLES30.GL_TEXTURE0);
221
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
222
      return true;
223
      }
224

    
225
    return false;
226
    }
227

    
228
///////////////////////////////////////////////////////////////////////////////////////////////////
229
/**
230
 * Enable.disable DEPTH and STENCIL buffers.
231
 *
232
 * @param depthStencil Valid values: NO_DEPTH_NO_STENCIL, DEPTH_NO_STENCIL, BOTH_DEPTH_STENCIL.
233
 */
234
  public void enableDepthStencil(int depthStencil)
235
    {
236
    if( depthStencil != mDepthStencil )
237
      {
238
      mDepthStencil = depthStencil;
239

    
240
      if( depthStencil!= NO_DEPTH_NO_STENCIL && mDepthStencilCreated==DONT_CREATE )
241
        {
242
        mDepthStencilCreated = NOT_CREATED_YET;
243
        markForCreation();
244
        }
245
      if( depthStencil== NO_DEPTH_NO_STENCIL && mDepthStencilCreated!=DONT_CREATE )
246
        {
247
        mDepthStencilCreated = DONT_CREATE;
248
        markForCreation();
249
        }
250
      }
251
    }
252

    
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
/**
255
 * Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
256
 * <p>
257
 * Catch: this will only work if the library has had time to actually create the texture. Remember
258
 * that the texture gets created only on first render, thus creating a Texture object and immediately
259
 * calling this method will return an invalid (negative) result.
260
 *
261
 * @return If there was not a single render between creation of the Object and calling this method on
262
 *         it, return a negative value. Otherwise, return ID of COLOR attachment 0.
263
 */
264
  public int getTextureID()
265
    {
266
    return mColorH[0];
267
    }
268
  }
(4-4/24)