Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PApplet fileCapSendPAppletRef;
- {
- import javax.media.opengl.GL;
- import java.nio.ByteBuffer;
- import java.nio.channels.FileChannel;
- import java.nio.MappedByteBuffer;
- fileCapSendPAppletRef = this;
- new Thread(new Runnable() {
- public void run() {
- while(frameCount < 1) {}
- new FileCapSend();
- }
- }).start();
- }
- public class FileCapSend {
- class SendThread extends Thread {
- public void run() {
- for(;;) {
- if(!run) break;
- if(cap) {
- try {
- fc.write(bb);
- fc.position(0);
- } catch(Throwable fex) {fex.printStackTrace();}
- cap = false;
- } else {
- try {
- sleep(1);
- } catch(Throwable ex) {ex.printStackTrace();}
- }
- }
- }
- }
- PApplet p5;
- PGraphicsOpenGL pg;
- GL gl;
- ByteBuffer bb;
- String tmpLoc = "C:\\p5tmp";
- FileChannel fc;
- boolean run = true;
- boolean registered, cap;
- SendThread sendThread;
- int vpboInd, cpboInd;
- int[] pboIds;
- boolean buffReady;
- FileCapSend() {
- p5 = fileCapSendPAppletRef;
- pg = (PGraphicsOpenGL)p5.g;
- gl = pg.gl;
- bb = ByteBuffer.allocateDirect(width*height*4);
- if(run) beginCap();
- }
- void beginCap() {
- try {
- File prev = new File(tmpLoc);
- if(prev.exists()) prev.delete();
- fc = new RandomAccessFile(tmpLoc, "rw").getChannel();
- } catch(Throwable ex) {
- ex.printStackTrace();
- run = false;
- return;
- }
- sendThread = new SendThread();
- sendThread.start();
- register();
- }
- void register() {
- p5.registerDraw(this);
- p5.registerPost(this);
- registered = true;
- }
- void endCap() {
- p5.unregisterDraw(this);
- p5.unregisterPost(this);
- try {
- fc.close();
- } catch(Throwable ex) {ex.printStackTrace();}
- }
- public void draw() {
- bb.clear();
- if(!buffReady) {
- gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
- pboIds = new int[2];
- gl.glGenBuffers(2, pboIds, 0);
- gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[0]);
- gl.glBufferData(
- GL.GL_PIXEL_PACK_BUFFER, width*height*4,
- null, GL.GL_STREAM_READ);
- gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[1]);
- gl.glBufferData(
- GL.GL_PIXEL_PACK_BUFFER, width*height*4,
- null, GL.GL_STREAM_READ);
- gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, 0);
- buffReady = true;
- }
- vpboInd = (vpboInd+1)%2;
- cpboInd = (vpboInd+1)%2;
- gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[cpboInd]);
- try {
- bb =
- gl.glMapBuffer(GL.GL_PIXEL_PACK_BUFFER,GL.GL_READ_ONLY);
- } catch(Throwable ex) {ex.printStackTrace();}
- gl.glUnmapBuffer(GL.GL_PIXEL_PACK_BUFFER);
- cap = true;
- gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[vpboInd]);
- gl.glReadPixels(
- 0,0,width,height,GL.GL_BGRA,GL.GL_UNSIGNED_BYTE,0L);
- gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, 0);
- }
- public void post() {
- if(!run && registered) {
- endCap();
- return;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement