Skip to content

Commit 74e06b2

Browse files
committed
Merge branch 'testing'
2 parents d32a59f + 8d2a752 commit 74e06b2

File tree

2 files changed

+47
-13
lines changed

2 files changed

+47
-13
lines changed

README.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,4 @@ We cannot directly render to processing's own texture because it's bound `GL_TEX
55

66
**Behind the hood :**
77

8-
I create a framebuffer object and a custom texture object, which is bound to target `GL_TEXTURE_2D`, and attach this texture to the previously created framebuffer at `GL_COLOR_ATTACHMENT0`. I then render SurfaceTexture's shader output to this framebuffer and it atomatically gets rendered upon the custom texture I created. I then call `glReadPixels` and assign these read pixels to `PImage`'s pixel array.
9-
10-
For an alternative approach, please head over to pixel_manipulation branch.
8+
I create a framebuffer object and a custom texture object, which is bound to target `GL_TEXTURE_2D`, and attach this texture to the previously created framebuffer at `GL_COLOR_ATTACHMENT0`. I then render SurfaceTexture's shader output to this framebuffer and it atomatically gets rendered upon the custom texture I created. I then follow the approach as being done in the Syphon library - https://github.com/Syphon/Processing/blob/master/Processing_2_0/src/codeanticode/syphon/SyphonClient.java#L271

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import processing.core.PConstants;
1818
import processing.core.PApplet;
1919
import processing.core.PImage;
20+
import processing.opengl.PGL;
2021
import processing.opengl.PGraphicsOpenGL;
2122
import processing.opengl.Texture;
2223

@@ -48,14 +49,18 @@ public static void log(String log) {
4849
private int mTextureId;
4950
private final float[] mSTMatrix = new float[16];
5051

51-
private Texture customTexture;
52+
// private Texture customTexture;
5253
private PGraphicsOpenGL pg;
5354
private IntBuffer pixelBuffer;
55+
56+
private PGraphicsOpenGL destpg;
57+
PGL pgl;
5458

5559
private CameraHandler mCameraHandler;
5660

5761
IntBuffer frameBuffers = IntBuffer.allocate(1);
5862
IntBuffer renderBuffers = IntBuffer.allocate(1);
63+
IntBuffer customTexture = IntBuffer.allocate(1);
5964

6065
public Capture(PApplet context) {
6166
this(context, -1, -1);
@@ -77,10 +82,10 @@ public Capture(final PApplet applet, int width, int height) {
7782
applet.registerMethod("resume", this);
7883
glView = (GLSurfaceView) applet.getSurfaceView();
7984
pg = (PGraphicsOpenGL)applet.g;
80-
customTexture = new Texture(pg, width, height);
81-
customTexture.invertedY(true);
82-
log("cusotm texture address = " + customTexture.glName);
83-
pg.setCache(this, customTexture);
85+
// customTexture = new Texture(pg, width, height);
86+
// customTexture.invertedY(true);
87+
log("cusotm texture address = " + customTexture);
88+
// pg.setCache(this, customTexture);
8489
applet.runOnUiThread(new Runnable() {
8590
@Override
8691
public void run() {
@@ -289,11 +294,14 @@ public void run() {
289294
surfaceTexture.getTransformMatrix(mSTMatrix);
290295
mFullScreen.drawFrame(mTextureId, mSTMatrix);
291296

297+
getImage(false);
298+
299+
/*
292300
pixelBuffer.position(0);
293301
GLES20.glReadPixels(0, 0, width, height, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, pixelBuffer);
294302
pixelBuffer.position(0);
295303
pixelBuffer.get(Capture.this.pixels);
296-
updatePixels();
304+
updatePixels(); */
297305

298306
//Fall back to default frame buffer
299307
GLES20.glBindFramebuffer(GLES20.GL_FRAMEBUFFER, 0);
@@ -326,19 +334,22 @@ public void prepareFrameBuffers() {
326334
GLES20.glFramebufferRenderbuffer(GLES20.GL_FRAMEBUFFER, GLES20.GL_DEPTH_ATTACHMENT,
327335
GLES20.GL_RENDERBUFFER, renderBuffers.get(0));
328336
GlUtil.checkGlError("glFramebufferRenderbuffer");
337+
338+
GLES20.glGenTextures(1, customTexture);
339+
GlUtil.checkGlError("glGenTextures");
329340

330-
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, customTexture.glName);
341+
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, customTexture.get(0));
331342
GlUtil.checkGlError("glBindTexture");
332-
/*
343+
333344
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
334345
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
335346
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
336-
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);*/
347+
GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
337348
GLES20.glTexImage2D(GLES20.GL_TEXTURE_2D, 0, GLES20.GL_RGBA, width, height, 0, GLES20.GL_RGBA, GLES20.GL_UNSIGNED_BYTE, null);
338349
GlUtil.checkGlError("glTexImage2D");
339350

340351
GLES20.glFramebufferTexture2D(GLES20.GL_FRAMEBUFFER, GLES20.GL_COLOR_ATTACHMENT0,
341-
GLES20.GL_TEXTURE_2D, customTexture.glName, 0);
352+
GLES20.GL_TEXTURE_2D, customTexture.get(0), 0);
342353
GlUtil.checkGlError("glFramebufferTexture2D");
343354

344355
/*
@@ -354,4 +365,29 @@ public void prepareFrameBuffers() {
354365
throw new RuntimeException("Framebuffer not complete, status=" + status);
355366
}
356367
}
368+
369+
public void getImage(boolean loadPixels) {
370+
371+
if (destpg == null || destpg.width != width || destpg.height != height) {
372+
destpg = (PGraphicsOpenGL) parent.createGraphics(width, height, PConstants.P2D);
373+
destpg.pgl.setGlThread(Thread.currentThread());
374+
}
375+
376+
destpg.beginDraw();
377+
destpg.background(0, 0);
378+
PGL pgl = destpg.beginPGL();
379+
pgl.drawTexture(PGL.TEXTURE_2D, customTexture.get(0), width, height,
380+
0, 0, width, height);
381+
destpg.endPGL();
382+
destpg.endDraw();
383+
384+
// Uses the PGraphics texture as the cache object for the image
385+
Texture tex = destpg.getTexture();
386+
pg.setCache(this, tex);
387+
if (loadPixels) {
388+
this.loadPixels();
389+
tex.get(this.pixels);
390+
this.setLoaded(false);
391+
}
392+
}
357393
}

0 commit comments

Comments
 (0)