Processing Forum
PShape test; PMatrix3D pm; boolean resetBoth = false; boolean shapeForVerts = true; void setup() { size(400,400,P3D); frameRate(1); ortho(); test = createShape(BOX, 100,100,100); noStroke(); pm = doTx(test); if(shapeForVerts) shape(test); } void mouseClicked() { if(resetBoth) pm.reset(); pm.apply(doTx(test)); if(shapeForVerts) shape(test); } PMatrix3D doTx(PShape in) { if(resetBoth) in.resetMatrix(); PVector tx = new PVector( random(-25,25),random(-25,25),random(-25,25)); PVector rx = new PVector( random(-1,1),random(-1,1),random(-1,1)); PMatrix3D ret = new PMatrix3D(); tx.mult(-1); ret.translate(tx.x, tx.y, tx.z); ret.rotateX(rx.x); ret.rotateY(rx.y); ret.rotateZ(rx.z); tx.mult(-1); ret.translate(tx.x, tx.y, tx.z); in.applyMatrix(ret); return ret; } void draw() { background(204); translate(width*.5, height*.5); // shape(test); hint(DISABLE_DEPTH_TEST); fill(255,0,0); pushMatrix(); applyMatrix(pm); pushStyle(); stroke(0); fill(255); box(100,100,100); popStyle(); for(int a=0; a<8; a++) { pushMatrix(); PVector cur = test.getVertex(a); translate(cur.x, cur.y, cur.z); sphere(10); popMatrix(); } popMatrix(); PVector pv = test.getVertex(0).get(); pm.mult(pv, pv); fill(0,0,255); pushMatrix(); translate(pv.x, pv.y, pv.z); sphere(5); popMatrix(); hint(ENABLE_DEPTH_TEST); }
import processing.opengl.*; PVector[] test; void setup() { size(400,400,OPENGL); colorMode(HSB); noStroke(); test = new PVector[] { new PVector(1955,1754), new PVector(1925,1871), new PVector(2042,2054), new PVector(2000,2250), new PVector(1858,2495), new PVector(1517,2525), new PVector(1262,2432), new PVector(1268,2060), new PVector(1529,2126), new PVector(1676,1856), new PVector(1880,1622)}; pushMatrix(); translate(-300,-430); beginShape(); for(PVector cur : test) { fill(random(255),255,255); cur.mult(.3); vertex(cur.x, cur.y); } endShape(CLOSE); popMatrix(); }
PApplet fileCapSendPAppletRef;
{
import javax.media.opengl.GLContext;
import javax.media.opengl.GL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
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) {
glc.makeCurrent();
gl.glReadPixels(
0, 0, width, height,
GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, bb);
glc.release();
try {
fc.write(bb);
fc.position(0);
} catch(Throwable fex) {fex.printStackTrace();}
bb.clear();
cap = false;
} else {
try {
sleep(1);
} catch(Throwable ex) {ex.printStackTrace();}
}
}
}
}
PApplet p5;
PGraphicsOpenGL pg;
GLContext glc;
GL gl;
ByteBuffer bb;
String tmpLoc = "C:\\p5tmp";
FileChannel fc;
boolean run = true;
boolean registered, cap;
SendThread sendThread;
FileCapSend() {
p5 = fileCapSendPAppletRef;
pg = (PGraphicsOpenGL)p5.g;
glc = pg.pgl.context;
gl = pg.pgl.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.registerPost(this);
registered = true;
}
void endCap() {
p5.unregisterPost(this);
try {
fc.close();
} catch(Throwable ex) {ex.printStackTrace();}
}
public void post() {
if(!run && registered) {
endCap();
return;
}
cap = true;
}
}
fileCapSave.pde
import processing.opengl.PGL; import java.nio.ByteOrder; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; int ww = 1280; int hh = 720; String tmpLoc = "C:\\p5tmp"; String saveLoc = sketchPath("test\\"); ByteBuffer bb; FileChannel fc; int ind = -1; PImage pi; void setup() { try { fc = new RandomAccessFile(tmpLoc, "rw").getChannel(); fc.map(FileChannel.MapMode.READ_ONLY, 0, ww*hh*4); bb = ByteBuffer.allocateDirect(ww*hh*4); bb.order(ByteOrder.nativeOrder()); } catch(Throwable ex) {ex.printStackTrace();} pi = createImage(1280,720,RGB); pi.loadPixels(); } void draw() { ind++; try { fc.position(0); fc.read(bb); } catch(Throwable ex) {ex.printStackTrace();} regularSave(); bb.clear(); } void regularSave() { bb.rewind(); bb.asIntBuffer().get(pi.pixels); PGL.nativeToJavaRGB(pi.pixels, ww, hh); pi.save(saveLoc+nf(ind, 6)+".tga"); } void exit() { try { Thread.currentThread().sleep(500); } catch(Throwable ex) {} super.exit(); }
If you try this make sure to click to end the video so the file gets finished properly. I would like to use this to develop a tool that would allow video to be captured from any sketch without adding extra code. I hear there may be a new core video library coming , though. I also had trouble with GSVideo for this purpose.
