我正在创建一个立体测试应用程序,其中场景呈现为PGraphics left
和PGraphics right
,两个视点的摄像机角度不同。然后将这两个图像合并为draw()
函数中的并排输出。处理:与背景的性能问题()
场景由预渲染的背景组成,存储在单独的PGraphics
中,渲染一次,并为每个帧渲染旋转box()
。
问题是render()
中对gfx.background(gfxBackground);
的调用非常耗费CPU资源。如果我用gfx.background(0);
呼叫替换它,草图运行平稳。
我的假设是,从一个PGraphics
到另一个的blit'ing数据将用硬件加速完成,但它似乎不是。难道我做错了什么?
我的草图:
PGraphics leftBackground;
PGraphics rightBackground;
PGraphics left;
PGraphics right;
int sketchWidth() { return 1920; }
int sketchHeight() { return 1200; }
int sketchQuality() { return 8; }
String sketchRenderer() { return P3D; }
void setup()
{
noCursor();
leftBackground = createGraphics(width/2, height, P3D);
renderBackground(leftBackground, "L");
rightBackground = createGraphics(width/2, height, P3D);
renderBackground(rightBackground, "R");
left = createGraphics(width/2, height, P3D);
left.beginDraw();
left.endDraw();
left.camera(-10, 0, 220,
0, 0, 0,
0, 1, 0);
right = createGraphics(width/2, height, P3D);
right.beginDraw();
right.endDraw();
right.camera(10, 0, 220,
0, 0, 0,
0, 1, 0);
}
void draw()
{
render(left, leftBackground);
render(right, rightBackground);
image(left, 0, 0);
image(right, left.width, 0);
}
void renderBackground(PGraphics gfx, String str)
{
gfx.beginDraw();
gfx.background(0);
gfx.stroke(255);
gfx.noFill();
gfx.rect(0, 0, gfx.width, gfx.height);
gfx.scale(0.5, 1.0, 1.0);
gfx.textSize(40);
gfx.fill(255);
gfx.text(str, 30, 40);
gfx.endDraw();
}
void render(PGraphics gfx, PGraphics gfxBackground)
{
gfx.beginDraw();
gfx.background(gfxBackground);
gfx.scale(0.5, 1, 1);
gfx.rotateY((float)frameCount/100);
gfx.rotateX((float)frameCount/90);
gfx.stroke(255);
gfx.fill(0);
gfx.box(30);
gfx.endDraw();
}
谢谢你的回答。然而,我真正感兴趣的是,为什么从一个PGraphics到另一个PGI逐渐变得如此缓慢。在我看来,这个操作应该很快。因此,为了澄清这个问题,也许我应该问一些'如何使用硬件加速从一个屏幕外缓冲区复制到另一个屏幕外缓冲区'? 感谢jvisualvm提示:-) – anorm
不用担心,随意投票/标记,因为你认为合适;)另外,只是发现背景(PImage)不能硬件加速的原因。如果你看[源代码](https://github.com/processing/processing/blob/master/core/src/processing/core/PGraphics.java#L7017),PGraphics使用set(0,0,image)因为它继承它的父类PImage和[set()](https://github.com/processing/processing/blob/master/core/src/processing/core/PImage.java#L964)使用'系统。 arraycopy'。上面我推荐了'gfx.image(gfxBackground,x,y);'[渲染一个纹理四边形](http://goo.gl/z25Cu8) –