Advertisement
Guest User

capSendBeta.pde

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