2017-04-23 105 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