0
votes

I know I'm a bit ahead of libgdx because it's actually more a 2D-library but I'm working on a 3D app powered by libgdx and need to write text on a model.

I already come so far that I'm able to change texture of my model dynamically. Now I need to write text to a texture to apply this texture to my model... Is this already possible with libgdx? If yes, how? Till now I only found tutorials how to write text on screen wit BitmapFont but only via SpriteBatch and I don't think there would be a possibility to write the output of spritebatch on a texture...

Thanks in advance!

2

2 Answers

1
votes

You can send the output of a SpriteBatch (or any OpenGL drawing command) to a texture instead of sending it to the screen. In Libgdx you use a FrameBuffer object to accomplish this. This tutorial covers the basics and a bit more: https://github.com/mattdesl/lwjgl-basics/wiki/FrameBufferObjects

0
votes

With Normal bitmap fonts you get a pixmap of ALL the Glpths, and can do bitmap copies.

pixmap.drawPixmap(fontPixmap, x_place, (TILE_HEIGHT - aGlyph.height) / 2,
aGlyph.srcX, aGlyph.srcY, aGlyph.width, aGlyph.height);

The only way (I have found) of drawing raster fonts (`.ttf`) is as follows:

    Example framework:
    ==================
    package com.badlogic.gdx.tests.bullet;

    /**
    Question       :   libgdx write text on texture
    interpreted as :   In libgdx, how to create dynamic texture?
    Answer         :   Use a private render function to draw in a private frame buffer and convert the frame buffer to Pixmap, create Texture.
    Author  :   Jon Goodwin
    **/

    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.Pixmap;
    ...//(ctrl-shift-o) to auto-load imports in Eclipse


    public class BaseBulletTest extends BulletTest
    {
    //class variables
    =================
    public Texture           texture     = null;//create this
    public Array<Disposable> disposables = new Array<Disposable>();
    public Pixmap            pm          = null;
    //---------------------------
        @Override
        public void create ()
        {
            init();
        }
    //---------------------------
        public static void init ()
        {
            if(texture == null) texture(Color.BLUE, Color.WHITE);
            TextureAttribute ta_tex     = TextureAttribute.createDiffuse(texture);
            final Material material_box = new Material(ta_tex, ColorAttribute.createSpecular(1, 1, 1, 1),
                                                       FloatAttribute.createShininess(8f));
            final long attributes1      = Usage.Position | Usage.Normal | Usage.TextureCoordinates;
            final Model boxModel = modelBuilder.createBox(1f, 1f, 1f, material_box, attributes1);
            ...
        }
    //---------------------------
        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);
            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***
            font.draw(spriteBatch, "5\n6\n2016",  width/4, height - 20);//multi-line draw
            ...
            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;
        }
    //---------------------------
    }//class BaseBulletTest
    //---------------------------