我正在尝试在单独的线程中渲染来自处理草图的帧。Processing 有一个loadPixels()函数。问题在于 OpenGL 渲染器(P2D
或P3D
在处理中。):PGraphicsOpenGL loadPixels()
这是我的示例草图:
void setup(){
size(300,300,P2D);
noFill();
background(255);
rectMode(CENTER);
stroke(0,32);
smooth(8);
// instantiate and start rendering thread
new PNGRenderer(this);
}
void draw(){
// draw moving shapes
translate(width * 0.5, height * 0.5);
rotate(sin(frameCount * 0.075));
scale(sin(frameCount * 0.01));
translate(cos(frameCount * 0.01) * width * 0.1,sin(frameCount * 0.01) * height * 0.1);
rect(0,0,width * .9,height * .9);
}
public class PNGRenderer implements Runnable{
PApplet parent;
PImage frame;
boolean shouldSave = false;
int savedFrameCount;
boolean isRunning = true;
PNGRenderer(PApplet parent){
this.parent = parent;
this.parent.registerMethod("draw",this);
frame = createImage(parent.width,parent.height,ARGB);
frame.loadPixels();
Thread renderThread = new Thread(this);
renderThread.setName("Renderer-Thread");
renderThread.start();
}
public void draw(){
// all is well if I sample pixels in the same OpenGL thread
//parent.loadPixels();
shouldSave = true;
}
synchronized void sampleAndSavePixels(){
if(shouldSave){
// program crashes if I try to save in a separate thread
parent.loadPixels();
arrayCopy(parent.pixels,frame.pixels);
frame.updatePixels();
frame.save(dataPath("frames/frame_"+nf(savedFrameCount++,4)+".png"));
println("saved frame",savedFrameCount);
shouldSave = false;
}
}
public void run(){
while(isRunning){
sampleAndSavePixels();
}
}
}
据我所知(到目前为止我还不是 OpenGL 向导)读取像素需要在同一个 OpenGL 线程中完成。这是我试图避免的放缓。
慕码人2483693
相关分类