2014-03-13 22 views
-1

我正在製作一款文字遊戲,並且在遊戲加載時動態地爲信件拼貼創建紋理,包括背景圖像和字體。使用libgdx製作字體的動態紋理

爲此,我將pixmaps繪製到pixmaps上,這一切都很好,直到我開始縮放。儘管我的縮放字體在其他地方看起來很不錯,但即使打開了雙線性過濾(左下圖),字體縮放也很糟糕。

所以我決定繞過這個我會使用一個幀緩衝區,渲染一切,然後將其複製到一個像素圖,並從中創建一個紋理。這樣我可以使用gpu過濾,它應該與我的其他字體完全相同(下圖中的中間圖像),但它仍然看起來不如其他字體好。外面有一條細微的黑線,看起來像阿爾法混合不能正常工作。

然後,我嘗試在運行時直接在字體上繪製字體,以確保它不是我的想象,並且這與下面的圖像(下圖右圖)中的平滑混合顯然更好看,但這會影響我的框架率很高。

examples

所以我的問題是,爲什麼被繪製到幀緩衝區不產生相同的結果,當我畫到屏幕上?下面的代碼。

Texture tx = Assets.loadTexture("bubbles/BubbleBlue.png"); 
    tx.setFilter(TextureFilter.Linear, TextureFilter.Linear); 
    SpriteBatch sb = new SpriteBatch(); 
    FrameBuffer fb = new FrameBuffer(Format.RGBA8888, 
      LayoutManager.getWidth(), LayoutManager.getHeight(), false); 
    fb.begin(); 
    sb.begin(); 
    sb.draw(tx, 0, 0, LetterGrid.blockWidth, LetterGrid.blockHeight); 

    Assets.candara80.font.getRegion().getTexture() 
      .setFilter(TextureFilter.Linear, TextureFilter.Linear); 
    Assets.candara80.setSize(0.15f); 

    TextBounds textBounds = Assets.candara80.getBounds(letter); 

    Assets.candara80.drawText(sb, letter, 
      (LetterGrid.blockWidth - textBounds.width)/2, 
      (LetterGrid.blockHeight + textBounds.height)/2); 

    sb.end(); 

    Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, 
      (int) LetterGrid.blockWidth, (int) LetterGrid.blockHeight); 
    Pixmap flipped = flipPixmap(pm); 

    result = new Texture(flipped); 

    fb.end(); 
    pm.dispose(); 
    flipped.dispose(); 
    tx.dispose(); 
    fb.dispose(); 
    sb.dispose(); 
+0

嘗試設置幀緩衝器大小的大小與屏幕('gdx.graphics.getWidth /高度()')相同。這會將FBO大小設置爲與窗口/屏幕相同的大小,因此您可以確定大小不會影響結果。但我不太瞭解FBO,所以只是一個想法,不知道它是否有幫助 – Springrbua

+0

@Springrbua謝謝,購買我的LayoutManager寬度和高度與屏幕大小相同,所以這不是問題。 –

+0

只是我還是第一張圖片中的字體大小不一樣?我的意思是,即使他們是各種大小的例子,第一個圖像看起來比較大的背景.... 你說動態創建,但包括使用FreeTypeFontGenerator?如果超級漂亮的文本是你的目標,那麼爲什麼不以所需的大小生成字體位圖,而不是試圖縮放它們呢? – Chase

回答

0

set PROJECTION是問題所在。

 public Texture texture(Color fg_color, Color bg_color) 
     { 
      Pixmap pm = render(fg_color, bg_color); 
      texture = new Texture(pm);//***here's your new dynamic texture*** 
      disposables.add(texture);//store the texture 
     } 
    //--------------------------- 
     public Pixmap render(Color fg_color, Color bg_color) 
     { 
      int width = Gdx.graphics.getWidth(); 
      int height = Gdx.graphics.getHeight(); 

      SpriteBatch spriteBatch = new SpriteBatch(); 

      m_fbo = new FrameBuffer(Format.RGB565, (int)(width * m_fboScaler), (int)(height * m_fboScaler), false); 
      m_fbo.begin(); 
      Gdx.gl.glClearColor(bg_color.r, bg_color.g, bg_color.b, bg_color.a); 
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
/**set PROJECTION**/ 
      Matrix4 normalProjection = new Matrix4().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 
      spriteBatch.setProjectionMatrix(normalProjection); 

      spriteBatch.begin(); 
      spriteBatch.setColor(fg_color); 
      //do some drawing ***here's where you draw your dynamic texture*** 
      ... 
      spriteBatch.end();//finish write to buffer 

      pm = ScreenUtils.getFrameBufferPixmap(0, 0, (int) width, (int) height);//write frame buffer to Pixmap 

      m_fbo.end(); 
    //  pm.dispose(); 
    //  flipped.dispose(); 
    //  tx.dispose(); 
      m_fbo.dispose(); 
      m_fbo = null; 
      spriteBatch.dispose(); 
    //  return texture; 
      return pm; 
     }