23 |
23 |
import android.opengl.Matrix;
|
24 |
24 |
|
25 |
25 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
26 |
|
|
|
26 |
/**
|
|
27 |
* Class which represents a OpenGL Framebuffer object.
|
|
28 |
*
|
|
29 |
* User is able to create either WRITE-only Framebuffers from objects already constructed outside
|
|
30 |
* of the library (the first constructor; primary use case: the screen) or an offscreen READ-WRITE
|
|
31 |
* FBOs (used by the DistortedNode, but also can be used by external users of the library)
|
|
32 |
*/
|
27 |
33 |
public class DistortedFramebuffer
|
28 |
34 |
{
|
29 |
35 |
private static final int TEXTURE_FAILED_TO_CREATE = -1;
|
... | ... | |
83 |
89 |
private boolean createFBO()
|
84 |
90 |
{
|
85 |
91 |
GLES20.glGenTextures(1, texIds, 0);
|
86 |
|
|
87 |
92 |
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texIds[0]);
|
88 |
93 |
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
|
89 |
94 |
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
|
... | ... | |
94 |
99 |
GLES20.glGenFramebuffers(1, fboIds, 0);
|
95 |
100 |
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, fboIds[0]);
|
96 |
101 |
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0, GLES20.GL_TEXTURE_2D, texIds[0], 0);
|
|
102 |
|
97 |
103 |
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
|
98 |
104 |
|
99 |
105 |
if(status != GLES20.GL_FRAMEBUFFER_COMPLETE)
|
100 |
106 |
{
|
101 |
107 |
android.util.Log.e("DistortedFramebuffer", "failed to create framebuffer, error="+status);
|
102 |
|
|
103 |
108 |
GLES20.glDeleteTextures(1, texIds, 0);
|
104 |
109 |
GLES20.glDeleteFramebuffers(1, fboIds, 0);
|
105 |
110 |
fboIds[0] = 0;
|
... | ... | |
107 |
112 |
return false;
|
108 |
113 |
}
|
109 |
114 |
|
110 |
|
android.util.Log.e("FBO", "creating ("+mWidth+","+mHeight+") "+fboIds[0]);
|
|
115 |
DistortedFramebufferList.add(this);
|
|
116 |
android.util.Log.e("FBO", "created ("+mWidth+","+mHeight+") "+fboIds[0]);
|
111 |
117 |
|
112 |
118 |
return true;
|
113 |
119 |
}
|
... | ... | |
146 |
152 |
|
147 |
153 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
148 |
154 |
// PUBLIC API
|
149 |
|
|
|
155 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
|
156 |
/**
|
|
157 |
* Create a new offscreen FBO.
|
|
158 |
*
|
|
159 |
* @param width Width of the COLOR attachment.
|
|
160 |
* @param height Height of the COLOR attachment.
|
|
161 |
*/
|
150 |
162 |
public DistortedFramebuffer(int width, int height)
|
151 |
163 |
{
|
152 |
164 |
mProjectionMatrix = new float[16];
|
... | ... | |
162 |
174 |
mMarked = false;
|
163 |
175 |
|
164 |
176 |
createProjection();
|
165 |
|
|
166 |
|
DistortedFramebufferList.add(this);
|
167 |
177 |
}
|
168 |
178 |
|
169 |
179 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
170 |
|
|
|
180 |
/**
|
|
181 |
* Create a new DistortedFramebuffer from an already created OpenGL Framebuffer.
|
|
182 |
*
|
|
183 |
* @param fbo the ID of a OpenGL Framebuffer object. Typically 0 (the screen)
|
|
184 |
*/
|
171 |
185 |
public DistortedFramebuffer(int fbo)
|
172 |
186 |
{
|
173 |
187 |
mProjectionMatrix = new float[16];
|
... | ... | |
182 |
196 |
}
|
183 |
197 |
|
184 |
198 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
185 |
|
|
|
199 |
/**
|
|
200 |
* Mark the underlying OpenGL object for deletion. Actual deletion will take place on the next render.
|
|
201 |
*/
|
186 |
202 |
public void markForDeletion()
|
187 |
203 |
{
|
188 |
204 |
android.util.Log.e("FBO", "marking for deletion ("+mWidth+","+mHeight+") "+fboIds[0]);
|
... | ... | |
192 |
208 |
}
|
193 |
209 |
|
194 |
210 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
195 |
|
|
196 |
|
public void setProjection(float FOV, float x, float y)
|
|
211 |
/**
|
|
212 |
* Create new Projection matrix.
|
|
213 |
*
|
|
214 |
* @param fov Vertical 'field of view' of the Projection frustrum (in degrees).
|
|
215 |
* @param x X-coordinate of the point at which our camera looks at. -1<x<1
|
|
216 |
* @param y Y-coordinate of the point at which our camera looks at. -1<y<1
|
|
217 |
*/
|
|
218 |
public void setProjection(float fov, float x, float y)
|
197 |
219 |
{
|
198 |
|
mFOV = FOV;
|
|
220 |
mFOV = fov;
|
199 |
221 |
mX = x;
|
200 |
222 |
mY = y;
|
201 |
223 |
|
... | ... | |
203 |
225 |
}
|
204 |
226 |
|
205 |
227 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
206 |
|
|
|
228 |
/**
|
|
229 |
* Resize the underlying Framebuffer.
|
|
230 |
*
|
|
231 |
* As the Framebuffer is not created until the first render, typical usage of this API is actually
|
|
232 |
* to set the size of an not-yet-created Framebuffer of an object that has been created with the
|
|
233 |
* second constructor.
|
|
234 |
*
|
|
235 |
* Fully creating an object, rendering to it, then resizing mid-render is also possible.
|
|
236 |
*
|
|
237 |
* @param width The new width.
|
|
238 |
* @param height The new height.
|
|
239 |
*/
|
207 |
240 |
public void resize(int width, int height)
|
208 |
241 |
{
|
209 |
242 |
if( mWidth!=width || mHeight!=height )
|
... | ... | |
218 |
251 |
}
|
219 |
252 |
|
220 |
253 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
221 |
|
|
222 |
|
public int getFBO()
|
223 |
|
{
|
224 |
|
return fboIds[0];
|
225 |
|
}
|
226 |
|
|
227 |
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
228 |
|
// set this as Render Target to draw to. Must be called from a thread holding OpenGL context.
|
|
254 |
/**
|
|
255 |
* Set this as the Framebuffer to write to.
|
|
256 |
*/
|
229 |
257 |
|
230 |
258 |
public void setOutput()
|
231 |
259 |
{
|
... | ... | |
235 |
263 |
}
|
236 |
264 |
|
237 |
265 |
///////////////////////////////////////////////////////////////////////////////////////////////////
|
238 |
|
// set this as Render Target to draw from. Must be called from a thread holding OpenGL context.
|
|
266 |
/**
|
|
267 |
* Set this as the Framebuffer to read from.
|
|
268 |
*/
|
239 |
269 |
|
240 |
270 |
public void setInput()
|
241 |
271 |
{
|
Javadoc