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.
|
30
|
*/
|
31
|
public class DistortedFramebuffer extends DistortedRenderable implements DistortedInputSurface, DistortedOutputSurface
|
32
|
{
|
33
|
private int[] mDepthH = new int[1];
|
34
|
private int[] mFBOH = new int[1];
|
35
|
private boolean mDepthEnabled;
|
36
|
private DistortedProjection mProjection;
|
37
|
|
38
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
39
|
// Must be called from a thread holding OpenGL Context
|
40
|
// Watch out - this has the side-effect of binding a Texture and a Framebuffer!
|
41
|
|
42
|
public void create()
|
43
|
{
|
44
|
if( mColorH[0]==NOT_CREATED_YET )
|
45
|
{
|
46
|
GLES30.glGenTextures(1, mColorH, 0);
|
47
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
48
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
|
49
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
|
50
|
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
|
51
|
GLES30.glTexParameterf(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR);
|
52
|
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, mSizeX, mSizeY, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null);
|
53
|
|
54
|
GLES30.glGenFramebuffers(1, mFBOH, 0);
|
55
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
56
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_COLOR_ATTACHMENT0, GLES30.GL_TEXTURE_2D, mColorH[0], 0);
|
57
|
|
58
|
checkStatus("color");
|
59
|
}
|
60
|
if( mDepthEnabled && mDepthH[0]==NOT_CREATED_YET ) // we need to create a new DEPTH attachment
|
61
|
{
|
62
|
GLES30.glGenTextures(1, mDepthH, 0);
|
63
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mDepthH[0]);
|
64
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_REPEAT);
|
65
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_REPEAT);
|
66
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST);
|
67
|
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST);
|
68
|
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_DEPTH_COMPONENT, mSizeX, mSizeY, 0, GLES30.GL_DEPTH_COMPONENT, GLES30.GL_UNSIGNED_SHORT, null);
|
69
|
|
70
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
71
|
GLES30.glFramebufferTexture2D(GLES30.GL_FRAMEBUFFER, GLES30.GL_DEPTH_ATTACHMENT, GLES30.GL_TEXTURE_2D, mDepthH[0], 0);
|
72
|
|
73
|
checkStatus("depth");
|
74
|
}
|
75
|
if( !mDepthEnabled && mDepthH[0]!=NOT_CREATED_YET ) // we need to detach and destroy the DEPTH attachment.
|
76
|
{
|
77
|
GLES30.glDeleteTextures(1, mDepthH, 0);
|
78
|
mDepthH[0]=NOT_CREATED_YET;
|
79
|
}
|
80
|
}
|
81
|
|
82
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
83
|
|
84
|
private boolean checkStatus(String message)
|
85
|
{
|
86
|
int status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER);
|
87
|
|
88
|
if(status != GLES30.GL_FRAMEBUFFER_COMPLETE)
|
89
|
{
|
90
|
android.util.Log.e("DistortedFramebuffer", "FRAMEBUFFER INCOMPLETE, "+message+" error="+status);
|
91
|
|
92
|
GLES30.glDeleteTextures(1, mColorH, 0);
|
93
|
GLES30.glDeleteTextures(1, mDepthH, 0);
|
94
|
GLES30.glDeleteFramebuffers(1, mFBOH, 0);
|
95
|
mFBOH[0] = 0;
|
96
|
mColorH[0] = FAILED_TO_CREATE;
|
97
|
mDepthH[0] = FAILED_TO_CREATE;
|
98
|
|
99
|
return false;
|
100
|
}
|
101
|
|
102
|
return true;
|
103
|
}
|
104
|
|
105
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
106
|
// Must be called from a thread holding OpenGL Context
|
107
|
|
108
|
void delete()
|
109
|
{
|
110
|
if( mColorH[0]>=0 )
|
111
|
{
|
112
|
if( mDepthH[0]>=0 )
|
113
|
{
|
114
|
GLES30.glDeleteTextures(1, mDepthH, 0);
|
115
|
mDepthH[0]=NOT_CREATED_YET;
|
116
|
}
|
117
|
|
118
|
GLES30.glDeleteTextures(1, mColorH, 0);
|
119
|
mColorH[0] = NOT_CREATED_YET;
|
120
|
|
121
|
GLES30.glDeleteFramebuffers(1, mFBOH, 0);
|
122
|
mFBOH[0] = 0;
|
123
|
}
|
124
|
}
|
125
|
|
126
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
127
|
// called from onDestroy(); mark OpenGL assets as 'not created'
|
128
|
|
129
|
void destroy()
|
130
|
{
|
131
|
if( mColorH[0]!=DONT_CREATE ) mColorH[0] = NOT_CREATED_YET;
|
132
|
if( mDepthEnabled ) mDepthH[0] = NOT_CREATED_YET;
|
133
|
}
|
134
|
|
135
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
136
|
// if new size fits into the size of the underlying Texture, just change the projection without
|
137
|
// reallocating the Texture. Otherwise, we need to reallocate.
|
138
|
//
|
139
|
// Must be called form a thread holding the OpenGL context.
|
140
|
|
141
|
void resizeFast(int width, int height)
|
142
|
{
|
143
|
if( mProjection.resize(width,height) )
|
144
|
{
|
145
|
if( width> mSizeX || height> mSizeY)
|
146
|
{
|
147
|
mSizeX = width;
|
148
|
mSizeY = height;
|
149
|
delete();
|
150
|
}
|
151
|
}
|
152
|
|
153
|
create();
|
154
|
}
|
155
|
|
156
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
157
|
// PUBLIC API
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
/**
|
160
|
* Create a new offscreen Framebuffer.
|
161
|
*
|
162
|
* @param width Width of the COLOR attachment.
|
163
|
* @param height Height of the COLOR attachment.
|
164
|
* @param depthEnabled Add DEPTH attachment?
|
165
|
*/
|
166
|
@SuppressWarnings("unused")
|
167
|
public DistortedFramebuffer(int width, int height, boolean depthEnabled)
|
168
|
{
|
169
|
super(width,height,NOT_CREATED_YET);
|
170
|
|
171
|
mProjection = new DistortedProjection(width,height);
|
172
|
mDepthEnabled= depthEnabled;
|
173
|
mFBOH[0] = NOT_CREATED_YET;
|
174
|
mDepthH[0] = NOT_CREATED_YET;
|
175
|
}
|
176
|
|
177
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
178
|
|
179
|
/**
|
180
|
* Create a new offscreen Framebuffer.
|
181
|
*
|
182
|
* @param width Width of the COLOR attachment.
|
183
|
* @param height Height of the COLOR attachment.
|
184
|
*/
|
185
|
@SuppressWarnings("unused")
|
186
|
public DistortedFramebuffer(int width, int height)
|
187
|
{
|
188
|
super(width,height,NOT_CREATED_YET);
|
189
|
|
190
|
mProjection = new DistortedProjection(width,height);
|
191
|
mDepthEnabled= false;
|
192
|
mFBOH[0] = NOT_CREATED_YET;
|
193
|
mDepthH[0] = NOT_CREATED_YET;
|
194
|
}
|
195
|
|
196
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
197
|
/**
|
198
|
* Bind the underlying rectangle of pixels as a OpenGL Texture.
|
199
|
*/
|
200
|
public boolean setAsInput()
|
201
|
{
|
202
|
if( mColorH[0]>0 )
|
203
|
{
|
204
|
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, mColorH[0]);
|
205
|
return true;
|
206
|
}
|
207
|
|
208
|
return false;
|
209
|
}
|
210
|
|
211
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
212
|
/**
|
213
|
* Bind this Surface as a Framebuffer we can render to.
|
214
|
*/
|
215
|
public void setAsOutput()
|
216
|
{
|
217
|
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, mFBOH[0]);
|
218
|
|
219
|
if( mDepthH[0]!=NOT_CREATED_YET )
|
220
|
{
|
221
|
GLES30.glEnable(GLES30.GL_DEPTH_TEST);
|
222
|
GLES30.glDepthMask(true);
|
223
|
}
|
224
|
else
|
225
|
{
|
226
|
GLES30.glDisable(GLES30.GL_DEPTH_TEST);
|
227
|
GLES30.glDepthMask(false);
|
228
|
}
|
229
|
}
|
230
|
|
231
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
232
|
/**
|
233
|
* Create a new DEPTH buffer and attach it or (param=false) detach an existing DEPTh attachment and destroy it.
|
234
|
*
|
235
|
* @param enable <bold>true</bold> if we want to attach a new DEPTH buffer to the FBO.<br>
|
236
|
* <bold>false</bold> if we want to detach the DEPTH attachment.
|
237
|
*/
|
238
|
public void enableDepthAttachment(boolean enable)
|
239
|
{
|
240
|
mDepthEnabled = enable;
|
241
|
}
|
242
|
|
243
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
244
|
|
245
|
/**
|
246
|
* Draw the (texture,mesh,effects) object to the Framebuffer.
|
247
|
* <p>
|
248
|
* Must be called from a thread holding OpenGL Context.
|
249
|
*
|
250
|
* @param surface InputSurface to skin our Mesh with.
|
251
|
* @param mesh Class descendant from MeshObject
|
252
|
* @param effects The DistortedEffects to use when rendering
|
253
|
* @param time Current time, in milliseconds.
|
254
|
*/
|
255
|
public void renderTo(DistortedInputSurface surface, MeshObject mesh, DistortedEffects effects, long time)
|
256
|
{
|
257
|
surface.create(); // Watch out - this needs to be before
|
258
|
create(); // the 'setAsInput' because this has side-effects!
|
259
|
|
260
|
if( surface.setAsInput() )
|
261
|
{
|
262
|
DistortedRenderable.deleteAllMarked();
|
263
|
effects.drawPriv(surface.getWidth()/2.0f, surface.getHeight()/2.0f, mesh, this, time);
|
264
|
}
|
265
|
}
|
266
|
|
267
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
268
|
/**
|
269
|
* Draws the Tree, and all its children, to the Framebuffer.
|
270
|
* <p>
|
271
|
* Must be called from a thread holding OpenGL Context.
|
272
|
*
|
273
|
* @param dt DistortedTree to render.
|
274
|
* @param time Current time, in milliseconds. This will be passed to all the Effects stored in the Tree.
|
275
|
*/
|
276
|
public void renderTo(DistortedTree dt, long time)
|
277
|
{
|
278
|
DistortedRenderable.deleteAllMarked();
|
279
|
create();
|
280
|
dt.drawRecursive(time,this);
|
281
|
}
|
282
|
|
283
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
284
|
/**
|
285
|
* Create new Projection matrix.
|
286
|
*
|
287
|
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
288
|
* @param x X-coordinate of the point at which our camera looks at. 0 is the center.
|
289
|
* @param y Y-coordinate of the point at which our camera looks at. 0 is the center.
|
290
|
*/
|
291
|
public void setProjection(float fov, float x, float y)
|
292
|
{
|
293
|
mProjection.set(fov,x,y);
|
294
|
mProjection.createProjection();
|
295
|
}
|
296
|
|
297
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
298
|
/**
|
299
|
* Resize the underlying Framebuffer.
|
300
|
*
|
301
|
* @param width The new width.
|
302
|
* @param height The new height.
|
303
|
*/
|
304
|
public void resize(int width, int height)
|
305
|
{
|
306
|
if( mProjection.resize(width,height) )
|
307
|
{
|
308
|
mSizeX = width;
|
309
|
mSizeY = height;
|
310
|
if( mColorH[0]>0 ) markForDeletion();
|
311
|
}
|
312
|
}
|
313
|
|
314
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
315
|
/**
|
316
|
* Return the ID of the Texture (COLOR attachment 0) that's backing this FBO.
|
317
|
* <p>
|
318
|
* Catch: this will only work if the library has had time to actually create the texture. Remember
|
319
|
* that the texture gets created only on first render, thus creating a Texture object and immediately
|
320
|
* calling this method will return an invalid (negative) result.
|
321
|
*
|
322
|
* @return If there was not a single render between creation of the Object and calling this method on
|
323
|
* it, return a negative value. Otherwise, return ID of COLOR attachment 0.
|
324
|
*/
|
325
|
public int getTextureID()
|
326
|
{
|
327
|
return mColorH[0];
|
328
|
}
|
329
|
|
330
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
331
|
/**
|
332
|
* Return true if the FBO contains a DEPTH attachment.
|
333
|
*
|
334
|
* @return <bold>true</bold> if the FBO contains a DEPTH attachment.
|
335
|
*/
|
336
|
public boolean hasDepth()
|
337
|
{
|
338
|
return mDepthEnabled;
|
339
|
}
|
340
|
|
341
|
//////////////////////////////////////////////////////////////////////////////////////////////////
|
342
|
/**
|
343
|
* Return Projection stored in this Framebuffer.
|
344
|
*
|
345
|
* @return DistortedProjection object.
|
346
|
*/
|
347
|
public DistortedProjection getProjection()
|
348
|
{
|
349
|
return mProjection;
|
350
|
}
|
351
|
}
|