2017-04-23 38 views
0

因为我设计的,我想摆脱NinePatch的对象,但我用它使用帧缓冲初始化在所需大小的纹理,然后应用纹理我的自定义对象Libgdx - 不同的尺寸时,帧缓存的纹理渲染到屏幕

进出口新的LibGDX和GL的东西。

问题是,当我画FBO纹理屏幕上的图像是如此之小,IDK的为什么

我抓住了这一切骨架从这里:https://stackoverflow.com/a/7632680/401529

我RenderToTexture类是:

public class RenderToTexture { 
private float fbScaler = 1f; 
private boolean fbEnabled = true; 
private FrameBuffer fb = null; 
private TextureRegion fbReg = null; 


[... more constructors ... ] 

/** 
* 
* @param scale 
*/ 
public RenderToTexture(int width, int height, float scale, boolean hasDepth) { 
    if(width == -1){ 
     width = (int) (Gdx.graphics.getDisplayMode().width * scale); 
    } 
    if(height == -1){ 
     height = (int) (Gdx.graphics.getDisplayMode().height * scale); 
    } 

    fb = new FrameBuffer(
      Pixmap.Format.RGBA8888, 
      (int)(width * fbScaler), 
      (int)(height * fbScaler), 
      hasDepth 
    ); 

    fbReg = new TextureRegion(fb.getColorBufferTexture()); 
    fbReg.flip(false,false); 
} 

public void begin(){ 
    if(fbEnabled) { 
     fb.begin(); 
     Gdx.gl.glClearColor(0, 0,0,0); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    } 
} 
public TextureRegion end(){ 
    if(fb != null) 
    { 
     fb.end(); 
     return fbReg; 
     // batch.draw(fboRegion, 0, 0); 
    } 
    return null; 
} 

}

而且我的Util类:

public class ImageUtils { 
public static TextureRegion ninePatchToTextureRegion(NinePatch patch, int width, int height){ 
    return ninePatchToTextureRegion(new NinePatchDrawable(patch), width, height); 
} 

public static TextureRegion ninePatchToTextureRegion(NinePatchDrawable patch, int width, int height){ 
    RenderToTexture r2t = new RenderToTexture(width, height, 1, false); 
    SpriteBatch sb = new SpriteBatch(); 
    r2t.begin(); 
    sb.begin(); 
    patch.draw(sb, 0, 0, width, height); 
    sb.end(); 
    sb.dispose(); 

    return r2t.end(); 
} 

}

然后我把这种util的类时,我创建一个精灵:

NinePatchDrawable ninepatchtest = new NinePatchDrawable(
       new NinePatch(new Texture(Gdx.files.internal("img/ui/btn-retro-blue.png")), 5, 5, 5, 5) 
     ); 
TextureRegion result = ImageUtils.ninePatchToTextureRegion(ninepatchtest, 200, 100); 
     sprite = new Sprite(result); 

当前结果是(左)和模拟的期望的结果(右)的大小

screenshot

编辑:相机尺寸是displaymode的大小,粘在屏幕上,没有变焦,没有移动。

编辑:固定失踪处置由@建议Tenfour04

如果这种方式是不这样做的最佳方式,即时通信开放到新的替代品

回答

1

嗯,我发现我的问题的“半壁江山”的解决方案:

正如我在此实例中LibGDX - Drawing to a FrameBuffer does not work看到在这里,我错过setProjectionMatrix到SpriteBatch对象,所以纹理渲染类begin()方法目前是:

public SpriteBatch begin(){ 
    SpriteBatch batch = null; 

    if(fbEnabled) { 
     fb.begin(); 
     Gdx.gl.glClearColor(0, 0,0,0); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     Matrix4 m = new Matrix4(); 
     m.setToOrtho2D(0, 0, fb.getWidth(), fb.getHeight()); 

     batch = new SpriteBatch(); 
     batch.setProjectionMatrix(m); 
    } 

    return batch; 
} 

现在renderToTexture开始()返回spritebatch,所以:

public static TextureRegion ninePatchToTextureRegion(NinePatchDrawable patch, int width, int height){ 
    RenderToTexture r2t = new RenderToTexture(width, height, 1, false); 
    SpriteBatch sb = r2t.begin(); 
    sb.begin(); 
    patch.draw(sb, 0, 0, width, height); 
    sb.end(); 
    sb.dispose(); 
    return r2t.end(); 
} 

这解决了从FBO drawed纹理大小,但ninepatch是drawed OK之外帧缓冲,并与帧缓冲拉伸...

+1

试着看一下这是为了纹理缩放,因为你说你是OpenGL的新手:https://open.gl/textures –

+1

你正在泄漏你的SpriteBatch。 SpriteBatches必须被丢弃。而且它们是需要大量时间实例化的大对象,因此您应该在游戏的整个生命周期中重复使用一个实例。 – Tenfour04

+0

@ NickClark2016我太好奇,为什么spritebatch是中拉伸ninepatch内FBO和良好,无FBO – Lyoneel