Skip to content

Commit 82ad474

Browse files
committed
Prepare FrameBffer Objects
Signed-off-by: Umair Khan <[email protected]>
1 parent e943e64 commit 82ad474

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

src/in/omerjerk/processing/video/android/Capture.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package in.omerjerk.processing.video.android;
22

3+
import java.nio.IntBuffer;
34
import java.util.ArrayList;
45
import java.util.List;
56

67
import android.content.pm.PackageManager;
78
import android.graphics.SurfaceTexture;
89
import android.hardware.Camera;
910
import android.hardware.Camera.Size;
11+
import android.opengl.GLES11Ext;
12+
import android.opengl.GLES20;
1013
import android.opengl.GLSurfaceView;
1114
import android.os.Handler;
1215
import android.os.Message;
@@ -16,6 +19,7 @@
1619
import processing.core.PApplet;
1720
import processing.core.PImage;
1821
import processing.opengl.PGraphicsOpenGL;
22+
import processing.opengl.Texture;
1923

2024
@SuppressWarnings("deprecation")
2125
public class Capture extends PImage implements PConstants,
@@ -43,8 +47,12 @@ public static void log(String log) {
4347
private SurfaceTexture mSurfaceTexture;
4448
private FullFrameRect mFullScreen;
4549
private int mTextureId;
50+
private Texture appletTexture;
4651

4752
private CameraHandler mCameraHandler;
53+
54+
IntBuffer frameBuffers = IntBuffer.allocate(1);
55+
IntBuffer renderBuffers = IntBuffer.allocate(1);
4856

4957
public Capture(PApplet context) {
5058
this(context, -1, -1);
@@ -63,6 +71,7 @@ public Capture(final PApplet applet, int width, int height) {
6371
applet.registerMethod("pause", this);
6472
applet.registerMethod("resume", this);
6573
glView = (GLSurfaceView) applet.getSurfaceView();
74+
appletTexture = ((PGraphicsOpenGL)applet.g).getTexture();
6675
applet.runOnUiThread(new Runnable() {
6776
@Override
6877
public void run() {
@@ -81,6 +90,7 @@ public void run() {
8190
mSurfaceTexture.setOnFrameAvailableListener(Capture.this);
8291
mCameraHandler.sendMessage(mCameraHandler.obtainMessage(CameraHandler.MSG_START_CAMERA, null));
8392
System.out.println("sent starting message to UI thread");
93+
prepareFrameBuffers();
8494
}
8595
});
8696
}
@@ -257,4 +267,51 @@ public void run() {
257267
}
258268
});
259269
}
270+
271+
public void prepareFrameBuffers() {
272+
273+
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
274+
275+
//Generate frame buffer
276+
GLES20.glGenFramebuffers(1, frameBuffers);
277+
GlUtil.checkGlError("glGenFramebuffers");
278+
//Bind frame buffer
279+
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, frameBuffers.get(0));
280+
GlUtil.checkGlError("glBindFramebuffer");
281+
282+
//Generate render buffers
283+
GLES20.glGenRenderbuffers(1, renderBuffers);
284+
GlUtil.checkGlError("glGenRenderbuffers");
285+
//Bind render buffers
286+
GLES20.glBindRenderbuffer(GLES20.GL_RENDERBUFFER, renderBuffers.get(0));
287+
GlUtil.checkGlError("glBindRenderbuffer");
288+
//Allocate memory to render buffers
289+
GLES20.glRenderbufferStorage(GLES20.GL_RENDERBUFFER, GLES20.GL_DEPTH_COMPONENT16, 1080, 1920);
290+
GlUtil.checkGlError("glRenderbufferStorage");
291+
292+
//Attach render buffer to frame buffer
293+
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT,
294+
GLES20.GL_RENDERBUFFER, renderBuffers.get(0));
295+
GlUtil.checkGlError("glFramebufferRenderbuffer");
296+
297+
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, 1080, 1920, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
298+
GlUtil.checkGlError("glTexImage2D");
299+
300+
System.out.println("applet's texture name = " + appletTexture.glName);
301+
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
302+
GLES20.GL_TEXTURE_2D, appletTexture.glName, 0);
303+
GlUtil.checkGlError("glFramebufferTexture2D");
304+
305+
// See if GLES is happy with all this.
306+
int status = GLES20.glCheckFramebufferStatus(GLES20.GL_FRAMEBUFFER);
307+
if (status != GLES20.GL_FRAMEBUFFER_COMPLETE) {
308+
throw new RuntimeException("Framebuffer not complete, status=" + status);
309+
}
310+
311+
GLES20.glActiveTexture(GLES20.GL_TEXTURE1);
312+
GlUtil.checkGlError("glActiveTexture GLES20.GL_TEXTURE1");
313+
314+
GLES20.glBindTexture(GLES11Ext.GL_TEXTURE_EXTERNAL_OES, mTextureId);
315+
GlUtil.checkGlError("glBindTexture GLES11Ext.GL_TEXTURE_EXTERNAL_OES");
316+
}
260317
}

0 commit comments

Comments
 (0)