Project

General

Profile

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

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

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

    
36
  DistortedFramebuffer getBuffer()
37
    {
38
    return this;
39
    }
40

    
41
///////////////////////////////////////////////////////////////////////////////////////////////////
42
// Must be called from a thread holding OpenGL Context
43
// Watch out - this has the side-effect of binding a Texture and a Framebuffer!
44

    
45
  void create()
46
    {
47
    if( mColorCreated==NOT_CREATED_YET )
48
      {
49
      GLES30.glGenTextures(1, mColorH, 0);
50
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
51
      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
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
56

    
57
      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

    
61
      mColorCreated = checkStatus("color");
62
      }
63
    if( mDepthCreated==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
64
      {
65
      GLES30.glGenTextures(1, mDepthH, 0);
66
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
67
      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
      GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
72

    
73
      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

    
76
      mDepthCreated = checkStatus("depth");
77
      }
78
    if( mDepthCreated==DONT_CREATE && mDepthH[0]>0 ) // we need to detach and recreate the DEPTH attachment.
79
      {
80
      GLES30.glDeleteTextures(1, mDepthH, 0);
81
      mDepthH[0]=0;
82
      }
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  private int checkStatus(String message)
88
    {
89
    int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
90

    
91
    if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
92
      {
93
      android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
94

    
95
      GLES30.glDeleteTextures(1, mColorH, 0);
96
      GLES30.glDeleteTextures(1, mDepthH, 0);
97
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
98
      mFBOH[0]= 0;
99

    
100
      return FAILED_TO_CREATE;
101
      }
102

    
103
    return CREATED;
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107
// Must be called from a thread holding OpenGL Context
108

    
109
  void delete()
110
    {
111
    if( mColorH[0]>0 )
112
      {
113
      if( mDepthH[0]>0 )
114
        {
115
        GLES30.glDeleteTextures(1, mDepthH, 0);
116
        mDepthH[0]=0;
117
        mDepthCreated = NOT_CREATED_YET;
118
        }
119

    
120
      GLES30.glDeleteTextures(1, mColorH, 0);
121
      mColorH[0] = 0;
122
      mColorCreated = NOT_CREATED_YET;
123

    
124
      GLES30.glDeleteFramebuffers(1, mFBOH, 0);
125
      mFBOH[0] = 0;
126
      }
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// called from onDestroy(); mark OpenGL assets as 'not created'
131

    
132
  void recreate()
133
    {
134
    if( mColorCreated!=DONT_CREATE ) mColorCreated = NOT_CREATED_YET;
135
    if( mDepthCreated!=DONT_CREATE ) mDepthCreated = NOT_CREATED_YET;
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
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
// Must be called from a thread holding the OpenGL context.
143

    
144
  void resizeFast(int width, int height)
145
    {
146
    if( mWidth!=width || mHeight!=height )
147
      {
148
      mWidth = width;
149
      mHeight= height;
150
      createProjection();
151

    
152
      if( width> mSizeX || height> mSizeY)
153
        {
154
        mSizeX = width;
155
        mSizeY = height;
156
        delete();
157
        }
158
      }
159

    
160
    create();
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164
// 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

    
169
  DistortedFramebuffer(boolean depthEnabled, int type, int width, int height)
170
    {
171
    super(width,height,NOT_CREATED_YET, (depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET, type);
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175
// PUBLIC API
176
///////////////////////////////////////////////////////////////////////////////////////////////////
177
/**
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
    super(width,height,NOT_CREATED_YET,(depthEnabled ? NOT_CREATED_YET:DONT_CREATE),NOT_CREATED_YET,TYPE_USER);
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
/**
193
 * Create a new offscreen Framebuffer.
194
 *
195
 * @param width Width of the COLOR attachment.
196
 * @param height Height of the COLOR attachment.
197
 */
198
  @SuppressWarnings("unused")
199
  public DistortedFramebuffer(int width, int height)
200
    {
201
    super(width,height,NOT_CREATED_YET,DONT_CREATE,NOT_CREATED_YET,TYPE_USER);
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205
/**
206
 * Bind the underlying rectangle of pixels as a OpenGL Texture.
207
 *
208
 * @return <code>true</code> if successful.
209
 */
210
  public boolean setAsInput()
211
    {
212
    if( mColorH[0]>0 )
213
      {
214
      GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
215
      return true;
216
      }
217

    
218
    return false;
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222
/**
223
 * 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
 * @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
 */
232
  public int getTextureID()
233
    {
234
    return mColorH[0];
235
    }
236
  }
(5-5/23)