0

Because of my design, i want to get rid of NinePatch object, but i use it to initialize textures at the desired size using framebuffer, and then apply that textures to my custom objects

Im new to LibGDX and GL stuff.

The problem is when i draw FBO texture to screen the image is so small, idk why

I grab skeleton of all this from here: https://stackoverflow.com/a/7632680/401529

My RenderToTexture class is:

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;
}

}

And my util class:

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();
}

}

Then i call this util class when i create a sprite:

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);

The current result is (left) and the size simulated desired result (right)

screenshot

EDIT: Camera size is displaymode size, glued to screen, no zoom, no movement.

EDIT: Fixed missing dispose suggested by @Tenfour04

If this way is not the best way to do it, im open to new alternatives

Community
  • 1
  • 1
Lyoneel
  • 419
  • 6
  • 16

1 Answers1

1

Well i found a "HALF" solution of my problem:

As i see here in this example LibGDX - Drawing to a FrameBuffer does not work i miss the setProjectionMatrix to SpriteBatch object, so render to texture class begin() method now is:

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;
}

Now renderToTexture begin() returns spritebatch, so:

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();
}

This solves the size of drawed texture from FBO, but ninepatch is drawed ok outside framebuffer, and stretched with framebuffer...

Community
  • 1
  • 1
Lyoneel
  • 419
  • 6
  • 16
  • 1
    Try looking at this for texture scaling, since you said you were new to OpenGL: https://open.gl/textures – Nick Clark Apr 25 '17 at 17:36
  • 1
    You are leaking your SpriteBatch. SpriteBatches must be disposed. And they are big objects that take significant time to instantiate, so you should reuse a single instance for the lifetime of your game. – Tenfour04 Apr 25 '17 at 18:46
  • @NickClark2016 im so curious why the spritebatch is drawing stretched ninepatch inside fbo, and well rendered without fbo – Lyoneel Apr 26 '17 at 16:50
  • @Tenfour04 i fix missing dispose, ill reuse the main batch outside class, anyway the render2texture is used on initialization outside the game loop – Lyoneel Apr 26 '17 at 17:08