Advertisement
Guest User

capSend.pde

a guest
Feb 17th, 2013
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. PApplet fileCapSendPAppletRef;
  2. {
  3.   import javax.media.opengl.GL;
  4.   import java.nio.ByteBuffer;
  5.   import java.nio.channels.FileChannel;
  6.   import java.nio.MappedByteBuffer;
  7.  
  8.   fileCapSendPAppletRef = this;
  9.   new Thread(new Runnable() {
  10.     public void run() {
  11.       while(frameCount < 1) {}
  12.       new FileCapSend();
  13.     }
  14.   }).start();
  15. }
  16.  
  17. public class FileCapSend {
  18.  
  19.   class SendThread extends Thread {
  20.     public void run() {
  21.       for(;;) {
  22.         if(!run) break;
  23.         if(cap) {
  24.           try {
  25.             fc.write(bb);
  26.             fc.position(0);
  27.           } catch(Throwable fex) {fex.printStackTrace();}
  28.           cap = false;
  29.         } else {
  30.           try {
  31.             sleep(1);
  32.           } catch(Throwable ex) {ex.printStackTrace();}
  33.         }
  34.       }
  35.     }
  36.   }
  37.    
  38.   PApplet p5;
  39.   PGraphicsOpenGL pg;
  40.   GL gl;
  41.   ByteBuffer bb;
  42.   String tmpLoc = "C:\\p5tmp";
  43.   FileChannel fc;
  44.   boolean run = true;
  45.   boolean registered, cap;
  46.   SendThread sendThread;
  47.  
  48.   int vpboInd, cpboInd;
  49.   int[] pboIds;
  50.   boolean buffReady;
  51.  
  52.   FileCapSend() {
  53.     p5 = fileCapSendPAppletRef;
  54.     pg = (PGraphicsOpenGL)p5.g;
  55.     gl = pg.gl;
  56.     bb = ByteBuffer.allocateDirect(width*height*4);
  57.    
  58.     if(run) beginCap();
  59.   }
  60.  
  61.   void beginCap() {
  62.     try {
  63.       File prev = new File(tmpLoc);
  64.       if(prev.exists()) prev.delete();
  65.       fc = new RandomAccessFile(tmpLoc, "rw").getChannel();
  66.     } catch(Throwable ex) {
  67.       ex.printStackTrace();
  68.       run = false;
  69.       return;
  70.     }
  71.     sendThread = new SendThread();
  72.     sendThread.start();
  73.     register();
  74.   }
  75.  
  76.   void register() {
  77.     p5.registerDraw(this);
  78.     p5.registerPost(this);
  79.     registered = true;
  80.   }
  81.  
  82.   void endCap() {
  83.     p5.unregisterDraw(this);
  84.     p5.unregisterPost(this);
  85.     try {
  86.       fc.close();
  87.     } catch(Throwable ex) {ex.printStackTrace();}
  88.   }
  89.  
  90.   public void draw() {
  91.     bb.clear();
  92.    
  93.     if(!buffReady) {
  94.       gl.glPixelStorei(GL.GL_PACK_ALIGNMENT, 1);
  95.       pboIds = new int[2];
  96.       gl.glGenBuffers(2, pboIds, 0);
  97.       gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[0]);
  98.       gl.glBufferData(
  99.         GL.GL_PIXEL_PACK_BUFFER, width*height*4,
  100.         null, GL.GL_STREAM_READ);
  101.       gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[1]);
  102.       gl.glBufferData(
  103.         GL.GL_PIXEL_PACK_BUFFER, width*height*4,
  104.         null, GL.GL_STREAM_READ);
  105.       gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, 0);
  106.       buffReady = true;
  107.     }
  108.  
  109.     vpboInd = (vpboInd+1)%2;
  110.     cpboInd = (vpboInd+1)%2;
  111.    
  112.     gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[cpboInd]);
  113.     try {
  114.       bb =
  115.         gl.glMapBuffer(GL.GL_PIXEL_PACK_BUFFER,GL.GL_READ_ONLY);
  116.     } catch(Throwable ex) {ex.printStackTrace();}
  117.     gl.glUnmapBuffer(GL.GL_PIXEL_PACK_BUFFER);
  118.    
  119.     cap = true;
  120.    
  121.     gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, pboIds[vpboInd]);
  122.     gl.glReadPixels(
  123.       0,0,width,height,GL.GL_BGRA,GL.GL_UNSIGNED_BYTE,0L);
  124.     gl.glBindBuffer(GL.GL_PIXEL_PACK_BUFFER, 0);
  125.   }
  126.  
  127.   public void post() {
  128.     if(!run && registered) {
  129.       endCap();
  130.       return;
  131.     }
  132.   }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement