2014-02-24 21 views
0

我正在创建一个立体测试应用程序,其中场景呈现为PGraphics leftPGraphics 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(); 
} 

回答

0

你有多种选择来实现相同的视觉输出。 这里有几个选项:

简单叠加 “L”/ “R” 文本:

在拉伸

():

render(left, bgl); 
    render(right, bgr); 
    image(right, 0, 0); 
    image(right, left.width, 0); 
    text("L",100,100); 
    text("R",width/2+100,100); 

使用gfx.background(0)在渲染() 。

PGraphics延伸PImage所以不是

gfx.background(gfxBackground);

可以使用

gfx.image(gfxBackground,xoffset,yoffset); 

您需要偏移,因为摄像头调用,另外,你需要在翻译框Z方向,因为默认情况下它将在(0,0,0)处并且与渲染背景图像的四边形相交。如果您想更深入地发现其他瓶颈,请使用jvisualvm(如果您安装了JDK并设置了PATH,则应该能够从终端/命令行运行此命令,否则应用程序位于YOUR_JDK_INSTALL_PATH \仓)。 以不同的时间间隔拍摄几张快照并比较性能。您可能会发现一些其他绘图命令可以更改为每帧获得几个ms。

+0

谢谢你的回答。然而,我真正感兴趣的是,为什么从一个PGraphics到另一个PGI逐渐变得如此缓慢。在我看来,这个操作应该很快。因此,为了澄清这个问题,也许我应该问一些'如何使用硬件加速从一个屏幕外缓冲区复制到另一个屏幕外缓冲区'? 感谢jvisualvm提示:-) – anorm

+0

不用担心,随意投票/标记,因为你认为合适;)另外,只是发现背景(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) –